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
1520B
Problem - 1520B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math number theory *800 No tag edit access → Contest materials . Then $$$t$$$ test cases follow. Each test case is characterized by one integer $$$n$$$ ($$$1 le n le 10^9$$$). Output For each test case output the number of ordinary numbers among numbers from $$$1$$$ to $$$n$$$. Example Input 6 1 2 3 4 5 100 Output 1 2 3 4 5 18
800
true
false
false
false
false
false
true
false
false
false
3,062
650E
My name is James diGriz, I'm the most clever robber and treasure hunter in the whole galaxy. There are books written about my adventures and songs about my operations, though you were able to catch me up in a pretty awkward moment. I was able to hide from cameras, outsmart all the guards and pass numerous traps, but when I finally reached the treasure box and opened it, I have accidentally started the clockwork bomb! Luckily, I have met such kind of bombs before and I know that the clockwork mechanism can be stopped by connecting contacts with wires on the control panel of the bomb in a certain manner. I see _n_ contacts connected by _n_u2009-u20091 wires. Contacts are numbered with integers from 1 to _n_. Bomb has a security mechanism that ensures the following condition: if there exist _k_u2009≥u20092 contacts _c_1,u2009_c_2,u2009...,u2009_c__k_ forming a circuit, i. e. there exist _k_ distinct wires between contacts _c_1 and _c_2, _c_2 and _c_3, ..., _c__k_ and _c_1, then the bomb immediately explodes and my story ends here. In particular, if two contacts are connected by more than one wire they form a circuit of length 2. It is also prohibited to connect a contact with itself. On the other hand, if I disconnect more than one wire (i. e. at some moment there will be no more than _n_u2009-u20092 wires in the scheme) then the other security check fails and the bomb also explodes. So, the only thing I can do is to unplug some wire and plug it into a new place ensuring the fact that no circuits appear. I know how I should put the wires in order to stop the clockwork. But my time is running out! Help me get out of this alive: find the sequence of operations each of which consists of unplugging some wire and putting it into another place so that the bomb is defused. Input The first line of the input contains _n_ (2u2009≤u2009_n_u2009≤u2009500u2009000), the number of contacts. Each of the following _n_u2009-u20091 lines contains two of integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_, _x__i_u2009≠u2009_y__i_) denoting the contacts currently connected by the _i_-th wire. The remaining _n_u2009-u20091 lines contain the description of the sought scheme in the same format. It is guaranteed that the starting and the ending schemes are correct (i. e. do not contain cicuits nor wires connecting contact with itself). Output The first line should contain _k_ (_k_u2009≥u20090)xa0— the minimum number of moves of unplugging and plugging back some wire required to defuse the bomb. In each of the following _k_ lines output four integers _a__i_, _b__i_, _c__i_, _d__i_ meaning that on the _i_-th step it is neccesary to unplug the wire connecting the contacts _a__i_ and _b__i_ and plug it to the contacts _c__i_ and _d__i_. Of course the wire connecting contacts _a__i_ and _b__i_ should be present in the scheme. If there is no correct sequence transforming the existing scheme into the sought one, output -1.
3,200
false
true
false
false
true
false
false
false
false
false
7,213
796F
Zane once had a good sequence _a_ consisting of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_xa0— but he has lost it. A sequence is said to be good if and only if all of its integers are non-negative and do not exceed 109 in value. However, Zane remembers having played around with his sequence by applying _m_ operations to it. There are two types of operations: 1. Find the maximum value of integers with indices _i_ such that _l_u2009≤u2009_i_u2009≤u2009_r_, given _l_ and _r_. 2. Assign _d_ as the value of the integer with index _k_, given _k_ and _d_. After he finished playing, he restored his sequence to the state it was before any operations were applied. That is, sequence _a_ was no longer affected by the applied type 2 operations. Then, he lost his sequence at some time between now and then. Fortunately, Zane remembers all the operations and the order he applied them to his sequence, along with the distinct results of all type 1 operations. Moreover, among all good sequences that would produce the same results when the same operations are applied in the same order, he knows that his sequence _a_ has the greatest cuteness. We define cuteness of a sequence as the bitwise OR result of all integers in such sequence. For example, the cuteness of Zane's sequence _a_ is _a_1 OR _a_2 OR ... OR _a__n_. Zane understands that it might not be possible to recover exactly the lost sequence given his information, so he would be happy to get any good sequence _b_ consisting of _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ that: 1. would give the same results when the same operations are applied in the same order, and 2. has the same cuteness as that of Zane's original sequence _a_. If there is such a sequence, find it. Otherwise, it means that Zane must have remembered something incorrectly, which is possible. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20093·105)xa0— the number of integers in Zane's original sequence and the number of operations that have been applied to the sequence, respectively. The _i_-th of the following _m_ lines starts with one integer _t__i_ ()xa0— the type of the _i_-th operation. If the operation is type 1 (_t__i_u2009=u20091), then three integers _l__i_, _r__i_, and _x__i_ follow (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_, 0u2009≤u2009_x__i_u2009≤u2009109)xa0— the leftmost index to be considered, the rightmost index to be considered, and the maximum value of all integers with indices between _l__i_ and _r__i_, inclusive, respectively. If the operation is type 2 (_t__i_u2009=u20092), then two integers _k__i_ and _d__i_ follow (1u2009≤u2009_k__i_u2009≤u2009_n_, 0u2009≤u2009_d__i_u2009≤u2009109)xa0— meaning that the integer with index _k__i_ should become _d__i_ after this operation. It is guaranteed that _x__i_u2009≠u2009_x__j_ for all pairs (_i_,u2009_j_) where 1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_m_ and _t__i_u2009=u2009_t__j_u2009=u20091. The operations are given in the same order they were applied. That is, the operation that is given first was applied first, the operation that is given second was applied second, and so on. Output If there does not exist a valid good sequence, print "NO" (without quotation marks) in the first line. Otherwise, print "YES" (without quotation marks) in the first line, and print _n_ space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (0u2009≤u2009_b__i_u2009≤u2009109) in the second line. If there are multiple answers, print any of them. Note In the first sample, it is easy to verify that this good sequence is valid. In particular, its cuteness is 19 OR 0 OR 0 OR 0 OR 1 u2009=u2009 19. In the second sample, the two operations clearly contradict, so there is no such good sequence.
2,800
false
true
false
false
true
false
false
false
false
false
6,588
960F
You are given a directed graph with _n_ nodes and _m_ edges, with all edges having a certain weight. There might be multiple edges and self loops, and the graph can also be disconnected. You need to choose a path (possibly passing through same vertices multiple times) in the graph such that the weights of the edges are in strictly increasing order, and these edges come in the order of input. Among all such paths, you need to find the the path that has the maximum possible number of edges, and report this value. Please note that the edges picked don't have to be consecutive in the input. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100000,1u2009≤u2009_m_u2009≤u2009100000)xa0— the number of vertices and edges in the graph, respectively. _m_ lines follows. The _i_-th of these lines contains three space separated integers _a__i_, _b__i_ and _w__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, 0u2009≤u2009_w__i_u2009≤u2009100000), denoting an edge from vertex _a__i_ to vertex _b__i_ having weight _w__i_ Output Print one integer in a single line — the maximum number of edges in the path. Examples Input 5 5 1 3 2 3 2 3 3 4 5 5 4 0 4 5 8 Note The answer for the first sample input is 2: . Note that you cannot traverse because edge appears earlier in the input than the other two edges and hence cannot be picked/traversed after either of the other two edges. In the second sample, it's optimal to pick 1-st, 3-rd and 5-th edges to get the optimal answer: .
2,100
false
false
false
true
true
false
false
false
false
true
5,866
1973E
Fox has found an array $$$p_1, p_2, ldots, p_n$$$, that is a permutation of length $$$n^dagger$$$ of the numbers $$$1, 2, ldots, n$$$. She wants to sort the elements in increasing order. Cat wants to help her — he is able to swap any two numbers $$$x$$$ and $$$y$$$ in the array, but only if $$$l leq x + y leq r$$$ (note that the constraint is imposed on the values of the elements, not their positions). He can make such swaps any number of times. They don't know the numbers $$$l$$$, $$$r$$$ yet, they only know that it's true that $$$1 leq l leq r leq 2 cdot n$$$. You are given the number $$$n$$$ and the array $$$p_1, p_2, ldots, p_n$$$. Determine how many pairs $$$(l, r)$$$ satisfying the conditions are there such that you can sort the permutation if you can only swap two number $$$(x, y)$$$ such that $$$l leq x + y leq r$$$ (arbitrary number of times, possibly $$$0$$$). $$$^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 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. Description of each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 leq n leq 10^5$$$). The second line contains $$$n$$$ integers: the array $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$). It is guaranteed that this array is a permutation of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print the number of pairs of integers $$$(l, r)$$$ such that $$$1 leq l leq r leq 2 cdot n$$$, and you can sort the array under the constraints. Example Input 7 2 2 1 3 3 1 2 4 3 2 1 4 5 5 3 1 2 4 5 1 2 3 4 5 6 3 2 1 5 4 6 6 1 3 2 4 5 6 Output 6 11 23 29 55 46 58 Note In the first example, we need to be able to swap $$$1$$$ and $$$2$$$, so we must be able to swap numbers with sum $$$3$$$. There are exactly $$$6$$$ pairs satisfying the condition: $$$(1, 3), (2, 3), (3, 3), (1, 4), (2, 4)$$$ and $$$(3, 4)$$$, so the answer is $$$6$$$. In the second example, the $$$11$$$ pairs satisfying the condition are $$$(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6), (4, 5)$$$ and $$$(4, 6)$$$. For example, if we pick the pair $$$(3, 4)$$$ we can first swap the numbers $$$1$$$ and $$$2$$$ and then the numbers $$$1$$$ and $$$3$$$, after this, the permutation is sorted.
2,500
true
false
false
false
false
false
false
false
true
true
451
280C
Momiji has got a rooted tree, consisting of _n_ nodes. The tree nodes are numbered by integers from 1 to _n_. The root has number 1. Momiji decided to play a game on this tree. The game consists of several steps. On each step, Momiji chooses one of the remaining tree nodes (let's denote it by _v_) and removes all the subtree nodes with the root in node _v_ from the tree. Node _v_ gets deleted as well. The game finishes when the tree has no nodes left. In other words, the game finishes after the step that chooses the node number 1. Each time Momiji chooses a new node uniformly among all the remaining nodes. Your task is to find the expectation of the number of steps in the described game. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of nodes in the tree. The next _n_u2009-u20091 lines contain the tree edges. The _i_-th line contains integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa0_a__i_u2009≠u2009_b__i_) — the numbers of the nodes that are connected by the _i_-th edge. It is guaranteed that the given graph is a tree. Output Print a single real number — the expectation of the number of steps in the described game. The answer will be considered correct if the absolute or relative error doesn't exceed 10u2009-u20096. Examples Output 1.50000000000000000000 Output 2.00000000000000000000 Note In the first sample, there are two cases. One is directly remove the root and another is remove the root after one step. Thus the expected steps are: 1u2009×u2009(1u2009/u20092)u2009+u20092u2009×u2009(1u2009/u20092)u2009=u20091.5 In the second sample, things get more complex. There are two cases that reduce to the first sample, and one case cleaned at once. Thus the expected steps are: 1u2009×u2009(1u2009/u20093)u2009+u2009(1u2009+u20091.5)u2009×u2009(2u2009/u20093)u2009=u2009(1u2009/u20093)u2009+u2009(5u2009/u20093)u2009=u20092
2,200
true
false
true
false
false
false
false
false
false
false
8,713
788D
After some programming contest Roma decided to try himself in tourism. His home country Uzhlyandia is a Cartesian plane. He wants to walk along each of the Main Straight Lines in Uzhlyandia. It is known that each of these lines is a straight line parallel to one of the axes (i.e. it is described with the equation _x_u2009=u2009_a_ or _y_u2009=u2009_a_, where _a_ is integer called the coordinate of this line). Roma lost his own map, so he should find out the coordinates of all lines at first. Uncle Anton agreed to help him, using the following rules: Initially Roma doesn't know the number of vertical and horizontal lines and their coordinates; Roma can announce integer coordinates of some point in Uzhlandia, and Anton then will tell him the minimum among the distances from the chosen point to each of the lines. However, since the coordinates of the lines don't exceed 108 by absolute value, Roma can't choose a point with coordinates exceeding 108 by absolute value. Uncle Anton is in a hurry to the UOI (Uzhlandian Olympiad in Informatics), so he can only answer no more than 3·105 questions. The problem is that Roma doesn't know how to find out the coordinates of the lines. Write a program that plays Roma's role and finds the coordinates. Interaction To make a query, print a line "0 x y" (-108u2009≤u2009_x_,u2009_y_u2009≤u2009108), where _x_ and _y_ are the coordinates of the point. After each query you need to print end-of-line, make "flush" operation, and then read the answer to the queryxa0— the minimum among the distances prom this point to the Main Straight Lines of Uzhlyandia. You can do no more than 3·105 queries. When you are ready to print the answer, print three lines: 1. In the first line print "1 n m", where _n_ is the number of vertical lines (parallel to _OY_), and _m_ is the number of horizontal lines (parallel to _OX_). 2. In the second line print _n_ integers _x_1,u2009_x_2,u2009...,u2009_x__n_xa0— the coordinates of the vertical lines. 3. In the third line in the same format print _m_ integers _y_1,u2009_y_2,u2009...,u2009_y__m_xa0— the coordinates of the horizontal lines. You can print coordinates in arbitrary order. To make "flush", you can use (just after printing a query/answer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; see the documentation for other languages. You will get Wrong Answer if you make more queries than allowed or make an invalid query. You can get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output. If at any moment your program reads -1 as an answer, it should immediately exit normally (for example, by calling exit(0)). You will get Wrong Answer in this case, it means that you made more queries than allowed, or made an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Making test for hacking The first line should contain two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009104). The second line should contain _n_ distinct integers _x__i_ (-108u2009≤u2009_x__i_u2009≤u2009108)xa0— the coordinates of the vertical lines. The third line should contain _m_ distinct integers _y__i_ (-108u2009≤u2009_y__i_u2009≤u2009108)xa0— the coordinates of the horizontal lines. You can write coordinates in arbitrary order. You can see the example case in the notes.
3,000
false
false
false
false
false
true
false
false
false
false
6,618
1991F
You are given $$$n$$$ sticks, numbered from $$$1$$$ to $$$n$$$. The length of the $$$i$$$-th stick is $$$a_i$$$. You need to answer $$$q$$$ queries. In each query, you are given two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$, $$$r - l + 1 ge 6$$$). Determine whether it is possible to choose $$$6$$$ distinct sticks from the sticks numbered $$$l$$$ to $$$r$$$, to form $$$2$$$ non-degenerate triangles$$$^{ ext{∗}}$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$6 le n le 10^5$$$, $$$1 le q le 10^5$$$)xa0— the number of sticks and the number of queries respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— $$$a_i$$$ denotes the length of the $$$i$$$-th stick. Each of the following $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$, $$$r - l + 1 ge 6$$$)xa0— the parameters of each query. Output For each query, output "YES" (without quotes) if it is possible to form $$$2$$$ triangles, and "NO" (without quotes) otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Note In the first query, the lengths of the sticks are $$$[5, 2, 2, 10, 4, 10]$$$. Two sets of sticks $$$[2, 4, 5]$$$ and $$$[2, 10, 10]$$$ can be selected to form $$$2$$$ non-degenerate triangles. In the second query, the lengths of the sticks are $$$[2, 2, 10, 4, 10, 6]$$$. It can be shown that it is impossible to form $$$2$$$ non-degenerate triangles. In the third query, the lengths of the sticks are $$$[2, 2, 10, 4, 10, 6, 1]$$$. Two sets of sticks $$$[1, 2, 2]$$$ and $$$[4, 10, 10]$$$ can be selected to form $$$2$$$ non-degenerate triangles. In the fourth query, the lengths of the sticks are $$$[4, 10, 6, 1, 5, 3]$$$. It can be shown that it is impossible to form $$$2$$$ non-degenerate triangles. In the fifth query, the lengths of the sticks are $$$[10, 4, 10, 6, 1, 5, 3]$$$. Two sets of sticks $$$[1, 10, 10]$$$ and $$$[3, 4, 5]$$$ can be selected to form $$$2$$$ non-degenerate triangles.
2,200
true
true
true
false
false
false
true
false
true
false
323
39C
There are lots of theories concerning the origin of moon craters. Most scientists stick to the meteorite theory, which says that the craters were formed as a result of celestial bodies colliding with the Moon. The other version is that the craters were parts of volcanoes. An extraterrestrial intelligence research specialist professor Okulov (the namesake of the Okulov, the author of famous textbooks on programming) put forward an alternate hypothesis. Guess what kind of a hypothesis it was –– sure, the one including extraterrestrial mind involvement. Now the professor is looking for proofs of his hypothesis. Professor has data from the moon robot that moves linearly in one direction along the Moon surface. The moon craters are circular in form with integer-valued radii. The moon robot records only the craters whose centers lay on his path and sends to the Earth the information on the distance from the centers of the craters to the initial point of its path and on the radii of the craters. According to the theory of professor Okulov two craters made by an extraterrestrial intelligence for the aims yet unknown either are fully enclosed one in the other or do not intersect at all. Internal or external tangency is acceptable. However the experimental data from the moon robot do not confirm this theory! Nevertheless, professor Okulov is hopeful. He perfectly understands that to create any logical theory one has to ignore some data that are wrong due to faulty measuring (or skillful disguise by the extraterrestrial intelligence that will be sooner or later found by professor Okulov!) That’s why Okulov wants to choose among the available crater descriptions the largest set that would satisfy his theory. Input The first line has an integer _n_ (1u2009≤u2009_n_u2009≤u20092000) — the number of discovered craters. The next _n_ lines contain crater descriptions in the "_c__i_ _r__i_" format, where _c__i_ is the coordinate of the center of the crater on the moon robot’s path, _r__i_ is the radius of the crater. All the numbers _c__i_ and _r__i_ are positive integers not exceeding 109. No two craters coincide. Output In the first line output the number of craters in the required largest set. In the next line output space-separated numbers of craters that this set consists of. The craters are numbered from 1 to _n_ in the order in which they were given in the input data. The numbers may be output in any order. If the result is not unique, output any.
2,100
false
false
false
true
false
false
false
false
true
false
9,798
1515C
Phoenix has $$$n$$$ blocks of height $$$h_1, h_2, dots, h_n$$$, and all $$$h_i$$$ don't exceed some value $$$x$$$. He plans to stack all $$$n$$$ blocks into $$$m$$$ separate towers. The height of a tower is simply the sum of the heights of its blocks. For the towers to look beautiful, no two towers may have a height difference of strictly more than $$$x$$$. Please help Phoenix build $$$m$$$ towers that look beautiful. Each tower must have at least one block and all blocks must be used. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$x$$$ ($$$1 le m le n le 10^5$$$; $$$1 le x le 10^4$$$)xa0— the number of blocks, the number of towers to build, and the maximum acceptable height difference of any two towers, respectively. The second line of each test case contains $$$n$$$ space-separated integers ($$$1 le h_i le x$$$)xa0— the heights of the blocks. It is guaranteed that the sum of $$$n$$$ over all the test cases will not exceed $$$10^5$$$. Output For each test case, if Phoenix cannot build $$$m$$$ towers that look beautiful, print NO. Otherwise, print YES, followed by $$$n$$$ integers $$$y_1, y_2, dots, y_n$$$, where $$$y_i$$$ ($$$1 le y_i le m$$$) is the index of the tower that the $$$i$$$-th block is placed in. If there are multiple solutions, print any of them. Example Input 2 5 2 3 1 2 3 1 2 4 3 3 1 1 2 3 Output YES 1 1 1 2 2 YES 1 2 2 3 Note In the first test case, the first tower has height $$$1+2+3=6$$$ and the second tower has height $$$1+2=3$$$. Their difference is $$$6-3=3$$$ which doesn't exceed $$$x=3$$$, so the towers are beautiful. In the second test case, the first tower has height $$$1$$$, the second tower has height $$$1+2=3$$$, and the third tower has height $$$3$$$. The maximum height difference of any two towers is $$$3-1=2$$$ which doesn't exceed $$$x=3$$$, so the towers are beautiful.
1,400
false
true
false
false
true
true
false
false
false
false
3,089
843A
You are given a sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also will be sorted in increasing order. Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places. Every element of the sequence must appear in exactly one subsequence. Input The first line of input data contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the length of the sequence. The second line of input data contains _n_ different integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109)xa0— the elements of the sequence. It is guaranteed that all elements of the sequence are distinct. Output In the first line print the maximum number of subsequences _k_, which the original sequence can be split into while fulfilling the requirements. In the next _k_ lines print the description of subsequences in the following format: the number of elements in subsequence _c__i_ (0u2009<u2009_c__i_u2009≤u2009_n_), then _c__i_ integers _l_1,u2009_l_2,u2009...,u2009_l__c__i_ (1u2009≤u2009_l__j_u2009≤u2009_n_)xa0— indices of these elements in the original sequence. Indices could be printed in any order. Every index from 1 to _n_ must appear in output exactly once. If there are several possible answers, print any of them. Examples Input 6 83 -75 -49 11 37 62 Note In the first sample output: After sorting the first subsequence we will get sequence 1xa02xa03xa06xa05xa04. Sorting the second subsequence changes nothing. After sorting the third subsequence we will get sequence 1xa02xa03xa04xa05xa06. Sorting the last subsequence changes nothing.
1,400
true
false
true
false
false
false
false
false
true
false
6,391
1237F
Consider a square grid with $$$h$$$ rows and $$$w$$$ columns with some dominoes on it. Each domino covers exactly two cells of the grid that share a common side. Every cell is covered by at most one domino. Let's call a placement of dominoes on the grid perfectly balanced if no row and no column contains a pair of cells covered by two different dominoes. In other words, every row and column may contain no covered cells, one covered cell, or two covered cells that belong to the same domino. You are given a perfectly balanced placement of dominoes on a grid. Find the number of ways to place zero or more extra dominoes on this grid to keep the placement perfectly balanced. Output this number modulo $$$998,244,353$$$. Input The first line contains three integers $$$h$$$, $$$w$$$, and $$$n$$$ ($$$1 le h, w le 3600$$$; $$$0 le n le 2400$$$), denoting the dimensions of the grid and the number of already placed dominoes. The rows are numbered from $$$1$$$ to $$$h$$$, and the columns are numbered from $$$1$$$ to $$$w$$$. Each of the next $$$n$$$ lines contains four integers $$$r_{i, 1}, c_{i, 1}, r_{i, 2}, c_{i, 2}$$$ ($$$1 le r_{i, 1} le r_{i, 2} le h$$$; $$$1 le c_{i, 1} le c_{i, 2} le w$$$), denoting the row id and the column id of the cells covered by the $$$i$$$-th domino. Cells $$$(r_{i, 1}, c_{i, 1})$$$ and $$$(r_{i, 2}, c_{i, 2})$$$ are distinct and share a common side. The given domino placement is perfectly balanced. Output Output the number of ways to place zero or more extra dominoes on the grid to keep the placement perfectly balanced, modulo $$$998,244,353$$$. Note In the first example, the initial grid looks like this: Here are $$$8$$$ ways to place zero or more extra dominoes to keep the placement perfectly balanced: In the second example, the initial grid looks like this: No extra dominoes can be placed here.
2,600
false
false
false
true
false
false
false
false
false
false
4,506
2029G
There is an array $$$a$$$ consisting of $$$n$$$ integers. Initially, all elements of $$$a$$$ are equal to $$$0$$$. Kevin can perform several operations on the array. Each operation is one of the following two types: Prefix additionxa0— Kevin first selects an index $$$x$$$ ($$$1le xle n$$$), and then for each $$$1le jle x$$$, increases $$$a_j$$$ by $$$1$$$; Suffix additionxa0— Kevin first selects an index $$$x$$$ ($$$1le xle n$$$), and then for each $$$xle jle n$$$, increases $$$a_j$$$ by $$$1$$$. In the country of KDOI, people think that the integer $$$v$$$ is balanced. Thus, Iris gives Kevin an array $$$c$$$ consisting of $$$n$$$ integers and defines the beauty of the array $$$a$$$ as follows: Initially, set $$$b=0$$$; For each $$$1le ile n$$$, if $$$a_i=v$$$, add $$$c_i$$$ to $$$b$$$; The beauty of $$$a$$$ is the final value of $$$b$$$. Kevin wants to maximize the beauty of $$$a$$$ after all the operations. However, he had already performed $$$m$$$ operations when he was sleepy. Now, he can perform an arbitrary number (possibly zero) of new operations. You have to help Kevin find the maximum possible beauty if he optimally performs the new operations. However, to make sure that you are not just rolling the dice, Kevin gives you an integer $$$V$$$, and you need to solve the problem for each $$$1le vle V$$$. Input Each test contains multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1le tle 1000$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$V$$$ ($$$1le n, mle 2cdot 10^5$$$, $$$1le Vle 2000$$$)xa0— the length of the array $$$a$$$, the number of initial operations, and the number that Kevin gives you. The second line contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1le c_ile 10^9$$$)xa0— the elements in the array $$$c$$$. Then $$$m$$$ lines follow, the $$$i$$$-th line containing a character $$$op$$$ and an integer $$$x$$$ ($$$op=mathtt{L}$$$ or $$$mathtt{R}$$$, $$$1le xle n$$$)xa0— the type of the $$$i$$$-th operation and the selected index. If $$$op=mathtt{L}$$$, this operation is a prefix addition on index $$$x$$$; If $$$op=mathtt{R}$$$, this operation is a suffix addition on index $$$x$$$. It is guaranteed that: the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$; the sum of $$$m$$$ over all test cases does not exceed $$$2cdot 10^5$$$; the sum of $$$V^2$$$ over all test cases does not exceed $$$4cdot 10^6$$$. Output For each test case, output $$$V$$$ integers in a single line, the $$$i$$$-th integer denoting the maximum possible beauty after Kevin performs some new operations when $$$v=i$$$. Example Input 5 3 3 2 1 2 4 L 3 R 3 L 1 3 3 2 5 1 4 L 3 R 3 L 1 5 4 5 1 1 1 1 1 L 3 R 2 L 5 L 4 10 12 9 10 9 8 7 6 5 4 3 2 1 L 2 L 4 R 4 R 4 L 6 R 8 L 3 L 2 R 1 R 10 L 8 L 1 1 1 4 1000000000 L 1 Output 2 6 1 9 0 1 3 5 5 0 0 0 6 25 32 35 44 51 1000000000 1000000000 1000000000 1000000000 Note In the first test case, the array $$$a$$$ changes as follows for the initial operations: $$$[0, 0, 0] xrightarrow{mathtt{L} 3} [1, 1, 1] xrightarrow{mathtt{R} 3} [1, 1, 2] xrightarrow{mathtt{L} 1} [2, 1, 2]$$$. For $$$v=1$$$, it is optimal to not perform any new operations, and the beauty is $$$b=c_2=2$$$; For $$$v=2$$$, it is optimal to perform a prefix addition operation on index $$$2$$$. After that, $$$a$$$ becomes $$$[3,2,2]$$$, and the beauty is $$$b=c_2+c_3=6$$$. In the second test case, for both $$$v=1$$$ and $$$v=2$$$, it is optimal to not perform any new operations.
3,000
false
false
false
true
true
false
false
false
false
false
91
1408H
You are given a sequence $$$a_1, a_2, ldots, a_n$$$ of non-negative integers. You need to find the largest number $$$m$$$ of triples $$$(i_1, j_1, k_1)$$$, $$$(i_2, j_2, k_2)$$$, ..., $$$(i_m, j_m, k_m)$$$ such that: $$$1 leq i_p < j_p < k_p leq n$$$ for each $$$p$$$ in $$$1, 2, ldots, m$$$; $$$a_{i_p} = a_{k_p} = 0$$$, $$$a_{j_p} eq 0$$$; all $$$a_{j_1}, a_{j_2}, ldots, a_{j_m}$$$ are different; all $$$i_1, j_1, k_1, i_2, j_2, k_2, ldots, i_m, j_m, k_m$$$ are different. Input The first line of input contains one integer $$$t$$$ ($$$1 leq t leq 500,000$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 500,000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq n$$$). The total sum of $$$n$$$ is at most $$$500,000$$$. Output For each test case, print one integer $$$m$$$: the largest number of proper triples that you can find. Example Input 8 1 1 2 0 0 3 0 1 0 6 0 0 1 2 0 0 6 0 1 0 0 1 0 6 0 1 3 2 0 0 6 0 0 0 0 5 0 12 0 1 0 2 2 2 0 0 3 3 4 0 Note In the first two test cases, there are not enough elements even for a single triple, so the answer is $$$0$$$. In the third test case we can select one triple $$$(1, 2, 3)$$$. In the fourth test case we can select two triples $$$(1, 3, 5)$$$ and $$$(2, 4, 6)$$$. In the fifth test case we can select one triple $$$(1, 2, 3)$$$. We can't select two triples $$$(1, 2, 3)$$$ and $$$(4, 5, 6)$$$, because $$$a_2 = a_5$$$.
3,300
false
true
false
false
true
false
false
true
false
false
3,629
883H
Problem - 883H - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation strings *1800 No tag edit access → Contest materials ") — the length of string _s_. The second line contains a string _s_ of length _n_ consisting of lowercase and uppercase Latin letters and digits. Output Print to the first line an integer _k_ — minimum number of palindromes into which you can cut a given string. Print to the second line _k_ strings — the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length. Examples Input 6 aabaac Output 2 aba aca Input 8 0rTrT022 Output 1 02TrrT20 Input 2 aA Output 2 a A
1,800
false
false
true
false
false
false
true
false
false
false
6,220
1978F
Given an array $$$a$$$ of length $$$n$$$. Let's construct a square matrix $$$b$$$ of size $$$n imes n$$$, in which the $$$i$$$-th row contains the array $$$a$$$ cyclically shifted to the right by $$$(i - 1)$$$. For example, for the array $$$a = — 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 10^6$$$, $$$2 le k le 2 cdot 10^6$$$) — the length of the array and the parameter $$$k$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$) — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single integer — the number of connected components in the obtained graph. Example Input 6 3 3 3 4 5 3 3 3 4 9 3 2 3 4 9 2 2 2 8 5 3 8 27 5 4 3 4 10 2 2 2 2 Note In the first test case, the matrix $$$b$$$ is given in the statement. The first connected component contains the vertices $$$(1, 1)$$$, $$$(2, 2)$$$, and $$$(3, 3)$$$. The second connected component contains the vertices $$$(1, 2)$$$, $$$(2, 3)$$$, and $$$(3, 1)$$$. The third connected component contains the vertices $$$(1, 3)$$$, $$$(2, 1)$$$, and $$$(3, 2)$$$. Thus, the graph has $$$3$$$ connected components. In the second test case, the following matrix is obtained: $$$$$$b = begin{bmatrix} 3 & 4 & 9 9 & 3 & 4 4 & 9 & 3 end{bmatrix}$$$$$$ The first connected component contains all vertices corresponding to elements with values $$$3$$$ and $$$9$$$. The second connected component contains all vertices corresponding to elements with the value $$$4$$$. In the fourth test case, all vertices are in one connected component.
2,400
false
false
false
false
true
false
false
false
false
true
417
1583F
Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right? You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city. Liyue can be represented as a directed graph containing $$$n$$$ nodes. Nodes are labeled from $$$1$$$ to $$$n$$$. There is a directed edge from node $$$a$$$ to node $$$b$$$ if and only if $$$a < b$$$. A path between nodes $$$a$$$ and $$$b$$$ is defined as a sequence of edges such that you can start at $$$a$$$, travel along all of these edges in the corresponding direction, and end at $$$b$$$. The length of a path is defined by the number of edges. A rainbow path of length $$$x$$$ is defined as a path in the graph such that there exists at least 2 distinct colors among the set of $$$x$$$ edges. Teucer's favorite number is $$$k$$$. You are curious about the following scenario: If you were to label each edge with a color, what is the minimum number of colors needed to ensure that all paths of length $$$k$$$ or longer are rainbow paths? Teucer wants to surprise his older brother with a map of Liyue. He also wants to know a valid coloring of edges that uses the minimum number of colors. Please help him with this task! Input The only line of input contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq k < n leq 1000$$$). Output On the first line, output $$$c$$$, the minimum colors you need to satisfy the above requirements. On the second line, print a valid edge coloring as an array of $$$frac{n(n-1)}{2}$$$ integers ranging from $$$1$$$ to $$$c$$$. Exactly $$$c$$$ distinct colors should exist in the construction. Print the edges in increasing order by the start node first, then by the second node. For example, if $$$n=4$$$, the edge colors will correspond to this order of edges: ($$$1$$$, $$$2$$$), ($$$1$$$, $$$3$$$), ($$$1$$$, $$$4$$$), ($$$2$$$, $$$3$$$), ($$$2$$$, $$$4$$$), ($$$3$$$, $$$4$$$) Examples Output 2 1 2 2 2 2 2 2 1 1 1 Output 3 3 2 2 1 2 2 1 3 1 1 Output 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Note The corresponding construction for the first test case looks like this: It is impossible to satisfy the constraints with less than $$$2$$$ colors. The corresponding construction for the second test case looks like this: One can show there exists no construction using less than $$$3$$$ colors.
2,500
false
false
false
false
false
true
false
false
false
false
2,712
1864C
You are given an integer $$$x$$$. Your task is to reduce $$$x$$$ to $$$1$$$. To do that, you can do the following operation: select a divisor $$$d$$$ of $$$x$$$, then change $$$x$$$ to $$$x-d$$$, i.e. reduce $$$x$$$ by $$$d$$$. (We say that $$$d$$$ is a divisor of $$$x$$$ if $$$d$$$ is an positive integer and there exists an integer $$$q$$$ such that $$$x = d cdot q$$$.) There is an additional constraint: you cannot select the same value of $$$d$$$ more than twice. For example, for $$$x=5$$$, the following scheme is invalid because $$$1$$$ is selected more than twice: $$$5xrightarrow{-1}4xrightarrow{-1}3xrightarrow{-1}2xrightarrow{-1}1$$$. The following scheme is however a valid one: $$$5xrightarrow{-1}4xrightarrow{-2}2xrightarrow{-1}1$$$. Output any scheme which reduces $$$x$$$ to $$$1$$$ with at most $$$1000$$$ operations. It can be proved that such a scheme always exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The only line of each test case contains a single integer $$$x$$$ ($$$2le x le 10^{9}$$$). Output For each test case, output two lines. The first line should contain an integer $$$k$$$ ($$$1 le k le 1001$$$). The next line should contain $$$k$$$ integers $$$a_1,a_2,ldots,a_k$$$, which satisfy the following: $$$a_1=x$$$; $$$a_k=1$$$; for each $$$2 le i le k$$$, the value $$$(a_{i-1}-a_i)$$$ is a divisor of $$$a_{i-1}$$$. Each number may occur as a divisor at most twice. Example Output 3 3 2 1 4 5 4 2 1 6 14 12 6 3 2 1 Note In the first test case, we use the following scheme: $$$3xrightarrow{-1}2xrightarrow{-1}1$$$. In the second test case, we use the following scheme: $$$5xrightarrow{-1}4xrightarrow{-2}2xrightarrow{-1}1$$$. In the third test case, we use the following scheme: $$$14xrightarrow{-2}12xrightarrow{-6}6xrightarrow{-3}3xrightarrow{-1}2xrightarrow{-1}1$$$.
1,300
true
false
false
false
false
true
false
false
false
false
1,095
1891F
You are given a rooted tree with the root at vertex $$$1$$$, initially consisting of a single vertex. Each vertex has a numerical value, initially set to $$$0$$$. There are also $$$q$$$ queries of two types: The first type: add a child vertex with the number $$$sz + 1$$$ to vertex $$$v$$$, where $$$sz$$$ is the current size of the tree. The numerical value of the new vertex will be $$$0$$$. The second type: add $$$x$$$ to the numerical values of all vertices in the subtree of vertex $$$v$$$. After all queries, output the numerical value of all of the vertices in the final tree. Input The first line contains a single integer $$$T$$$ ($$$1 leq T leq 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$q$$$ ($$$1 leq q leq 5 cdot 10^5$$$) — the number of queries. The following $$$q$$$ lines can fall into two cases: The first type of query: The $$$i$$$-th line contains two integers $$$t_i$$$ ($$$t_i = 1$$$), $$$v_i$$$. You need to add a child with the number $$$sz + 1$$$ to vertex $$$v_i$$$, where $$$sz$$$ is the current size of the tree. It is guaranteed that $$$1 leq v_i leq sz$$$. The second type of query: The $$$i$$$-th line contains three integers $$$t_i$$$ ($$$t_i = 2$$$), $$$v_i$$$, $$$x_i$$$ ($$$-10^9 leq x_i leq 10^9$$$). You need to add $$$x_i$$$ to all numerical values of vertices in the subtree of $$$v_i$$$. It is guaranteed that $$$1 leq v_i leq sz$$$, where $$$sz$$$ is the current size of the tree. It is guaranteed that the sum of $$$q$$$ across all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output the numerical value of each vertex of the final tree after all queries have been performed. Example Input 3 9 2 1 3 1 1 2 2 1 1 1 2 3 2 1 3 2 1 4 1 3 2 3 2 5 2 1 1 1 1 2 1 -1 1 1 2 1 1 5 1 1 1 1 2 1 1 2 1 3 2 2 10 Output 7 5 8 6 2 1 0 1 4 14 4 Note In the first case, the final tree with the assigned numerical values will look like this: The final tree with the assigned numerical values
2,000
false
false
false
false
true
false
false
false
false
false
952
1400F
You are given an integer value $$$x$$$ and a string $$$s$$$ consisting of digits from $$$1$$$ to $$$9$$$ inclusive. A substring of a string is a contiguous subsequence of that string. Let $$$f(l, r)$$$ be the sum of digits of a substring $$$s[l..r]$$$. Let's call substring $$$s[l_1..r_1]$$$ $$$x$$$-prime if $$$f(l_1, r_1) = x$$$; there are no values $$$l_2, r_2$$$ such that $$$l_1 le l_2 le r_2 le r_1$$$; $$$f(l_2, r_2) eq x$$$; $$$x$$$ is divisible by $$$f(l_2, r_2)$$$. You are allowed to erase some characters from the string. If you erase a character, the two resulting parts of the string are concatenated without changing their order. What is the minimum number of characters you should erase from the string so that there are no $$$x$$$-prime substrings in it? If there are no $$$x$$$-prime substrings in the given string $$$s$$$, then print $$$0$$$. Input The first line contains a string $$$s$$$ ($$$1 le s le 1000$$$). $$$s$$$ contains only digits from $$$1$$$ to $$$9$$$ inclusive. The second line contains an integer $$$x$$$ ($$$1 le x le 20$$$). Output Print a single integerxa0— the minimum number of characters you should erase from the string so that there are no $$$x$$$-prime substrings in it. If there are no $$$x$$$-prime substrings in the given string $$$s$$$, then print $$$0$$$. Note In the first example there are two $$$8$$$-prime substrings "8" and "53". You can erase these characters to get rid of both: "116285317". The resulting string "1162317" contains no $$$8$$$-prime substrings. Removing these characters is also a valid answer: "116285317". In the second example you just have to erase both ones. In the third example there are no $$$13$$$-prime substrings. There are no substrings with the sum of digits equal to $$$13$$$ at all. In the fourth example you can have neither "34", nor "43" in a string. Thus, you have to erase either all threes or all fours. There are $$$5$$$ of each of them, so it doesn't matter which.
2,800
false
false
false
true
false
false
true
false
false
false
3,667
268C
Problem - 268C - 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 implementation *1500 No tag edit access → Contest materials ") — Problems Analysis") — Разбор задач") which satisfy the inequations: 0u2009≤u2009_x_u2009≤u2009_n_; 0u2009≤u2009_y_u2009≤u2009_m_; _x_u2009+u2009_y_u2009>u20090. Choose their subset of maximum size such that it is also a beautiful set of points. Input The single line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100). Output In the first line print a single integer — the size _k_ of the found beautiful set. In each of the next _k_ lines print a pair of space-separated integers — the _x_- and _y_- coordinates, respectively, of a point from the set. If there are several optimal solutions, you may print any of them. Examples Input 2 2 Output 3 0 1 1 2 2 0 Input 4 3 Output 4 0 3 2 1 3 0 4 2 Note Consider the first sample. The distance between points (0, 1) and (1, 2) equals , between (0, 1) and (2, 0) — , between (1, 2) and (2, 0) — . Thus, these points form a beautiful set. You cannot form a beautiful set with more than three points out of the given points. Note that this is not the only solution.
1,500
false
false
true
false
false
true
false
false
false
false
8,761
853A
Helen works in Metropolis airport. She is responsible for creating a departure schedule. There are _n_ flights that must depart today, the _i_-th of them is planned to depart at the _i_-th minute of the day. Metropolis airport is the main transport hub of Metropolia, so it is difficult to keep the schedule intact. This is exactly the case today: because of technical issues, no flights were able to depart during the first _k_ minutes of the day, so now the new departure schedule must be created. All _n_ scheduled flights must now depart at different minutes between (_k_u2009+u20091)-th and (_k_u2009+u2009_n_)-th, inclusive. However, it's not mandatory for the flights to depart in the same order they were initially scheduled to do soxa0— their order in the new schedule can be different. There is only one restriction: no flight is allowed to depart earlier than it was supposed to depart in the initial schedule. Helen knows that each minute of delay of the _i_-th flight costs airport _c__i_ burles. Help her find the order for flights to depart in the new schedule that minimizes the total cost for the airport. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009300u2009000), here _n_ is the number of flights, and _k_ is the number of minutes in the beginning of the day that the flights did not depart. The second line contains _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u2009107), here _c__i_ is the cost of delaying the _i_-th flight for one minute. Output The first line must contain the minimum possible total cost of delaying the flights. The second line must contain _n_ different integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (_k_u2009+u20091u2009≤u2009_t__i_u2009≤u2009_k_u2009+u2009_n_), here _t__i_ is the minute when the _i_-th flight must depart. If there are several optimal schedules, print any of them. Note Let us consider sample test. If Helen just moves all flights 2 minutes later preserving the order, the total cost of delaying the flights would be (3u2009-u20091)·4u2009+u2009(4u2009-u20092)·2u2009+u2009(5u2009-u20093)·1u2009+u2009(6u2009-u20094)·10u2009+u2009(7u2009-u20095)·2u2009=u200938 burles. However, the better schedule is shown in the sample answer, its cost is (3u2009-u20091)·4u2009+u2009(6u2009-u20092)·2u2009+u2009(7u2009-u20093)·1u2009+u2009(4u2009-u20094)·10u2009+u2009(5u2009-u20095)·2u2009=u200920 burles.
1,500
false
true
false
false
false
false
false
false
false
false
6,334
182B
Vasya lives in a strange world. The year has _n_ months and the _i_-th month has _a__i_ days. Vasya got a New Year present — the clock that shows not only the time, but also the date. The clock's face can display any number from 1 to _d_. It is guaranteed that _a__i_u2009≤u2009_d_ for all _i_ from 1 to _n_. The clock does not keep information about the current month, so when a new day comes, it simply increases the current day number by one. The clock cannot display number _d_u2009+u20091, so after day number _d_ it shows day 1 (the current day counter resets). The mechanism of the clock allows you to increase the day number by one manually. When you execute this operation, day _d_ is also followed by day 1. Vasya begins each day checking the day number on the clock. If the day number on the clock does not match the actual day number in the current month, then Vasya manually increases it by one. Vasya is persistent and repeats this operation until the day number on the clock matches the actual number of the current day in the current month. A year passed and Vasya wonders how many times he manually increased the day number by one, from the first day of the first month to the last day of the _n_-th month inclusive, considering that on the first day of the first month the clock display showed day 1. Input The first line contains the single number _d_ — the maximum number of the day that Vasya's clock can show (1u2009≤u2009_d_u2009≤u2009106). The second line contains a single integer _n_ — the number of months in the year (1u2009≤u2009_n_u2009≤u20092000). The third line contains _n_ space-separated integers: _a__i_ (1u2009≤u2009_a__i_u2009≤u2009_d_) — the number of days in each month in the order in which they follow, starting from the first one. Output Print a single number — the number of times Vasya manually increased the day number by one throughout the last year. Examples Input 31 12 31 28 31 30 31 30 31 31 30 31 30 31 Note In the first sample the situation is like this: Day 1. Month 1. The clock shows 1. Vasya changes nothing. Day 2. Month 1. The clock shows 2. Vasya changes nothing. Day 1. Month 2. The clock shows 3. Vasya manually increases the day number by 1. After that the clock shows 4. Vasya increases the day number by 1 manually. After that the clock shows 1. Day 2. Month 2. The clock shows 2. Vasya changes nothing. In total, Vasya manually changed the day number by 1 exactly 2 times.
1,000
false
false
true
false
false
false
false
false
false
false
9,113
1624E
Masha meets a new friend and learns his phone numberxa0— $$$s$$$. She wants to remember it as soon as possible. The phone numberxa0— is a string of length $$$m$$$ that consists of digits from $$$0$$$ to $$$9$$$. The phone number may start with 0. Masha already knows $$$n$$$ phone numbers (all numbers have the same length $$$m$$$). It will be easier for her to remember a new number if the $$$s$$$ is represented as segments of numbers she already knows. Each such segment must be of length at least $$$2$$$, otherwise there will be too many segments and Masha will get confused. For example, Masha needs to remember the number: $$$s = $$$ '12345678' and she already knows $$$n = 4$$$ numbers: '12340219', '20215601', '56782022', '12300678'. You can represent $$$s$$$ as a $$$3$$$ segment: '1234' of number one, '56' of number two, and '78' of number three. There are other ways to represent $$$s$$$. Masha asks you for help, she asks you to break the string $$$s$$$ into segments of length $$$2$$$ or more of the numbers she already knows. If there are several possible answers, print any of them. Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0—the number of test cases. Before each test case there is a blank line. Then there is a line containing integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^3$$$)xa0—the number of phone numbers that Masha knows and the number of digits in each phone number. Then follow $$$n$$$ line, $$$i$$$-th of which describes the $$$i$$$-th number that Masha knows. The next line contains the phone number of her new friend $$$s$$$. Among the given $$$n+1$$$ phones, there may be duplicates (identical phones). It is guaranteed that the sum of $$$n cdot m$$$ ($$$n$$$ multiplied by $$$m$$$) values over all input test cases does not exceed $$$10^6$$$. Output You need to print the answers to $$$t$$$ test cases. The first line of the answer should contain one number $$$k$$$, corresponding to the number of segments into which you split the phone number $$$s$$$. Print -1 if you cannot get such a split. If the answer is yes, then follow $$$k$$$ lines containing triples of numbers $$$l, r, i$$$. Such triplets mean that the next $$$r-l+1$$$ digits of number $$$s$$$ are equal to a segment (substring) with boundaries $$$[l, r]$$$ of the phone under number $$$i$$$. Both the phones and the digits in them are numbered from $$$1$$$. Note that $$$r-l+1 ge 2$$$ for all $$$k$$$ lines. Example Input 5 4 8 12340219 20215601 56782022 12300678 12345678 2 3 134 126 123 1 4 1210 1221 4 3 251 064 859 957 054 4 7 7968636 9486033 4614224 5454197 9482268 Output 3 1 4 1 5 6 2 3 4 3 -1 2 1 2 1 2 3 1 -1 3 1 3 2 5 6 3 3 4 1 Note The example from the statement. In the second case, it is impossible to represent by segments of known numbers of length 2 or more. In the third case, you can get the segments '12' and '21' from the first phone number.
2,000
false
false
true
true
false
true
true
false
false
false
2,496
1004B
Sonya decided to organize an exhibition of flowers. Since the girl likes only roses and lilies, she decided that only these two kinds of flowers should be in this exhibition. There are $$$n$$$ flowers in a row in the exhibition. Sonya can put either a rose or a lily in the $$$i$$$-th position. Thus each of $$$n$$$ positions should contain exactly one flower: a rose or a lily. She knows that exactly $$$m$$$ people will visit this exhibition. The $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. The girl knows that each segment has its own beauty that is equal to the product of the number of roses and the number of lilies. Sonya wants her exhibition to be liked by a lot of people. That is why she wants to put the flowers in such way that the sum of beauties of all segments would be maximum possible. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1leq n, mleq 10^3$$$)xa0— the number of flowers and visitors respectively. Each of the next $$$m$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1leq l_ileq r_ileq n$$$), meaning that $$$i$$$-th visitor will visit all flowers from $$$l_i$$$ to $$$r_i$$$ inclusive. Output Print the string of $$$n$$$ characters. The $$$i$$$-th symbol should be «0» if you want to put a rose in the $$$i$$$-th position, otherwise «1» if you want to put a lily. If there are multiple answers, print any. Note In the first example, Sonya can put roses in the first, fourth, and fifth positions, and lilies in the second and third positions; in the segment $$$[1ldots3]$$$, there are one rose and two lilies, so the beauty is equal to $$$1cdot 2=2$$$; in the segment $$$[2ldots4]$$$, there are one rose and two lilies, so the beauty is equal to $$$1cdot 2=2$$$; in the segment $$$[2ldots5]$$$, there are two roses and two lilies, so the beauty is equal to $$$2cdot 2=4$$$. The total beauty is equal to $$$2+2+4=8$$$. In the second example, Sonya can put roses in the third, fourth, and sixth positions, and lilies in the first, second, and fifth positions; in the segment $$$[5ldots6]$$$, there are one rose and one lily, so the beauty is equal to $$$1cdot 1=1$$$; in the segment $$$[1ldots4]$$$, there are two roses and two lilies, so the beauty is equal to $$$2cdot 2=4$$$; in the segment $$$[4ldots6]$$$, there are two roses and one lily, so the beauty is equal to $$$2cdot 1=2$$$. The total beauty is equal to $$$1+4+2=7$$$.
1,300
true
true
true
false
false
true
false
false
false
false
5,667
435C
In this problem, your task is to use ASCII graphics to paint a cardiogram. A cardiogram is a polyline with the following corners: That is, a cardiogram is fully defined by a sequence of positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Your task is to paint a cardiogram by given sequence _a__i_. Input The first line contains integer _n_ (2u2009≤u2009_n_u2009≤u20091000). The next line contains the sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091000). It is guaranteed that the sum of all _a__i_ doesn't exceed 1000. Output Print _max_xa0_y__i_u2009-u2009_y__j_ lines (where _y__k_ is the _y_ coordinate of the _k_-th point of the polyline), in each line print characters. Each character must equal either «u2009/u2009» (slash), « » (backslash), « » (space). The printed image must be the image of the given polyline. Please study the test samples for better understanding of how to print a cardiogram. Note that in this problem the checker checks your answer taking spaces into consideration. Do not print any extra characters. Remember that the wrong answer to the first pretest doesn't give you a penalty. Examples Output u2009/u2009 u2009/u2009u2009/u2009 u2009/u2009 u2009/u2009 u2009/u2009 Note Due to the technical reasons the answers for the samples cannot be copied from the statement. We've attached two text documents with the answers below.
1,600
false
false
true
false
false
false
false
false
false
false
8,100
1334E
You are given a positive integer $$$D$$$. Let's build the following graph from it: each vertex is a divisor of $$$D$$$ (not necessarily prime, $$$1$$$ and $$$D$$$ itself are also included); two vertices $$$x$$$ and $$$y$$$ ($$$x > y$$$) have an undirected edge between them if $$$x$$$ is divisible by $$$y$$$ and $$$frac x y$$$ is a prime; the weight of an edge is the number of divisors of $$$x$$$ that are not divisors of $$$y$$$. For example, here is the graph for $$$D=12$$$: Edge $$$(4,12)$$$ has weight $$$3$$$ because $$$12$$$ has divisors $$$[1,2,3,4,6,12]$$$ and $$$4$$$ has divisors $$$[1,2,4]$$$. Thus, there are $$$3$$$ divisors of $$$12$$$ that are not divisors of $$$4$$$ — $$$[3,6,12]$$$. There is no edge between $$$3$$$ and $$$2$$$ because $$$3$$$ is not divisible by $$$2$$$. There is no edge between $$$12$$$ and $$$3$$$ because $$$frac{12}{3}=4$$$ is not a prime. Let the length of the path between some vertices $$$v$$$ and $$$u$$$ in the graph be the total weight of edges on it. For example, path $$$[(1, 2), (2, 6), (6, 12), (12, 4), (4, 2), (2, 6)]$$$ has length $$$1+2+2+3+1+2=11$$$. The empty path has length $$$0$$$. So the shortest path between two vertices $$$v$$$ and $$$u$$$ is the path that has the minimal possible length. Two paths $$$a$$$ and $$$b$$$ are different if there is either a different number of edges in them or there is a position $$$i$$$ such that $$$a_i$$$ and $$$b_i$$$ are different edges. You are given $$$q$$$ queries of the following form: $$$v$$$ $$$u$$$ — calculate the number of the shortest paths between vertices $$$v$$$ and $$$u$$$. The answer for each query might be large so print it modulo $$$998244353$$$. Input The first line contains a single integer $$$D$$$ ($$$1 le D le 10^{15}$$$) — the number the graph is built from. The second line contains a single integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$) — the number of queries. Each of the next $$$q$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le D$$$). It is guaranteed that $$$D$$$ is divisible by both $$$v$$$ and $$$u$$$ (both $$$v$$$ and $$$u$$$ are divisors of $$$D$$$).
2,200
true
true
false
false
false
false
false
false
false
true
4,033
1998E2
Drink water. — Sun Tzu, The Art of Becoming a Healthy Programmer This is the hard version of the problem. The only difference is that $$$x=1$$$ in this version. You must solve both versions to be able to hack. You are given two integers $$$n$$$ and $$$x$$$ ($$$x=1$$$). There are $$$n$$$ balls lined up in a row, numbered from $$$1$$$ to $$$n$$$ from left to right. Initially, there is a value $$$a_i$$$ written on the $$$i$$$-th ball. For each integer $$$i$$$ from $$$1$$$ to $$$n$$$, we define a function $$$f(i)$$$ as follows: Suppose you have a set $$$S = {1, 2, ldots, i}$$$. In each operation, you have to select an integer $$$l$$$ ($$$1 leq l < i$$$) from $$$S$$$ such that $$$l$$$ is not the largest element of $$$S$$$. Suppose $$$r$$$ is the smallest element in $$$S$$$ which is greater than $$$l$$$. If $$$a_l > a_r$$$, you set $$$a_l = a_l + a_r$$$ and remove $$$r$$$ from $$$S$$$. If $$$a_l < a_r$$$, you set $$$a_r = a_l + a_r$$$ and remove $$$l$$$ from $$$S$$$. If $$$a_l = a_r$$$, you choose either the integer $$$l$$$ or $$$r$$$ to remove from $$$S$$$: If you choose to remove $$$l$$$ from $$$S$$$, you set $$$a_r = a_l + a_r$$$ and remove $$$l$$$ from $$$S$$$. If you choose to remove $$$r$$$ from $$$S$$$, you set $$$a_l = a_l + a_r$$$ and remove $$$r$$$ from $$$S$$$. $$$f(i)$$$ denotes the number of integers $$$j$$$ ($$$1 le j le i$$$) such that it is possible to obtain $$$S = {j}$$$ after performing the above operations exactly $$$i - 1$$$ times. For each integer $$$i$$$ from $$$x$$$ to $$$n$$$, you need to find $$$f(i)$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 leq n leq 2 cdot 10^5; x = 1$$$)xa0— the number of balls and the smallest index $$$i$$$ for which you need to find $$$f(i)$$$. The second line of each test case contains $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the initial number written on each ball. 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-x+1$$$ space separated integers on a new line, where the $$$j$$$-th integer should represent $$$f(x+j-1)$$$. Example Input 3 5 1 1 2 3 2 1 7 1 4 5 1 2 1 4 5 11 1 1 2 3 1 1 9 3 2 4 1 3 Output 1 1 2 2 3 1 1 1 1 1 3 4 1 1 2 2 2 1 1 1 3 3 4 Note In the first test case, below are the possible values of $$$j$$$ for each $$$f(i)$$$ from $$$1$$$ to $$$n$$$. For $$$f(1)$$$, the only possible value of $$$j$$$ is $$$1$$$. For $$$f(2)$$$, the only possible value of $$$j$$$ is $$$2$$$. For $$$f(3)$$$, the possible values of $$$j$$$ are $$$2$$$ and $$$3$$$. For $$$f(4)$$$, the possible values of $$$j$$$ are $$$2$$$ and $$$3$$$. For $$$f(5)$$$, the possible values of $$$j$$$ are $$$2$$$, $$$3$$$, and $$$4$$$.
2,500
false
true
true
false
true
false
true
true
false
false
272
1165D
We guessed some integer number $$$x$$$. You are given a list of almost all its divisors. Almost all means that there are all divisors except $$$1$$$ and $$$x$$$ in the list. Your task is to find the minimum possible integer $$$x$$$ that can be the guessed number, or say that the input data is contradictory and it is impossible to find such number. You have to answer $$$t$$$ independent queries. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 25$$$) — the number of queries. Then $$$t$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 300$$$) — the number of divisors in the list. The second line of the query contains $$$n$$$ integers $$$d_1, d_2, dots, d_n$$$ ($$$2 le d_i le 10^6$$$), where $$$d_i$$$ is the $$$i$$$-th divisor of the guessed number. It is guaranteed that all values $$$d_i$$$ are distinct. Output For each query print the answer to it. If the input data in the query is contradictory and it is impossible to find such number $$$x$$$ that the given list of divisors is the list of almost all its divisors, print -1. Otherwise print the minimum possible $$$x$$$. Example Input 2 8 8 2 12 6 4 24 16 3 1 2
1,600
true
false
false
false
false
false
false
false
false
false
4,893
1994C
Yaroslav is playing a computer game, and at one of the levels, he encountered $$$n$$$ mushrooms arranged in a row. Each mushroom has its own level of toxicity; the $$$i$$$-th mushroom from the beginning has a toxicity level of $$$a_i$$$. Yaroslav can choose two integers $$$1 le l le r le n$$$, and then his character will take turns from left to right to eat mushrooms from this subsegment one by one, i.e., the mushrooms with numbers $$$l, l+1, l+2, ldots, r$$$. The character has a toxicity level $$$g$$$, initially equal to $$$0$$$. The computer game is defined by the number $$$x$$$xa0— the maximum toxicity level at any given time. When eating a mushroom with toxicity level $$$k$$$, the following happens: 1. The toxicity level of the character is increased by $$$k$$$. 2. If $$$g leq x$$$, the process continues; otherwise, $$$g$$$ becomes zero and the process continues. Yaroslav became interested in how many ways there are to choose the values of $$$l$$$ and $$$r$$$ such that the final value of $$$g$$$ is not zero. Help Yaroslav find this number! Input Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^{4}$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $$$n$$$, $$$x$$$ ($$$1 leq n leq 2 cdot 10^5, 1 le x le 10^9$$$)xa0— the number of mushrooms and the maximum toxicity level. The second line of each test case contains $$$n$$$ numbers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single numberxa0— the number of subsegments such that the final value of $$$g$$$ will not be zero. Example Input 5 4 2 1 1 1 1 3 2 1 2 3 1 6 10 6 3 1 2 1 4 3 8 5 999999999 999999999 999999998 1000000000 1000000000 500000000 Note In the first test case, the subsegments $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$ are suitable. In the second test case, non-zero $$$g$$$ will remain only on the subsegments $$$(1, 1)$$$ and $$$(2, 2)$$$. In the third test case, on the only possible subsegment, $$$g$$$ will be zero.
1,600
false
false
false
true
false
false
false
true
false
false
303
1488B
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. You are given a string $$$s$$$, which is an RBS. You can apply any number of operations to this string. Each operation can have one of the following types: 1. choose some non-empty prefix of $$$s$$$ and remove it from $$$s$$$, so $$$s$$$ is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" $$$ o$$$ "()()" (the first $$$10$$$ characters are removed); 2. choose some contiguous non-empty substring of $$$s$$$ and remove it from $$$s$$$, so $$$s$$$ is still an RBS. For example, we can apply this operation as follows: "(())()(())()()" $$$ o$$$ "(())()()()" (the characters from the $$$7$$$-th to the $$$10$$$-th are removed). The operation $$$2$$$ can be applied at most $$$k$$$ times. Calculate the maximum number of operations you can apply until $$$s$$$ becomes empty. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. Each test case is described by two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le k le n$$$; $$$n$$$ is even) — the length of $$$s$$$ and the maximum number of operations of type $$$2$$$ you can apply. The second line contains a string $$$s$$$ of $$$n$$$ characters '(' and ')'. This string is an RBS. The sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print one integer — the maximum number of operations you can apply. Example Input 3 12 2 (()())((())) 6 3 ()()() 8 1 (((())))
1,800
false
true
false
false
false
false
false
false
false
false
3,238
577A
Problem - 577A - 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 number theory *1000 No tag edit access → Contest materials — the size of the table and the number that we are looking for in the table. Output Print a single number: the number of times _x_ occurs in the table. Examples Input 10 5 Output 2 Input 6 12 Output 4 Input 5 13 Output 0 Note A table for the second sample test is given below. The occurrences of number 12 are marked bold.
1,000
false
false
true
false
false
false
false
false
false
false
7,538
740B
Little Alyona is celebrating Happy Birthday! Her mother has an array of _n_ flowers. Each flower has some mood, the mood of _i_-th flower is _a__i_. The mood can be positive, zero or negative. Let's define a subarray as a segment of consecutive flowers. The mother suggested some set of subarrays. Alyona wants to choose several of the subarrays suggested by her mother. After that, each of the flowers will add to the girl's happiness its mood multiplied by the number of chosen subarrays the flower is in. For example, consider the case when the mother has 5 flowers, and their moods are equal to 1,u2009u2009-u20092,u20091,u20093,u2009u2009-u20094. Suppose the mother suggested subarrays (1,u2009u2009-u20092), (3,u2009u2009-u20094), (1,u20093), (1,u2009u2009-u20092,u20091,u20093). Then if the girl chooses the third and the fourth subarrays then: the first flower adds 1·1u2009=u20091 to the girl's happiness, because he is in one of chosen subarrays, the second flower adds (u2009-u20092)·1u2009=u2009u2009-u20092, because he is in one of chosen subarrays, the third flower adds 1·2u2009=u20092, because he is in two of chosen subarrays, the fourth flower adds 3·2u2009=u20096, because he is in two of chosen subarrays, the fifth flower adds (u2009-u20094)·0u2009=u20090, because he is in no chosen subarrays. Thus, in total 1u2009+u2009(u2009-u20092)u2009+u20092u2009+u20096u2009+u20090u2009=u20097 is added to the girl's happiness. Alyona wants to choose such subarrays from those suggested by the mother that the value added to her happiness would be as large as possible. Help her do this! Alyona can choose any number of the subarrays, even 0 or all suggested by her mother. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100)xa0— the number of flowers and the number of subarrays suggested by the mother. The second line contains the flowers moodsxa0— _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009100u2009≤u2009_a__i_u2009≤u2009100). The next _m_ lines contain the description of the subarrays suggested by the mother. The _i_-th of these lines contain two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_) denoting the subarray _a_[_l__i_],u2009_a_[_l__i_u2009+u20091],u2009...,u2009_a_[_r__i_]. Each subarray can encounter more than once. Note The first example is the situation described in the statements. In the second example Alyona should choose all subarrays. The third example has answer 0 because Alyona can choose none of the subarrays.
1,200
false
false
false
false
false
true
false
false
false
false
6,837
1263F
An electrical grid in Berland palaces consists of 2 grids: main and reserve. Wires in palaces are made of expensive material, so selling some of them would be a good idea! Each grid (main and reserve) has a head node (its number is $$$1$$$). Every other node gets electricity from the head node. Each node can be reached from the head node by a unique path. Also, both grids have exactly $$$n$$$ nodes, which do not spread electricity further. In other words, every grid is a rooted directed tree on $$$n$$$ leaves with a root in the node, which number is $$$1$$$. Each tree has independent enumeration and nodes from one grid are not connected with nodes of another grid. Also, the palace has $$$n$$$ electrical devices. Each device is connected with one node of the main grid and with one node of the reserve grid. Devices connect only with nodes, from which electricity is not spread further (these nodes are the tree's leaves). Each grid's leaf is connected with exactly one device. In this example the main grid contains $$$6$$$ nodes (the top tree) and the reserve grid contains $$$4$$$ nodes (the lower tree). There are $$$3$$$ devices with numbers colored in blue. It is guaranteed that the whole grid (two grids and $$$n$$$ devices) can be shown in this way (like in the picture above): main grid is a top tree, whose wires are directed 'from the top to the down', reserve grid is a lower tree, whose wires are directed 'from the down to the top', devicesxa0— horizontal row between two grids, which are numbered from $$$1$$$ to $$$n$$$ from the left to the right, wires between nodes do not intersect. Formally, for each tree exists a depth-first search from the node with number $$$1$$$, that visits leaves in order of connection to devices $$$1, 2, dots, n$$$ (firstly, the node, that is connected to the device $$$1$$$, then the node, that is connected to the device $$$2$$$, etc.). Businessman wants to sell (remove) maximal amount of wires so that each device will be powered from at least one grid (main or reserve). In other words, for each device should exist at least one path to the head node (in the main grid or the reserve grid), which contains only nodes from one grid. Input The first line contains an integer $$$n$$$ ($$$1 le n le 1000$$$) — the number of devices in the palace. The next line contains an integer $$$a$$$ ($$$1 + n le a le 1000 + n$$$) — the amount of nodes in the main grid. Next line contains $$$a - 1$$$ integers $$$p_i$$$ ($$$1 le p_i le a$$$). Each integer $$$p_i$$$ means that the main grid contains a wire from $$$p_i$$$-th node to $$$(i + 1)$$$-th. The next line contains $$$n$$$ integers $$$x_i$$$ ($$$1 le x_i le a$$$) — the number of a node in the main grid that is connected to the $$$i$$$-th device. The next line contains an integer $$$b$$$ ($$$1 + n le b le 1000 + n$$$) — the amount of nodes in the reserve grid. Next line contains $$$b - 1$$$ integers $$$q_i$$$ ($$$1 le q_i le b$$$). Each integer $$$q_i$$$ means that the reserve grid contains a wire from $$$q_i$$$-th node to $$$(i + 1)$$$-th. The next line contains $$$n$$$ integers $$$y_i$$$ ($$$1 le y_i le b$$$) — the number of a node in the reserve grid that is connected to the $$$i$$$-th device. It is guaranteed that each grid is a tree, which has exactly $$$n$$$ leaves and each leaf is connected with one device. Also, it is guaranteed, that for each tree exists a depth-first search from the node $$$1$$$, that visits leaves in order of connection to devices. Note For the first example, the picture below shows one of the possible solutions (wires that can be removed are marked in red): The second and the third examples can be seen below:
2,400
false
false
false
true
true
false
false
false
false
true
4,382
1638C
You are given a permutation $$$p_1, p_2, dots, p_n$$$. Then, an undirected graph is constructed in the following way: add an edge between vertices $$$i$$$, $$$j$$$ such that $$$i < j$$$ if and only if $$$p_i > p_j$$$. Your task is to count the number of connected components in this graph. Two vertices $$$u$$$ and $$$v$$$ belong to the same connected component if and only if there is at least one path along edges connecting $$$u$$$ and $$$v$$$. A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the permutation. The second line of each test case contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$)xa0— the elements of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integer $$$k$$$xa0— the number of connected components. Example Input 6 3 1 2 3 5 2 1 4 3 5 6 6 1 4 2 5 3 1 1 6 3 2 1 6 5 4 5 3 1 5 2 4 Note Each separate test case is depicted in the image below. The colored squares represent the elements of the permutation. For one permutation, each color represents some connected component. The number of distinct colors is the answer.
1,300
true
false
false
false
true
false
false
false
false
true
2,424
873B
Problem - 873B - Codeforces =============== xa0 equals to the number of ones in this substring. You have to determine the length of the longest balanced substring of _s_. Input The first line contains _n_ (1u2009≤u2009_n_u2009≤u2009100000) — the number of characters in _s_. The second line contains a string _s_ consisting of exactly _n_ characters. Only characters 0 and 1 can appear in _s_. Output If there is no non-empty balanced substring in _s_, print 0. Otherwise, print the length of the longest balanced substring. Examples Input 8 11010111 Output 4 Input 3 111 Output 0 Note In the first example you can choose the substring
1,500
false
false
true
true
false
false
false
false
false
false
6,253
188H
Problem - 188H - Codeforces =============== xa0 on them and push the result on the stack. You are given a string which describes the sequence of operations to be performed on the stack. _i_-th character corresponds to _i_-th operation: If _i_-th character is a digit, push the corresponding number on the stack. If _i_-th character is «+» or «*», perform the corresponding operation. Initially the stack is empty. Output the topmost number on the stack after executing all given operations. Input The only line of input contains a string of operations, consisting of characters «+», «*» and digits (0..9). The length of the string will be between 1 and 20 characters, inclusive. The given sequence of operations is guaranteed to be correct, i.e. the stack will have at least two elements before every math operation. The numbers on the stack will never exceed 106. Output Output a single number — the topmost element of the stack after performing all given operations. Examples Input 12+3*66*+ Output 45 Input 149 Output 9 Note In the first case the stack will end up containing a single number — the result of calculating (1+2)*3+6*6. In the second case there are no math operations, so the answer will be the last number pushed on the stack.
1,800
false
false
true
false
false
false
false
false
false
false
9,085
1598E
You are given a matrix, consisting of $$$n$$$ rows and $$$m$$$ columns. The rows are numbered top to bottom, the columns are numbered left to right. Each cell of the matrix can be either free or locked. Let's call a path in the matrix a staircase if it: starts and ends in the free cell; visits only free cells; has one of the two following structures: 1. the second cell is $$$1$$$ to the right from the first one, the third cell is $$$1$$$ to the bottom from the second one, the fourth cell is $$$1$$$ to the right from the third one, and so on; 2. the second cell is $$$1$$$ to the bottom from the first one, the third cell is $$$1$$$ to the right from the second one, the fourth cell is $$$1$$$ to the bottom from the third one, and so on. In particular, a path, consisting of a single cell, is considered to be a staircase. Here are some examples of staircases: Initially all the cells of the matrix are free. You have to process $$$q$$$ queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free. Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 le n, m le 1000$$$; $$$1 le q le 10^4$$$)xa0— the sizes of the matrix and the number of queries. Each of the next $$$q$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x le n$$$; $$$1 le y le m$$$)xa0— the description of each query.
2,100
true
false
true
true
true
false
true
false
false
false
2,667
774F
Stepan has _n_ pens. Every day he uses them, and on the _i_-th day he uses the pen number _i_. On the (_n_u2009+u20091)-th day again he uses the pen number 1, on the (_n_u2009+u20092)-th — he uses the pen number 2 and so on. On every working day (from Monday to Saturday, inclusive) Stepan spends exactly 1 milliliter of ink of the pen he uses that day. On Sunday Stepan has a day of rest, he does not stend the ink of the pen he uses that day. Stepan knows the current volume of ink in each of his pens. Now it's the Monday morning and Stepan is going to use the pen number 1 today. Your task is to determine which pen will run out of ink before all the rest (that is, there will be no ink left in it), if Stepan will use the pens according to the conditions described above. Input The first line contains the integer _n_ (1u2009≤u2009_n_u2009≤u200950u2009000) — the number of pens Stepan has. The second line contains the sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is equal to the number of milliliters of ink which the pen number _i_ currently has. Output Print the index of the pen which will run out of ink before all (it means that there will be no ink left in it), if Stepan will use pens according to the conditions described above. Pens are numbered in the order they are given in input data. The numeration begins from one. Note that the answer is always unambiguous, since several pens can not end at the same time. Note In the first test Stepan uses ink of pens as follows: 1. on the day number 1 (Monday) Stepan will use the pen number 1, after that there will be 2 milliliters of ink in it; 2. on the day number 2 (Tuesday) Stepan will use the pen number 2, after that there will be 2 milliliters of ink in it; 3. on the day number 3 (Wednesday) Stepan will use the pen number 3, after that there will be 2 milliliters of ink in it; 4. on the day number 4 (Thursday) Stepan will use the pen number 1, after that there will be 1 milliliters of ink in it; 5. on the day number 5 (Friday) Stepan will use the pen number 2, after that there will be 1 milliliters of ink in it; 6. on the day number 6 (Saturday) Stepan will use the pen number 3, after that there will be 1 milliliters of ink in it; 7. on the day number 7 (Sunday) Stepan will use the pen number 1, but it is a day of rest so he will not waste ink of this pen in it; 8. on the day number 8 (Monday) Stepan will use the pen number 2, after that this pen will run out of ink. So, the first pen which will not have ink is the pen number 2.
2,700
false
false
false
false
false
false
false
true
false
false
6,676
452C
Alex enjoys performing magic tricks. He has a trick that requires a deck of _n_ cards. He has _m_ identical decks of _n_ different cards each, which have been mixed together. When Alex wishes to perform the trick, he grabs _n_ cards at random and performs the trick with those. The resulting deck looks like a normal deck, but may have duplicates of some cards. The trick itself is performed as follows: first Alex allows you to choose a random card from the deck. You memorize the card and put it back in the deck. Then Alex shuffles the deck, and pulls out a card. If the card matches the one you memorized, the trick is successful. You don't think Alex is a very good magician, and that he just pulls a card randomly from the deck. Determine the probability of the trick being successful if this is the case. Input First line of the input consists of two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000), separated by space — number of cards in each deck, and number of decks. Output On the only line of the output print one floating point number – probability of Alex successfully performing the trick. Relative or absolute error of your answer should not be higher than 10u2009-u20096. Note In the first sample, with probability Alex will perform the trick with two cards with the same value from two different decks. In this case the trick is guaranteed to succeed. With the remaining probability he took two different cards, and the probability of pulling off the trick is . The resulting probability is
2,100
true
false
false
false
false
false
false
false
false
false
8,030
1916E
Egor and his friend Arseniy are finishing school this year and will soon enter university. And since they are very responsible guys, they have started preparing for admission already. First of all, they decided to take care of where they will live for the long four years of study, and after visiting the university's website, they found out that the university dormitory can be represented as a root tree with $$$n$$$ vertices with the root at vertex $$$1$$$. In the tree, each vertex represents a recreation with some type of activity $$$a_i$$$. The friends need to choose $$$2$$$ recreations (not necessarily different) in which they will settle. The guys are convinced that the more the value of the following function $$$f(u, v) = diff(u, lca(u, v)) cdot diff(v, lca(u, v))$$$, the more fun their life will be. Help Egor and Arseniy and find the maximum value of $$$f(u, v)$$$ among all pairs of recreations! $$$^{dagger} diff(u, v)$$$xa0— the number of different activities listed on the simple path from vertex $$$u$$$ to vertex $$$v$$$. $$$^{dagger} lca(u, v)$$$xa0— a vertex $$$p$$$ such that it is at the maximum distance from the root and is a parent of both vertex $$$u$$$ and vertex $$$v$$$. Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^{5}$$$). The second line of each test case contains $$${n - 1}$$$ integers $$$p_2, p_3, ldots,p_n$$$ ($$$1 le p_i le i - 1$$$), where $$$p_i$$$xa0— the parent of vertex $$$i$$$. The third line of each test case contains $$${n}$$$ integers $$$a_1, a_2, ldots,a_n$$$ ($$$1 le a_i le n$$$), where $$$a_i$$$xa0— the number of the activity located at vertex $$$i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output the maximum value of $$$f(u, v)$$$ for all pairs of recreations $$$(u, v)$$$. Example Input 4 2 1 1 2 7 1 1 2 2 3 3 6 5 2 3 6 5 6 13 1 1 1 2 2 2 3 3 4 5 6 6 2 2 2 1 4 9 7 2 5 2 1 11 2 12 1 1 1 2 2 3 4 4 7 7 6 11 2 1 11 12 8 5 8 8 5 11 7 Note Consider the fourth test case. The tree has the following structure: All recreations are colored. The same colors mean that the activities in the recreations match. Consider the pair of vertices $$$(11, 12)$$$, $$$lca(11, 12) = 1$$$. Write down all activities on the path from $$$11$$$ to $$$1$$$xa0— $$$[11, 5, 1, 11]$$$, among them there are $$$3$$$ different activities, so $$$diff(11, 1) = 3$$$. Also write down all activities on the path from $$$12$$$ to $$$1$$$xa0— $$$[7, 8, 2, 11]$$$, among them there are $$$4$$$ different activities, so $$$diff(12, 1) = 4$$$. We get that $$$f(11, 12) = diff(12, 1) cdot diff(11, 1) = 4 cdot 3 = 12$$$, which is the answer for this tree. It can be shown that a better answer is impossible to obtain.
2,300
false
true
false
false
true
false
false
false
false
false
799
922C
Problem - 922C - 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 number theory *1600 No tag edit access → Contest materials ") that: 1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_k_, , where is the remainder of division _x_ by _y_. Input The only line contains two integers _n_, _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20091018). Output Print "Yes", if all the remainders are distinct, and "No" otherwise. You can print each letter in arbitrary case (lower or upper). Examples Input 4 4 Output No Input 5 3 Output Yes Note In the first sample remainders modulo 1 and 4 coincide.
1,600
false
false
false
false
false
false
true
false
false
false
6,027
182A
Vasya lagged behind at the University and got to the battlefield. Just joking! He's simply playing some computer game. The field is a flat platform with _n_ trenches dug on it. The trenches are segments on a plane parallel to the coordinate axes. No two trenches intersect. There is a huge enemy laser far away from Vasya. The laser charges for _a_ seconds, and then shoots continuously for _b_ seconds. Then, it charges for _a_ seconds again. Then it shoots continuously for _b_ seconds again and so on. Vasya knows numbers _a_ and _b_. He also knows that while the laser is shooting, Vasya must be in the trench, but while the laser is charging, Vasya can safely move around the field. The main thing is to have time to hide in the trench before the shot. If Vasya reaches the trench exactly at the moment when the laser starts shooting, we believe that Vasya managed to hide. Coincidentally, the length of any trench in meters numerically does not exceed _b_. Initially, Vasya is at point _A_. He needs to get to point _B_. Vasya moves at speed 1 meter per second in either direction. You can get in or out of the trench at any its point. Getting in or out of the trench takes no time. It is also possible to move in the trench, without leaving it. What is the minimum time Vasya needs to get from point _A_ to point _B_, if at the initial time the laser has just started charging? If Vasya cannot get from point _A_ to point _B_, print -1. If Vasya reaches point _B_ at the moment when the laser begins to shoot, it is believed that Vasya managed to reach point _B_. Input The first line contains two space-separated integers: _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u20091000), — the duration of charging and the duration of shooting, in seconds. The second line contains four space-separated integers: _A__x_, _A__y_, _B__x_, _B__y_ (u2009-u2009104u2009≤u2009_A__x_,u2009_A__y_,u2009_B__x_,u2009_B__y_u2009≤u2009104) — the coordinates of points _А_ and _B_. It is guaranteed that points _A_ and _B_ do not belong to any trench. The third line contains a single integer: _n_ (1u2009≤u2009_n_u2009≤u20091000), — the number of trenches. Each of the following _n_ lines contains four space-separated integers: _x_1, _y_1, _x_2, _y_2 (u2009-u2009104u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009104) — the coordinates of ends of the corresponding trench. All coordinates are given in meters. It is guaranteed that for any trench either _x_1u2009=u2009_x_2, or _y_1u2009=u2009_y_2. No two trenches intersect. The length of any trench in meters doesn't exceed _b_ numerically. Output If Vasya can get from point _A_ to point _B_, print the minimum time he will need for it. Otherwise, print number -1. The answer will be considered correct if the absolute or relative error does not exceed 10u2009-u20094 Examples Input 2 4 0 5 6 5 3 0 0 0 4 1 1 4 1 6 0 6 4 Input 5 10 0 0 10 10 1 5 0 5 9
2,200
false
false
true
false
false
false
false
false
false
true
9,114
978D
Polycarp likes arithmetic progressions. A sequence $$$[a_1, a_2, dots, a_n]$$$ is called an arithmetic progression if for each $$$i$$$ ($$$1 le i < n$$$) the value $$$a_{i+1} - a_i$$$ is the same. For example, the sequences $$$[42]$$$, $$$[5, 5, 5]$$$, $$$[2, 11, 20, 29]$$$ and $$$[3, 2, 1, 0]$$$ are arithmetic progressions, but $$$[1, 0, 1]$$$, $$$[1, 3, 9]$$$ and $$$[2, 3, 1]$$$ are not. It follows from the definition that any sequence of length one or two is an arithmetic progression. Polycarp found some sequence of positive integers $$$[b_1, b_2, dots, b_n]$$$. He agrees to change each element by at most one. In the other words, for each element there are exactly three options: an element can be decreased by $$$1$$$, an element can be increased by $$$1$$$, an element can be left unchanged. Determine a minimum possible number of elements in $$$b$$$ which can be changed (by exactly one), so that the sequence $$$b$$$ becomes an arithmetic progression, or report that it is impossible. It is possible that the resulting sequence contains element equals $$$0$$$. Input The first line contains a single integer $$$n$$$ $$$(1 le n le 100,000)$$$ — the number of elements in $$$b$$$. The second line contains a sequence $$$b_1, b_2, dots, b_n$$$ $$$(1 le b_i le 10^{9})$$$. Output If it is impossible to make an arithmetic progression with described operations, print -1. In the other case, print non-negative integer — the minimum number of elements to change to make the given sequence becomes an arithmetic progression. The only allowed operation is to add/to subtract one from an element (can't use operation twice to the same position). Note In the first example Polycarp should increase the first number on $$$1$$$, decrease the second number on $$$1$$$, increase the third number on $$$1$$$, and the fourth number should left unchanged. So, after Polycarp changed three elements by one, his sequence became equals to $$$[25, 20, 15, 10]$$$, which is an arithmetic progression. In the second example Polycarp should not change anything, because his sequence is an arithmetic progression. In the third example it is impossible to make an arithmetic progression. In the fourth example Polycarp should change only the first element, he should decrease it on one. After that his sequence will looks like $$$[0, 3, 6, 9, 12]$$$, which is an arithmetic progression.
1,500
true
false
true
false
false
false
true
false
false
false
5,815
1728E
Monocarp is going to host a party for his friends. He prepared $$$n$$$ dishes and is about to serve them. First, he has to add some powdered pepper to each of themxa0— otherwise, the dishes will be pretty tasteless. The $$$i$$$-th dish has two values $$$a_i$$$ and $$$b_i$$$xa0— its tastiness with red pepper added or black pepper added, respectively. Monocarp won't add both peppers to any dish, won't add any pepper multiple times, and won't leave any dish without the pepper added. Before adding the pepper, Monocarp should first purchase the said pepper in some shop. There are $$$m$$$ shops in his local area. The $$$j$$$-th of them has packages of red pepper sufficient for $$$x_j$$$ servings and packages of black pepper sufficient for $$$y_j$$$ servings. Monocarp goes to exactly one shop, purchases multiple (possibly, zero) packages of each pepper in such a way that each dish will get the pepper added once, and no pepper is left. More formally, if he purchases $$$x$$$ red pepper packages and $$$y$$$ black pepper packages, then $$$x$$$ and $$$y$$$ should be non-negative and $$$x cdot x_j + y cdot y_j$$$ should be equal to $$$n$$$. For each shop, determine the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it's impossible to purchase the packages in the said way, print -1. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the number of dishes. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 10^9$$$)xa0— the tastiness of the $$$i$$$-th dish with red pepper added or black pepper added, respectively. The next line contains a single integer $$$m$$$ ($$$1 le m le 3 cdot 10^5$$$)xa0— the number of shops. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$x_j$$$ and $$$y_j$$$ ($$$1 le x_j, y_j le n$$$)xa0— the number of servings the red and the black pepper packages are sufficient for in the $$$j$$$-th shop, respectively. Output Print $$$m$$$ integers. For each shop, print the maximum total tastiness of the dishes after Monocarp buys pepper packages only in this shop and adds the pepper to the dishes. If it's impossible to purchase the packages so that each dish will get the pepper added once and no pepper is left, print -1. Examples Input 3 5 10 100 50 2 2 4 2 3 1 1 3 2 2 2 Input 10 3 1 2 3 1 1 2 1 6 3 1 4 4 3 1 3 5 3 5 4 10 8 10 9 3 1 4 2 5 8 3 3 5 1 6 7 2 6 7 3 1 Output 26 -1 36 30 -1 26 34 26 -1 36 Note Consider the first example. In the first shop, Monocarp can only buy $$$0$$$ red pepper packages and $$$1$$$ black pepper package. Black pepper added to all dishes will sum up to $$$10 + 50 + 2 = 62$$$. In the second shop, Monocarp can buy any number of red and black pepper packages: $$$0$$$ and $$$3$$$, $$$1$$$ and $$$2$$$, $$$2$$$ and $$$1$$$ or $$$3$$$ and $$$0$$$. The optimal choice turns out to be either $$$1$$$ and $$$2$$$ or $$$2$$$ and $$$1$$$. Monocarp can add black pepper to the first dish, red pepper to the second dish and any pepper to the third dish, the total is $$$10 + 100 + 2 = 112$$$. In the third shop, Monocarp can only buy $$$1$$$ red pepper package and $$$0$$$ black pepper packages. Red pepper added to all dishes will sum up to $$$5 + 100 + 2 = 107$$$. In the fourth shop, Monocarp can only buy an even total number of packages. Since $$$n$$$ is odd, it's impossible to get exactly $$$n$$$ packages. Thus, the answer is $$$-1$$$.
2,300
true
true
false
false
true
false
true
false
false
false
1,921
597A
. Output Print the required number.
1,600
true
false
false
false
false
false
false
false
false
false
7,460
1950C
Given the time in 24-hour format, output the equivalent time in 12-hour format. xa0— the number of test cases. The only line of each test case contains a string $$$s$$$ of length $$$5$$$ with format hh:mm representing a valid time in the 24-hour format. hh represents the hour from $$$00$$$ to $$$23$$$, and mm represents the minute from $$$00$$$ to $$$59$$$. The input will always be a valid time in 24-hour format. Output For each test case, output two strings separated by a space ("hh:mm AM" or "hh:mm PM"), which are the 12-hour equivalent to the time provided in the test case (without quotes). You should output the time exactly as indicated; in particular, you should not remove leading zeroes. Example Input 11 09:41 18:06 12:14 00:59 00:00 14:34 01:01 19:07 11:59 12:00 21:37 Output 09:41 AM 06:06 PM 12:14 PM 12:59 AM 12:00 AM 02:34 PM 01:01 AM 07:07 PM 11:59 AM 12:00 PM 09:37 PM
800
true
false
true
false
false
false
false
false
false
false
579
1310C
VK just opened its second HQ in St. Petersburg! Side of its office building has a huge string $$$s$$$ written on its side. This part of the office is supposed to be split into $$$m$$$ meeting rooms in such way that meeting room walls are strictly between letters on the building. Obviously, meeting rooms should not be of size 0, but can be as small as one letter wide. Each meeting room will be named after the substring of $$$s$$$ written on its side. For each possible arrangement of $$$m$$$ meeting rooms we ordered a test meeting room label for the meeting room with lexicographically minimal name. When delivered, those labels got sorted backward lexicographically. What is printed on $$$k$$$th label of the delivery? Input In the first line, you are given three integer numbers $$$n, m, k$$$xa0— length of string $$$s$$$, number of planned meeting rooms to split $$$s$$$ into and number of the interesting label ($$$2 le n le 1,000; 1 le m le 1,000; 1 le k le 10^{18}$$$). Second input line has string $$$s$$$, consisting of $$$n$$$ lowercase english letters. For given $$$n, m, k$$$ there are at least $$$k$$$ ways to split $$$s$$$ into $$$m$$$ substrings. Output Output single string – name of meeting room printed on $$$k$$$-th label of the delivery. Examples Input 19 5 1821 aupontrougevkoffice Note In the first example, delivery consists of the labels "aba", "ab", "a". In the second example, delivery consists of $$$3060$$$ labels. The first label is "aupontrougevkof" and the last one is "a".
2,800
false
false
false
true
false
false
false
true
false
false
4,147
1651B
Recently, your friend discovered one special operation on an integer array $$$a$$$: 1. Choose two indices $$$i$$$ and $$$j$$$ ($$$i eq j$$$); 2. Set $$$a_i = a_j = a_i - a_j$$$. After playing with this operation for a while, he came to the next conclusion: For every array $$$a$$$ of $$$n$$$ integers, where $$$1 le a_i le 10^9$$$, you can find a pair of indices $$$(i, j)$$$ such that the total sum of $$$a$$$ will decrease after performing the operation. This statement sounds fishy to you, so you want to find a counterexample for a given integer $$$n$$$. Can you find such counterexample and prove him wrong? In other words, find an array $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) such that for all pairs of indices $$$(i, j)$$$ performing the operation won't decrease the total sum (it will increase or not change the sum). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first and only line of each test case contains a single integer $$$n$$$ ($$$2 le n le 1000$$$)xa0— the length of array $$$a$$$. Output For each test case, if there is no counterexample array $$$a$$$ of size $$$n$$$, print NO. Otherwise, print YES followed by the array $$$a$$$ itself ($$$1 le a_i le 10^9$$$). If there are multiple counterexamples, print any. Example Output YES 1 337 NO YES 31 4 159 Note In the first test case, the only possible pairs of indices are $$$(1, 2)$$$ and $$$(2, 1)$$$. If you perform the operation on indices $$$(1, 2)$$$ (or $$$(2, 1)$$$), you'll get $$$a_1 = a_2 = 1 - 337 = 336$$$, or array $$$[336, 336]$$$. In both cases, the total sum increases, so this array $$$a$$$ is a counterexample.
800
false
true
false
false
false
true
false
false
false
false
2,367
1535A
Four players participate in the playoff tournament. The tournament is held according to the following scheme: the first player will play with the second, and the third player with the fourth, then the winners of the pairs will play in the finals of the tournament. It is known that in a match between two players, the one whose skill is greater will win. The skill of the $$$i$$$-th player is equal to $$$s_i$$$ and all skill levels are pairwise different (i.u2009e. there are no two identical values in the array $$$s$$$). The tournament is called fair if the two players with the highest skills meet in the finals. Determine whether the given tournament is fair. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. A single line of test case contains four integers $$$s_1, s_2, s_3, s_4$$$ ($$$1 le s_i le 100$$$)xa0— skill of the players. It is guaranteed that all the numbers in the array are different. Output For each testcase, output YES if the tournament is fair, or NO otherwise. Example Input 4 3 7 9 5 4 5 6 9 5 3 8 1 6 5 3 2 Note Consider the example: 1. in the first test case, players $$$2$$$ and $$$3$$$ with skills $$$7$$$ and $$$9$$$ advance to the finals; 2. in the second test case, players $$$2$$$ and $$$4$$$ with skills $$$5$$$ and $$$9$$$ advance to the finals. The player with skill $$$6$$$ does not advance, but the player with skill $$$5$$$ advances to the finals, so the tournament is not fair; 3. in the third test case, players $$$1$$$ and $$$3$$$ with skills $$$5$$$ and $$$8$$$ advance to the finals; 4. in the fourth test case, players $$$1$$$ and $$$3$$$ with skills $$$6$$$ and $$$3$$$ advance to the finals. The player with skill $$$5$$$ does not advance, but the player with skill $$$3$$$ advances to the finals, so the tournament is not fair.
800
false
false
true
false
false
false
true
false
false
false
2,974
712D
Memory and his friend Lexa are competing to get higher score in one popular computer game. Memory starts with score _a_ and Lexa starts with score _b_. In a single turn, both Memory and Lexa get some integer in the range [u2009-u2009_k_;_k_] (i.e. one integer among u2009-u2009_k_,u2009u2009-u2009_k_u2009+u20091,u2009u2009-u2009_k_u2009+u20092,u2009...,u2009u2009-u20092,u2009u2009-u20091,u20090,u20091,u20092,u2009...,u2009_k_u2009-u20091,u2009_k_) and add them to their current scores. The game has exactly _t_ turns. Memory and Lexa, however, are not good at this game, so they both always get a random integer at their turn. Memory wonders how many possible games exist such that he ends with a strictly higher score than Lexa. Two games are considered to be different if in at least one turn at least one player gets different score. There are (2_k_u2009+u20091)2_t_ games in total. Since the answer can be very large, you should print it modulo 109u2009+u20097. Please solve this problem for Memory. Input The first and only line of input contains the four integers _a_, _b_, _k_, and _t_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u20091000, 1u2009≤u2009_t_u2009≤u2009100)xa0— the amount Memory and Lexa start with, the number _k_, and the number of turns respectively. Output Print the number of possible games satisfying the conditions modulo 1u2009000u2009000u2009007 (109u2009+u20097) in one line. Note In the first sample test, Memory starts with 1 and Lexa starts with 2. If Lexa picks u2009-u20092, Memory can pick 0, 1, or 2 to win. If Lexa picks u2009-u20091, Memory can pick 1 or 2 to win. If Lexa picks 0, Memory can pick 2 to win. If Lexa picks 1 or 2, Memory cannot win. Thus, there are 3u2009+u20092u2009+u20091u2009=u20096 possible games in which Memory wins.
2,200
true
false
false
true
false
false
false
false
false
false
6,968
1394B
Boboniu has a directed graph with $$$n$$$ vertices and $$$m$$$ edges. The out-degree of each vertex is at most $$$k$$$. Each edge has an integer weight between $$$1$$$ and $$$m$$$. No two edges have equal weights. Boboniu likes to walk on the graph with some specific rules, which is represented by a tuple $$$(c_1,c_2,ldots,c_k)$$$. If he now stands on a vertex $$$u$$$ with out-degree $$$i$$$, then he will go to the next vertex by the edge with the $$$c_i$$$-th $$$(1le c_ile i)$$$ smallest weight among all edges outgoing from $$$u$$$. Now Boboniu asks you to calculate the number of tuples $$$(c_1,c_2,ldots,c_k)$$$ such that $$$1le c_ile i$$$ for all $$$i$$$ ($$$1le ile k$$$). Starting from any vertex $$$u$$$, it is possible to go back to $$$u$$$ in finite time by walking on the graph under the described rules. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2le nle 2cdot 10^5$$$, $$$2le mle min(2cdot 10^5,n(n-1) )$$$, $$$1le kle 9$$$). Each of the next $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$w$$$ $$$(1le u,vle n,u e v,1le wle m)$$$, denoting an edge from $$$u$$$ to $$$v$$$ with weight $$$w$$$. It is guaranteed that there are no self-loops or multiple edges and each vertex has at least one edge starting from itself. It is guaranteed that the out-degree of each vertex is at most $$$k$$$ and no two edges have equal weight. Output Print one integer: the number of tuples. Examples Input 4 6 3 4 2 1 1 2 2 2 4 3 4 1 4 4 3 5 3 1 6 Input 5 5 1 1 4 1 5 1 2 2 5 3 4 3 4 3 2 5 Input 6 13 4 3 5 1 2 5 2 6 3 3 1 4 4 2 6 5 5 3 6 4 1 7 4 3 8 5 2 9 4 2 10 2 1 11 6 1 12 4 6 13 Note For the first example, there are two tuples: $$$(1,1,3)$$$ and $$$(1,2,3)$$$. The blue edges in the picture denote the $$$c_i$$$-th smallest edges for each vertex, which Boboniu chooses to go through. For the third example, there's only one tuple: $$$(1,2,2,2)$$$. The out-degree of vertex $$$u$$$ means the number of edges outgoing from $$$u$$$.
2,300
false
false
false
false
false
false
true
false
false
true
3,700
197A
Problem - 197A - 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 games math *1600 No tag edit access → Contest materials , and so that any point on any plate is located within the table's border. During the game one cannot move the plates that already lie on the table. The player who cannot make another move loses. Determine which player wins, the one who moves first or the one who moves second, provided that both players play optimally well. Input A single line contains three space-separated integers _a_, _b_, _r_ (1u2009≤u2009_a_,u2009_b_,u2009_r_u2009≤u2009100) — the table sides and the plates' radius, correspondingly. Output If wins the player who moves first, print "First" (without the quotes). Otherwise print "Second" (without the quotes). Examples Input 5 5 2 Output First Input 6 7 4 Output Second Note In the first sample the table has place for only one plate. The first player puts a plate on the table, the second player can't do that and loses. In the second sample the table is so small that it doesn't have enough place even for one plate. So the first player loses without making a single move.
1,600
true
false
false
false
false
true
false
false
false
false
9,053
1608G
Problem - 1608G - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search data structures dfs and similar hashing string suffix structures strings trees *3500 No tag edit access → Contest materials ") ") $$$ in strings with indices from $$$l$$$ to $$$r$$$. $$$str(u,v)$$$ is defined as the string that is made by concatenating letters written on the edges on the shortest path from $$$u$$$ to $$$v$$$ (in order that they are traversed). Input The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$2 le n le 10^5$$$, $$$1 le m,q le 10^5$$$). The $$$i$$$-th of the following $$$n-1$$$ lines contains two integers $$$u_i, v_i$$$ and a lowercase Latin letter $$$c_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$), denoting the edge between nodes $$$u_i, v_i$$$ with a character $$$c_i$$$ on it. It's guaranteed that these edges form a tree. The following $$$m$$$ lines contain the strings consisting of lowercase Latin letters. The total length of those strings does not exceed $$$10^5$$$. Then $$$q$$$ lines follow, each containing four integers $$$u$$$, $$$v$$$, $$$l$$$ and $$$r$$$ ($$$1 le u,v le n$$$, $$$u eq v$$$, $$$1 le l le r le m$$$), denoting the queries. Output For each query print a single integer — the answer to the query. Examples Input 2 5 3 1 2 a aab abab aaa b a 2 1 1 5 1 2 1 3 2 1 3 5 Output 8 7 4 Input 9 5 6 1 2 a 2 7 c 1 3 b 3 4 b 4 6 b 3 5 a 5 8 b 5 9 c ababa cabbb bac bbbac abacaba 2 7 1 4 2 5 1 5 6 3 4 4 6 9 4 5 5 7 3 5 5 3 1 5 Output 3 4 2 1 1 10
3,500
false
false
false
false
true
false
false
true
false
false
2,609
1725C
Pak Chanek has a mirror in the shape of a circle. There are $$$N$$$ lamps on the circumference numbered from $$$1$$$ to $$$N$$$ in clockwise order. The length of the arc from lamp $$$i$$$ to lamp $$$i+1$$$ is $$$D_i$$$ for $$$1 leq i leq N-1$$$. Meanwhile, the length of the arc between lamp $$$N$$$ and lamp $$$1$$$ is $$$D_N$$$. Pak Chanek wants to colour the lamps with $$$M$$$ different colours. Each lamp can be coloured with one of the $$$M$$$ colours. However, there cannot be three different lamps such that the colours of the three lamps are the same and the triangle made by considering the three lamps as vertices is a right triangle (triangle with one of its angles being exactly $$$90$$$ degrees). The following are examples of lamp colouring configurations on the circular mirror. Before colouring the lamps, Pak Chanek wants to know the number of distinct colouring configurations he can make. Count the number of distinct possible lamp colouring configurations, modulo $$$998,244,353$$$. Input The first line contains two integers $$$N$$$ and $$$M$$$ ($$$1 le N le 3 cdot 10^5$$$, $$$2 le M le 3 cdot 10^5$$$) — the number of lamps in the mirror and the number of different colours used. The second line contains $$$N$$$ integers $$$D_1, D_2, ldots, D_N$$$ ($$$1 le D_i le 10^9$$$) — the lengths of the arcs between the lamps in the mirror. Output An integer representing the number of possible lamp colouring configurations, modulo $$$998,244,353$$$. Note In the first example, all correct lamp colouring configurations are $$$[1, 1, 2, 1]$$$, $$$[1, 1, 2, 2]$$$, $$$[1, 2, 1, 2]$$$, $$$[1, 2, 2, 1]$$$, $$$[1, 2, 2, 2]$$$, $$$[2, 1, 1, 1]$$$, $$$[2, 1, 1, 2]$$$, $$$[2, 1, 2, 1]$$$, $$$[2, 2, 1, 1]$$$, and $$$[2, 2, 1, 2]$$$.
2,000
true
false
false
false
false
false
false
true
false
false
1,944
1614C
Once Divan analyzed a sequence $$$a_1, a_2, ldots, a_n$$$ consisting of $$$n$$$ non-negative integers as follows. He considered each non-empty subsequence of the sequence $$$a$$$, computed the elements. For example, $$$ — the number of test cases. The first line of each test case contains two integer numbers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the length of the sequence and the number of contiguous segments whose bitwise OR values Divan remembers, respectively. The following $$$m$$$ lines describe the segments, one per line. Each segment is described with three integers $$$l$$$, $$$r$$$, and $$$x$$$ ($$$1 le l le r le n$$$, $$$0 le x le 2^{30} - 1$$$) — the first and last elements of the segment and the bitwise OR of $$$a_l, a_{l + 1}, ldots, a_r$$$, respectively. It is guaranteed that each element of the sequence is contained in at least one of the segments. It is guaranteed that there exists a sequence that satisfies all constraints. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Output For each test case print the coziness any suitable sequence $$$a$$$ modulo $$$10^9 + 7$$$. Example Input 3 2 1 1 2 2 3 2 1 3 5 2 3 5 5 4 1 2 7 3 3 7 4 4 0 4 5 2 Note In first example, one of the sequences that fits the constraints is $$$[0, 2]$$$. Consider all its non-empty subsequences: $$$[0]$$$: the bitwise XOR of this subsequence is $$$0$$$; $$$[2]$$$: the bitwise XOR of this subsequence is $$$2$$$; $$$[0, 2]$$$: the bitwise XOR of this subsequence is $$$2$$$. The sum of all results is $$$4$$$, so it is the answer. In second example, one of the sequences that fits the constraints is $$$[0, , 5, , 5]$$$. In third example, one of the sequences that fits the constraints is $$$[5, , 6, , 7, , 0, , 2]$$$.
1,500
true
false
false
true
false
true
false
false
false
false
2,568
1109B
Reading books is one of Sasha's passions. Once while he was reading one book, he became acquainted with an unusual character. The character told about himself like that: "Many are my names in many countries. Mithrandir among the Elves, Tharkûn to the Dwarves, Olórin I was in my youth in the West that is forgotten, in the South Incánus, in the North Gandalf; to the East I go not." And at that moment Sasha thought, how would that character be called in the East? In the East all names are palindromes. A string is a palindrome if it reads the same backward as forward. For example, such strings as "kazak", "oo" and "r" are palindromes, but strings "abb" and "ij" are not. Sasha believed that the hero would be named after one of the gods of the East. As long as there couldn't be two equal names, so in the East people did the following: they wrote the original name as a string on a piece of paper, then cut the paper minimum number of times $$$k$$$, so they got $$$k+1$$$ pieces of paper with substrings of the initial string, and then unite those pieces together to get a new string. Pieces couldn't be turned over, they could be shuffled. In this way, it's possible to achive a string abcdefg from the string fdeabcg using $$$3$$$ cuts (by swapping papers with substrings f and abc). The string cbadefg can't be received using the same cuts. More formally, Sasha wants for the given palindrome $$$s$$$ find such minimum $$$k$$$, that you can cut this string into $$$k + 1$$$ parts, and then unite them in such a way that the final string will be a palindrome and it won't be equal to the initial string $$$s$$$. It there is no answer, then print "Impossible" (without quotes). Input The first line contains one string $$$s$$$ ($$$1 le s le 5,000$$$)xa0— the initial name, which consists only of lowercase Latin letters. It is guaranteed that $$$s$$$ is a palindrome. Note In the first example, you can cut the string in those positions: nolon, and then unite them as follows onlno. It can be shown that there is no solution with one cut. In the second example, you can cut the string right in the middle, and swap peaces, so you get toot. In the third example, you can't make a string, that won't be equal to the initial one. In the fourth example, you can cut the suffix nik and add it to the beginning, so you get nikkinnikkin.
1,800
false
false
false
false
false
true
false
false
false
false
5,154
733D
Kostya is a genial sculptor, he has an idea: to carve a marble sculpture in the shape of a sphere. Kostya has a friend Zahar who works at a career. Zahar knows about Kostya's idea and wants to present him a rectangular parallelepiped of marble from which he can carve the sphere. Zahar has _n_ stones which are rectangular parallelepipeds. The edges sizes of the _i_-th of them are _a__i_, _b__i_ and _c__i_. He can take no more than two stones and present them to Kostya. If Zahar takes two stones, he should glue them together on one of the faces in order to get a new piece of rectangular parallelepiped of marble. Thus, it is possible to glue a pair of stones together if and only if two faces on which they are glued together match as rectangles. In such gluing it is allowed to rotate and flip the stones in any way. Help Zahar choose such a present so that Kostya can carve a sphere of the maximum possible volume and present it to Zahar. Input The first line contains the integer _n_ (1u2009≤u2009_n_u2009≤u2009105). _n_ lines follow, in the _i_-th of which there are three integers _a__i_,u2009_b__i_ and _c__i_ (1u2009≤u2009_a__i_,u2009_b__i_,u2009_c__i_u2009≤u2009109)xa0— the lengths of edges of the _i_-th stone. Note, that two stones may have exactly the same sizes, but they still will be considered two different stones. Output In the first line print _k_ (1u2009≤u2009_k_u2009≤u20092) the number of stones which Zahar has chosen. In the second line print _k_ distinct integers from 1 to _n_xa0— the numbers of stones which Zahar needs to choose. Consider that stones are numbered from 1 to _n_ in the order as they are given in the input data. You can print the stones in arbitrary order. If there are several answers print any of them. Examples Input 6 5 5 5 3 2 4 1 4 1 2 1 3 3 2 4 3 3 4 Input 7 10 7 8 5 10 3 4 2 6 5 5 5 10 2 8 4 2 1 7 7 7 Note In the first example we can connect the pairs of stones: 2 and 4, the size of the parallelepiped: 3u2009×u20092u2009×u20095, the radius of the inscribed sphere 1 2 and 5, the size of the parallelepiped: 3u2009×u20092u2009×u20098 or 6u2009×u20092u2009×u20094 or 3u2009×u20094u2009×u20094, the radius of the inscribed sphere 1, or 1, or 1.5 respectively. 2 and 6, the size of the parallelepiped: 3u2009×u20095u2009×u20094, the radius of the inscribed sphere 1.5 4 and 5, the size of the parallelepiped: 3u2009×u20092u2009×u20095, the radius of the inscribed sphere 1 5 and 6, the size of the parallelepiped: 3u2009×u20094u2009×u20095, the radius of the inscribed sphere 1.5 Or take only one stone: 1 the size of the parallelepiped: 5u2009×u20095u2009×u20095, the radius of the inscribed sphere 2.5 2 the size of the parallelepiped: 3u2009×u20092u2009×u20094, the radius of the inscribed sphere 1 3 the size of the parallelepiped: 1u2009×u20094u2009×u20091, the radius of the inscribed sphere 0.5 4 the size of the parallelepiped: 2u2009×u20091u2009×u20093, the radius of the inscribed sphere 0.5 5 the size of the parallelepiped: 3u2009×u20092u2009×u20094, the radius of the inscribed sphere 1 6 the size of the parallelepiped: 3u2009×u20093u2009×u20094, the radius of the inscribed sphere 1.5 It is most profitable to take only the first stone.
1,600
false
false
false
false
true
false
false
false
false
false
6,861
749C
There are _n_ employees in Alternative Cake Manufacturing (ACM). They are now voting on some very important question and the leading world media are trying to predict the outcome of the vote. Each of the employees belongs to one of two fractions: depublicans or remocrats, and these two fractions have opposite opinions on what should be the outcome of the vote. The voting procedure is rather complicated: 1. Each of _n_ employees makes a statement. They make statements one by one starting from employees 1 and finishing with employee _n_. If at the moment when it's time for the _i_-th employee to make a statement he no longer has the right to vote, he just skips his turn (and no longer takes part in this voting). 2. When employee makes a statement, he can do nothing or declare that one of the other employees no longer has a right to vote. It's allowed to deny from voting people who already made the statement or people who are only waiting to do so. If someone is denied from voting he no longer participates in the voting till the very end. 3. When all employees are done with their statements, the procedure repeats: again, each employees starting from 1 and finishing with _n_ who are still eligible to vote make their statements. 4. The process repeats until there is only one employee eligible to vote remaining and he determines the outcome of the whole voting. Of course, he votes for the decision suitable for his fraction. You know the order employees are going to vote and that they behave optimal (and they also know the order and who belongs to which fraction). Predict the outcome of the vote. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of employees. The next line contains _n_ characters. The _i_-th character is 'D' if the _i_-th employee is from depublicans fraction or 'R' if he is from remocrats. Output Print 'D' if the outcome of the vote will be suitable for depublicans and 'R' if remocrats will win. Note Consider one of the voting scenarios for the first sample: 1. Employee 1 denies employee 5 to vote. 2. Employee 2 denies employee 3 to vote. 3. Employee 3 has no right to vote and skips his turn (he was denied by employee 2). 4. Employee 4 denies employee 2 to vote. 5. Employee 5 has no right to vote and skips his turn (he was denied by employee 1). 6. Employee 1 denies employee 4. 7. Only employee 1 now has the right to vote so the voting ends with the victory of depublicans.
1,500
false
true
true
false
false
false
false
false
false
false
6,796
1090E
, # Problem A. Horseback Riding Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 512 megabytes It is well known that chess is a sport. But Dima, who is into horseback riding, considers it boring, and not a sport at all. His friend Sasha has decided to show him how complex and interesting chess can be. Sasha has placed k knights on a 8 x02 8 chessboard. These knights are on a promenade and want to return to their stables. Unfortunately, they don’t remember the way home. Let us say that the knights are in their stables if the following conditions are satisfied: some (specifically, k div 8 ) lower rows are filled with knights, and the next row only contains knights in its leftmost cells (if k mod 8 ̸ = 0 , then k mod 8 knights take leftmost positions in this row). > There are the desired cells for k= 11 knights, one per knight. This is a chess problem, so the knights move as they do in chess: exactly two cells in one direction, and then exactly one cell in another direction. > Possible moves of a knight Knight make moves one by one, in any order. In a single moment at most one knight can be located in any cell. Page 1 of 2 , Dima spent two days solving this problem, and now asks you to find the sequence of knights moves, so that no two knight ever occupied the same cell, and all the knight ended up in the stables. You don’t have to minimize the number of moves, but you are limited to no more than 1500 moves in total. # Input The first line of input contains an integer k — the number of knights at the chessboard ( 1 x14 k x14 64 ). The following k lines contains the positions of the knigths. Each position is formatted as xy , where x is the column letter, and y is the row digit. Columns are numbered from « a» to « h» left-to-right, rows are numbered with digits from 1 to 8 bottom-to-top. # Output The first line of output must contain the number of moves you need to get knights into the stables. Next, output these moves one per line, in the order they are made. Format your moves as xy-zt , whehe x and z are column letters, and y and t are row digits. Remember that you don’t have to minimize the number of moves, but you have to fit into the limit of 1500 . # Examples standard input standard output 2a5 f1 4a5-b3 b3-a1 f1-d2 d2-b1 3a1 b3 c2 5b3-d2 d2-b1 c2-b4 b4-d3 d3-c1 Page 2 of 2
2,300
false
false
false
false
false
false
false
false
false
true
5,268
1488D
Polycarp has decided to do a problemsolving marathon. He wants to solve $$$s$$$ problems in $$$n$$$ days. Let $$$a_i$$$ be the number of problems he solves during the $$$i$$$-th day. He wants to find a distribution of problems into days such that: $$$a_i$$$ is an integer value for all $$$i$$$ from $$$1$$$ to $$$n$$$; $$$a_i ge 1$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$; $$$a_{i + 1} ge a_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$; $$$a_{i + 1} le 2 cdot a_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$; $$$a_n$$$ is maximized. Note that $$$a_1$$$ can be arbitrarily large. What is the largest value of $$$a_n$$$ Polycarp can obtain? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then the descriptions of $$$t$$$ testcases follow. The only line of each testcase contains two integers $$$n$$$ and $$$s$$$ ($$$1 le n le s le 10^{18}$$$)xa0— the number of days and the number of problems Polycarp wants to solve. It's guaranteed that the distribution always exists within the given constraints. Output For each testcase print a single integerxa0— the maximum value of $$$a_n$$$. Note In the first testcase there is only one distribution: $$$[15]$$$. In the second testcase the distribution that maximizes $$$a_n$$$ is: $$$[2, 3, 4]$$$. In the third testcase the distribution that maximizes $$$a_n$$$ is: $$$[2, 4]$$$. $$$[3, 3]$$$ is a valid distribution but $$$a_n=3$$$ which is smaller than $$$4$$$. $$$[1, 5]$$$ is not a valid distribution because $$$5 > 2 cdot 1$$$.
1,900
false
true
false
false
false
false
false
true
false
false
3,236
1906J
You are currently researching a graph traversal algorithm called the Breadth First Search (BFS). Suppose you have an input graph of $$$N$$$ nodes (numbered from $$$1$$$ to $$$N$$$). The graph is represented by an adjacency matrix $$$M$$$, for which node $$$u$$$ can traverse to node $$$v$$$ if $$$M_{u, v}$$$ is $$$1$$$, otherwise it is $$$0$$$. Your algorithm will output the order the nodes are visited in the BFS. The pseudocode of the algorithm is presented as follows. BFS(M[1..N][1..N]): let A be an empty array let Q be an empty queueappend 1 to A push 1 to Q while Q is not empty: pop the front element of Q into u for v = 1 to N: if M[u][v] == 1 and v is not in A: append v to A push v to Q return A During your research, you are interested in the following problem. Given an array $$$A$$$ such that $$$A$$$ is a permutation of $$$1$$$ to $$$N$$$ and $$$A_1 = 1$$$. How many simple undirected graph with $$$N$$$ nodes and adjacency matrix $$$M$$$ such that $$$ ext{BFS}(M) = A$$$? Since the answer can be very large, calculate the answer modulo $$$998,244,353$$$. A simple graph has no self-loop ($$$M_{i, i} = 0$$$ for $$$1 leq i leq N$$$) and there is at most one edge that connects a pair of nodes. In an undirected graph, if node $$$u$$$ is adjacent to node $$$v$$$, then node $$$v$$$ is also adjacent to node $$$u$$$; formally, $$$M_{u, v} = M_{v, u}$$$ for $$$1 leq u < v leq N$$$. Two graphs are considered different if there is an edge that exists in one graph but not the other. In other words, two graphs are considered different if their adjacency matrices are different. Input The first line consists of an integer $$$N$$$ ($$$2 leq N leq 5000$$$). The second line consists of $$$N$$$ integers $$$A_i$$$. The array $$$A$$$ is a permutation of $$$1$$$ to $$$N$$$ and $$$A_1 = 1$$$. Output Output an integer representing the number of simple undirected graphs with $$$N$$$ nodes and adjacency matrix $$$M$$$ such that $$$ ext{BFS}(M) = A$$$. Since the answer can be very large, output the answer modulo $$$998,244,353$$$. Note Explanation for the sample input/output #1 The following illustration shows all graphs that satisfy the requirements. Explanation for the sample input/output #2 The only graph that satisfies the requirements is a graph with two edges: one that connects nodes $$$1$$$ and $$$3$$$, and another one that connects nodes $$$3$$$ and $$$2$$$.
2,100
false
false
false
true
false
false
false
false
false
false
868
154D
The King of Flatland will organize a knights' tournament! The winner will get half the kingdom and the favor of the princess of legendary beauty and wisdom. The final test of the applicants' courage and strength will be a fencing tournament. The tournament is held by the following rules: the participants fight one on one, the winner (or rather, the survivor) transfers to the next round. Before the battle both participants stand at the specified points on the _Ox_ axis with integer coordinates. Then they make moves in turn. The first participant moves first, naturally. During a move, the first participant can transfer from the point _x_ to any integer point of the interval [_x_u2009+u2009_a_; _x_u2009+u2009_b_]. The second participant can transfer during a move to any integer point of the interval [_x_u2009-u2009_b_; _x_u2009-u2009_a_]. That is, the options for the players' moves are symmetric (note that the numbers _a_ and _b_ are not required to be positive, and if _a_u2009≤u20090u2009≤u2009_b_, then staying in one place is a correct move). At any time the participants can be located arbitrarily relative to each other, that is, it is allowed to "jump" over the enemy in any direction. A participant wins if he uses his move to transfer to the point where his opponent is. Of course, the princess has already chosen a husband and now she wants to make her sweetheart win the tournament. He has already reached the tournament finals and he is facing the last battle. The princess asks the tournament manager to arrange the tournament finalists in such a way that her sweetheart wins the tournament, considering that both players play optimally. However, the initial location of the participants has already been announced, and we can only pull some strings and determine which participant will be first and which one will be second. But how do we know which participant can secure the victory? Alas, the princess is not learned in the military affairs... Therefore, she asks you to determine how the battle will end considering that both opponents play optimally. Also, if the first player wins, your task is to determine his winning move. Input The first line contains four space-separated integers — _x_1, _x_2, _a_ and _b_ (_x_1u2009≠u2009_x_2, _a_u2009≤u2009_b_, u2009-u2009109u2009≤u2009_x_1,u2009_x_2,u2009_a_,u2009_b_u2009≤u2009109) — coordinates of the points where the first and the second participant start, and the numbers that determine the players' moves, correspondingly. Output On the first line print the outcome of the battle as "FIRST" (without the quotes), if both players play optimally and the first player wins. Print "SECOND" (without the quotes) if the second player wins and print "DRAW" (without the quotes), if nobody is able to secure the victory. If the first player wins, print on the next line the single integer _x_ — the coordinate of the point where the first player should transfer to win. The indicated move should be valid, that is, it should meet the following condition: _x_1u2009+u2009_a_u2009≤u2009_x_u2009≤u2009_x_1u2009+u2009_b_. If there are several winning moves, print any of them. If the first participant can't secure the victory, then you do not have to print anything. Note In the first sample the first player can win in one move. In the second sample the first participant must go to point 1, where the second participant immediately goes and wins. In the third sample changing the position isn't profitable to either participant, so nobody wins.
2,400
true
false
false
false
false
false
false
false
false
false
9,256
1797A
There is a rectangular maze of size $$$n imes m$$$. Denote $$$(r,c)$$$ as the cell on the $$$r$$$-th row from the top and the $$$c$$$-th column from the left. Two cells are adjacent if they share an edge. A path is a sequence of adjacent empty cells. Each cell is initially empty. Li Hua can choose some cells (except $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$) and place an obstacle in each of them. He wants to know the minimum number of obstacles needed to be placed so that there isn't a path from $$$(x_1, y_1)$$$ to $$$(x_2, y_2)$$$. Suppose you were Li Hua, please solve this problem. Input The first line contains the single integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n,m$$$ ($$$4le n,mle 10^9$$$)xa0— the size of the maze. The second line of each test case contains four integers $$$x_1,y_1,x_2,y_2$$$ ($$$1le x_1,x_2le n, 1le y_1,y_2le m$$$)xa0— the coordinates of the start and the end. It is guaranteed that $$$x_1-x_2+y_1-y_2ge 2$$$. Output For each test case print the minimum number of obstacles you need to put on the field so that there is no path from $$$(x_1, y_1)$$$ to $$$(x_2, y_2)$$$. Example Input 3 4 4 2 2 3 3 6 7 1 1 2 3 9 9 5 1 3 6 Note In test case 1, you can put obstacles on $$$(1,3), (2,3), (3,2), (4,2)$$$. Then the path from $$$(2,2)$$$ to $$$(3,3)$$$ will not exist.
800
false
true
true
false
false
true
false
false
false
true
1,496
2035B
Problem - 2035B - Codeforces =============== xa0 ]( "Codeforces Global Round 27") xa0— the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1le nle 500$$$)xa0— the length of the decimal representation. Output For each test case, output the smallest required integer if such an integer exists and $$$-1$$$ otherwise. Example Input 6 1 2 3 4 5 7 Output -1 66 -1 3366 36366 3336366 Note For $$$n=1$$$, no such integer exists as neither $$$3$$$ nor $$$6$$$ is divisible by $$$33$$$. For $$$n=2$$$, $$$66$$$ consists only of $$$6$$$s and it is divisible by both $$$33$$$ and $$$66$$$. For $$$n=3$$$, no such integer exists. Only $$$363$$$ is divisible by $$$33$$$, but it is not divisible by $$$66$$$. For $$$n=4$$$, $$$3366$$$ and $$$6666$$$ are divisible by both $$$33$$$ and $$$66$$$, and $$$3366$$$ is the smallest.
900
true
true
false
false
false
true
false
false
false
false
60
1428G2
This is the hard version of the problem. The only difference is in the constraint on $$$q$$$. You can make hacks only if all versions of the problem are solved. Zookeeper has been teaching his $$$q$$$ sheep how to write and how to add. The $$$i$$$-th sheep has to write exactly $$$k$$$ non-negative integers with the sum $$$n_i$$$. Strangely, sheep have superstitions about digits and believe that the digits $$$3$$$, $$$6$$$, and $$$9$$$ are lucky. To them, the fortune of a number depends on the decimal representation of the number; the fortune of a number is equal to the sum of fortunes of its digits, and the fortune of a digit depends on its value and position and can be described by the following table. For example, the number $$$319$$$ has fortune $$$F_{2} + 3F_{0}$$$. Each sheep wants to maximize the sum of fortune among all its $$$k$$$ written integers. Can you help them? Input The first line contains a single integer $$$k$$$ ($$$1 leq k leq 999999$$$): the number of numbers each sheep has to write. The next line contains six integers $$$F_0$$$, $$$F_1$$$, $$$F_2$$$, $$$F_3$$$, $$$F_4$$$, $$$F_5$$$ ($$$1 leq F_i leq 10^9$$$): the fortune assigned to each digit. The next line contains a single integer $$$q$$$ ($$$1 leq q leq 100,000$$$): the number of sheep. Each of the next $$$q$$$ lines contains a single integer $$$n_i$$$ ($$$1 leq n_i leq 999999$$$): the sum of numbers that $$$i$$$-th sheep has to write. Output Print $$$q$$$ lines, where the $$$i$$$-th line contains the maximum sum of fortune of all numbers of the $$$i$$$-th sheep. Example Input 3 1 2 3 4 5 6 2 57 63 Note In the first test case, $$$57 = 9 + 9 + 39$$$. The three $$$9$$$'s contribute $$$1 cdot 3$$$ and $$$3$$$ at the tens position contributes $$$2 cdot 1$$$. Hence the sum of fortune is $$$11$$$. In the second test case, $$$63 = 35 + 19 + 9$$$. The sum of fortune is $$$8$$$.
3,000
false
true
false
true
false
false
false
false
false
false
3,518
2032B
You are given an array $$$a = [1, 2, ldots, n]$$$, where $$$n$$$ is odd, and an integer $$$k$$$. Your task is to choose an odd positive integer $$$m$$$ and to split $$$a$$$ into $$$m$$$ subarrays$$$^{dagger}$$$ $$$b_1, b_2, ldots, b_m$$$ such that: Each element of the array $$$a$$$ belongs to exactly one subarray. For all $$$1 le i le m$$$, $$$b_i$$$ is odd, i.e., the length of each subarray is odd. $$$operatorname{median}([operatorname{median}(b_1), operatorname{median}(b_2), ldots, operatorname{median}(b_m)]) = k$$$, i.e., the median$$$^{ddagger}$$$ of the array of medians of all subarrays must equal $$$k$$$. $$$operatorname{median}(c)$$$ denotes the median of the array $$$c$$$. $$$^{dagger}$$$A subarray of the array $$$a$$$ of length $$$n$$$ is the array $$$[a_l, a_{l + 1}, ldots, a_r]$$$ for some integers $$$1 le l le r le n$$$. $$$^{ddagger}$$$A median of the array of odd length is the middle element after the array is sorted in non-decreasing order. For example: $$$operatorname{median}([1,2,5,4,3]) = 3$$$, $$$operatorname{median}([3,2,1]) = 2$$$, $$$operatorname{median}([2,1,2,1,2,2,2]) = 2$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 5000$$$) — 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$$$ ($$$1 le k le n < 2 cdot 10^5$$$, $$$n$$$ is odd)xa0— the length of array $$$a$$$ and the desired median of the array of medians of all subarrays. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case: If there is no suitable partition, output $$$-1$$$ in a single line. Otherwise, in the first line, output an odd integer $$$m$$$ ($$$1 le m le n$$$), and in the second line, output $$$m$$$ distinct integers $$$p_1, p_2 , p_3 , ldots, p_m$$$ ($$$1 = p_1 < p_2 < p_3 < ldots < p_m le n$$$)xa0— denoting the left borders of each subarray. In detail, for a valid answer $$$[p_1, p_2, ldots, p_m]$$$: $$$b_1 = left[ a_{p_1}, a_{p_1 + 1}, ldots, a_{p_2 - 1} ight]$$$ $$$b_2 = left[ a_{p_2}, a_{p_2 + 1}, ldots, a_{p_3 - 1} ight]$$$ $$$ldots$$$ $$$b_m = left[ a_{p_m}, a_{p_m + 1}, ldots, a_n ight]$$$. If there are multiple solutions, you can output any of them. Example Output 1 1 3 1 2 3 -1 5 1 4 7 10 13 Note In the first test case, the given partition has $$$m = 1$$$ and $$$b_1 = [1]$$$. It is obvious that $$$operatorname{median}([operatorname{median}([1])]) = operatorname{median}([1]) = 1$$$. In the second test case, the given partition has $$$m = 3$$$ and: $$$b_1 = [1]$$$ $$$b_2 = [2]$$$ $$$b_3 = [3]$$$ Therefore, $$$operatorname{median}([operatorname{median}([1]), operatorname{median}([2]), operatorname{median}([3])]) = operatorname{median}([1, 2, 3]) = 2$$$. In the third test case, there is no valid partition for $$$k = 3$$$. In the fourth test case, the given partition has $$$m = 5$$$ and: $$$b_1 = [1, 2, 3]$$$ $$$b_2 = [4, 5, 6]$$$ $$$b_3 = [7, 8, 9]$$$ $$$b_4 = [10, 11, 12]$$$ $$$b_5 = [13, 14, 15]$$$ Therefore, $$$operatorname{median}([operatorname{median}([1, 2, 3]), operatorname{median}([4, 5, 6]), operatorname{median}([7, 8, 9]), operatorname{median}([10, 11, 12]), operatorname{median}([13, 14, 15])]) = operatorname{median}([2, 5, 8, 11, 14]) = 8$$$.
1,100
true
true
true
false
false
true
false
false
false
false
73
777C
During the lesson small girl Alyona works with one famous spreadsheet computer program and learns how to edit tables. Now she has a table filled with integers. The table consists of _n_ rows and _m_ columns. By _a__i_,u2009_j_ we will denote the integer located at the _i_-th row and the _j_-th column. We say that the table is sorted in non-decreasing order in the column _j_ if _a__i_,u2009_j_u2009≤u2009_a__i_u2009+u20091,u2009_j_ for all _i_ from 1 to _n_u2009-u20091. Teacher gave Alyona _k_ tasks. For each of the tasks two integers _l_ and _r_ are given and Alyona has to answer the following question: if one keeps the rows from _l_ to _r_ inclusive and deletes all others, will the table be sorted in non-decreasing order in at least one column? Formally, does there exist such _j_ that _a__i_,u2009_j_u2009≤u2009_a__i_u2009+u20091,u2009_j_ for all _i_ from _l_ to _r_u2009-u20091 inclusive. Alyona is too small to deal with this task and asks you to help! Input The first line of the input contains two positive integers _n_ and _m_ (1u2009≤u2009_n_·_m_u2009≤u2009100u2009000)xa0— the number of rows and the number of columns in the table respectively. Note that your are given a constraint that bound the product of these two integers, i.e. the number of elements in the table. Each of the following _n_ lines contains _m_ integers. The _j_-th integers in the _i_ of these lines stands for _a__i_,u2009_j_ (1u2009≤u2009_a__i_,u2009_j_u2009≤u2009109). The next line of the input contains an integer _k_ (1u2009≤u2009_k_u2009≤u2009100u2009000)xa0— the number of task that teacher gave to Alyona. The _i_-th of the next _k_ lines contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). Output Print "Yes" to the _i_-th line of the output if the table consisting of rows from _l__i_ to _r__i_ inclusive is sorted in non-decreasing order in at least one column. Otherwise, print "No". Example Input 5 4 1 2 3 5 3 1 3 2 4 5 2 3 5 5 3 2 4 4 3 4 6 1 1 2 5 4 5 3 5 1 3 1 5 Note In the sample, the whole table is not sorted in any column. However, rows 1–3 are sorted in column 1, while rows 4–5 are sorted in column 3.
1,600
false
true
true
true
true
false
false
true
false
false
6,659
1556G
As mentioned previously William really likes playing video games. In one of his favorite games, the player character is in a universe where every planet is designated by a binary number from $$$0$$$ to $$$2^n - 1$$$. On each planet, there are gates that allow the player to move from planet $$$i$$$ to planet $$$j$$$ if the binary representations of $$$i$$$ and $$$j$$$ differ in exactly one bit. William wants to test you and see how you can handle processing the following queries in this game universe: Destroy planets with numbers from $$$l$$$ to $$$r$$$ inclusively. These planets cannot be moved to anymore. Figure out if it is possible to reach planet $$$b$$$ from planet $$$a$$$ using some number of planetary gates. It is guaranteed that the planets $$$a$$$ and $$$b$$$ are not destroyed. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n leq 50$$$, $$$1 leq m leq 5 cdot 10^4$$$), which are the number of bits in binary representation of each planets' designation and the number of queries, respectively. Each of the next $$$m$$$ lines contains a query of two types: block l rxa0— query for destruction of planets with numbers from $$$l$$$ to $$$r$$$ inclusively ($$$0 le l le r < 2^n$$$). It's guaranteed that no planet will be destroyed twice. ask a bxa0— query for reachability between planets $$$a$$$ and $$$b$$$ ($$$0 le a, b < 2^n$$$). It's guaranteed that planets $$$a$$$ and $$$b$$$ hasn't been destroyed yet. Output For each query of type ask you must output "1" in a new line, if it is possible to reach planet $$$b$$$ from planet $$$a$$$ and "0" otherwise (without quotation marks). Examples Input 3 3 ask 0 7 block 3 6 ask 0 7 Input 6 10 block 12 26 ask 44 63 block 32 46 ask 1 54 block 27 30 ask 10 31 ask 11 31 ask 49 31 block 31 31 ask 2 51 Note The first example test can be visualized in the following way: Response to a query ask 0 7 is positive. Next after query block 3 6 the graph will look the following way (destroyed vertices are highlighted): Response to a query ask 0 7 is negative, since any path from vertex $$$0$$$ to vertex $$$7$$$ must go through one of the destroyed vertices.
3,300
false
false
false
false
true
false
false
false
false
false
2,849
1920A
Alex is solving a problem. He has $$$n$$$ constraints on what the integer $$$k$$$ can be. There are three types of constraints: 1. $$$k$$$ must be greater than or equal to some integer $$$x$$$; 2. $$$k$$$ must be less than or equal to some integer $$$x$$$; 3. $$$k$$$ must be not equal to some integer $$$x$$$. Help Alex find the number of integers $$$k$$$ that satisfy all $$$n$$$ constraints. It is guaranteed that the answer is finite (there exists at least one constraint of type $$$1$$$ and at least one constraint of type $$$2$$$). Also, it is guaranteed that no two constraints are the exact same. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 500$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 100$$$) — the number of constraints. The following $$$n$$$ lines describe the constraints. Each line contains two integers $$$a$$$ and $$$x$$$ ($$$a in {1,2,3}, , 1 leq x leq 10^9$$$). $$$a$$$ denotes the type of constraint. If $$$a=1$$$, $$$k$$$ must be greater than or equal to $$$x$$$. If $$$a=2$$$, $$$k$$$ must be less than or equal to $$$x$$$. If $$$a=3$$$, $$$k$$$ must be not equal to $$$x$$$. It is guaranteed that there is a finite amount of integers satisfying all $$$n$$$ constraints (there exists at least one constraint of type $$$1$$$ and at least one constraint of type $$$2$$$). It is also guaranteed that no two constraints are the exact same (in other words, all pairs $$$(a, x)$$$ are distinct). Output For each test case, output a single integer — the number of integers $$$k$$$ that satisfy all $$$n$$$ constraints. Example Input 6 4 1 3 2 10 3 1 3 5 2 1 5 2 4 10 3 6 3 7 1 2 1 7 3 100 3 44 2 100 2 98 1 3 3 99 6 1 5 2 10 1 9 2 2 3 2 3 9 5 1 1 2 2 3 1 3 2 3 3 6 1 10000 2 900000000 3 500000000 1 100000000 3 10000 3 900000001 Output 7 0 90 0 0 800000000 Note In the first test case, $$$k geq 3$$$ and $$$k leq 10$$$. Furthermore, $$$k eq 1$$$ and $$$k eq 5$$$. The possible integers $$$k$$$ that satisfy the constraints are $$$3,4,6,7,8,9,10$$$. So the answer is $$$7$$$. In the second test case, $$$k ge 5$$$ and $$$k le 4$$$, which is impossible. So the answer is $$$0$$$.
800
true
true
false
false
false
false
true
false
false
false
772
859C
You may have heard of the pie rule before. It states that if two people wish to fairly share a slice of pie, one person should cut the slice in half, and the other person should choose who gets which slice. Alice and Bob have many slices of pie, and rather than cutting the slices in half, each individual slice will be eaten by just one person. The way Alice and Bob decide who eats each slice is as follows. First, the order in which the pies are to be handed out is decided. There is a special token called the "decider" token, initially held by Bob. Until all the pie is handed out, whoever has the decider token will give the next slice of pie to one of the participants, and the decider token to the other participant. They continue until no slices of pie are left. All of the slices are of excellent quality, so each participant obviously wants to maximize the total amount of pie they get to eat. Assuming both players make their decisions optimally, how much pie will each participant receive? Input Input will begin with an integer _N_ (1u2009≤u2009_N_u2009≤u200950), the number of slices of pie. Following this is a line with _N_ integers indicating the sizes of the slices (each between 1 and 100000, inclusive), in the order in which they must be handed out. Output Print two integers. First, the sum of the sizes of slices eaten by Alice, then the sum of the sizes of the slices eaten by Bob, assuming both players make their decisions optimally. Note In the first example, Bob takes the size 141 slice for himself and gives the decider token to Alice. Then Alice gives the size 592 slice to Bob and keeps the decider token for herself, so that she can then give the size 653 slice to herself.
1,500
false
false
false
true
false
false
false
false
false
false
6,306
1997D
You are given a rooted tree, consisting of $$$n$$$ vertices. The vertices in the tree are numbered from $$$1$$$ to $$$n$$$, and the root is the vertex $$$1$$$. The value $$$a_i$$$ is written at the $$$i$$$-th vertex. You can perform the following operation any number of times (possibly zero): choose a vertex $$$v$$$ which has at least one child; increase $$$a_v$$$ by $$$1$$$; and decrease $$$a_u$$$ by $$$1$$$ for all vertices $$$u$$$ that are in the subtree of $$$v$$$ (except $$$v$$$ itself). However, after each operation, the values on all vertices should be non-negative. Your task is to calculate the maximum possible value written at the root using the aforementioned operation. 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 a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of vertices in the tree. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the initial values written at vertices. The third line contains $$$n-1$$$ integers $$$p_2, p_3, dots, p_n$$$ ($$$1 le p_i le n$$$), where $$$p_i$$$ is the parent of the $$$i$$$-th vertex in the tree. Vertex $$$1$$$ is the root. Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the maximum possible value written at the root using the aforementioned operation. Example Input 3 4 0 1 0 2 1 1 3 2 3 0 1 5 2 5 3 9 6 3 1 5 2 Note In the first test case, the following sequence of operations is possible: perform the operation on $$$v=3$$$, then the values on the vertices will be $$$[0, 1, 1, 1]$$$; perform the operation on $$$v=1$$$, then the values on the vertices will be $$$[1, 0, 0, 0]$$$.
1,500
false
true
false
true
false
false
false
true
false
false
280
1769A
Statement is not available on English language Колонна из $$$n$$$ самокатов едет по узкой односторонней дороге в пункт Б. Самокаты пронумерованы от $$$1$$$ до $$$n$$$. Для каждого самоката $$$i$$$ известно, что текущее расстояние от него до пункта Б равно $$$a_i$$$ метров. При этом $$$a_1 < a_2 < ldots < a_n$$$, в частности, самокат $$$1$$$ находится ближе всего к пункту Б, а самокат $$$n$$$xa0— дальше всего. Самокат с номером $$$i$$$ движется в сторону пункта Б со скоростью $$$i$$$ метров в секунду (то есть чем ближе самокат в колонне к пункту Б, тем медленнее он едет). Так как дорога узкая, самокаты не могут обгонять друг друга. Более того, соседние самокаты в колонне должны соблюдать дистанцию хотя бы в $$$1$$$ метр. Поэтому когда более быстрый самокат догоняет более медленный, более быстрому приходится дальше ехать со скоростью более медленного, причём на расстоянии в $$$1$$$ метр от него. Определите, на каком расстоянии до пункта Б будет каждый самокат ровно через одну секунду. Входные данные В первой строке задано одно целое число $$$n$$$ ($$$1 le n le 100$$$)xa0— число самокатов в колонне. В $$$i$$$-й из следующих $$$n$$$ строк задано одно целое число $$$a_i$$$ ($$$1 le a_i le 1000$$$; $$$a_1 < a_2 < ldots < a_n$$$)xa0— текущее расстояние от самоката $$$i$$$ до пункта Б в метрах. Выходные данные Выведите $$$n$$$ целых чиселxa0— расстояния от самокатов $$$1, 2, ldots, n$$$ до пункта Б в метрах через одну секунду. Примеры Входные данные 4 20 30 50 100 Выходные данные 19 28 47 96 Входные данные 5 1 2 3 4 5 Входные данные 8 5 9 10 15 17 18 19 22 Выходные данные 4 7 8 11 12 13 14 15 Примечание В первом тесте самокаты пока не мешают друг другу ехать, поэтому каждый самокат $$$i$$$ продвигается на $$$i$$$ метров в сторону пункта Б. Во втором тесте самокаты уже выстроились в колонне на расстоянии $$$1$$$ метр друг от друга и вынуждены ехать со скоростью самого медленного самоката с номером $$$1$$$.
800
true
false
false
false
false
false
false
false
false
false
1,685
1506E
A permutation is a sequence of $$$n$$$ integers from $$$1$$$ to $$$n$$$, in which all numbers occur exactly once. For example, $$$[1]$$$, $$$[3, 5, 2, 1, 4]$$$, $$$[1, 3, 2]$$$ are permutations, and $$$[2, 3, 2]$$$, $$$[4, 3, 1]$$$, $$$[0]$$$ are not. Polycarp was presented with a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$. However, when Polycarp came home, he noticed that in his pocket, the permutation $$$p$$$ had turned into an array $$$q$$$ according to the following rule: $$$q_i = max(p_1, p_2, ldots, p_i)$$$. Now Polycarp wondered what lexicographically minimal and lexicographically maximal permutations could be presented to him. An array $$$a$$$ of length $$$n$$$ is lexicographically smaller than an array $$$b$$$ of length $$$n$$$ if there is an index $$$i$$$ ($$$1 le i le n$$$) such that the first $$$i-1$$$ elements of arrays $$$a$$$ and $$$b$$$ are the same, and the $$$i$$$-th element of the array $$$a$$$ is less than the $$$i$$$-th element of the array $$$b$$$. For example, the array $$$a=[1, 3, 2, 3]$$$ is lexicographically smaller than the array $$$b=[1, 3, 4, 2]$$$. For example, if $$$n=7$$$ and $$$p=[3, 2, 4, 1, 7, 5, 6]$$$, then $$$q=[3, 3, 4, 4, 7, 7, 7]$$$ and the following permutations could have been as $$$p$$$ initially: $$$[3, 1, 4, 2, 7, 5, 6]$$$ (lexicographically minimal permutation); $$$[3, 1, 4, 2, 7, 6, 5]$$$; $$$[3, 2, 4, 1, 7, 5, 6]$$$; $$$[3, 2, 4, 1, 7, 6, 5]$$$ (lexicographically maximum permutation). For a given array $$$q$$$, find the lexicographically minimal and lexicographically maximal permutations that could have been originally presented to Polycarp. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$q_1, q_2, ldots, q_n$$$ ($$$1 le q_i le n$$$). It is guaranteed that the array $$$q$$$ was obtained by applying the rule from the statement to some permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$.
1,500
false
false
true
false
false
true
false
false
false
false
3,139
670E
Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "1"-s to it. For example, sequences "(())()", "()" and "(()(()))" are correct, while ")(", "(()" and "(()))(" are not. Each bracket in CBS has a pair. For example, in "(()(()))": 1st bracket is paired with 8th, 2d bracket is paired with 3d, 3d bracket is paired with 2d, 4th bracket is paired with 7th, 5th bracket is paired with 6th, 6th bracket is paired with 5th, 7th bracket is paired with 4th, 8th bracket is paired with 1st. Polycarp's editor currently supports only three operations during the use of CBS. The cursor in the editor takes the whole position of one of the brackets (not the position between the brackets!). There are three operations being supported: «L»xa0— move the cursor one position to the left, «R»xa0— move the cursor one position to the right, «D»xa0— delete the bracket in which the cursor is located, delete the bracket it's paired to and all brackets between them (that is, delete a substring between the bracket in which the cursor is located and the one it's paired to). After the operation "D" the cursor moves to the nearest bracket to the right (of course, among the non-deleted). If there is no such bracket (that is, the suffix of the CBS was deleted), then the cursor moves to the nearest bracket to the left (of course, among the non-deleted). There are pictures illustrated several usages of operation "D" below. All incorrect operations (shift cursor over the end of CBS, delete the whole CBS, etc.) are not supported by Polycarp's editor. Polycarp is very proud of his development, can you implement the functionality of his editor? Input The first line contains three positive integers _n_, _m_ and _p_ (2u2009≤u2009_n_u2009≤u2009500u2009000, 1u2009≤u2009_m_u2009≤u2009500u2009000, 1u2009≤u2009_p_u2009≤u2009_n_)xa0— the number of brackets in the correct bracket sequence, the number of operations and the initial position of cursor. Positions in the sequence are numbered from left to right, starting from one. It is guaranteed that _n_ is even. It is followed by the string of _n_ characters "(" and ")" forming the correct bracket sequence. Then follow a string of _m_ characters "L", "R" and "D"xa0— a sequence of the operations. Operations are carried out one by one from the first to the last. It is guaranteed that the given operations never move the cursor outside the bracket sequence, as well as the fact that after all operations a bracket sequence will be non-empty. Note In the first sample the cursor is initially at position 5. Consider actions of the editor: 1. command "R"xa0— the cursor moves to the position 6 on the right; 2. command "D"xa0— the deletion of brackets from the position 5 to the position 6. After that CBS takes the form (())(), the cursor is at the position 5; 3. command "L"xa0— the cursor moves to the position 4 on the left; 4. command "D"xa0— the deletion of brackets from the position 1 to the position 4. After that CBS takes the form (), the cursor is at the position 1. Thus, the answer is equal to ().
1,700
false
false
false
false
true
false
false
false
false
false
7,142
1157F
There are $$$n$$$ people in a row. The height of the $$$i$$$-th person is $$$a_i$$$. You can choose any subset of these people and try to arrange them into a balanced circle. A balanced circle is such an order of people that the difference between heights of any adjacent people is no more than $$$1$$$. For example, let heights of chosen people be $$$[a_{i_1}, a_{i_2}, dots, a_{i_k}]$$$, where $$$k$$$ is the number of people you choose. Then the condition $$$a_{i_j} - a_{i_{j + 1}} le 1$$$ should be satisfied for all $$$j$$$ from $$$1$$$ to $$$k-1$$$ and the condition $$$a_{i_1} - a_{i_k} le 1$$$ should be also satisfied. $$$x$$$ means the absolute value of $$$x$$$. It is obvious that the circle consisting of one person is balanced. Your task is to choose the maximum number of people and construct a balanced circle consisting of all chosen people. It is obvious that the circle consisting of one person is balanced so the answer always exists. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of people. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$), where $$$a_i$$$ is the height of the $$$i$$$-th person. Output In the first line of the output print $$$k$$$ — the number of people in the maximum balanced circle. In the second line print $$$k$$$ integers $$$res_1, res_2, dots, res_k$$$, where $$$res_j$$$ is the height of the $$$j$$$-th person in the maximum balanced circle. The condition $$$res_{j} - res_{j + 1} le 1$$$ should be satisfied for all $$$j$$$ from $$$1$$$ to $$$k-1$$$ and the condition $$$res_{1} - res_{k} le 1$$$ should be also satisfied.
2,000
false
true
false
true
false
true
false
false
false
false
4,917
1856A
Alphen has an array of positive integers $$$a$$$ of length $$$n$$$. Alphen can perform the following operation: For all $$$i$$$ from $$$1$$$ to $$$n$$$, replace $$$a_i$$$ with $$$max(0, a_i - 1)$$$. Alphen will perform the above operation until $$$a$$$ is sorted, that is $$$a$$$ satisfies $$$a_1 leq a_2 leq ldots leq a_n$$$. How many operations will Alphen perform? Under the constraints of the problem, it can be proven that Alphen will perform a finite number of operations. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 500$$$)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$$$ ($$$2 le n le 50$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10 ^ 9$$$)xa0— the elements of the array $$$a$$$. Note In the first test case, we have $$$a=[1,2,3]$$$. Since $$$a$$$ is already sorted, Alphen will not need to perform any operations. So, the answer is $$$0$$$. In the second test case, we have $$$a=[2,1,2,1,2]$$$. Since $$$a$$$ is not initially sorted, Alphen will perform one operation to make $$$a=[1,0,1,0,1]$$$. After performing one operation, $$$a$$$ is still not sorted, so Alphen will perform another operation to make $$$a=[0,0,0,0,0]$$$. Since $$$a$$$ is sorted, Alphen will not perform any other operations. Since Alphen has performed two operations in total, the answer is $$$2$$$.
800
false
false
true
false
false
false
false
false
false
false
1,150
107C
In the year 2500 the annual graduation ceremony in the German University in Cairo (GUC) has run smoothly for almost 500 years so far. The most important part of the ceremony is related to the arrangement of the professors in the ceremonial hall. Traditionally GUC has _n_ professors. Each professor has his seniority level. All seniorities are different. Let's enumerate the professors from 1 to _n_, with 1 being the most senior professor and _n_ being the most junior professor. The ceremonial hall has _n_ seats, one seat for each professor. Some places in this hall are meant for more senior professors than the others. More specifically, _m_ pairs of seats are in "senior-junior" relation, and the tradition requires that for all _m_ pairs of seats (_a__i_,u2009_b__i_) the professor seated in "senior" position _a__i_ should be more senior than the professor seated in "junior" position _b__i_. GUC is very strict about its traditions, which have been carefully observed starting from year 2001. The tradition requires that: The seating of the professors changes every year. Year 2001 ceremony was using lexicographically first arrangement of professors in the ceremonial hall. Each consecutive year lexicographically next arrangement of the professors is used. The arrangement of the professors is the list of _n_ integers, where the first integer is the seniority of the professor seated in position number one, the second integer is the seniority of the professor seated in position number two, etc. Given _n_, the number of professors, _y_, the current year and _m_ pairs of restrictions, output the arrangement of the professors for this year. Input The first line contains three integers _n_, _y_ and _m_ (1u2009≤u2009_n_u2009≤u200916,u20092001u2009≤u2009_y_u2009≤u20091018,u20090u2009≤u2009_m_u2009≤u2009100) — the number of professors, the year for which the arrangement should be computed, and the number of pairs of seats for which the seniority relation should be kept, respectively. The next _m_ lines contain one pair of integers each, "_a__i_ _b__i_", indicating that professor on the _a__i_-th seat is more senior than professor on the _b__i_-th seat (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_). Some pair may be listed more than once. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin stream (you may also use the %I64d specificator). Output Print the order in which the professors should be seated in the requested year. If by this year the GUC would have ran out of arrangements, or the given "senior-junior" relation are contradictory, print "The times have changed" (without quotes). Examples Input 7 2020 6 1 2 1 3 2 4 2 5 3 6 3 7 Output The times have changed Output The times have changed Note In the first example the lexicographically first order of seating is 1 2 3. In the third example the GUC will run out of arrangements after the year 3630800. In the fourth example there are no valid arrangements for the seating. The lexicographical comparison of arrangements is performed by the < operator in modern programming languages. The arrangement _a_ is lexicographically less that the arrangement _b_, if there exists such _i_ (1u2009≤u2009_i_u2009≤u2009_n_), that _a__i_u2009<u2009_b__i_, and for any _j_ (1u2009≤u2009_j_u2009<u2009_i_) _a__j_u2009=u2009_b__j_.
2,400
false
false
false
true
false
false
false
false
false
false
9,454
1795F
You are given a tree, consisting of $$$n$$$ vertices. There are $$$k$$$ chips, placed in vertices $$$a_1, a_2, dots, a_k$$$. All $$$a_i$$$ are distinct. Vertices $$$a_1, a_2, dots, a_k$$$ are colored black initially. The remaining vertices are white. You are going to play a game where you perform some moves (possibly, zero). On the $$$i$$$-th move ($$$1$$$-indexed) you are going to move the $$$((i - 1) bmod k + 1)$$$-st chip from its current vertex to an adjacent white vertex and color that vertex black. So, if $$$k=3$$$, you move chip $$$1$$$ on move $$$1$$$, chip $$$2$$$ on move $$$2$$$, chip $$$3$$$ on move $$$3$$$, chip $$$1$$$ on move $$$4$$$, chip $$$2$$$ on move $$$5$$$ and so on. If there is no adjacent white vertex, then the game ends. What's the maximum number of moves you can perform? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of vertices of the tree. Each of the next $$$n - 1$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$)xa0— the descriptions of the edges. The given edges form a tree. The next line contains a single integer $$$k$$$ ($$$1 le k le n$$$)xa0— the number of chips. The next line contains $$$k$$$ integers $$$a_1, a_2, dots, a_k$$$ ($$$1 le a_i le n$$$)xa0— the vertices with the chips. All $$$a_i$$$ are distinct. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the maximum number of moves you can perform. Example Input 5 5 1 2 2 3 3 4 4 5 1 3 5 1 2 2 3 3 4 4 5 2 1 2 5 1 2 2 3 3 4 4 5 2 2 1 6 1 2 1 3 2 4 2 5 3 6 3 1 4 6 1 1 1
2,400
false
true
false
false
false
true
false
true
false
false
1,504
758B
Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland. Now he has a goal to replace dead light bulbs, however he doesn't know how many light bulbs for each color are required. It is guaranteed that for each of four colors at least one light is working. It is known that the garland contains light bulbs of four colors: red, blue, yellow and green. The garland is made as follows: if you take any four consecutive light bulbs then there will not be light bulbs with the same color among them. For example, the garland can look like "RYBGRYBGRY", "YBGRYBGRYBG", "BGRYB", but can not look like "BGRYG", "YBGRYBYGR" or "BGYBGY". Letters denote colors: 'R'xa0— red, 'B'xa0— blue, 'Y'xa0— yellow, 'G'xa0— green. Using the information that for each color at least one light bulb still works count the number of dead light bulbs of each four colors. Input The first and the only line contains the string _s_ (4u2009≤u2009_s_u2009≤u2009100), which describes the garland, the _i_-th symbol of which describes the color of the _i_-th light bulb in the order from the beginning of garland: 'R'xa0— the light bulb is red, 'B'xa0— the light bulb is blue, 'Y'xa0— the light bulb is yellow, 'G'xa0— the light bulb is green, '!'xa0— the light bulb is dead. The string _s_ can not contain other symbols except those five which were described. It is guaranteed that in the given string at least once there is each of four letters 'R', 'B', 'Y' and 'G'. It is guaranteed that the string _s_ is correct garland with some blown light bulbs, it means that for example the line "GRBY!!!B" can not be in the input data. Output In the only line print four integers _k__r_,u2009_k__b_,u2009_k__y_,u2009_k__g_xa0— the number of dead light bulbs of red, blue, yellow and green colors accordingly. Note In the first example there are no dead light bulbs. In the second example it is obvious that one blue bulb is blown, because it could not be light bulbs of other colors on its place according to the statements.
1,100
false
false
true
false
false
false
true
false
false
false
6,756
1615E
Two players, Red and Blue, are at it again, and this time they're playing with crayons! The mischievous duo is now vandalizing a rooted tree, by coloring the nodes while playing their favorite game. The game works as follows: there is a tree of size $$$n$$$, rooted at node $$$1$$$, where each node is initially white. Red and Blue get one turn each. Red goes first. In Red's turn, he can do the following operation any number of times: Pick any subtree of the rooted tree, and color every node in the subtree red. However, to make the game fair, Red is only allowed to color $$$k$$$ nodes of the tree. In other words, after Red's turn, at most $$$k$$$ of the nodes can be colored red. Then, it's Blue's turn. Blue can do the following operation any number of times: Pick any subtree of the rooted tree, and color every node in the subtree blue. However, he's not allowed to choose a subtree that contains a node already colored red, as that would make the node purple and no one likes purple crayon. Note: there's no restriction on the number of nodes Blue can color, as long as he doesn't color a node that Red has already colored. After the two turns, the score of the game is determined as follows: let $$$w$$$ be the number of white nodes, $$$r$$$ be the number of red nodes, and $$$b$$$ be the number of blue nodes. The score of the game is $$$w cdot (r - b)$$$. Red wants to maximize this score, and Blue wants to minimize it. If both players play optimally, what will the final score of the game be? Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le k le n$$$)xa0— the number of vertices in the tree and the maximum number of red nodes. Next $$$n - 1$$$ lines contains description of edges. The $$$i$$$-th line contains two space separated integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i eq v_i$$$)xa0— the $$$i$$$-th edge of the tree. It's guaranteed that given edges form a tree. Note In the first test case, the optimal strategy is as follows: Red chooses to color the subtrees of nodes $$$2$$$ and $$$3$$$. Blue chooses to color the subtree of node $$$4$$$. At the end of this process, nodes $$$2$$$ and $$$3$$$ are red, node $$$4$$$ is blue, and node $$$1$$$ is white. The score of the game is $$$1 cdot (2 - 1) = 1$$$. In the second test case, the optimal strategy is as follows: Red chooses to color the subtree of node $$$4$$$. This colors both nodes $$$4$$$ and $$$5$$$. Blue does not have any options, so nothing is colored blue. At the end of this process, nodes $$$4$$$ and $$$5$$$ are red, and nodes $$$1$$$, $$$2$$$ and $$$3$$$ are white. The score of the game is $$$3 cdot (2 - 0) = 6$$$. For the third test case: The score of the game is $$$4 cdot (2 - 1) = 4$$$.
2,400
true
true
false
false
true
false
false
false
true
true
2,560
63E
Problem - 63E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks dfs and similar dp games implementation *2000 No tag edit access → Contest materials ") ") , otherwise print "Lillebror" (yet again without the quotes). Examples Input . . . . . O . . . O O . . . . . . . . Output Lillebror Input . . . . . . O . . . O . O . O . . O . Output Karlsson
2,000
false
false
true
true
false
false
false
false
false
false
9,656
213A
Furik and Rubik love playing computer games. Furik has recently found a new game that greatly interested Rubik. The game consists of _n_ parts and to complete each part a player may probably need to complete some other ones. We know that the game can be fully completed, that is, its parts do not form cyclic dependencies. Rubik has 3 computers, on which he can play this game. All computers are located in different houses. Besides, it has turned out that each part of the game can be completed only on one of these computers. Let's number the computers with integers from 1 to 3. Rubik can perform the following actions: Complete some part of the game on some computer. Rubik spends exactly 1 hour on completing any part on any computer. Move from the 1-st computer to the 2-nd one. Rubik spends exactly 1 hour on that. Move from the 1-st computer to the 3-rd one. Rubik spends exactly 2 hours on that. Move from the 2-nd computer to the 1-st one. Rubik spends exactly 2 hours on that. Move from the 2-nd computer to the 3-rd one. Rubik spends exactly 1 hour on that. Move from the 3-rd computer to the 1-st one. Rubik spends exactly 1 hour on that. Move from the 3-rd computer to the 2-nd one. Rubik spends exactly 2 hours on that. Help Rubik to find the minimum number of hours he will need to complete all parts of the game. Initially Rubik can be located at the computer he considers necessary. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009200) — the number of game parts. The next line contains _n_ integers, the _i_-th integer — _c__i_ (1u2009≤u2009_c__i_u2009≤u20093) represents the number of the computer, on which you can complete the game part number _i_. Next _n_ lines contain descriptions of game parts. The _i_-th line first contains integer _k__i_ (0u2009≤u2009_k__i_u2009≤u2009_n_u2009-u20091), then _k__i_ distinct integers _a__i_,u2009_j_ (1u2009≤u2009_a__i_,u2009_j_u2009≤u2009_n_;xa0_a__i_,u2009_j_u2009≠u2009_i_) — the numbers of parts to complete before part _i_. Numbers on all lines are separated by single spaces. You can assume that the parts of the game are numbered from 1 to _n_ in some way. It is guaranteed that there are no cyclic dependencies between the parts of the game. Output On a single line print the answer to the problem. Examples Input 5 2 2 1 1 3 1 5 2 5 1 2 5 4 1 5 0 Note Note to the second sample: before the beginning of the game the best strategy is to stand by the third computer. First we complete part 5. Then we go to the 1-st computer and complete parts 3 and 4. Then we go to the 2-nd computer and complete parts 1 and 2. In total we get 1+1+2+1+2, which equals 7 hours.
1,700
false
true
false
false
false
false
false
false
false
false
8,988
704A
Thor is getting used to the Earth. As a gift Loki gave him a smartphone. There are _n_ applications on this phone. Thor is fascinated by this phone. He has only one minor issue: he can't count the number of unread notifications generated by those applications (maybe Loki put a curse on it so he can't). _q_ events are about to happen (in chronological order). They are of three types: 1. Application _x_ generates a notification (this new notification is unread). 2. Thor reads all notifications generated so far by application _x_ (he may re-read some notifications). 3. Thor reads the first _t_ notifications generated by phone applications (notifications generated in first _t_ events of the first type). It's guaranteed that there were at least _t_ events of the first type before this event. Please note that he doesn't read first _t_ unread notifications, he just reads the very first _t_ notifications generated on his phone and he may re-read some of them in this operation. Please help Thor and tell him the number of unread notifications after each event. You may assume that initially there are no notifications in the phone. Input The first line of input contains two integers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009300u2009000)xa0— the number of applications and the number of events to happen. The next _q_ lines contain the events. The _i_-th of these lines starts with an integer _type__i_xa0— type of the _i_-th event. If _type__i_u2009=u20091 or _type__i_u2009=u20092 then it is followed by an integer _x__i_. Otherwise it is followed by an integer _t__i_ (1u2009≤u2009_type__i_u2009≤u20093,u20091u2009≤u2009_x__i_u2009≤u2009_n_,u20091u2009≤u2009_t__i_u2009≤u2009_q_). Output Print the number of unread notifications after each event. Examples Input 4 6 1 2 1 4 1 2 3 3 1 3 1 3 Note In the first sample: 1. Application 3 generates a notification (there is 1 unread notification). 2. Application 1 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads the notification generated by application 3, there are 2 unread notifications left. In the second sample test: 1. Application 2 generates a notification (there is 1 unread notification). 2. Application 4 generates a notification (there are 2 unread notifications). 3. Application 2 generates a notification (there are 3 unread notifications). 4. Thor reads first three notifications and since there are only three of them so far, there will be no unread notification left. 5. Application 3 generates a notification (there is 1 unread notification). 6. Application 3 generates a notification (there are 2 unread notifications).
1,600
false
false
true
false
true
false
true
false
false
false
7,006
1059D
There is a forest that we model as a plane and live $$$n$$$ rare animals. Animal number $$$i$$$ has its lair in the point $$$(x_{i}, y_{i})$$$. In order to protect them, a decision to build a nature reserve has been made. The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river. For convenience, scientists have made a transformation of coordinates so that the river is defined by $$$y = 0$$$. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve. Input The first line contains one integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of animals. Each of the next $$$n$$$ lines contains two integers $$$x_{i}$$$, $$$y_{i}$$$ ($$$-10^7 le x_{i}, y_{i} le 10^7$$$) — the coordinates of the $$$i$$$-th animal's lair. It is guaranteed that $$$y_{i} eq 0$$$. No two lairs coincide. Output If the reserve cannot be built, print $$$-1$$$. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is considered correct if $$$frac{a - b}{max{(1, b)}} le 10^{-6}$$$. Note In the first sample it is optimal to build the reserve with the radius equal to $$$0.5$$$ and the center in $$$(0, 0.5)$$$. In the second sample it is impossible to build a reserve. In the third sample it is optimal to build the reserve with the radius equal to $$$frac{5}{8}$$$ and the center in $$$(frac{1}{2}, frac{5}{8})$$$.
2,200
false
false
false
false
false
false
false
true
false
false
5,418
1467B
You are given a sequence of $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$. Let us call an index $$$j$$$ ($$$2 le j le {{n-1}}$$$) a hill if $$$a_j > a_{{j+1}}$$$ and $$$a_j > a_{{j-1}}$$$; and let us call it a valley if $$$a_j < a_{{j+1}}$$$ and $$$a_j < a_{{j-1}}$$$. Let us define the intimidation value of a sequence as the sum of the number of hills and the number of valleys in the sequence. You can change exactly one integer in the sequence to any number that you want, or let the sequence remain unchanged. What is the minimum intimidation value that you can achieve? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3cdot10^5$$$). The second line of each test case contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3cdot10^5$$$. Output For each test case, print a single integer — the minimum intimidation value that you can achieve. Example Input 4 3 1 5 3 5 2 2 2 2 2 6 1 6 2 5 2 10 5 1 6 2 5 1 Note In the first test case, changing $$$a_2$$$ to $$$2$$$ results in no hills and no valleys. In the second test case, the best answer is just to leave the array as it is. In the third test case, changing $$$a_3$$$ to $$$6$$$ results in only one valley (at the index $$$5$$$). In the fourth test case, changing $$$a_3$$$ to $$$6$$$ results in no hills and no valleys.
1,700
false
false
true
false
false
false
true
false
false
false
3,356
337A
The end of the school year is near and Ms. Manana, the teacher, will soon have to say goodbye to a yet another class. She decided to prepare a goodbye present for her _n_ students and give each of them a jigsaw puzzle (which, as wikipedia states, is a tiling puzzle that requires the assembly of numerous small, often oddly shaped, interlocking and tessellating pieces). The shop assistant told the teacher that there are _m_ puzzles in the shop, but they might differ in difficulty and size. Specifically, the first jigsaw puzzle consists of _f_1 pieces, the second one consists of _f_2 pieces and so on. Ms. Manana doesn't want to upset the children, so she decided that the difference between the numbers of pieces in her presents must be as small as possible. Let _A_ be the number of pieces in the largest puzzle that the teacher buys and _B_ be the number of pieces in the smallest such puzzle. She wants to choose such _n_ puzzles that _A_u2009-u2009_B_ is minimum possible. Help the teacher and find the least possible value of _A_u2009-u2009_B_. Input The first line contains space-separated integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009_m_u2009≤u200950). The second line contains _m_ space-separated integers _f_1,u2009_f_2,u2009...,u2009_f__m_ (4u2009≤u2009_f__i_u2009≤u20091000) — the quantities of pieces in the puzzles sold in the shop. Output Print a single integer — the least possible difference the teacher can obtain. Note Sample 1. The class has 4 students. The shop sells 6 puzzles. If Ms. Manana buys the first four puzzles consisting of 10, 12, 10 and 7 pieces correspondingly, then the difference between the sizes of the largest and the smallest puzzle will be equal to 5. It is impossible to obtain a smaller difference. Note that the teacher can also buy puzzles 1, 3, 4 and 5 to obtain the difference 5.
900
false
true
false
false
false
false
false
false
false
false
8,487
1575A
Andi and Budi were given an assignment to tidy up their bookshelf of $$$n$$$ books. Each book is represented by the book title — a string $$$s_i$$$ numbered from $$$1$$$ to $$$n$$$, each with length $$$m$$$. Andi really wants to sort the book lexicographically ascending, while Budi wants to sort it lexicographically descending. Settling their fight, they decided to combine their idea and sort it asc-desc-endingly, where the odd-indexed characters will be compared ascendingly, and the even-indexed characters will be compared descendingly. A string $$$a$$$ occurs before a string $$$b$$$ in asc-desc-ending order if and only if in the first position where $$$a$$$ and $$$b$$$ differ, the following holds: if it is an odd position, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$; if it is an even position, the string $$$a$$$ has a letter that appears later in the alphabet than the corresponding letter in $$$b$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n cdot m leq 10^6$$$). The $$$i$$$-th of the next $$$n$$$ lines contains a string $$$s_i$$$ consisting of $$$m$$$ uppercase Latin letters — the book title. The strings are pairwise distinct. Output Output $$$n$$$ integers — the indices of the strings after they are sorted asc-desc-endingly. Note The following illustrates the first example.
1,100
false
false
false
false
true
false
false
false
true
false
2,768
1633D
You have an array of integers $$$a$$$ of size $$$n$$$. Initially, all elements of the array are equal to $$$1$$$. You can perform the following operation: choose two integers $$$i$$$ ($$$1 le i le n$$$) and $$$x$$$ ($$$x > 0$$$), and then increase the value of $$$a_i$$$ by $$$leftlfloorfrac{a_i}{x} ight floor$$$ (i.e. make $$$a_i = a_i + leftlfloorfrac{a_i}{x} ight floor$$$). After performing all operations, you will receive $$$c_i$$$ coins for all such $$$i$$$ that $$$a_i = b_i$$$. Your task is to determine the maximum number of coins that you can receive by performing no more than $$$k$$$ operations. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^3; 0 le k le 10^6$$$)xa0— the size of the array and the maximum number of operations, respectively. The second line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^3$$$). The third line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^6$$$). The sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. Output For each test case, print one integerxa0— the maximum number of coins that you can get by performing no more than $$$k$$$ operations. Example Input 4 4 4 1 7 5 2 2 6 5 2 3 0 3 5 2 5 4 7 5 9 5 2 5 6 3 5 9 1 9 7 6 14 11 4 6 2 8 16 43 45 9 41 15 38
1,600
false
true
false
true
false
false
false
false
false
false
2,449
1620G
For a sequence of strings $$$[t_1, t_2, dots, t_m]$$$, let's define the function $$$f([t_1, t_2, dots, t_m])$$$ as the number of different strings (including the empty string) that are subsequences of at least one string $$$t_i$$$. $$$f([]) = 0$$$ (i.u2009e. the number of such strings for an empty sequence is $$$0$$$). You are given a sequence of strings $$$[s_1, s_2, dots, s_n]$$$. Every string in this sequence consists of lowercase Latin letters and is sorted (i.u2009e., each string begins with several (maybe zero) characters a, then several (maybe zero) characters b, ..., ends with several (maybe zero) characters z). For each of $$$2^n$$$ subsequences of $$$[s_1, s_2, dots, s_n]$$$, calculate the value of the function $$$f$$$ modulo $$$998244353$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 23$$$) — the number of strings. Then $$$n$$$ lines follow. The $$$i$$$-th line contains the string $$$s_i$$$ ($$$1 le s_i le 2 cdot 10^4$$$), consisting of lowercase Latin letters. Each string $$$s_i$$$ is sorted. Output Since printing up to $$$2^{23}$$$ integers would be really slow, you should do the following: For each of the $$$2^n$$$ subsequences (which we denote as $$$[s_{i_1}, s_{i_2}, dots, s_{i_k}]$$$), calculate $$$f([s_{i_1}, s_{i_2}, dots, s_{i_k}])$$$, take it modulo $$$998244353$$$, then multiply it by $$$k cdot (i_1 + i_2 + dots + i_k)$$$. Print the XOR of all $$$2^n$$$ integers you get. The indices $$$i_1, i_2, dots, i_k$$$ in the description of each subsequences are $$$1$$$-indexed (i.u2009e. are from $$$1$$$ to $$$n$$$). Examples Input 3 ddd aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaabbbbbbbbbbbcccccccccccciiiiiiiiiiiiiiiiiiiiiiooooooooooqqqqqqqqqqqqqqqqqqvvvvvzzzzzzzzzzzz
2,400
false
false
false
true
false
false
false
false
false
false
2,521
372D
There is a tree consisting of _n_ vertices. The vertices are numbered from 1 to _n_. Let's define the length of an interval [_l_,u2009_r_] as the value _r_u2009-u2009_l_u2009+u20091. The score of a subtree of this tree is the maximum length of such an interval [_l_,u2009_r_] that, the vertices with numbers _l_,u2009_l_u2009+u20091,u2009...,u2009_r_ belong to the subtree. Considering all subtrees of the tree whose size is at most _k_, return the maximum score of the subtree. Note, that in this problem tree is not rooted, so a subtree — is an arbitrary connected subgraph of the tree. Input There are two integers in the first line, _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105). Each of the next _n_u2009-u20091 lines contains integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_). That means _a__i_ and _b__i_ are connected by a tree edge. It is guaranteed that the input represents a tree. Output Output should contain a single integer — the maximum possible score. Examples Input 10 6 4 10 10 6 2 9 9 6 8 5 7 1 4 7 7 3 1 8 Input 16 7 13 11 12 11 2 14 8 6 9 15 16 11 5 14 6 15 4 3 11 15 15 14 10 1 3 14 14 7 1 7 Note For the first case, there is some subtree whose size is at most 6, including 3 consecutive numbers of vertices. For example, the subtree that consists of {1,u20093,u20094,u20095,u20097,u20098} or of {1,u20094,u20096,u20097,u20098,u200910} includes 3 consecutive numbers of vertices. But there is no subtree whose size is at most 6, which includes 4 or more consecutive numbers of vertices.
2,600
false
false
false
false
true
false
false
true
false
false
8,340
240F
A boy named Leo doesn't miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following problem. He was given a string _s_, consisting of _n_ lowercase English letters, and _m_ queries. Each query is characterised by a pair of integers _l__i_,u2009_r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). We'll consider the letters in the string numbered from 1 to _n_ from left to right, that is, _s_u2009=u2009_s_1_s_2... _s__n_. After each query he must swap letters with indexes from _l__i_ to _r__i_ inclusive in string _s_ so as to make substring (_l__i_,u2009_r__i_) a palindrome. If there are multiple such letter permutations, you should choose the one where string (_l__i_,u2009_r__i_) will be lexicographically minimum. If no such permutation exists, you should ignore the query (that is, not change string _s_). Everybody knows that on TorCoder rounds input line and array size limits never exceed 60, so Leo solved this problem easily. Your task is to solve the problem on a little bit larger limits. Given string _s_ and _m_ queries, print the string that results after applying all _m_ queries to string _s_. Input The first input line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the string length and the number of the queries. The second line contains string _s_, consisting of _n_ lowercase Latin letters. Each of the next _m_ lines contains a pair of integers _l__i_,u2009_r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_) — a query to apply to the string. Output In a single line print the result of applying _m_ queries to string _s_. Print the queries in the order in which they are given in the input. Note A substring (_l__i_,u2009_r__i_) 1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_) of string _s_u2009=u2009_s_1_s_2... _s__n_ of length _n_ is a sequence of characters _s__l__i__s__l__i_u2009+u20091..._s__r__i_. A string is a palindrome, if it reads the same from left to right and from right to left. String _x_1_x_2... _x__p_ is lexicographically smaller than string _y_1_y_2... _y__q_, if either _p_u2009<u2009_q_ and _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__p_u2009=u2009_y__p_, or exists such number _r_ (_r_u2009<u2009_p_,u2009_r_u2009<u2009_q_), that _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__r_u2009=u2009_y__r_ and _x__r_u2009+u20091u2009<u2009_y__r_u2009+u20091.
2,600
false
false
false
false
true
false
false
false
false
false
8,876
1349F2
Note that the only differences between easy and hard versions are the constraints on $$$n$$$ and the time limit. You can make hacks only if all versions are solved. Slime is interested in sequences. He defined good positive integer sequences $$$p$$$ of length $$$n$$$ as follows: For each $$$k>1$$$ that presents in $$$p$$$, there should be at least one pair of indices $$$i,j$$$, such that $$$1 leq i < j leq n$$$, $$$p_i = k - 1$$$ and $$$p_j = k$$$. For the given integer $$$n$$$, the set of all good sequences of length $$$n$$$ is $$$s_n$$$. For the fixed integer $$$k$$$ and the sequence $$$p$$$, let $$$f_p(k)$$$ be the number of times that $$$k$$$ appears in $$$p$$$. For each $$$k$$$ from $$$1$$$ to $$$n$$$, Slime wants to know the following value: $$$$$$left(sum_{pin s_n} f_p(k) ight) extrm{mod} 998,244,353$$$$$$ Input The first line contains one integer $$$n (1le nle 100,000)$$$. Output Print $$$n$$$ integers, the $$$i$$$-th of them should be equal to $$$left(sum_{pin s_n} f_p(i) ight) extrm{mod} 998,244,353$$$. Note In the first example, $$$s={[1,1],[1,2]}$$$. In the second example, $$$s={[1,1,1],[1,1,2],[1,2,1],[1,2,2],[2,1,2],[1,2,3]}$$$. In the third example, $$$s={[1]}$$$.
3,500
true
false
false
true
false
false
false
false
false
false
3,958
802B
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. data structures greedy *1800 No tag edit access time limit per test 10 seconds memory limit per test 256 megabytes Whereas humans nowadays read fewer and fewer books on paper, book readership among marmots has surged. Heidi has expanded the library and is now serving longer request sequences. Input Same as the easy version, but the limits have changed: 1u2009≤u2009_n_,u2009_k_u2009≤u2009400u2009000. Output Same as the easy version.
1,800
false
true
false
false
true
false
false
false
false
false
6,566
847D
A new dog show on TV is starting next week. On the show dogs are required to demonstrate bottomless stomach, strategic thinking and self-preservation instinct. You and your dog are invited to compete with other participants and naturally you want to win! On the show a dog needs to eat as many bowls of dog food as possible (bottomless stomach helps here). Dogs compete separately of each other and the rules are as follows: At the start of the show the dog and the bowls are located on a line. The dog starts at position _x_u2009=u20090 and _n_ bowls are located at positions _x_u2009=u20091,u2009_x_u2009=u20092,u2009...,u2009_x_u2009=u2009_n_. The bowls are numbered from 1 to _n_ from left to right. After the show starts the dog immediately begins to run to the right to the first bowl. The food inside bowls is not ready for eating at the start because it is too hot (dog's self-preservation instinct prevents eating). More formally, the dog can eat from the _i_-th bowl after _t__i_ seconds from the start of the show or later. It takes dog 1 second to move from the position _x_ to the position _x_u2009+u20091. The dog is not allowed to move to the left, the dog runs only to the right with the constant speed 1 distance unit per second. When the dog reaches a bowl (say, the bowl _i_), the following cases are possible: the food had cooled down (i.e. it passed at least _t__i_ seconds from the show start): the dog immediately eats the food and runs to the right without any stop, the food is hot (i.e. it passed less than _t__i_ seconds from the show start): the dog has two options: to wait for the _i_-th bowl, eat the food and continue to run at the moment _t__i_ or to skip the _i_-th bowl and continue to run to the right without any stop. After _T_ seconds from the start the show ends. If the dog reaches a bowl of food at moment _T_ the dog can not eat it. The show stops before _T_ seconds if the dog had run to the right of the last bowl. You need to help your dog create a strategy with which the maximum possible number of bowls of food will be eaten in _T_ seconds. Input Two integer numbers are given in the first line - _n_ and _T_ (1u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_T_u2009≤u20092·109) — the number of bowls of food and the time when the dog is stopped. On the next line numbers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u2009109) are given, where _t__i_ is the moment of time when the _i_-th bowl of food is ready for eating.
2,200
false
true
false
false
true
true
false
false
false
false
6,368
432E
You have an _n_u2009×u2009_m_ rectangle table, its cells are not initially painted. Your task is to paint all cells of the table. The resulting picture should be a tiling of the table with squares. More formally: each cell must be painted some color (the colors are marked by uppercase Latin letters); we will assume that two cells of the table are connected if they are of the same color and share a side; each connected region of the table must form a square. Given _n_ and _m_, find lexicographically minimum coloring of the table that meets the described properties. Input The first line contains two integers, _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100). Output Print lexicographically minimum coloring of the table that meets the described conditions. One coloring (let's call it X) is considered lexicographically less than the other one (let's call it Y), if: consider all the table cells from left to right and from top to bottom (first, the first cell in the first row, then the second cell in the first row and so on); let's find in this order the first cell that has distinct colors in two colorings; the letter that marks the color of the cell in X, goes alphabetically before the letter that marks the color of the cell in Y.
2,300
false
true
false
false
false
true
false
false
false
false
8,110
630B
Problem - 630B - Codeforces =============== xa0 ]( "Experimental Educational Round: VolBIT Formulas Blitz") and _t_ (0u2009≤u2009_t_u2009≤u20092xa0000xa0000xa0000)xa0— the number of transistors in the initial time and the number of seconds passed since the initial time. Output Output one number — the estimate of the number of transistors in a dence integrated circuit in _t_ seconds since the initial time. The relative error of your answer should not be greater than 10u2009-u20096. Examples Input 1000 1000000 Output 1011.060722383550382782399454922040
1,200
true
false
false
false
false
false
false
false
false
false
7,303
1925D
There are $$$n$$$ children in a class, $$$m$$$ pairs among them are friends. The $$$i$$$-th pair who are friends have a friendship value of $$$f_i$$$. The teacher has to go for $$$k$$$ excursions, and for each of the excursions she chooses a pair of children randomly, equiprobably and independently. If a pair of children who are friends is chosen, their friendship value increases by $$$1$$$ for all subsequent excursions (the teacher can choose a pair of children more than once). The friendship value of a pair who are not friends is considered $$$0$$$, and it does not change for subsequent excursions. Find the expected value of the sum of friendship values of all $$$k$$$ pairs chosen for the excursions (at the time of being chosen). It can be shown that this answer can always be expressed as a fraction $$$dfrac{p}{q}$$$ where $$$p$$$ and $$$q$$$ are coprime integers. Calculate $$$pcdot q^{-1} bmod (10^9+7)$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5 cdot 10^4$$$). Description of the test cases follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 10^5$$$, $$$0 le m le min Big(10^5$$$, $$$ frac{n(n-1)}{2} Big)$$$, $$$1 le k le 2 cdot 10^5$$$) — the number of children, pairs of friends and excursions respectively. The next $$$m$$$ lines contain three integers each — $$$a_i$$$, $$$b_i$$$, $$$f_i$$$ — the indices of the pair of children who are friends and their friendship value. ($$$a_i eq b_i$$$, $$$1 le a_i,b_i le n$$$, $$$1 le f_i le 10^9$$$). It is guaranteed that all pairs of friends are distinct. It is guaranteed that the sum of $$$n$$$ and sum $$$m$$$ over all test cases does not exceed $$$10^5$$$ and the sum of $$$k$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integer — the answer to the problem. Example Input 4 100 0 24 2 1 10 1 2 1 3 1 2 2 1 1 5 2 4 1 2 25 3 2 24 Output 0 55 777777784 40000020 Note For the first test case, there are no pairs of friends, so the friendship value of all pairs is $$$0$$$ and stays $$$0$$$ for subsequent rounds, hence the friendship value for all excursions is $$$0$$$. For the second test case, there is only one pair possible $$$(1, 2)$$$ and its friendship value is initially $$$1$$$, so each turn they are picked and their friendship value increases by $$$1$$$. Therefore, the total sum is $$$1+2+3+ldots+10 = 55$$$. For the third test case, the final answer is $$$frac{7}{9} = 777,777,784bmod (10^9+7)$$$.
1,900
true
false
false
true
false
false
false
false
false
false
738
1968D
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 943 (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 brute force dfs and similar games graphs greedy math *1300 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D. Permutation Game time limit per test2 seconds memory limit per test256 megabytes Bodya and Sasha found a permutation $$$p_1,dots,p_n$$$ and an array $$$a_1,dots,a_n$$$. They decided to play a well-known "Permutation game". 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). Both of them chose a starting position in the permutation. The game lasts $$$k$$$ turns. The players make moves simultaneously. On each turn, two things happen to each player: If the current position of the player is $$$x$$$, his score increases by $$$a_x$$$. Then the player either stays at his current position $$$x$$$ or moves from $$$x$$$ to $$$p_x$$$. The winner of the game is the player with the higher score after exactly $$$k$$$ turns. Knowing Bodya's starting position $$$P_B$$$ and Sasha's starting position $$$P_S$$$, determine who wins the game if both players are trying to win. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of testcases. The first line of each testcase contains integers $$$n$$$, $$$k$$$, $$$P_B$$$, $$$P_S$$$ ($$$1le P_B,P_Sle nle 2cdot 10^5$$$, $$$1le kle 10^9$$$)xa0— length of the permutation, duration of the game, starting positions respectively. The next line contains $$$n$$$ integers $$$p_1,dots,p_n$$$ ($$$1 le p_i le n$$$)xa0— elements of the permutation $$$p$$$. The next line contains $$$n$$$ integers $$$a_1,dots,a_n$$$ ($$$1le a_ile 10^9$$$)xa0— elements of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each testcase output: "Bodya" if Bodya wins the game. "Sasha" if Sasha wins the game. "Draw" if the players have the same score. Example input 10 4 2 3 2 4 1 2 3 7 2 5 6 10 8 2 10 3 1 4 5 2 7 8 10 6 9 5 10 5 1 3 7 10 15 4 3 2 1000000000 1 2 1 2 4 4 8 10 4 1 5 1 4 3 2 8 6 7 1 1 2 1 2 100 101 102 5 1 2 5 1 2 4 5 3 4 6 9 4 2 4 2 3 1 4 1 3 2 6 8 5 3 6 9 5 4 6 1 3 5 2 4 6 9 8 9 5 10 4 8 4 2 2 3 4 1 5 2 8 7 4 2 3 1 4 1 3 2 6 8 5 3 2 1000000000 1 2 1 2 1000000000 2 output Bodya Sasha Draw Draw Bodya Sasha Sasha Sasha Sasha Bodya Note Below you can find the explanation for the first testcase, where the game consists of $$$k=2$$$ turns. Turn Bodya's position Bodya's score Bodya's move Sasha's position Sasha's score Sasha's move first $$$3$$$ $$$0 + a_3 = 0 + 5 = 5$$$ stays on the same position $$$2$$$ $$$0 + a_2 = 0 + 2 = 2$$$ moves to $$$p_2=1$$$ second $$$3$$$ $$$5 + a_3 = 5 + 5 = 10$$$ stays on the same position $$$1$$$ $$$2 + a_1 = 2 + 7 = 9$$$ stays on the same position final results $$$3$$$ $$$10$$$ $$$1$$$ $$$9$$$ As we may see, Bodya's score is greater, so he wins the game. It can be shown that Bodya always can win this game. Codeforces (c)
1,300
true
true
false
false
false
false
true
false
false
true
497
1809B
You are given a two-dimensional plane, and you need to place $$$n$$$ chips on it. You can place a chip only at a point with integer coordinates. The cost of placing a chip at the point $$$(x, y)$$$ is equal to $$$x + y$$$ (where $$$a$$$ is the absolute value of $$$a$$$). The cost of placing $$$n$$$ chips is equal to the maximum among the costs of each chip. You need to place $$$n$$$ chips on the plane in such a way that the Euclidean distance between each pair of chips is strictly greater than $$$1$$$, and the cost is the minimum possible. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^{18}$$$)xa0— the number of chips you need to place. Note In the first test case, you can place the only chip at point $$$(0, 0)$$$ with total cost equal to $$$0 + 0 = 0$$$. In the second test case, you can, for example, place chips at points $$$(-1, 0)$$$, $$$(0, 1)$$$ and $$$(1, 0)$$$ with costs $$$-1 + 0 = 1$$$, $$$0 + 1 = 1$$$ and $$$0 + 1 = 1$$$. Distance between each pair of chips is greater than $$$1$$$ (for example, distance between $$$(-1, 0)$$$ and $$$(0, 1)$$$ is equal to $$$sqrt{2}$$$). The total cost is equal to $$$max(1, 1, 1) = 1$$$. In the third test case, you can, for example, place chips at points $$$(-1, -1)$$$, $$$(-1, 1)$$$, $$$(1, 1)$$$, $$$(0, 0)$$$ and $$$(0, 2)$$$. The total cost is equal to $$$max(2, 2, 2, 0, 2) = 2$$$.
1,000
true
true
false
false
false
false
false
true
false
false
1,419