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
1227D1
This is the easier version of the problem. In this version $$$1 le n, m le 100$$$. You can hack this problem only if you solve and lock both problems. You are given a sequence of integers $$$a=[a_1,a_2,dots,a_n]$$$ of length $$$n$$$. Its subsequence is obtained by removing zero or more elements from the sequence $$$a$$$ (they do not necessarily go consecutively). For example, for the sequence $$$a=[11,20,11,33,11,20,11]$$$: $$$[11,20,11,33,11,20,11]$$$, $$$[11,20,11,33,11,20]$$$, $$$[11,11,11,11]$$$, $$$[20]$$$, $$$[33,20]$$$ are subsequences (these are just some of the long list); $$$[40]$$$, $$$[33,33]$$$, $$$[33,20,20]$$$, $$$[20,20,11,11]$$$ are not subsequences. Suppose that an additional non-negative integer $$$k$$$ ($$$1 le k le n$$$) is given, then the subsequence is called optimal if: it has a length of $$$k$$$ and the sum of its elements is the maximum possible among all subsequences of length $$$k$$$; and among all subsequences of length $$$k$$$ that satisfy the previous item, it is lexicographically minimal. Recall that the sequence $$$b=[b_1, b_2, dots, b_k]$$$ is lexicographically smaller than the sequence $$$c=[c_1, c_2, dots, c_k]$$$ if the first element (from the left) in which they differ less in the sequence $$$b$$$ than in $$$c$$$. Formally: there exists $$$t$$$ ($$$1 le t le k$$$) such that $$$b_1=c_1$$$, $$$b_2=c_2$$$, ..., $$$b_{t-1}=c_{t-1}$$$ and at the same time $$$b_t<c_t$$$. For example: $$$[10, 20, 20]$$$ lexicographically less than $$$[10, 21, 1]$$$, $$$[7, 99, 99]$$$ is lexicographically less than $$$[10, 21, 1]$$$, $$$[10, 21, 0]$$$ is lexicographically less than $$$[10, 21, 1]$$$. You are given a sequence of $$$a=[a_1,a_2,dots,a_n]$$$ and $$$m$$$ requests, each consisting of two numbers $$$k_j$$$ and $$$pos_j$$$ ($$$1 le k le n$$$, $$$1 le pos_j le k_j$$$). For each query, print the value that is in the index $$$pos_j$$$ of the optimal subsequence of the given sequence $$$a$$$ for $$$k=k_j$$$. For example, if $$$n=4$$$, $$$a=[10,20,30,20]$$$, $$$k_j=2$$$, then the optimal subsequence is $$$[20,30]$$$ — it is the minimum lexicographically among all subsequences of length $$$2$$$ with the maximum total sum of items. Thus, the answer to the request $$$k_j=2$$$, $$$pos_j=1$$$ is the number $$$20$$$, and the answer to the request $$$k_j=2$$$, $$$pos_j=2$$$ is the number $$$30$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 100$$$) — the length of the sequence $$$a$$$. The second line contains elements of the sequence $$$a$$$: integer numbers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The third line contains an integer $$$m$$$ ($$$1 le m le 100$$$) — the number of requests. The following $$$m$$$ lines contain pairs of integers $$$k_j$$$ and $$$pos_j$$$ ($$$1 le k le n$$$, $$$1 le pos_j le k_j$$$) — the requests.
1,600
false
true
false
false
true
false
false
false
false
false
4,541
208E
Polycarpus got hold of a family relationship tree. The tree describes family relationships of _n_ people, numbered 1 through _n_. Each person in the tree has no more than one parent. Let's call person _a_ a 1-ancestor of person _b_, if _a_ is the parent of _b_. Let's call person _a_ a _k_-ancestor (_k_u2009>u20091) of person _b_, if person _b_ has a 1-ancestor, and _a_ is a (_k_u2009-u20091)-ancestor of _b_'s 1-ancestor. Family relationships don't form cycles in the found tree. In other words, there is no person who is his own ancestor, directly or indirectly (that is, who is an _x_-ancestor for himself, for some _x_, _x_u2009>u20090). Let's call two people _x_ and _y_ (_x_u2009≠u2009_y_) _p_-th cousins (_p_u2009>u20090), if there is person _z_, who is a _p_-ancestor of _x_ and a _p_-ancestor of _y_. Polycarpus wonders how many counsins and what kinds of them everybody has. He took a piece of paper and wrote _m_ pairs of integers _v__i_, _p__i_. Help him to calculate the number of _p__i_-th cousins that person _v__i_ has, for each pair _v__i_, _p__i_. Input The first input line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of people in the tree. The next line contains _n_ space-separated integers _r_1,u2009_r_2,u2009...,u2009_r__n_, where _r__i_ (1u2009≤u2009_r__i_u2009≤u2009_n_) is the number of person _i_'s parent or 0, if person _i_ has no parent. It is guaranteed that family relationships don't form cycles. The third line contains a single number _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of family relationship queries Polycarus has. Next _m_ lines contain pairs of space-separated integers. The _i_-th line contains numbers _v__i_, _p__i_ (1u2009≤u2009_v__i_,u2009_p__i_u2009≤u2009_n_).
2,100
false
false
false
false
true
false
false
true
false
false
8,997
683D
Problem - 683D - Codeforces =============== xa0 times. The final piece of the chocolate bar should be whole, and breaks are made along the line of slices' section for the whole length of the current piece. Input The first line contains the positive integer _q_ (1u2009≤u2009_q_u2009≤u2009100) — the number of chocolate bars. Each of the following _q_ lines contains three positive integers _n_, _m_ and _p_ (1u2009≤u2009_n_,u2009_m_,u2009_p_u2009≤u20091000) — the size of the chocolate bar, and the weight of the piece which should be obtained. Output The output should contain _q_ lines and the _i_-th line must contain "Yes" (without the quotes), if it is possible to perform the task for _i_-th chocolate bar, or "No" otherwise. Example Input 2 3 3 4 4 4 7 Output Yes No
1,400
true
false
false
false
false
false
false
false
false
false
7,090
1433B
There is a bookshelf which can fit $$$n$$$ books. The $$$i$$$-th position of bookshelf is $$$a_i = 1$$$ if there is a book on this position and $$$a_i = 0$$$ otherwise. It is guaranteed that there is at least one book on the bookshelf. In one move, you can choose some contiguous segment $$$[l; r]$$$ consisting of books (i.e. for each $$$i$$$ from $$$l$$$ to $$$r$$$ the condition $$$a_i = 1$$$ holds) and: Shift it to the right by $$$1$$$: move the book at index $$$i$$$ to $$$i + 1$$$ for all $$$l le i le r$$$. This move can be done only if $$$r+1 le n$$$ and there is no book at the position $$$r+1$$$. Shift it to the left by $$$1$$$: move the book at index $$$i$$$ to $$$i-1$$$ for all $$$l le i le r$$$. This move can be done only if $$$l-1 ge 1$$$ and there is no book at the position $$$l-1$$$. Your task is to find the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without any gaps). For example, for $$$a = [0, 0, 1, 0, 1]$$$ there is a gap between books ($$$a_4 = 0$$$ when $$$a_3 = 1$$$ and $$$a_5 = 1$$$), for $$$a = [1, 1, 0]$$$ there are no gaps between books and for $$$a = [0, 0,0]$$$ there are also no gaps between books. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 200$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) — the number of places on a bookshelf. The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 1$$$), where $$$a_i$$$ is $$$1$$$ if there is a book at this position and $$$0$$$ otherwise. It is guaranteed that there is at least one book on the bookshelf. Output For each test case, print one integer: the minimum number of moves required to collect all the books on the shelf as a contiguous (consecutive) segment (i.e. the segment without gaps). Example Input 5 7 0 0 1 0 1 0 1 3 1 0 0 5 1 1 0 0 1 6 1 0 0 0 0 1 5 1 1 0 1 1 Note In the first test case of the example, you can shift the segment $$$[3; 3]$$$ to the right and the segment $$$[4; 5]$$$ to the right. After all moves, the books form the contiguous segment $$$[5; 7]$$$. So the answer is $$$2$$$. In the second test case of the example, you have nothing to do, all the books on the bookshelf form the contiguous segment already. In the third test case of the example, you can shift the segment $$$[5; 5]$$$ to the left and then the segment $$$[4; 4]$$$ to the left again. After all moves, the books form the contiguous segment $$$[1; 3]$$$. So the answer is $$$2$$$. In the fourth test case of the example, you can shift the segment $$$[1; 1]$$$ to the right, the segment $$$[2; 2]$$$ to the right, the segment $$$[6; 6]$$$ to the left and then the segment $$$[5; 5]$$$ to the left. After all moves, the books form the contiguous segment $$$[3; 4]$$$. So the answer is $$$4$$$. In the fifth test case of the example, you can shift the segment $$$[1; 2]$$$ to the right. After all moves, the books form the contiguous segment $$$[2; 5]$$$. So the answer is $$$1$$$.
800
false
true
true
false
false
false
false
false
false
false
3,498
1904A
Lunchbox is done with playing chess! His queen and king just got forked again! In chess, a fork is when a knight attacks two pieces of higher value, commonly the king and the queen. Lunchbox knows that knights can be tricky, and in the version of chess that he is playing, knights are even trickier: instead of moving $$$1$$$ tile in one direction and $$$2$$$ tiles in the other, knights in Lunchbox's modified game move $$$a$$$ tiles in one direction and $$$b$$$ tiles in the other. Lunchbox is playing chess on an infinite chessboard which contains all cells $$$(x,y)$$$ where $$$x$$$ and $$$y$$$ are (possibly negative) integers. Lunchbox's king and queen are placed on cells $$$(x_K,y_K)$$$ and $$$(x_Q,y_Q)$$$ respectively. Find the number of positions such that if a knight was placed on that cell, it would attack both the king and queen. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^8$$$)xa0— describing the possible moves of the knight. The second line of each test case contains two integers $$$x_K$$$ and $$$y_K$$$ ($$$0 le x_K, y_K le 10^8$$$)xa0— the position of Lunchbox's king. The third line in a test case contains $$$x_Q$$$ and $$$y_Q$$$ ($$$0 le x_Q, y_Q le 10^8$$$)xa0— the position of Lunchbox's queen. It is guaranteed that Lunchbox's queen and king will occupy different cells. That is, $$$(x_K,y_K) eq (x_Q,y_Q)$$$. Output For each test case, output the number of positions on an infinite chessboard such that a knight can attack both the king and the queen. Example Input 4 2 1 0 0 3 3 1 1 3 1 1 3 4 4 0 0 8 0 4 2 1 4 3 4 Note In the first test case, the knight can move 2 squares in one direction and 1 square in the other (it is essentially the same as the knight in standard chess). A knight placed on $$$(2, 1)$$$ or $$$(1, 2)$$$ would attack both the king and queen. Example of a knight placement that forks the queen and king in the first test case. The squares that the knight attacks are highlighted in red. In the second test case, a knight placed on $$$(2, 2)$$$ would attack both the king and queen. Example of a knight placement that does not fork the queen and king in the second test case. The knight attacks the king but not the queen. In the third test case, a knight placed on $$$(4, 4)$$$ or $$$(4, -4)$$$ would attack both the king and queen. In the fourth test case, there are no positions where the knight can attack both the king and the queen. (Credits to EnDeRBeaT for the nice images)
900
false
false
true
false
false
false
true
false
false
false
890
1797C
This is an interactive problem. Li Ming and Li Hua are playing a game. Li Hua has a chessboard of size $$$n imes m$$$. Denote $$$(r, c)$$$ ($$$1le rle n, 1le cle m$$$) as the cell on the $$$r$$$-th row from the top and on the $$$c$$$-th column from the left. Li Ming put a king on the chessboard and Li Hua needs to guess its position. Li Hua can ask Li Ming no more than $$$3$$$ questions. In each question, he can choose a cell and ask the minimum steps needed to move the king to the chosen cell. Each question is independent, which means the king doesn't actually move. A king can move from $$$(x,y)$$$ to $$$(x',y')$$$ if and only if $$$max{x-x',y-y'}=1$$$ (shown in the following picture). The position of the king is chosen before the interaction. Suppose you were Li Hua, please solve this problem. Interaction The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The first line of each test case contains two integers $$$n,m$$$ ($$$1le n,mle 10^9$$$)xa0— the size of the chessboard, and then the interaction begins. To ask a question, print "? $$$r$$$ $$$c$$$" (without quotes, $$$1 leq r leq n, 1 leq c leq m$$$). Then you should input the response from standard inputxa0— the minimum steps the king needs to move to the chosen cell. If your program has asked an invalid question or has run out of questions, the interactor will terminate immediately and your program will get a verdict Wrong answer. To give the final answer, print "! $$$r$$$ $$$c$$$" (without the quotes, $$$(r,c)$$$ is the king's initial coordinate). Note that giving this answer is not counted towards the limit of $$$3$$$ questions. After asking a question do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To hack, use the following format. The first line should contain a single integer $$$t$$$ ($$$1 le t le 10^3$$$). The first and only line of each test case should contain four integers $$$n,m,r,c$$$ ($$$1le rle nle 10^9,1le cle mle 10^9$$$). Note In test case 1, the king is at $$$(2,2)$$$. It takes $$$1$$$ step to move to $$$(2,3)$$$ and $$$2$$$ steps to move to $$$(2,4)$$$. Note that the questions may not seem sensible. They are just a sample of questions you may ask.
1,600
false
true
false
false
false
true
false
false
false
false
1,494
1800C1
This is an easy version of the problem. It differs from the hard one only by constraints on $$$n$$$ and $$$t$$$. There is a deck of $$$n$$$ cards, each of which is characterized by its power. There are two types of cards: a hero card, the power of such a card is always equal to $$$0$$$; a bonus card, the power of such a card is always positive. You can do the following with the deck: take a card from the top of the deck; if this card is a bonus card, you can put it on top of your bonus deck or discard; if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards. Your task is to use such actions to gather an army with the maximum possible total power. Input The first line of input data contains single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases in the test. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 5000$$$)xa0— the number of cards in the deck. The second line of each test case contains $$$n$$$ integers $$$s_1, s_2, dots, s_n$$$ ($$$0 le s_i le 10^9$$$)xa0— card powers in top-down order. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output Output $$$t$$$ numbers, each of which is the answer to the corresponding test case — the maximum possible total power of the army that can be achieved. Example Input 5 5 3 3 3 0 0 6 0 3 3 0 0 3 7 1 2 3 0 4 5 0 7 1 2 5 0 4 3 0 5 3 1 0 0 4 Note In the first sample, you can take bonuses $$$1$$$ and $$$2$$$. Both hero cards will receive $$$3$$$ power. If you take all the bonuses, one of them will remain unused. In the second sample, the hero's card on top of the deck cannot be powered up, and the rest can be powered up with $$$2$$$ and $$$3$$$ bonuses and get $$$6$$$ total power. In the fourth sample, you can take bonuses $$$1$$$, $$$2$$$, $$$3$$$, $$$5$$$ and skip the bonus $$$6$$$, then the hero $$$4$$$ will be enhanced with a bonus $$$3$$$ by $$$5$$$, and the hero $$$7$$$ with a bonus $$$5$$$ by $$$4$$$. $$$4+5=9$$$.
1,000
false
true
false
false
true
false
false
false
false
false
1,473
1670C
While searching for the pizza, baby Hosssam came across two permutations $$$a$$$ and $$$b$$$ of length $$$n$$$. Recall that 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). Baby Hosssam forgot about the pizza and started playing around with the two permutations. While he was playing with them, some elements of the first permutation got mixed up with some elements of the second permutation, and to his surprise those elements also formed a permutation of size $$$n$$$. Specifically, he mixed up the permutations to form a new array $$$c$$$ in the following way. For each $$$i$$$ ($$$1le ile n$$$), he either made $$$c_i=a_i$$$ or $$$c_i=b_i$$$. The array $$$c$$$ is a permutation. You know permutations $$$a$$$, $$$b$$$, and values at some positions in $$$c$$$. Please count the number different permutations $$$c$$$ that are consistent with the described process and the given values. Since the answer can be large, print it modulo $$$10^9+7$$$. It is guaranteed that there exists at least one permutation $$$c$$$ that satisfies all the requirements. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 10^5$$$) — the length of the permutations. The next line contains $$$n$$$ distinct integers $$$a_1,a_2,ldots,a_n$$$ ($$$1le a_ile n$$$) — the first permutation. The next line contains $$$n$$$ distinct integers $$$b_1,b_2,ldots,b_n$$$ ($$$1le b_ile n$$$) — the second permutation. The next line contains $$$n$$$ distinct integers $$$d_1,d_2,ldots,d_n$$$ ($$$d_i$$$ is either $$$0$$$, $$$a_i$$$, or $$$b_i$$$) — the description of the known values of $$$c$$$. If $$$d_i=0$$$, then there are no requirements on the value of $$$c_i$$$. Otherwise, it is required that $$$c_i=d_i$$$. It is guaranteed that there exists at least one permutation $$$c$$$ that satisfies all the requirements. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Note In the first test case, there are $$$4$$$ distinct permutation that can be made using the process: $$$[2,3,1,4,5,6,7]$$$, $$$[2,3,1,7,6,5,4]$$$, $$$[2,3,1,4,6,5,7]$$$, $$$[2,3,1,7,5,6,4]$$$. In the second test case, there is only one distinct permutation that can be made using the process: $$$[1]$$$. In the third test case, there are $$$2$$$ distinct permutation that can be made using the process: $$$[6,5,2,1,4,3]$$$, $$$[6,5,3,1,4,2]$$$. In the fourth test case, there are $$$2$$$ distinct permutation that can be made using the process: $$$[1,2,8,7,4,3,6,5]$$$, $$$[1,6,4,7,2,3,8,5]$$$. In the fifth test case, there is only one distinct permutation that can be made using the process: $$$[1,9,2,3,4,10,8,6,7,5]$$$.
1,400
true
false
true
false
true
false
false
false
false
true
2,255
893E
Problem - 893E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp math number theory *2000 No tag edit access → Contest materials such that _A__i_u2009≠u2009_B__i_. Since the answer can be very large, print it modulo 109u2009+u20097. Input The first line contains one integer _q_ (1u2009≤u2009_q_u2009≤u2009105) — the number of testcases to solve. Then _q_ lines follow, each containing two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009106). Each of these lines represents a testcase. Output Print _q_ integers. _i_-th integer has to be equal to the number of _y__i_-factorizations of _x__i_ modulo 109u2009+u20097. Example Input 2 6 3 4 2 Output 36 6 Note In the second testcase of the example there are six _y_-factorizations: {u2009-u20094,u2009u2009-u20091}; {u2009-u20092,u2009u2009-u20092}; {u2009-u20091,u2009u2009-u20094}; {1,u20094}; {2,u20092}; {4,u20091}.
2,000
true
false
false
true
false
false
false
false
false
false
6,177
1762B
An array $$$b$$$ of $$$m$$$ positive integers is good if for all pairs $$$i$$$ and $$$j$$$ ($$$1 leq i,j leq m$$$), $$$max(b_i,b_j)$$$ is divisible by $$$min(b_i,b_j)$$$. You are given an array $$$a$$$ of $$$n$$$ positive integers. You can perform the following operation: Select an index $$$i$$$ ($$$1 leq i leq n$$$) and an integer $$$x$$$ ($$$0 leq x leq a_i$$$) and add $$$x$$$ to $$$a_i$$$, in other words, $$$a_i := a_i+x$$$. After this operation, $$$a_i leq 10^{18}$$$ should be satisfied. You have to construct a sequence of at most $$$n$$$ operations that will make $$$a$$$ good. It can be proven that under the constraints of the problem, such a sequence of operations always exists. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i leq 10^9$$$) — representing the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test, output a single integer $$$p$$$ ($$$0 leq p leq n$$$) — denoting the number of operations in your solution. In each of the following $$$p$$$ lines, output two space-separated integers — $$$i$$$ and $$$x$$$. You do not need to minimize the number of operations. It can be proven that a solution always exists. Example Input 4 4 2 3 5 5 2 4 8 5 3 4 343 5 6 3 31 5 17 Output 4 1 2 1 1 2 2 3 0 0 5 1 3 1 4 2 1 5 4 3 7 3 1 29 2 5 3 3 Note In the first test case, array $$$a$$$ becomes $$$[5,5,5,5]$$$ after the operations. It is easy to see that $$$[5,5,5,5]$$$ is good. In the second test case, array $$$a$$$ is already good. In the third test case, after performing the operations, array $$$a$$$ becomes $$$[10,5,350,5,10]$$$, which is good. In the fourth test case, after performing the operations, array $$$a$$$ becomes $$$[60,10,20]$$$, which is good.
1,100
false
false
true
false
false
true
false
false
true
false
1,739
838E
You are given an strictly convex polygon with _n_ vertices. It is guaranteed that no three points are collinear. You would like to take a maximum non intersecting path on the polygon vertices that visits each point at most once. More specifically your path can be represented as some sequence of distinct polygon vertices. Your path is the straight line segments between adjacent vertices in order. These segments are not allowed to touch or intersect each other except at the vertices in your sequence. Given the polygon, print the maximum length non-intersecting path that visits each point at most once. Input The first line of input will contain a single integer _n_ (2u2009≤u2009_n_u2009≤u20092u2009500), the number of points. The next _n_ lines will contain two integers _x__i_,u2009_y__i_ (_x__i_,u2009_y__i_u2009≤u2009109), denoting the coordinates of the _i_-th vertex. It is guaranteed that these points are listed in clockwise order. Output Print a single floating point number, representing the longest non-intersecting path that visits the vertices at most once. Your answer will be accepted if it has absolute or relative error at most 10u2009-u20099. More specifically, if your answer is _a_ and the jury answer is _b_, your answer will be accepted if . Note One optimal path is to visit points 0,1,3,2 in order.
2,300
false
false
false
true
false
false
false
false
false
false
6,410
1936C
You are at a dueling arena. You also possess $$$n$$$ Pokémons. Initially, only the $$$1$$$-st Pokémon is standing in the arena. Each Pokémon has $$$m$$$ attributes. The $$$j$$$-th attribute of the $$$i$$$-th Pokémon is $$$a_{i,j}$$$. Each Pokémon also has a cost to be hired: the $$$i$$$-th Pokémon's cost is $$$c_i$$$. You want to have the $$$n$$$-th Pokémon stand in the arena. To do that, you can perform the following two types of operations any number of times in any order: Choose three integers $$$i$$$, $$$j$$$, $$$k$$$ ($$$1 le i le n$$$, $$$1 le j le m$$$, $$$k > 0$$$), increase $$$a_{i,j}$$$ by $$$k$$$ permanently. The cost of this operation is $$$k$$$. Choose two integers $$$i$$$, $$$j$$$ ($$$1 le i le n$$$, $$$1 le j le m$$$) and hire the $$$i$$$-th Pokémon to duel with the current Pokémon in the arena based on the $$$j$$$-th attribute. The $$$i$$$-th Pokémon will win if $$$a_{i,j}$$$ is greater than or equal to the $$$j$$$-th attribute of the current Pokémon in the arena (otherwise, it will lose). After the duel, only the winner will stand in the arena. The cost of this operation is $$$c_i$$$. Find the minimum cost you need to pay to have the $$$n$$$-th Pokémon stand in the arena. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 4 cdot 10^5$$$, $$$1 le m le 2 cdot 10^5$$$, $$$2 leq n cdot m leq 4 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 le c_i le 10^9$$$). The $$$i$$$-th of the following $$$n$$$ lines contains $$$m$$$ integers $$$a_{i,1}, a_{i,2}, ldots, a_{i,m}$$$ ($$$1 le a_{i,j} le 10^9$$$). It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Note In the first test case, the attribute array of the $$$1$$$-st Pokémon (which is standing in the arena initially) is $$$[2,9,9]$$$. In the first operation, you can choose $$$i=3$$$, $$$j=1$$$, $$$k=1$$$, and increase $$$a_{3,1}$$$ by $$$1$$$ permanently. Now the attribute array of the $$$3$$$-rd Pokémon is $$$[2,2,1]$$$. The cost of this operation is $$$k = 1$$$. In the second operation, you can choose $$$i=3$$$, $$$j=1$$$, and hire the $$$3$$$-rd Pokémon to duel with the current Pokémon in the arena based on the $$$1$$$-st attribute. Since $$$a_{i,j}=a_{3,1}=2 ge 2=a_{1,1}$$$, the $$$3$$$-rd Pokémon will win. The cost of this operation is $$$c_3 = 1$$$. Thus, we have made the $$$3$$$-rd Pokémon stand in the arena within the cost of $$$2$$$. It can be proven that $$$2$$$ is minimum possible. In the second test case, the attribute array of the $$$1$$$-st Pokémon in the arena is $$$[9,9,9]$$$. In the first operation, you can choose $$$i=2$$$, $$$j=3$$$, $$$k=2$$$, and increase $$$a_{2,3}$$$ by $$$2$$$ permanently. Now the attribute array of the $$$2$$$-nd Pokémon is $$$[6,1,9]$$$. The cost of this operation is $$$k = 2$$$. In the second operation, you can choose $$$i=2$$$, $$$j=3$$$, and hire the $$$2$$$-nd Pokémon to duel with the current Pokémon in the arena based on the $$$3$$$-rd attribute. Since $$$a_{i,j}=a_{2,3}=9 ge 9=a_{1,3}$$$, the $$$2$$$-nd Pokémon will win. The cost of this operation is $$$c_2 = 3$$$. In the third operation, you can choose $$$i=3$$$, $$$j=2$$$, and hire the $$$3$$$-rd Pokémon to duel with the current Pokémon in the arena based on the $$$2$$$-nd attribute. Since $$$a_{i,j}=a_{1,2}=2 ge 1=a_{2,2}$$$, the $$$3$$$-rd Pokémon can win. The cost of this operation is $$$c_3 = 1$$$. Thus, we have made the $$$3$$$-rd Pokémon stand in the arena within the cost of $$$6$$$. It can be proven that $$$6$$$ is minimum possible.
2,400
false
true
true
false
true
false
false
false
true
true
666
1979B
You are given two distinct non-negative integers $$$x$$$ and $$$y$$$. Consider two infinite sequences $$$a_1, a_2, a_3, ldots$$$ and $$$b_1, b_2, b_3, ldots$$$, where $$$a_n = n oplus x$$$; $$$b_n = n oplus y$$$. Here, $$$x oplus y$$$ denotes the [bitwise XOR]( operation of integers $$$x$$$ and $$$y$$$. For example, with $$$x = 6$$$, the first $$$8$$$ elements of sequence $$$a$$$ will look as follows: $$$[7, 4, 5, 2, 3, 0, 1, 14, ldots]$$$. Note that the indices of elements start with $$$1$$$. Your task is to find the length of the longest common subsegment$$$^dagger$$$ of sequences $$$a$$$ and $$$b$$$. In other words, find the maximum integer $$$m$$$ such that $$$a_i = b_j, a_{i + 1} = b_{j + 1}, ldots, a_{i + m - 1} = b_{j + m - 1}$$$ for some $$$i, j ge 1$$$. $$$^dagger$$$A subsegment of sequence $$$p$$$ is a sequence $$$p_l,p_{l+1},ldots,p_r$$$, where $$$1 le l le r$$$. Note In the first test case, the first $$$7$$$ elements of sequences $$$a$$$ and $$$b$$$ are as follows: $$$a = [1, 2, 3, 4, 5, 6, 7,ldots]$$$ $$$b = [0, 3, 2, 5, 4, 7, 6,ldots]$$$ It can be shown that there isn't a positive integer $$$k$$$ such that the sequence $$$[k, k + 1]$$$ occurs in $$$b$$$ as a subsegment. So the answer is $$$1$$$. In the third test case, the first $$$20$$$ elements of sequences $$$a$$$ and $$$b$$$ are as follows: $$$a = [56, 59, 58, 61, 60, 63, 62, 49, 48, 51, 50, 53, 52, 55, 54, extbf{41, 40, 43, 42}, 45, ldots]$$$ $$$b = [36, 39, 38, 33, 32, 35, 34, 45, 44, 47, 46, extbf{41, 40, 43, 42}, 53, 52, 55, 54, 49, ldots]$$$ It can be shown that one of the longest common subsegments is the subsegment $$$[41, 40, 43, 42]$$$ with a length of $$$4$$$.
1,000
false
true
false
false
false
false
false
false
false
false
415
717A
Problem - 717A - Codeforces =============== xa0 . Input The first line of the input contains three integers _k_, _l_ and _r_ (1u2009≤u2009_k_u2009≤u2009200, 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u20091018). Output Output a single number: the number of ways to select _k_ different tours of the same length, modulo 1u2009000u2009000u2009007. Example Input 1 1 2 Output 5
2,900
true
false
false
false
false
false
false
false
false
false
6,952
1891E
The boy Smilo is learning algorithms with a teacher named Brukhovich. Over the course of the year, Brukhovich will administer $$$n$$$ exams. For each exam, its difficulty $$$a_i$$$ is known, which is a non-negative integer. Smilo doesn't like when the greatest common divisor of the difficulties of two consecutive exams is equal to $$$1$$$. Therefore, he considers the sadness of the academic year to be the number of such pairs of exams. More formally, the sadness is the number of indices $$$i$$$ ($$$1 leq i leq n - 1$$$) such that $$$gcd(a_i, a_{i+1}) = 1$$$, where $$$gcd(x, y)$$$ is the greatest common divisor of integers $$$x$$$ and $$$y$$$. Brukhovich wants to minimize the sadness of the year of Smilo. To do this, he can set the difficulty of any exam to $$$0$$$. However, Brukhovich doesn't want to make his students' lives too easy. Therefore, he will perform this action no more than $$$k$$$ times. Help Smilo determine the minimum sadness that Brukhovich can achieve if he performs no more than $$$k$$$ operations. As a reminder, the greatest common divisor (GCD) of two non-negative integers $$$x$$$ and $$$y$$$ is the maximum integer that is a divisor of both $$$x$$$ and $$$y$$$ and is denoted as $$$gcd(x, y)$$$. In particular, $$$gcd(x, 0) = gcd(0, x) = x$$$ for any non-negative integer $$$x$$$. 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 two integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 10^5$$$) — the total number of exams and the maximum number of exams that can be simplified, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, a_3, ldots, a_n$$$ — the elements of array $$$a$$$, which are the difficulties of the exams ($$$0 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$. Note In the first test case, a sadness of $$$1$$$ can be achieved. To this, you can simplify the second and fourth exams. After this, there will be only one pair of adjacent exams with a greatest common divisor (GCD) equal to one, which is the first and second exams. In the second test case, a sadness of $$$0$$$ can be achieved by simplifying the second and fourth exams.
2,500
true
true
true
false
false
false
true
false
true
false
953
1375B
# Neighbor Grid Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes You are given a grid with n rows and m columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number k > 0 written on it, then exactly k of its neighboring cells have a number greater than 0 written on them. Note that if the number in the cell is 0, there is no such restriction on neighboring cells. You are allowed to take any number in the grid and increase it by 1. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them. Two cells are considered to be neighboring if they have a common edge. # Input The input consists of multiple test cases. The first line contains an integer t (1 ≤ t ≤ 5000 ) x16 the number of test cases. The description of the test cases follows. The first line of each test case contains two integers n and m (2 ≤ n, m ≤ 300 ) x16 the number of rows and columns, respectively. The following n lines contain m integers each, the j-th element in the i-th line ai,j is the number written in the j-th cell of the i-th row ( 0 ≤ ai,j ≤ 10 9). It is guaranteed that the sum of n · m over all test cases does not exceed 10 5. # Output If it is impossible to obtain a good grid, print a single line containing “ NO ”. Otherwise, print a single line containing “ YES ”, followed by n lines each containing m integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero). If there are multiple possible answers, you may print any of them. Page 1 of 2 Example standard input standard output 53 4 0 0 0 0 0 1 0 0 0 0 0 0 2 2 3 0 0 0 2 2 0 0 0 0 2 3 0 0 0 0 4 0 4 4 0 0 0 0 0 2 0 1 0 0 0 0 0 0 0 0 YES 0 0 0 0 0 1 1 0 0 0 0 0 NO YES 0 0 0 0 NO YES 0 1 0 0 1 4 2 1 0 2 0 0 1 3 1 0 # Note In the first test case, we can obtain the resulting grid by increasing the number in row 2, column 3 once. Both of the cells that contain 1 have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid 0 1 0 0 0 2 1 0 0 0 0 0 All of them are accepted as valid answers. In the second test case, it is impossible to make the grid good. In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good. Page 2 of 2
1,200
false
true
false
false
false
true
false
false
false
false
3,786
1213E
You are given two strings $$$s$$$ and $$$t$$$ both of length $$$2$$$ and both consisting only of characters 'a', 'b' and 'c'. Possible examples of strings $$$s$$$ and $$$t$$$: "ab", "ca", "bb". You have to find a string $$$res$$$ consisting of $$$3n$$$ characters, $$$n$$$ characters should be 'a', $$$n$$$ characters should be 'b' and $$$n$$$ characters should be 'c' and $$$s$$$ and $$$t$$$ should not occur in $$$res$$$ as substrings. A substring of a string is a contiguous subsequence of that string. So, the strings "ab", "ac" and "cc" are substrings of the string "abacc", but the strings "bc", "aa" and "cb" are not substrings of the string "abacc". If there are multiple answers, you can print any of them. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of characters 'a', 'b' and 'c' in the resulting string. The second line of the input contains one string $$$s$$$ of length $$$2$$$ consisting of characters 'a', 'b' and 'c'. The third line of the input contains one string $$$t$$$ of length $$$2$$$ consisting of characters 'a', 'b' and 'c'. Output If it is impossible to find the suitable string, print "NO" on the first line. Otherwise print "YES" on the first line and string $$$res$$$ on the second line. $$$res$$$ should consist of $$$3n$$$ characters, $$$n$$$ characters should be 'a', $$$n$$$ characters should be 'b' and $$$n$$$ characters should be 'c' and $$$s$$$ and $$$t$$$ should not occur in $$$res$$$ as substrings. If there are multiple answers, you can print any of them.
1,900
false
false
false
false
false
true
true
false
false
false
4,614
1839C
You have a sequence $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$, each element of which is either $$$0$$$ or $$$1$$$, and a sequence $$$b$$$, which is initially empty. You are going to perform $$$n$$$ operations. On each of them you will increase the length of $$$b$$$ by $$$1$$$. On the $$$i$$$-th operation you choose an integer $$$p$$$ between $$$0$$$ and $$$i-1$$$. You insert $$$0$$$ in the sequence $$$b$$$ on position $$$p+1$$$ (after the first $$$p$$$ elements), and then you invert the first $$$p$$$ elements of $$$b$$$. More formally: let's denote the sequence $$$b$$$ before the $$$i$$$-th ($$$1 le i le n$$$) operation as $$$b_1, b_2, ldots, b_{i-1}$$$. On the $$$i$$$-th operation you choose an integer $$$p$$$ between $$$0$$$ and $$$i-1$$$ and replace $$$b$$$ with $$$overline{b_1}, overline{b_2}, ldots, overline{b_{p}}, 0, b_{p+1}, b_{p+2}, ldots, b_{i-1}$$$. Here, $$$overline{x}$$$ denotes the binary inversion. Hence, $$$overline{0} = 1$$$ and $$$overline{1} = 0$$$. You can find examples of operations in the Notes section. Determine if there exists a sequence of operations that makes $$$b$$$ equal to $$$a$$$. If such sequence of operations exists, find it. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the sequence $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 1$$$)xa0— the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note In the first test case, 1. Before the first operation, $$$b = [,]$$$. You choose $$$p = 0$$$ and replace $$$b$$$ with $$$[, underline{0} ,]$$$ 2. On the second operation you choose $$$p = 0$$$ and replace $$$b$$$ with $$$[, underline{0}, 0 ,]$$$. 3. On the third operation you choose $$$p = 2$$$ and replace $$$b$$$ with $$$[, 1, 1, underline{0} ,]$$$. 4. On the fourth operation you choose $$$p = 1$$$ and replace $$$b$$$ with $$$[, 0, underline{0}, 1, 0 ,]$$$. 5. On the fifth operation you choose $$$p = 3$$$ and replace $$$b$$$ with $$$[, 1, 1, 0, underline{0}, 0 ,]$$$. Hence, sequence $$$b$$$ changes in the following way: $$$[,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0} ,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0}, 0 ,]$$$ $$$xrightarrow{p , = , 2}$$$ $$$[, 1, 1, underline{0} ,]$$$ $$$xrightarrow{p , = , 1}$$$ $$$[, 0, underline{0}, 1, 0 ,]$$$ $$$xrightarrow{p , = , 3}$$$ $$$[, 1, 1, 0, underline{0}, 0 ,]$$$. In the end the sequence $$$b$$$ is equal to the sequence $$$a$$$, so this way to perform operations is one of the correct answers. In the second test case, $$$n = 1$$$ and the only achiveable sequence $$$b$$$ is $$$[, 0 , ]$$$. In the third test case, there are six possible sequences of operations: 1. $$$[,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0} ,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0}, 0 ,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0}, 0, 0 ,]$$$. 2. $$$[,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0} ,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0}, 0 ,]$$$ $$$xrightarrow{p , = , 1}$$$ $$$[, 1, underline{0}, 0 ,]$$$. 3. $$$[,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0} ,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0}, 0 ,]$$$ $$$xrightarrow{p , = , 2}$$$ $$$[, 1, 1, underline{0} ,]$$$. 4. $$$[,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0} ,]$$$ $$$xrightarrow{p , = , 1}$$$ $$$[, 1, underline{0} ,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0}, 1, 0 ,]$$$. 5. $$$[,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0} ,]$$$ $$$xrightarrow{p , = , 1}$$$ $$$[, 1, underline{0} ,]$$$ $$$xrightarrow{p , = , 1}$$$ $$$[, 0, underline{0}, 0 ,]$$$. 6. $$$[,]$$$ $$$xrightarrow{p , = , 0}$$$ $$$[, underline{0} ,]$$$ $$$xrightarrow{p , = , 1}$$$ $$$[, 1, underline{0} ,]$$$ $$$xrightarrow{p , = , 2}$$$ $$$[, 0, 1, underline{0} ,]$$$. None of them makes $$$b$$$ equal to $$$[, 0, 1, 1 ,]$$$, so the answer is "NO".
1,300
false
false
false
false
false
true
false
false
false
false
1,256
946G
Problem - 946G - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures dp *2500 No tag edit access → Contest materials . You are given an array _a_ consisting of _n_ elements. You are allowed to replace any element with any integer number (and you may do so any number of times you need). What is the minimum number of replacements you have to perform in order to make the array almost increasing? Input The first line contains one integer _n_ (2u2009≤u2009_n_u2009≤u2009200000) — the number of elements in _a_. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the array _a_. Output Print the minimum number of replaces you have to perform so that _a_ is almost increasing. Examples Input 5 5 4 3 2 1 Output 3 Input 5 1 2 8 9 5 Output 0
2,500
false
false
false
true
true
false
false
false
false
false
5,926
1220E
Alex decided to go on a touristic trip over the country. For simplicity let's assume that the country has $$$n$$$ cities and $$$m$$$ bidirectional roads connecting them. Alex lives in city $$$s$$$ and initially located in it. To compare different cities Alex assigned each city a score $$$w_i$$$ which is as high as interesting city seems to Alex. Alex believes that his trip will be interesting only if he will not use any road twice in a row. That is if Alex came to city $$$v$$$ from city $$$u$$$, he may choose as the next city in the trip any city connected with $$$v$$$ by the road, except for the city $$$u$$$. Your task is to help Alex plan his city in a way that maximizes total score over all cities he visited. Note that for each city its score is counted at most once, even if Alex been there several times during his trip. Input First line of input contains two integers $$$n$$$ and $$$m$$$, ($$$1 le n le 2 cdot 10^5$$$, $$$0 le m le 2 cdot 10^5$$$) which are numbers of cities and roads in the country. Second line contains $$$n$$$ integers $$$w_1, w_2, ldots, w_n$$$ ($$$0 le w_i le 10^9$$$) which are scores of all cities. The following $$$m$$$ lines contain description of the roads. Each of these $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$) which are cities connected by this road. It is guaranteed that there is at most one direct road between any two cities, no city is connected to itself by the road and, finally, it is possible to go from any city to any other one using only roads. The last line contains single integer $$$s$$$ ($$$1 le s le n$$$), which is the number of the initial city. Output Output single integer which is the maximum possible sum of scores of visited cities. Examples Input 5 7 2 2 8 6 9 1 2 1 3 2 4 3 2 4 5 2 5 1 5 2 Input 10 12 1 7 1 9 3 3 6 30 1 10 1 2 1 3 3 5 5 7 2 3 5 4 6 9 4 6 3 7 6 8 9 4 9 10 6
2,200
false
true
false
true
false
false
false
false
false
true
4,569
1006E
In this problem you will have to help Berland army with organizing their command delivery system. There are $$$n$$$ officers in Berland army. The first officer is the commander of the army, and he does not have any superiors. Every other officer has exactly one direct superior. If officer $$$a$$$ is the direct superior of officer $$$b$$$, then we also can say that officer $$$b$$$ is a direct subordinate of officer $$$a$$$. Officer $$$x$$$ is considered to be a subordinate (direct or indirect) of officer $$$y$$$ if one of the following conditions holds: officer $$$y$$$ is the direct superior of officer $$$x$$$; the direct superior of officer $$$x$$$ is a subordinate of officer $$$y$$$. For example, on the picture below the subordinates of the officer $$$3$$$ are: $$$5, 6, 7, 8, 9$$$. The structure of Berland army is organized in such a way that every officer, except for the commander, is a subordinate of the commander of the army. Formally, let's represent Berland army as a tree consisting of $$$n$$$ vertices, in which vertex $$$u$$$ corresponds to officer $$$u$$$. The parent of vertex $$$u$$$ corresponds to the direct superior of officer $$$u$$$. The root (which has index $$$1$$$) corresponds to the commander of the army. Berland War Ministry has ordered you to give answers on $$$q$$$ queries, the $$$i$$$-th query is given as $$$(u_i, k_i)$$$, where $$$u_i$$$ is some officer, and $$$k_i$$$ is a positive integer. To process the $$$i$$$-th query imagine how a command from $$$u_i$$$ spreads to the subordinates of $$$u_i$$$. Typical DFS (depth first search) algorithm is used here. Suppose the current officer is $$$a$$$ and he spreads a command. Officer $$$a$$$ chooses $$$b$$$ — one of his direct subordinates (i.e. a child in the tree) who has not received this command yet. If there are many such direct subordinates, then $$$a$$$ chooses the one having minimal index. Officer $$$a$$$ gives a command to officer $$$b$$$. Afterwards, $$$b$$$ uses exactly the same algorithm to spread the command to its subtree. After $$$b$$$ finishes spreading the command, officer $$$a$$$ chooses the next direct subordinate again (using the same strategy). When officer $$$a$$$ cannot choose any direct subordinate who still hasn't received this command, officer $$$a$$$ finishes spreading the command. Let's look at the following example: If officer $$$1$$$ spreads a command, officers receive it in the following order: $$$[1, 2, 3, 5 ,6, 8, 7, 9, 4]$$$. If officer $$$3$$$ spreads a command, officers receive it in the following order: $$$[3, 5, 6, 8, 7, 9]$$$. If officer $$$7$$$ spreads a command, officers receive it in the following order: $$$[7, 9]$$$. If officer $$$9$$$ spreads a command, officers receive it in the following order: $$$[9]$$$. To answer the $$$i$$$-th query $$$(u_i, k_i)$$$, construct a sequence which describes the order in which officers will receive the command if the $$$u_i$$$-th officer spreads it. Return the $$$k_i$$$-th element of the constructed list or -1 if there are fewer than $$$k_i$$$ elements in it. You should process queries independently. A query doesn't affect the following queries. Input The first line of the input contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 2 cdot 10^5, 1 le q le 2 cdot 10^5$$$) — the number of officers in Berland army and the number of queries. The second line of the input contains $$$n - 1$$$ integers $$$p_2, p_3, dots, p_n$$$ ($$$1 le p_i < i$$$), where $$$p_i$$$ is the index of the direct superior of the officer having the index $$$i$$$. The commander has index $$$1$$$ and doesn't have any superiors. The next $$$q$$$ lines describe the queries. The $$$i$$$-th query is given as a pair ($$$u_i, k_i$$$) ($$$1 le u_i, k_i le n$$$), where $$$u_i$$$ is the index of the officer which starts spreading a command, and $$$k_i$$$ is the index of the required officer in the command spreading sequence.
1,600
false
false
false
false
false
false
false
false
false
true
5,651
611B
Problem - 611B - Codeforces =============== xa0 ]( "Announcement") xa0— the first year and the last year in Limak's interval respectively. Output Print one integerxa0– the number of years Limak will count in his chosen interval. Examples Input 5 10 Output 2 Input 2015 2015 Output 1 Input 100 105 Output 0 Input 72057594000000000 72057595000000000 Output 26 Note In the first sample Limak's interval contains numbers 510u2009=u20091012, 610u2009=u20091102, 710u2009=u20091112, 810u2009=u200910002, 910u2009=u200910012 and 1010u2009=u200910102. Two of them (1012 and 1102) have the described property.
1,300
false
false
true
false
false
false
true
false
false
false
7,400
1672H
You have a binary string $$$a$$$ of length $$$n$$$ consisting only of digits $$$0$$$ and $$$1$$$. You are given $$$q$$$ queries. In the $$$i$$$-th query, you are given two indices $$$l$$$ and $$$r$$$ such that $$$1 le l le r le n$$$. Let $$$s=a[l,r]$$$. You are allowed to do the following operation on $$$s$$$: 1. Choose two indices $$$x$$$ and $$$y$$$ such that $$$1 le x le y le s$$$. Let $$$t$$$ be the substring $$$t = s[x, y]$$$. Then for all $$$1 le i le t - 1$$$, the condition $$$t_i eq t_{i+1}$$$ has to hold. Note that $$$x = y$$$ is always a valid substring. 2. Delete the substring $$$s[x, y]$$$ from $$$s$$$. For each of the $$$q$$$ queries, find the minimum number of operations needed to make $$$s$$$ an empty string. Note that for a string $$$s$$$, $$$s[l,r]$$$ denotes the subsegment $$$s_l,s_{l+1},ldots,s_r$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10 ^ 5$$$) xa0— the length of the binary string $$$a$$$ and the number of queries respectively. The second line contains a binary string $$$a$$$ of length $$$n$$$ ($$$a_i in {0, 1}$$$). Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$) xa0— representing the substring of each query. Output Print $$$q$$$ lines, the $$$i$$$-th line representing the minimum number of operations needed for the $$$i$$$-th query. Examples Input 5 3 11011 2 4 1 5 3 5 Input 10 3 1001110110 1 10 2 5 5 10 Note In the first test case, 1. The substring is $$$ exttt{101}$$$, so we can do one operation to make the substring empty. 2. The substring is $$$ exttt{11011}$$$, so we can do one operation on $$$s[2, 4]$$$ to make $$$ exttt{11}$$$, then use two more operations to make the substring empty. 3. The substring is $$$ exttt{011}$$$, so we can do one operation on $$$s[1, 2]$$$ to make $$$ exttt{1}$$$, then use one more operation to make the substring empty.
2,700
false
true
false
false
true
true
false
false
false
false
2,237
1040A
A group of $$$n$$$ dancers rehearses a performance for the closing ceremony. The dancers are arranged in a row, they've studied their dancing moves and can't change positions. For some of them, a white dancing suit is already bought, for some of them — a black one, and for the rest the suit will be bought in the future. On the day when the suits were to be bought, the director was told that the participants of the olympiad will be happy if the colors of the suits on the scene will form a palindrome. A palindrome is a sequence that is the same when read from left to right and when read from right to left. The director liked the idea, and she wants to buy suits so that the color of the leftmost dancer's suit is the same as the color of the rightmost dancer's suit, the 2nd left is the same as 2nd right, and so on. The director knows how many burls it costs to buy a white suit, and how many burls to buy a black suit. You need to find out whether it is possible to buy suits to form a palindrome, and if it's possible, what's the minimal cost of doing so. Remember that dancers can not change positions, and due to bureaucratic reasons it is not allowed to buy new suits for the dancers who already have suits, even if it reduces the overall spending. Input The first line contains three integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 leq n leq 20$$$, $$$1 leq a, b leq 100$$$)xa0— the number of dancers, the cost of a white suit, and the cost of a black suit. The next line contains $$$n$$$ numbers $$$c_i$$$, $$$i$$$-th of which denotes the color of the suit of the $$$i$$$-th dancer. Number $$$0$$$ denotes the white color, $$$1$$$xa0— the black color, and $$$2$$$ denotes that a suit for this dancer is still to be bought. Output If it is not possible to form a palindrome without swapping dancers and buying new suits for those who have one, then output -1. Otherwise, output the minimal price to get the desired visual effect. Note In the first sample, the cheapest way to obtain palindromic colors is to buy a black suit for the third from left dancer and a white suit for the rightmost dancer. In the second sample, the leftmost dancer's suit already differs from the rightmost dancer's suit so there is no way to obtain the desired coloring. In the third sample, all suits are already bought and their colors form a palindrome.
1,000
false
true
false
false
false
false
false
false
false
false
5,497
1970E1
Harry Potter is hiking in the Alps surrounding Lake Geneva. In this area there are $$$m$$$ cabins, numbered 1 to $$$m$$$. Each cabin is connected, with one or more trails, to a central meeting point next to the lake. Each trail is either short or long. Cabin $$$i$$$ is connected with $$$s_i$$$ short trails and $$$l_i$$$ long trails to the lake. Each day, Harry walks a trail from the cabin where he currently is to Lake Geneva, and then from there he walks a trail to any of the $$$m$$$ cabins (including the one he started in). However, as he has to finish the hike in a day, at least one of the two trails has to be short. How many possible combinations of trails can Harry take if he starts in cabin 1 and walks for $$$n$$$ days? Give the answer modulo $$$10^9 + 7$$$. Input The first line contains the integers $$$m$$$ and $$$n$$$. The second line contains $$$m$$$ integers, $$$s_1, dots, s_m$$$, where $$$s_i$$$ is the number of short trails between cabin $$$i$$$ and Lake Geneva. The third and last line contains $$$m$$$ integers, $$$l_1, dots, l_m$$$, where $$$l_i$$$ is the number of long trails between cabin $$$i$$$ and Lake Geneva. We have the following constraints: $$$0 le s_i, l_i le 10^3$$$. $$$1 le m le 10^2$$$. $$$1 le n le 10^3$$$. Output The number of possible combinations of trails, modulo $$$10^9 + 7$$$.
1,800
false
false
false
true
false
false
false
false
false
false
474
383C
Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of _n_ nodes numbered from 1 to _n_, each node _i_ having an initial value _a__i_. The root of the tree is node 1. This tree has a special property: when a value _val_ is added to a value of node _i_, the value -_val_ is added to values of all the children of node _i_. Note that when you add value -_val_ to a child of node _i_, you also add -(-_val_) to all children of the child of node _i_ and so on. Look an example explanation to understand better how it works. This tree supports two types of queries: "1 _x_ _val_" — _val_ is added to the value of node _x_; "2 _x_" — print the current value of node _x_. In order to help Iahub understand the tree better, you must answer _m_ queries of the preceding type. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009200000). The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u20091000). Each of the next _n_–1 lines contains two integers _v__i_ and _u__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_), meaning that there is an edge between nodes _v__i_ and _u__i_. Each of the next _m_ lines contains a query in the format described above. It is guaranteed that the following constraints hold for all queries: 1u2009≤u2009_x_u2009≤u2009_n_,u20091u2009≤u2009_val_u2009≤u20091000. Output For each query of type two (print the value of node _x_) you must print the answer to the query on a separate line. The queries must be answered in the order given in the input. Examples Input 5 5 1 2 1 1 2 1 2 1 3 2 4 2 5 1 2 3 1 1 2 2 1 2 2 2 4 Note The values of the nodes are [1,u20092,u20091,u20091,u20092] at the beginning. Then value 3 is added to node 2. It propagates and value -3 is added to it's sons, node 4 and node 5. Then it cannot propagate any more. So the values of the nodes are [1,u20095,u20091,u2009u2009-u20092,u2009u2009-u20091]. Then value 2 is added to node 1. It propagates and value -2 is added to it's sons, node 2 and node 3. From node 2 it propagates again, adding value 2 to it's sons, node 4 and node 5. Node 3 has no sons, so it cannot propagate from there. The values of the nodes are [3,u20093,u2009u2009-u20091,u20090,u20091]. You can see all the definitions about the tree at the following link:
2,000
false
false
false
false
true
false
false
false
false
false
8,296
1016F
There are $$$n$$$ cities in the country of Berland. Some of them are connected by bidirectional roads in such a way that there exists exactly one path, which visits each road no more than once, between every pair of cities. Each road has its own length. Cities are numbered from $$$1$$$ to $$$n$$$. The travelling time between some cities $$$v$$$ and $$$u$$$ is the total length of the roads on the shortest path from $$$v$$$ to $$$u$$$. The two most important cities in Berland are cities $$$1$$$ and $$$n$$$. The Berland Ministry of Transport decided to build a single new road to decrease the traffic between the most important cities. However, lots of people are used to the current travelling time between the most important cities, so the new road shouldn't change it too much. The new road can only be built between such cities $$$v$$$ and $$$u$$$ that $$$v eq u$$$ and $$$v$$$ and $$$u$$$ aren't already connected by some road. They came up with $$$m$$$ possible projects. Each project is just the length $$$x$$$ of the new road. Polycarp works as a head analyst at the Berland Ministry of Transport and it's his job to deal with all those $$$m$$$ projects. For the $$$i$$$-th project he is required to choose some cities $$$v$$$ and $$$u$$$ to build the new road of length $$$x_i$$$ between such that the travelling time between the most important cities is maximal possible. Unfortunately, Polycarp is not a programmer and no analyst in the world is capable to process all projects using only pen and paper. Thus, he asks you to help him to calculate the maximal possible travelling time between the most important cities for each project. Note that the choice of $$$v$$$ and $$$u$$$ can differ for different projects. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n le 3 cdot 10^5$$$, $$$1 le m le 3 cdot 10^5$$$) — the number of cities and the number of projects, respectively. Each of the next $$$n - 1$$$ lines contains three integers $$$v_i$$$, $$$u_i$$$ and $$$w_i$$$ ($$$1 le v_i, u_i le n$$$, $$$1 le w_i le 10^9$$$) — the description of the $$$i$$$-th road. It is guaranteed that there exists exactly one path, which visits each road no more than once, between every pair of cities. Each of the next $$$m$$$ lines contains a single integer $$$x_j$$$ ($$$1 le x_j le 10^9$$$) — the length of the road for the $$$j$$$-th project. Note The road network from the first example: You can build the road with length $$$1$$$ between cities $$$5$$$ and $$$6$$$ to get $$$83$$$ as the travelling time between $$$1$$$ and $$$7$$$ ($$$1 ightarrow 2 ightarrow 6 ightarrow 5 ightarrow 3 ightarrow 4 ightarrow 7$$$ $$$=$$$ $$$18 + 4 + 1 + 12 + 24 + 24 = 83$$$). Other possible pairs of cities will give answers less or equal to $$$83$$$.
2,600
false
false
false
true
false
false
false
false
false
false
5,607
1704A
AquaMoon has two binary sequences $$$a$$$ and $$$b$$$, which contain only $$$0$$$ and $$$1$$$. AquaMoon can perform the following two operations any number of times ($$$a_1$$$ is the first element of $$$a$$$, $$$a_2$$$ is the second element of $$$a$$$, and so on): Operation 1: if $$$a$$$ contains at least two elements, change $$$a_2$$$ to $$$operatorname{min}(a_1,a_2)$$$, and remove the first element of $$$a$$$. Operation 2: if $$$a$$$ contains at least two elements, change $$$a_2$$$ to $$$operatorname{max}(a_1,a_2)$$$, and remove the first element of $$$a$$$. Note that after a removal of the first element of $$$a$$$, the former $$$a_2$$$ becomes the first element of $$$a$$$, the former $$$a_3$$$ becomes the second element of $$$a$$$ and so on, and the length of $$$a$$$ reduces by one. Determine if AquaMoon can make $$$a$$$ equal to $$$b$$$ by using these operations. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2,000$$$) — the number of test cases. Description of test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n,m leq 50$$$, $$$m leq n$$$) — the lengths of $$$a$$$ and $$$b$$$ respectively. The second line of each test case contains a string $$$a$$$ of length $$$n$$$, consisting only $$$0$$$ and $$$1$$$. The third line of each test case contains a string $$$b$$$ of length $$$m$$$, consisting only $$$0$$$ and $$$1$$$. Output For each test case, output "YES" if AquaMoon can change $$$a$$$ to $$$b$$$ by using these options; otherwise, output "NO". You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer). Example Input 10 6 2 001001 11 6 2 110111 01 6 2 000001 11 6 2 111111 01 8 5 10000101 11010 7 4 1010001 1001 8 6 01010010 010010 8 4 01010101 1001 8 4 10101010 0110 7 5 1011100 11100 Output YES YES NO NO NO YES YES NO NO YES Note In the first test case, you can use Operation 2 four times to make $$$a$$$ equals to $$$b$$$. In the second test case, you can use Operation 1 four times to make $$$a$$$ equals to $$$b$$$. In the third test case, it can be proved that no matter how we use the operations, it is impossible to make $$$a$$$ equal to $$$b$$$. In the fourth test case, it can be proved that no matter how we use the operations, it is impossible to make $$$a$$$ equal to $$$b$$$. In the fifth test case, you can use Operation 2 three times to make $$$a$$$ become $$$10101$$$, so the first element of $$$a$$$ equals to the first element of $$$b$$$, but it can be proved that no matter how to operate, the second to the fifth elements of $$$a$$$ can't be the same as $$$b$$$.
800
false
true
false
false
false
true
false
false
false
false
2,055
965E
Arkady's code contains $$$n$$$ variables. Each variable has a unique name consisting of lowercase English letters only. One day Arkady decided to shorten his code. He wants to replace each variable name with its non-empty prefix so that these new names are still unique (however, a new name of some variable can coincide with some old name of another or same variable). Among such possibilities he wants to find the way with the smallest possible total length of the new names. A string $$$a$$$ is a prefix of a string $$$b$$$ if you can delete some (possibly none) characters from the end of $$$b$$$ and obtain $$$a$$$. Please find this minimum possible total length of new names. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of variables. The next $$$n$$$ lines contain variable names, one per line. Each name is non-empty and contains only lowercase English letters. The total length of these strings is not greater than $$$10^5$$$. The variable names are distinct. Output Print a single integerxa0— the minimum possible total length of new variable names. Examples Input 3 codeforces codehorses code Input 3 telegram digital resistance Note In the first example one of the best options is to shorten the names in the given order as "cod", "co", "c". In the second example we can shorten the last name to "aac" and the first name to "a" without changing the other names.
2,200
false
true
false
true
true
false
false
false
false
false
5,838
1923C
An array $$$a$$$ of length $$$m$$$ is considered good if there exists an integer array $$$b$$$ of length $$$m$$$ such that the following conditions hold: 1. $$$sumlimits_{i=1}^{m} a_i = sumlimits_{i=1}^{m} b_i$$$; 2. $$$a_i eq b_i$$$ for every index $$$i$$$ from $$$1$$$ to $$$m$$$; 3. $$$b_i > 0$$$ for every index $$$i$$$ from $$$1$$$ to $$$m$$$. You are given an array $$$c$$$ of length $$$n$$$. Each element of this array is greater than $$$0$$$. You have to answer $$$q$$$ queries. During the $$$i$$$-th query, you have to determine whether the subarray $$$c_{l_{i}}, c_{l_{i}+1}, dots, c_{r_{i}}$$$ is good. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 3 cdot 10^5$$$) — the length of the array $$$c$$$ and the number of queries. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^9$$$). Then $$$q$$$ lines follow. The $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) — the borders of the $$$i$$$-th subarray. Additional constraints on the input: the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$; the sum of $$$q$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each query, print YES if the subarray is good. Otherwise, print NO. You can output each letter of the answer in any case (upper or lower). For example, the strings yEs, yes, Yes, and YES will all be recognized as positive responses. Example Input 1 5 4 1 2 1 4 5 1 5 4 4 3 4 1 3
1,400
false
true
false
false
false
true
false
false
false
false
750
214A
Problem - 214A - 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 *800 No tag edit access → Contest materials (0u2009≤u2009_a_,u2009_b_) which satisfy the system. Input A single line contains two integers _n_,u2009_m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000) — the parameters of the system. The numbers on the line are separated by a space. Output On a single line print the answer to the problem. Examples Input 9 3 Output 1 Input 14 28 Output 1 Input 4 20 Output 0 Note In the first sample the suitable pair is integers (3,u20090). In the second sample the suitable pair is integers (3,u20095). In the third sample there is no suitable pair.
800
false
false
false
false
false
false
true
false
false
false
8,983
681E
Dima is living in a dormitory, as well as some cockroaches. At the moment 0 Dima saw a cockroach running on a table and decided to kill it. Dima needs exactly _T_ seconds for aiming, and after that he will precisely strike the cockroach and finish it. To survive the cockroach has to run into a shadow, cast by round plates standing on the table, in _T_ seconds. Shadow casted by any of the plates has the shape of a circle. Shadow circles may intersect, nest or overlap arbitrarily. The cockroach uses the following strategy: first he equiprobably picks a direction to run towards and then runs towards it with the constant speed _v_. If at some moment _t_u2009≤u2009_T_ it reaches any shadow circle, it immediately stops in the shadow and thus will stay alive. Otherwise the cockroach is killed by the Dima's precise strike. Consider that the Dima's precise strike is instant. Determine the probability of that the cockroach will stay alive. Input In the first line of the input the four integers _x_0, _y_0, _v_, _T_ (_x_0,u2009_y_0u2009≤u2009109, 0u2009≤u2009_v_,u2009_T_u2009≤u2009109) are givenxa0— the cockroach initial position on the table in the Cartesian system at the moment 0, the cockroach's constant speed and the time in seconds Dima needs for aiming respectively. In the next line the only number _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000) is givenxa0— the number of shadow circles casted by plates. In the next _n_ lines shadow circle description is given: the _i__th_ of them consists of three integers _x__i_, _y__i_, _r__i_ (_x__i_,u2009_y__i_u2009≤u2009109, 0u2009≤u2009_r_u2009≤u2009109)xa0— the _i__th_ shadow circle on-table position in the Cartesian system and its radius respectively. Consider that the table is big enough for the cockroach not to run to the table edges and avoid Dima's precise strike. Output Print the only real number _p_xa0— the probability of that the cockroach will stay alive. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20094. Examples Input 0 0 1 1 3 1 1 1 -1 -1 1 -2 2 1 Note The picture for the first sample is given below. Red color stands for points which being chosen as the cockroach's running direction will cause him being killed, green color for those standing for survival directions. Please note that despite containing a circle centered in (u2009-u20092,u20092) a part of zone is colored red because the cockroach is not able to reach it in one second.
2,500
false
false
false
false
false
false
false
false
true
false
7,099
38C
The blinds are known to consist of opaque horizontal stripes that can be rotated thus regulating the amount of light flowing in the room. There are _n_ blind stripes with the width of 1 in the factory warehouse for blind production. The problem is that all of them are spare details from different orders, that is, they may not have the same length (it is even possible for them to have different lengths) Every stripe can be cut into two or more parts. The cuttings are made perpendicularly to the side along which the length is measured. Thus the cuttings do not change the width of a stripe but each of the resulting pieces has a lesser length (the sum of which is equal to the length of the initial stripe) After all the cuttings the blinds are constructed through consecutive joining of several parts, similar in length, along sides, along which length is measured. Also, apart from the resulting pieces an initial stripe can be used as a blind if it hasn't been cut. It is forbidden to construct blinds in any other way. Thus, if the blinds consist of _k_ pieces each _d_ in length, then they are of form of a rectangle of _k_u2009×u2009_d_ bourlemeters. Your task is to find for what window possessing the largest possible area the blinds can be made from the given stripes if on technical grounds it is forbidden to use pieces shorter than _l_ bourlemeter. The window is of form of a rectangle with side lengths as positive integers. Input The first output line contains two space-separated integers _n_ and _l_ (1u2009≤u2009_n_,u2009_l_u2009≤u2009100). They are the number of stripes in the warehouse and the minimal acceptable length of a blind stripe in bourlemeters. The second line contains space-separated _n_ integers _a__i_. They are the lengths of initial stripes in bourlemeters (1u2009≤u2009_a__i_u2009≤u2009100). Output Print the single number — the maximal area of the window in square bourlemeters that can be completely covered. If no window with a positive area that can be covered completely without breaking any of the given rules exist, then print the single number 0. Note In the first sample test the required window is 2u2009×u20094 in size and the blinds for it consist of 4 parts, each 2 bourlemeters long. One of the parts is the initial stripe with the length of 2, the other one is a part of a cut stripe with the length of 3 and the two remaining stripes are parts of a stripe with the length of 4 cut in halves.
1,400
false
false
false
false
false
false
true
false
false
false
9,806
696C
As we all know Barney's job is "PLEASE" and he has not much to do at work. That's why he started playing "cups and key". In this game there are three identical cups arranged in a line from left to right. Initially key to Barney's heart is under the middle cup. Then at one turn Barney swaps the cup in the middle with any of other two cups randomly (he choses each with equal probability), so the chosen cup becomes the middle one. Game lasts _n_ turns and Barney independently choses a cup to swap with the middle one within each turn, and the key always remains in the cup it was at the start. After _n_-th turn Barney asks a girl to guess which cup contains the key. The girl points to the middle one but Barney was distracted while making turns and doesn't know if the key is under the middle cup. That's why he asked you to tell him the probability that girl guessed right. Number _n_ of game turns can be extremely large, that's why Barney did not give it to you. Instead he gave you an array _a_1,u2009_a_2,u2009...,u2009_a__k_ such that in other words, _n_ is multiplication of all elements of the given array. Because of precision difficulties, Barney asked you to tell him the answer as an irreducible fraction. In other words you need to find it as a fraction _p_u2009/u2009_q_ such that , where is the greatest common divisor. Since _p_ and _q_ can be extremely large, you only need to find the remainders of dividing each of them by 109u2009+u20097. Please note that we want of _p_ and _q_ to be 1, not of their remainders after dividing by 109u2009+u20097. Input The first line of input contains a single integer _k_ (1u2009≤u2009_k_u2009≤u2009105)xa0— the number of elements in array Barney gave you. The second line contains _k_ integers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≤u2009_a__i_u2009≤u20091018)xa0— the elements of the array.
2,000
true
false
true
true
false
false
false
false
false
false
7,039
1703E
You are given a square grid with $$$n$$$ rows and $$$n$$$ columns. Each cell contains either $$$0$$$ or $$$1$$$. In an operation, you can select a cell of the grid and flip it (from $$$0 o 1$$$ or $$$1 o 0$$$). Find the minimum number of operations you need to obtain a square that remains the same when rotated $$$0^{circ}$$$, $$$90^{circ}$$$, $$$180^{circ}$$$ and $$$270^{circ}$$$. The picture below shows an example of all rotations of a grid. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the size of the grid. Then $$$n$$$ lines follow, each with $$$n$$$ characters $$$a_{i,j}$$$ ($$$0 leq a_{i,j} leq 1$$$)xa0— the number written in each cell. Output For each test case output a single integer xa0— the minimum number of operations needed to make the square look the same rotated $$$0^{circ}$$$, $$$90^{circ}$$$, $$$180^{circ}$$$ and $$$270^{circ}$$$. Example Input 5 3 010 110 010 1 0 5 11100 11011 01011 10011 11000 5 01000 10101 01010 00010 01001 5 11001 00000 11111 10110 01111 Note In the first test case, we can perform one operations to make the grid $$$begin{matrix}0 & 1 & 0 1 & 1 & color{red}{1} 0 & 1 & 0end{matrix}$$$. Now, all rotations of the square are the same. In the second test case, all rotations of the square are already the same, so we don't need any flips.
1,200
false
false
true
false
false
false
false
false
false
false
2,058
901A
Sasha is taking part in a programming competition. In one of the problems she should check if some rooted trees are isomorphic or not. She has never seen this problem before, but, being an experienced participant, she guessed that she should match trees to some sequences and then compare these sequences instead of trees. Sasha wants to match each tree with a sequence _a_0,u2009_a_1,u2009...,u2009_a__h_, where _h_ is the height of the tree, and _a__i_ equals to the number of vertices that are at distance of _i_ edges from root. Unfortunately, this time Sasha's intuition was wrong, and there could be several trees matching the same sequence. To show it, you need to write a program that, given the sequence _a__i_, builds two non-isomorphic rooted trees that match that sequence, or determines that there is only one such tree. Two rooted trees are isomorphic, if you can reenumerate the vertices of the first one in such a way, that the index of the root becomes equal the index of the root of the second tree, and these two trees become equal. The height of a rooted tree is the maximum number of edges on a path from the root to any other vertex. Input The first line contains a single integer _h_ (2u2009≤u2009_h_u2009≤u2009105)xa0— the height of the tree. The second line contains _h_u2009+u20091 integersxa0— the sequence _a_0,u2009_a_1,u2009...,u2009_a__h_ (1u2009≤u2009_a__i_u2009≤u20092·105). The sum of all _a__i_ does not exceed 2·105. It is guaranteed that there is at least one tree matching this sequence. Output If there is only one tree matching this sequence, print "perfect". Otherwise print "ambiguous" in the first line. In the second and in the third line print descriptions of two trees in the following format: in one line print integers, the _k_-th of them should be the parent of vertex _k_ or be equal to zero, if the _k_-th vertex is the root. These treese should be non-isomorphic and should match the given sequence.
1,500
false
false
false
false
false
true
false
false
false
false
6,141
533B
One Big Software Company has _n_ employees numbered from 1 to _n_. The director is assigned number 1. Every employee of the company except the director has exactly one immediate superior. The director, of course, doesn't have a superior. We will call person _a_ a subordinates of another person _b_, if either _b_ is an immediate supervisor of _a_, or the immediate supervisor of _a_ is a subordinate to person _b_. In particular, subordinates of the head are all other employees of the company. To solve achieve an Important Goal we need to form a workgroup. Every person has some efficiency, expressed by a positive integer _a__i_, where _i_ is the person's number. The efficiency of the workgroup is defined as the total efficiency of all the people included in it. The employees of the big software company are obsessed with modern ways of work process organization. Today pair programming is at the peak of popularity, so the workgroup should be formed with the following condition. Each person entering the workgroup should be able to sort all of his subordinates who are also in the workgroup into pairs. In other words, for each of the members of the workgroup the number of his subordinates within the workgroup should be even. Your task is to determine the maximum possible efficiency of the workgroup formed at observing the given condition. Any person including the director of company can enter the workgroup. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the number of workers of the Big Software Company. Then _n_ lines follow, describing the company employees. The _i_-th line contains two integers _p__i_,u2009_a__i_ (1u2009≤u2009_a__i_u2009≤u2009105) — the number of the person who is the _i_-th employee's immediate superior and _i_-th employee's efficiency. For the director _p_1u2009=u2009u2009-u20091, for all other people the condition 1u2009≤u2009_p__i_u2009<u2009_i_ is fulfilled. Output Print a single integer — the maximum possible efficiency of the workgroup. Examples Input 7 -1 3 1 2 1 1 1 4 4 5 4 3 5 2 Note In the sample test the most effective way is to make a workgroup from employees number 1,u20092,u20094,u20095,u20096.
2,000
false
false
false
true
false
false
false
false
false
true
7,706
1552H
Let us call a point of the plane admissible if its coordinates are positive integers less than or equal to $$$200$$$. There is an invisible rectangle such that: its vertices are all admissible; its sides are parallel to the coordinate axes; its area is strictly positive. Your task is to guess the perimeter of this rectangle. In order to guess it, you may ask at most $$$4$$$ queries. In each query, you choose a nonempty subset of the admissible points and you are told how many of the chosen points are inside or on the boundary of the invisible rectangle. Interaction To ask a query (of the kind described in the statement), you shall print two lines: In the first line print "? $$$k$$$" (without the quotes) where $$$k$$$ ($$$kge 1$$$) is the number of chosen points. In the second line print $$$2k$$$ integers $$$x_1,, y_1,, x_2,, y_2,, dots,, x_k,, y_k$$$ ($$$1le x_i,y_ile 200$$$ for $$$i=1,2,dots,k$$$) where $$$(x_1, y_1),,(x_2, y_2),,(x_3, y_3),, dots,,(x_k, y_k)$$$ are the $$$k$$$ distinct admissible chosen points (the order of the points is not important). After this, you should read an integer — the number of chosen points that are inside or on the boundary of the invisible rectangle. When you have identified the perimeter $$$p$$$ of the invisible rectangle, you must print "! $$$p$$$" (without quotes) and terminate your program. If you ask more than $$$4$$$ queries or if one of the queries is malformed, the interactor terminates immediately and your program receives verdict Wrong Answer. The interactor may be adaptive (i.e., the hidden rectangle may not be chosen before the beginning of the interaction). After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks To hack a solution, use the following format. The input has only one line, containing the $$$4$$$ integers $$$x_0$$$, $$$y_0$$$, $$$x_1$$$, $$$y_1$$$ ($$$1le x_0<x_1le 200$$$, $$$1le y_0 < y_1 le 200$$$) — $$$(x_0,y_0)$$$ is the bottom-left vertex of the hidden rectangle and $$$(x_1, y_1)$$$ is the top-right vertex of the hidden rectangle. Note that for hacks the interaction won't be adaptive. Note The following is an example of interaction for the first sample intended to show the format of the queries. $$$$$$ begin{array}{lll} ext{Query (contestant program)} & ext{Answer (interactor)} & ext{Explanation} hline mathtt{? 4} & & ext{We choose the $4$ vertices of} mathtt{13 5 13 80 123 5 123 80} & mathtt{4} & ext{the hidden rectangle.} hline mathtt{? 5} & & ext{We choose $4$ points just outside the hidden} mathtt{100 4 100 81 12 40 124 40 50 50} & mathtt{1}& ext{rectangle and also the point $(50,50)$.} hline mathtt{? 2} & & ext{We choose the points $(1, 1)$} mathtt{200 200 1 1} & mathtt{0} & ext{and $(200,200)$.} hline mathtt{! 370} & & ext{This is the correct perimeter.} end{array} $$$$$$ For the second sample, a possible interaction is the following. $$$$$$ begin{array}{lll} ext{Query (contestant program)} & ext{Answer (interactor)} & ext{Explanation} hline mathtt{? 4} & & ext{We choose the points $(3, 2)$, $(4, 1)$,} mathtt{3 2 4 1 5 2 4 3} & 2 & ext{$(5, 2)$ and $(4, 3)$.} hline mathtt{? 7} & & ext{We choose the points $(1, 4)$, $(2, 4)$,} mathtt{1 4 2 4 1 5 2 5 5 5 5 6 6 5} & 1 & ext{$(1, 5)$, $(2, 5)$, $(5, 5)$, $(5, 6)$ and $(6, 5)$.} hline mathtt{! 8} & & ext{This is the correct perimeter.} end{array} $$$$$$ The situation is shown in the following picture: The green points are the ones belonging to the first query, while the orange points are the ones belonging to the second query. One can see that there are exactly two rectangles consistent with the interactor's answers: the rectangle of vertices $$$(2, 2)$$$ and $$$(4, 4)$$$, shown in red; the rectangle of vertices $$$(4, 2)$$$ and $$$(5, 5)$$$, shown in blue. Since both of these rectangles have perimeter $$$8$$$, this is the final answer.
3,300
false
false
false
false
false
false
false
true
false
false
2,877
455C
Andrew plays a game called "Civilization". Dima helps him. The game has _n_ cities and _m_ bidirectional roads. The cities are numbered from 1 to _n_. Between any pair of cities there either is a single (unique) path, or there is no path at all. A path is such a sequence of distinct cities _v_1,u2009_v_2,u2009...,u2009_v__k_, that there is a road between any contiguous cities _v__i_ and _v__i_u2009+u20091 (1u2009≤u2009_i_u2009<u2009_k_). The length of the described path equals to (_k_u2009-u20091). We assume that two cities lie in the same region if and only if, there is a path connecting these two cities. During the game events of two types take place: 1. Andrew asks Dima about the length of the longest path in the region where city _x_ lies. 2. Andrew asks Dima to merge the region where city _x_ lies with the region where city _y_ lies. If the cities lie in the same region, then no merging is needed. Otherwise, you need to merge the regions as follows: choose a city from the first region, a city from the second region and connect them by a road so as to minimize the length of the longest path in the resulting region. If there are multiple ways to do so, you are allowed to choose any of them. Dima finds it hard to execute Andrew's queries, so he asks you to help him. Help Dima. Input The first line contains three integers _n_, _m_, _q_ (1u2009≤u2009_n_u2009≤u20093·105; 0u2009≤u2009_m_u2009<u2009_n_; 1u2009≤u2009_q_u2009≤u20093·105) — the number of cities, the number of the roads we already have and the number of queries, correspondingly. Each of the following _m_ lines contains two integers, _a__i_ and _b__i_ (_a__i_u2009≠u2009_b__i_; 1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_). These numbers represent the road between cities _a__i_ and _b__i_. There can be at most one road between two cities. Each of the following _q_ lines contains one of the two events in the following format: 1 _x__i_. It is the request Andrew gives to Dima to find the length of the maximum path in the region that contains city _x__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_). 2 _x__i_ _y__i_. It is the request Andrew gives to Dima to merge the region that contains city _x__i_ and the region that contains city _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_). Note, that _x__i_ can be equal to _y__i_. Output For each event of the first type print the answer on a separate line. Examples Input 6 0 6 2 1 2 2 3 4 2 5 6 2 3 2 2 5 3 1 1
2,100
false
false
false
true
false
false
false
false
false
false
8,017
38G
On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya. Every man is characterized by two numbers: _a__i_, which is the importance of his current task (the greater the number is, the more important the task is) and number _c__i_, which is a picture of his conscience. Numbers _a__i_ form the permutation of numbers from 1 to _n_. Let the queue consist of _n_u2009-u20091 people at the moment. Let's look at the way the person who came number _n_ behaves. First, he stands at the end of the queue and the does the following: if importance of the task _a__i_ of the man in front of him is less than _a__n_, they swap their places (it looks like this: the man number _n_ asks the one before him: "Erm... Excuse me please but it's very important for me... could you please let me move up the queue?"), then he again poses the question to the man in front of him and so on. But in case when _a__i_ is greater than _a__n_, moving up the queue stops. However, the man number _n_ can perform the operation no more than _c__n_ times. In our task let us suppose that by the moment when the man number _n_ joins the queue, the process of swaps between _n_u2009-u20091 will have stopped. If the swap is possible it necessarily takes place. Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops. Input The first input line contains an integer _n_ which is the number of people who has joined the queue (1u2009≤u2009_n_u2009≤u2009105). In the next _n_ lines descriptions of the people are given in order of their coming — space-separated integers _a__i_ and _c__i_ (1u2009≤u2009_a__i_u2009≤u2009_n_, 0u2009≤u2009_c__i_u2009≤u2009_n_). Every description is located on s single line. All the _a__i_'s are different. Output Output the permutation of numbers from 1 to _n_, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the _i_-th number stands for the number of a person who will stand in line on the place number _i_ after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space.
2,300
false
false
false
false
true
false
false
false
false
false
9,802
1203D1
The only difference between easy and hard versions is the length of the string. You are given a string $$$s$$$ and a string $$$t$$$, both consisting only of lowercase Latin letters. It is guaranteed that $$$t$$$ can be obtained from $$$s$$$ by removing some (possibly, zero) number of characters (not necessary contiguous) from $$$s$$$ without changing order of remaining characters (in other words, it is guaranteed that $$$t$$$ is a subsequence of $$$s$$$). For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test". You want to remove some substring (contiguous subsequence) from $$$s$$$ of maximum possible length such that after removing this substring $$$t$$$ will remain a subsequence of $$$s$$$. If you want to remove the substring $$$s[l;r]$$$ then the string $$$s$$$ will be transformed to $$$s_1 s_2 dots s_{l-1} s_{r+1} s_{r+2} dots s_{s-1} s_{s}$$$ (where $$$s$$$ is the length of $$$s$$$). Your task is to find the maximum possible length of the substring you can remove so that $$$t$$$ is still a subsequence of $$$s$$$. Input The first line of the input contains one string $$$s$$$ consisting of at least $$$1$$$ and at most $$$200$$$ lowercase Latin letters. The second line of the input contains one string $$$t$$$ consisting of at least $$$1$$$ and at most $$$200$$$ lowercase Latin letters. It is guaranteed that $$$t$$$ is a subsequence of $$$s$$$.
1,600
false
true
true
false
false
false
false
false
false
false
4,680
1250K
There are $$$n$$$ lectures and $$$m$$$ seminars to be conducted today at the Faculty of Approximate Sciences. The $$$i$$$-th lecture starts at $$$a_i$$$ and ends at $$$b_i$$$ (formally, time of the lecture spans an interval $$$[a_i, b_i)$$$, the right bound is exclusive). The $$$j$$$-th seminar starts at $$$p_j$$$ and ends at $$$q_j$$$ (similarly, time of the seminar spans an interval $$$[p_j, q_j)$$$, the right bound is exclusive). There are $$$x$$$ HD-projectors numbered from $$$1$$$ to $$$x$$$ and $$$y$$$ ordinary projectors numbered from $$$x + 1$$$ to $$$x + y$$$ available at the faculty. Projectors should be distributed in such a way that: an HD-projector is used in each lecture; some projector (ordinary or HD) is used in each seminar; a projector (ordinary or HD) can only be used in one event at the same moment of time; if a projector is selected for an event, it is used there for the whole duration of the event; a projector can be reused in some following event, if it starts not earlier than current event finishes. You are to find such distribution of projectors, if it exists. Again, note that the right bound of the event's time range is not inclusive: if some event starts exactly when another event finishes, the projector can be reused (suppose that it is instantly transported to the location of the event). Input The first line contains an integer $$$t$$$ ($$$1 le t le 300$$$) — the number of test cases. Each test case starts with a line containing four integers $$$n, m, x, y$$$ ($$$0 le n, m, x, y le 300$$$; $$$n+m>0$$$, $$$x + y > 0$$$) — the number of lectures, the number of seminars, the number of HD projectors and the number of ordinary projectors, respectively. The next $$$n$$$ lines describe lectures. Each line contains two integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i < b_i le 10^6$$$) — the start time (inclusive) and finish time (exclusive) of the $$$i$$$-th lecture. The next $$$m$$$ lines describe seminars. Each line contains two integers $$$p_j$$$, $$$q_j$$$ ($$$1 le p_j < q_j le 10^6$$$) — the start time (inclusive) and finish time (exclusive) of the $$$j$$$-th seminar. Output For each test case, print YES if it is possible to distribute projectors in order to meet all requirements, or NO otherwise. In case of positive answer, output one additional line containing $$$n + m$$$ integers. The first $$$n$$$ integers should be not less than $$$1$$$ and not greater than $$$x$$$, and the $$$i$$$-th of them should be the index of HD projector used in the $$$i$$$-th lecture. The last $$$m$$$ integers should be not less than $$$1$$$ and not greater than $$$x + y$$$, and the $$$j$$$-th of them should be the index of projector used in the $$$j$$$-th seminar. If there are multiple answers, print any of them. Examples Input 2 2 2 2 2 1 5 2 5 1 5 1 4 2 0 2 10 1 3 1 3 Output YES 2 1 4 3 YES 2 1 Input 3 1 2 1 1 3 4 2 4 1 3 3 4 2 3 5 7 1 3 1 7 4 8 2 5 1 6 2 8 0 1 1 0 1 1000000
3,100
false
false
false
false
false
false
false
false
false
true
4,445
1715E
Stanley lives in a country that consists of $$$n$$$ cities (he lives in city $$$1$$$). There are bidirectional roads between some of the cities, and you know how long it takes to ride through each of them. Additionally, there is a flight between each pair of cities, the flight between cities $$$u$$$ and $$$v$$$ takes $$$(u - v)^2$$$ time. Stanley is quite afraid of flying because of watching "Sully: Miracle on the Hudson" recently, so he can take at most $$$k$$$ flights. Stanley wants to know the minimum time of a journey to each of the $$$n$$$ cities from the city $$$1$$$. Input In the first line of input there are three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 leq n leq 10^{5}$$$, $$$1 leq m leq 10^{5}$$$, $$$1 leq k leq 20$$$)xa0— the number of cities, the number of roads, and the maximal number of flights Stanley can take. The following $$$m$$$ lines describe the roads. Each contains three integers $$$u$$$, $$$v$$$, $$$w$$$ ($$$1 leq u, v leq n$$$, $$$u eq v$$$, $$$1 leq w leq 10^{9}$$$)xa0— the cities the road connects and the time it takes to ride through. Note that some pairs of cities may be connected by more than one road. Output Print $$$n$$$ integers, $$$i$$$-th of which is equal to the minimum time of traveling to city $$$i$$$. Examples Input 4 3 1 1 2 3 2 4 5 3 4 7 Input 5 5 2 2 1 33 1 5 93 5 3 48 2 3 21 4 2 1 Note In the first sample, it takes no time to get to city 1; to get to city 2 it is possible to use a flight between 1 and 2, which will take 1 unit of time; to city 3 you can get via a road from city 1, which will take 1 unit of time. In the second sample, it also takes no time to get to city 1. To get to city 2 Stanley should use a flight between 1 and 2, which will take 1 unit of time. To get to city 3 Stanley can ride between cities 1 and 2, which will take 3 units of time, and then use a flight between 2 and 3. To get to city 4 Stanley should use a flight between 1 and 2, then take a ride from 2 to 4, which will take 5 units of time.
2,400
false
true
false
true
true
false
false
false
false
true
1,989
115B
You have a garden consisting entirely of grass and weeds. Your garden is described by an _n_u2009×u2009_m_ grid, with rows numbered 1 to _n_ from top to bottom, and columns 1 to _m_ from left to right. Each cell is identified by a pair (_r_,u2009_c_) which means that the cell is located at row _r_ and column _c_. Each cell may contain either grass or weeds. For example, a 4u2009×u20095 garden may look as follows (empty cells denote grass): You have a land-mower with you to mow all the weeds. Initially, you are standing with your lawnmower at the top-left corner of the garden. That is, at cell (1,u20091). At any moment of time you are facing a certain direction — either left or right. And initially, you face right. In one move you can do either one of these: 1) Move one cell in the direction that you are facing. if you are facing right: move from cell (_r_,u2009_c_) to cell (_r_,u2009_c_u2009+u20091) if you are facing left: move from cell (_r_,u2009_c_) to cell (_r_,u2009_c_u2009-u20091) 2) Move one cell down (that is, from cell (_r_,u2009_c_) to cell (_r_u2009+u20091,u2009_c_)), and change your direction to the opposite one. if you were facing right previously, you will face left if you were facing left previously, you will face right You are not allowed to leave the garden. Weeds will be mowed if you and your lawnmower are standing at the cell containing the weeds (your direction doesn't matter). This action isn't counted as a move. What is the minimum number of moves required to mow all the weeds? Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009150) — the number of rows and columns respectively. Then follow _n_ lines containing _m_ characters each — the content of the grid. "G" means that this cell contains grass. "W" means that this cell contains weeds. It is guaranteed that the top-left corner of the grid will contain grass. Note For the first example, this is the picture of the initial state of the grid: A possible solution is by mowing the weeds as illustrated below:
1,500
false
true
false
false
false
false
false
false
true
false
9,427
1178A
Alice is the leader of the State Refactoring Party, and she is about to become the prime minister. The elections have just taken place. There are $$$n$$$ parties, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th party has received $$$a_i$$$ seats in the parliament. Alice's party has number $$$1$$$. In order to become the prime minister, she needs to build a coalition, consisting of her party and possibly some other parties. There are two conditions she needs to fulfil: The total number of seats of all parties in the coalition must be a strict majority of all the seats, i.e. it must have strictly more than half of the seats. For example, if the parliament has $$$200$$$ (or $$$201$$$) seats, then the majority is $$$101$$$ or more seats. Alice's party must have at least $$$2$$$ times more seats than any other party in the coalition. For example, to invite a party with $$$50$$$ seats, Alice's party must have at least $$$100$$$ seats. For example, if $$$n=4$$$ and $$$a=[51, 25, 99, 25]$$$ (note that Alice'a party has $$$51$$$ seats), then the following set $$$[a_1=51, a_2=25, a_4=25]$$$ can create a coalition since both conditions will be satisfied. However, the following sets will not create a coalition: $$$[a_2=25, a_3=99, a_4=25]$$$ since Alice's party is not there; $$$[a_1=51, a_2=25]$$$ since coalition should have a strict majority; $$$[a_1=51, a_2=25, a_3=99]$$$ since Alice's party should have at least $$$2$$$ times more seats than any other party in the coalition. Alice does not have to minimise the number of parties in a coalition. If she wants, she can invite as many parties as she wants (as long as the conditions are satisfied). If Alice's party has enough people to create a coalition on her own, she can invite no parties. Note that Alice can either invite a party as a whole or not at all. It is not possible to invite only some of the deputies (seats) from another party. In other words, if Alice invites a party, she invites all its deputies. Find and print any suitable coalition. Input The first line contains a single integer $$$n$$$ ($$$2 leq n leq 100$$$)xa0— the number of parties. The second line contains $$$n$$$ space separated integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 100$$$)xa0— the number of seats the $$$i$$$-th party has. Output If no coalition satisfying both conditions is possible, output a single line with an integer $$$0$$$. Otherwise, suppose there are $$$k$$$ ($$$1 leq k leq n$$$) parties in the coalition (Alice does not have to minimise the number of parties in a coalition), and their indices are $$$c_1, c_2, dots, c_k$$$ ($$$1 leq c_i leq n$$$). Output two lines, first containing the integer $$$k$$$, and the second the space-separated indices $$$c_1, c_2, dots, c_k$$$. You may print the parties in any order. Alice's party (number $$$1$$$) must be on that list. If there are multiple solutions, you may print any of them. Note In the first example, Alice picks the second party. Note that she can also pick the third party or both of them. However, she cannot become prime minister without any of them, because $$$100$$$ is not a strict majority out of $$$200$$$. In the second example, there is no way of building a majority, as both other parties are too large to become a coalition partner. In the third example, Alice already has the majority. The fourth example is described in the problem statement.
800
false
true
false
false
false
false
false
false
false
false
4,830
1601E
Students of one unknown college don't have PE courses. That's why $$$q$$$ of them decided to visit a gym nearby by themselves. The gym is open for $$$n$$$ days and has a ticket system. At the $$$i$$$-th day, the cost of one ticket is equal to $$$a_i$$$. You are free to buy more than one ticket per day. You can activate a ticket purchased at day $$$i$$$ either at day $$$i$$$ or any day later. Each activated ticket is valid only for $$$k$$$ days. In other words, if you activate ticket at day $$$t$$$, it will be valid only at days $$$t, t + 1, dots, t + k - 1$$$. You know that the $$$j$$$-th student wants to visit the gym at each day from $$$l_j$$$ to $$$r_j$$$ inclusive. Each student will use the following strategy of visiting the gym at any day $$$i$$$ ($$$l_j le i le r_j$$$): 1. person comes to a desk selling tickets placed near the entrance and buy several tickets with cost $$$a_i$$$ apiece (possibly, zero tickets); 2. if the person has at least one activated and still valid ticket, they just go in. Otherwise, they activate one of tickets purchased today or earlier and go in. Note that each student will visit gym only starting $$$l_j$$$, so each student has to buy at least one ticket at day $$$l_j$$$. Help students to calculate the minimum amount of money they have to spend in order to go to the gym. Input The first line contains three integers $$$n$$$, $$$q$$$ and $$$k$$$ ($$$1 le n, q le 300,000$$$; $$$1 le k le n$$$)xa0— the number of days, the number of students and the number of days each ticket is still valid. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the cost of one ticket at the corresponding day. Each of the next $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$)xa0— the segment of days the corresponding student want to visit the gym. Output For each student, print the minimum possible amount of money they have to spend in order to go to the gym at desired days. Example Input 7 5 2 2 15 6 3 7 5 6 1 2 3 7 5 5 7 7 3 5 Note Let's see how each student have to spend their money: The first student should buy one ticket at day $$$1$$$. The second student should buy one ticket at day $$$3$$$ and two tickets at day $$$4$$$. Note that student can keep purchased tickets for the next days. The third student should buy one ticket at day $$$5$$$. The fourth student should buy one ticket at day $$$7$$$. The fifth student should buy one ticket at day $$$3$$$ and one at day $$$4$$$.
2,900
false
true
false
true
true
false
false
false
false
false
2,647
523D
A social network for dogs called DH (DogHouse) has _k_ special servers to recompress uploaded videos of cute cats. After each video is uploaded, it should be recompressed on one (any) of the servers, and only after that it can be saved in the social network. We know that each server takes one second to recompress a one minute fragment. Thus, any server takes _m_ seconds to recompress a _m_ minute video. We know the time when each of the _n_ videos were uploaded to the network (in seconds starting from the moment all servers started working). All videos appear at different moments of time and they are recompressed in the order they appear. If some video appeared at time _s_, then its recompressing can start at that very moment, immediately. Some videos can await recompressing when all the servers are busy. In this case, as soon as a server is available, it immediately starts recompressing another video. The videos that await recompressing go in a queue. If by the moment the videos started being recompressed some servers are available, then any of them starts recompressing the video. For each video find the moment it stops being recompressed. Input The first line of the input contains integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20095·105) — the number of videos and servers, respectively. Next _n_ lines contain the descriptions of the videos as pairs of integers _s__i_,u2009_m__i_ (1u2009≤u2009_s__i_,u2009_m__i_u2009≤u2009109), where _s__i_ is the time in seconds when the _i_-th video appeared and _m__i_ is its duration in minutes. It is guaranteed that all the _s__i_'s are distinct and the videos are given in the chronological order of upload, that is in the order of increasing _s__i_. Output Print _n_ numbers _e_1,u2009_e_2,u2009...,u2009_e__n_, where _e__i_ is the time in seconds after the servers start working, when the _i_-th video will be recompressed. Examples Input 6 1 1 1000000000 2 1000000000 3 1000000000 4 1000000000 5 1000000000 6 3 Output 1000000001 2000000001 3000000001 4000000001 5000000001 5000000004
1,600
false
false
true
false
true
false
false
false
false
false
7,743
1488C
There is a street that can be represented as an array of length $$$n$$$. There are two policemen patrolling a street: the first one is standing at the point $$$x$$$ of the street and the second one is standing at the point $$$y$$$ of the street. During one minute, both policemen can decide what to do (independently): move left (if the current position is greater than $$$1$$$), move right (if the current position is less than $$$n$$$), or do nothing. The street is considered clear if each point of the street is visited by at least one policeman. Your task is to find the minimum number of minutes the policemen need to visit each point of the street (again, each point should be visited by at least one of them). You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains three integers $$$n$$$, $$$x$$$ and $$$y$$$ ($$$2 le n le 10^6$$$; $$$1 le x, y le n$$$; $$$x e y$$$) — the length of the street, the position of the first policeman and the position of the second policeman, respectively. It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^6$$$ ($$$sum n le 10^6$$$). Output For each test case, print one integer — the minimum number of minutes the policemen need to visit each point of the street. Example Input 6 4 1 2 7 7 1 10 2 6 8 5 2 2 1 2 20 4 14
1,900
true
false
false
false
false
false
true
true
false
false
3,237
863D
You are given an array _a_ of size _n_, and _q_ queries to it. There are queries of two types: 1 _l__i_ _r__i_ — perform a cyclic shift of the segment [_l__i_,u2009_r__i_] to the right. That is, for every _x_ such that _l__i_u2009≤u2009_x_u2009<u2009_r__i_ new value of _a__x_u2009+u20091 becomes equal to old value of _a__x_, and new value of _a__l__i_ becomes equal to old value of _a__r__i_; 2 _l__i_ _r__i_ — reverse the segment [_l__i_,u2009_r__i_]. There are _m_ important indices in the array _b_1, _b_2, ..., _b__m_. For each _i_ such that 1u2009≤u2009_i_u2009≤u2009_m_ you have to output the number that will have index _b__i_ in the array after all queries are performed. Input The first line contains three integer numbers _n_, _q_ and _m_ (1u2009≤u2009_n_,u2009_q_u2009≤u20092·105, 1u2009≤u2009_m_u2009≤u2009100). The second line contains _n_ integer numbers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Then _q_ lines follow. _i_-th of them contains three integer numbers _t__i_, _l__i_, _r__i_, where _t__i_ is the type of _i_-th query, and [_l__i_,u2009_r__i_] is the segment where this query is performed (1u2009≤u2009_t__i_u2009≤u20092, 1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). The last line contains _m_ integer numbers _b_1, _b_2, ..., _b__m_ (1u2009≤u2009_b__i_u2009≤u2009_n_) — important indices of the array. Output Print _m_ numbers, _i_-th of which is equal to the number at index _b__i_ after all queries are done. Example Input 6 3 5 1 2 3 4 5 6 2 1 3 2 3 6 1 1 6 2 2 1 5 3
1,800
false
false
true
false
true
false
false
false
false
false
6,291
1369C
Lee just became Master in Codeforces, and so, he went out to buy some gifts for his friends. He bought $$$n$$$ integers, now it's time to distribute them between his friends rationally... Lee has $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ in his backpack and he has $$$k$$$ friends. Lee would like to distribute all integers in his backpack between his friends, such that the $$$i$$$-th friend will get exactly $$$w_i$$$ integers and each integer will be handed over to exactly one friend. Let's define the happiness of a friend as the sum of the maximum and the minimum integer he'll get. Lee would like to make his friends as happy as possible, in other words, he'd like to maximize the sum of friends' happiness. Now he asks you to calculate the maximum sum of friends' happiness. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Next $$$3t$$$ lines contain test casesxa0— one per three lines. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le k le n$$$)xa0— the number of integers Lee has and the number of Lee's friends. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— the integers Lee has. The third line contains $$$k$$$ integers $$$w_1, w_2, ldots, w_k$$$ ($$$1 le w_i le n$$$; $$$w_1 + w_2 + ldots + w_k = n$$$)xa0— the number of integers Lee wants to give to each friend. It's guaranteed that the sum of $$$n$$$ over test cases is less than or equal to $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the maximum sum of happiness Lee can achieve. Example Input 3 4 2 1 13 7 17 1 3 6 2 10 10 10 10 11 11 3 3 4 4 1000000000 1000000000 1000000000 1000000000 1 1 1 1 Note In the first test case, Lee should give the greatest integer to the first friend (his happiness will be $$$17 + 17$$$) and remaining integers to the second friend (his happiness will be $$$13 + 1$$$). In the second test case, Lee should give $$${10, 10, 11}$$$ to the first friend and to the second friend, so the total happiness will be equal to $$$(11 + 10) + (11 + 10)$$$ In the third test case, Lee has four friends and four integers, it doesn't matter how he distributes the integers between his friends.
1,400
true
true
false
false
false
false
false
false
true
false
3,825
1899G
A tree is a connected graph without cycles. A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$[5, 1, 3, 2, 4]$$$ is a permutation, but $$$[2, 1, 1]$$$ is not a permutation (as $$$1$$$ appears twice in the array) and $$$[1, 3, 2, 5]$$$ is also not a permutation (as $$$n = 4$$$, but $$$5$$$ is present in the array). After a failed shoot in the BrMeast video, Alex fell into depression. Even his birthday did not make him happy. However, after receiving a gift from Timofey, Alex's mood suddenly improved. Now he spent days playing with the gifted constructor. Recently, he came up with an unusual entertainment. Alex builds a tree from his constructor, consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, with the root at vertex $$$1$$$. Then he writes down each integer from $$$1$$$ to $$$n$$$ in some order, obtaining a permutation $$$p$$$. After that, Alex comes up with $$$q$$$ triples of integers $$$l, r, x$$$. For each triple, he tries to determine if there is at least one descendant of vertex $$$x$$$ among the vertices $$$p_l, p_{l+1}, ldots, p_r$$$. A vertex $$$u$$$ is a descendant of vertex $$$v$$$ if and only if $$$mathrm{dist}(1, v) + mathrm{dist}(v, u) = mathrm{dist}(1, u)$$$, where $$$mathrm{dist}(a, b)$$$ is the distance between vertices $$$a$$$ and $$$b$$$. In other words, vertex $$$v$$$ must be on the path from the root to vertex $$$u$$$. Alex told Zakhar about this entertainment. Now Alex tells his friend $$$q$$$ triples as described above, hoping that Zakhar can check for the presence of a descendant. Zakhar is very sleepy, so he turned to you for help. Help Zakhar answer all of Alex's questions and finally go to sleep. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n, q$$$ ($$$1 le n, q le 10^5$$$)xa0— the number of vertices in the tree and the number of questions, respectively. Each of the next $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$), indicating that there is an edge between vertices $$$u_i$$$ and $$$v_i$$$ (it is guaranteed that the resulting graph is a tree). The next line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$)xa0— the permutation $$$p$$$ (it is guaranteed that each integer from $$$1$$$ to $$$n$$$ appears exactly once). Then follow $$$q$$$ lines describing Alex's questions. The $$$i$$$-th line contains three integers $$$l, r, x$$$ ($$$1 le l le r le n$$$, $$$1 le x le n$$$), as described in the statement. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$10^5$$$. Output For each of Alex's questions, print "Yes" (without quotes) if the described descendant exists, otherwise print "No" (without quotes). You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
1,900
false
false
false
false
true
false
false
false
true
false
916
1950D
Let's call a number a binary decimal if it is a positive integer and all digits in its decimal notation are either $$$0$$$ or $$$1$$$. For example, $$$1,010,111$$$ is a binary decimal, while $$$10,201$$$ and $$$787,788$$$ are not. Given a number $$$n$$$, you are asked whether or not it is possible to represent $$$n$$$ as a product of some (not necessarily distinct) binary decimals. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5 cdot 10^4$$$)xa0— the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$). Output For each test case, output "YES" (without quotes) if $$$n$$$ can be represented as a product of binary decimals, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yES", "yes", and "Yes" will be recognized as a positive response). Example Input 11 121 1 14641 12221 10110 100000 99 112 2024 12421 1001 Output YES YES YES YES YES YES NO NO NO NO YES Note The first five test cases can be represented as a product of binary decimals as follows: $$$121 = 11 imes 11$$$. $$$1 = 1$$$ is already a binary decimal. $$$14,641 = 11 imes 11 imes 11 imes 11$$$. $$$12,221 = 11 imes 11 imes 101$$$. $$$10,110 = 10,110$$$ is already a binary decimal.
1,100
false
false
true
true
false
false
true
false
false
false
578
1346D
Polycarp is developing an RPG game where the main character fights monsters and searches for treasure in dungeons. Now Polycarp is making one of the dungeons the character can explore. The dungeon consists of $$$n$$$ rooms connected by $$$m$$$ two-way tunnels, and it is possible to reach every room from every other room using tunnels. The rooms are guarded by monsters (the number of monsters in the $$$i$$$-th room is $$$a_i$$$), and the tunnels contain gold coins (the number of coins in the $$$i$$$-th tunnel is $$$w_i$$$). The $$$i$$$-th two-way tunnel connects rooms $$$v_i$$$ and $$$u_i$$$. Polycarp has already fixed the number of coins in each tunnel (the values of $$$w_i$$$ are already known), and now he tries to place the monsters in the rooms (the values of $$$a_i$$$ are not known yet). Polycarp wants to choose the number of monsters in each room in such a way that the following two conditions are met: the number of coins for the tunnel connecting the rooms $$$x$$$ and $$$y$$$ should be equal to the minimum of $$$a_x$$$ and $$$a_y$$$. That is, for each tunnel $$$i$$$, $$$w_i = min (a_{v_i}, a_{u_i})$$$; the number of monsters in the dungeon is as small as possible. That is, the value of $$$a_1 + a_2 + dots + a_n$$$ is minimum possible. Help Polycarp to choose the values $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, or tell him that it is impossible and he has to change something in his dungeon plan. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100000$$$) — the number of test cases. Then the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 200000$$$; $$$n - 1 le m le min(200000, frac{n(n-1)}{2})$$$) — the number of rooms and tunnels in the dungeon, respectively. Then $$$m$$$ lines follow, each line describing one of the tunnels in the dungeon. The $$$i$$$-th line contains three integers $$$v_i$$$, $$$u_i$$$ and $$$w_i$$$ ($$$1 le v_i, u_i le n$$$; $$$v_i e u_i$$$; $$$1 le w_i le 10^9$$$) denoting a two-way tunnel that connects rooms $$$v_i$$$ and $$$u_i$$$, and contains $$$w_i$$$ coins. The tunnel system is connected in each test case (it is possible to reach every room from every other room using the tunnels). Each pair of rooms is connected by at most one tunnel. The sum of $$$n$$$ over all test cases does not exceed $$$200000$$$. Similarly, the sum of $$$m$$$ over all test cases does not exceed $$$200000$$$. Output For each test case, print the answer as follows: If it is impossible to find the values of $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ satisfying all the constraints, print one single string NO on a separate line. Otherwise, print YES in the first line, and $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ in the second line. If there are multiple valid answers, print any of them. Example Input 3 3 2 1 2 1 2 3 1 5 7 3 2 7 3 4 9 1 5 5 1 2 5 4 1 5 4 2 7 3 1 5 4 4 1 2 5 3 2 2 4 1 3 3 4 4 Output YES 1 1 1 YES 5 7 9 9 5 NO
1,600
false
true
false
false
false
false
false
false
false
true
3,976
1237D
Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of $$$n$$$ tracks numbered from $$$1$$$ to $$$n$$$. The playlist is automatic and cyclic: whenever track $$$i$$$ finishes playing, track $$$i+1$$$ starts playing automatically; after track $$$n$$$ goes track $$$1$$$. For each track $$$i$$$, you have estimated its coolness $$$a_i$$$. The higher $$$a_i$$$ is, the cooler track $$$i$$$ is. Every morning, you choose a track. The playlist then starts playing from this track in its usual cyclic fashion. At any moment, you remember the maximum coolness $$$x$$$ of already played tracks. Once you hear that a track with coolness strictly less than $$$frac{x}{2}$$$ (no rounding) starts playing, you turn off the music immediately to keep yourself in a good mood. For each track $$$i$$$, find out how many tracks you will listen to before turning off the music if you start your morning with track $$$i$$$, or determine that you will never turn the music off. Note that if you listen to the same track several times, every time must be counted. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$), denoting the number of tracks in the playlist. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$), denoting coolnesses of the tracks. Output Output $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$, where $$$c_i$$$ is either the number of tracks you will listen to if you start listening from track $$$i$$$ or $$$-1$$$ if you will be listening to music indefinitely. Note In the first example, here is what will happen if you start with... track $$$1$$$: listen to track $$$1$$$, stop as $$$a_2 < frac{a_1}{2}$$$. track $$$2$$$: listen to track $$$2$$$, stop as $$$a_3 < frac{a_2}{2}$$$. track $$$3$$$: listen to track $$$3$$$, listen to track $$$4$$$, listen to track $$$1$$$, stop as $$$a_2 < frac{max(a_3, a_4, a_1)}{2}$$$. track $$$4$$$: listen to track $$$4$$$, listen to track $$$1$$$, stop as $$$a_2 < frac{max(a_4, a_1)}{2}$$$. In the second example, if you start with track $$$4$$$, you will listen to track $$$4$$$, listen to track $$$1$$$, listen to track $$$2$$$, listen to track $$$3$$$, listen to track $$$4$$$ again, listen to track $$$1$$$ again, and stop as $$$a_2 < frac{max(a_4, a_1, a_2, a_3, a_4, a_1)}{2}$$$. Note that both track $$$1$$$ and track $$$4$$$ are counted twice towards the result.
2,000
false
false
true
false
true
false
false
true
false
false
4,508
1355A
Let's define the following recurrence: $$$$$$a_{n+1} = a_{n} + minDigit(a_{n}) cdot maxDigit(a_{n}).$$$$$$ Here $$$minDigit(x)$$$ and $$$maxDigit(x)$$$ are the minimal and maximal digits in the decimal representation of $$$x$$$ without leading zeroes. For examples refer to notes. Your task is calculate $$$a_{K}$$$ for given $$$a_{1}$$$ and $$$K$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of independent test cases. Each test case consists of a single line containing two integers $$$a_{1}$$$ and $$$K$$$ ($$$1 le a_{1} le 10^{18}$$$, $$$1 le K le 10^{16}$$$) separated by a space. Note $$$a_{1} = 487$$$ $$$a_{2} = a_{1} + minDigit(a_{1}) cdot maxDigit(a_{1}) = 487 + min (4, 8, 7) cdot max (4, 8, 7) = 487 + 4 cdot 8 = 519$$$ $$$a_{3} = a_{2} + minDigit(a_{2}) cdot maxDigit(a_{2}) = 519 + min (5, 1, 9) cdot max (5, 1, 9) = 519 + 1 cdot 9 = 528$$$ $$$a_{4} = a_{3} + minDigit(a_{3}) cdot maxDigit(a_{3}) = 528 + min (5, 2, 8) cdot max (5, 2, 8) = 528 + 2 cdot 8 = 544$$$ $$$a_{5} = a_{4} + minDigit(a_{4}) cdot maxDigit(a_{4}) = 544 + min (5, 4, 4) cdot max (5, 4, 4) = 544 + 4 cdot 5 = 564$$$ $$$a_{6} = a_{5} + minDigit(a_{5}) cdot maxDigit(a_{5}) = 564 + min (5, 6, 4) cdot max (5, 6, 4) = 564 + 4 cdot 6 = 588$$$ $$$a_{7} = a_{6} + minDigit(a_{6}) cdot maxDigit(a_{6}) = 588 + min (5, 8, 8) cdot max (5, 8, 8) = 588 + 5 cdot 8 = 628$$$
1,200
true
false
true
false
false
false
true
false
false
false
3,931
283A
Bessie and the cows are playing with sequences and need your help. They start with a sequence, initially containing just the number 0, and perform _n_ operations. Each operation is one of the following: 1. Add the integer _x__i_ to the first _a__i_ elements of the sequence. 2. Append an integer _k__i_ to the end of the sequence. (And hence the size of the sequence increases by 1) 3. Remove the last element of the sequence. So, the size of the sequence decreases by one. Note, that this operation can only be done if there are at least two elements in the sequence. After each operation, the cows would like to know the average of all the numbers in the sequence. Help them! Input The first line contains a single integer _n_xa0(1u2009≤u2009_n_u2009≤u20092·105) — the number of operations. The next _n_ lines describe the operations. Each line will start with an integer _t__i_ (1u2009≤u2009_t__i_u2009≤u20093), denoting the type of the operation (see above). If _t__i_u2009=u20091, it will be followed by two integers _a__i_,u2009_x__i_ (_x__i_u2009≤u2009103;xa01u2009≤u2009_a__i_). If _t__i_u2009=u20092, it will be followed by a single integer _k__i_ (_k__i_u2009≤u2009103). If _t__i_u2009=u20093, it will not be followed by anything. It is guaranteed that all operations are correct (don't touch nonexistent elements) and that there will always be at least one element in the sequence. Output Output _n_ lines each containing the average of the numbers in the sequence after the corresponding operation. The answer will be considered correct if its absolute or relative error doesn't exceed 10u2009-u20096. Examples Output 0.500000 0.000000 1.500000 1.333333 1.500000 Input 6 2 1 1 2 20 2 2 1 2 -3 3 3 Output 0.500000 20.500000 14.333333 12.333333 17.500000 17.000000 Note In the second sample, the sequence becomes
1,600
false
false
true
false
true
true
false
false
false
false
8,703
793G
Oleg the bank client solves an interesting chess problem: place on _n_u2009×u2009_n_ chessboard the maximum number of rooks so that they don't beat each other. Of course, no two rooks can share the same cell. Remind that a rook standing in the cell (_a_,u2009_b_) beats a rook standing in the cell (_x_,u2009_y_) if and only if _a_u2009=u2009_x_ or _b_u2009=u2009_y_. Unfortunately (of fortunately?) for Oleg the answer in this problem was always _n_, so the task bored Oleg soon. He decided to make it more difficult by removing some cells from the board. If a cell is deleted, Oleg can't put a rook there, but rooks do beat each other "through" deleted cells. Oleg deletes the cells in groups, namely, he repeatedly choose a rectangle with sides parallel to the board sides and deletes all the cells inside the rectangle. Formally, if he chooses a rectangle, lower left cell of which has coordinates (_x_1,u2009_y_1), and upper right cell of which has coordinates (_x_2,u2009_y_2), then he deletes all such cells with coordinates (_x_,u2009_y_) that _x_1u2009≤u2009_x_u2009≤u2009_x_2 and _y_1u2009≤u2009_y_u2009≤u2009_y_2. It is guaranteed that no cell is deleted twice, i.e. the chosen rectangles do not intersect. This version of the problem Oleg can't solve, and his friend Igor is busy at a conference, so he can't help Oleg. You are the last hope for Oleg! Help him: given the size of the board and the deleted rectangles find the maximum possible number of rooks that could be placed on the board so that no two rooks beat each other. Input The first line contains single integer _n_ (1u2009u2009≤u2009u2009_n_u2009≤u2009u200910000)xa0— the size of the board. The second line contains single integer _q_ (0u2009u2009≤u2009u2009_q_u2009u2009≤u2009u200910000)xa0— the number of deleted rectangles. The next _q_ lines contain the information about the deleted rectangles. Each of these lines contains four integers _x_1, _y_1, _x_2 and _y_2 (1u2009u2009≤u2009_x_1u2009≤u2009_x_2u2009≤u2009_n_, 1u2009u2009≤u2009_y_1u2009≤u2009_y_2u2009≤u2009_n_)xa0— the coordinates of the lower left and the upper right cells of a deleted rectangle. If is guaranteed that the rectangles do not intersect.
3,400
false
false
false
false
true
false
false
false
false
false
6,601
336C
Vasily the bear has got a sequence of positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers _b_1,u2009_b_2,u2009...,u2009_b__k_ is such maximum non-negative integer _v_, that number _b_1 _and_ _b_2 _and_ ... _and_ _b__k_ is divisible by number 2_v_ without a remainder. If such number _v_ doesn't exist (that is, for any non-negative integer _v_, number _b_1 _and_ _b_2 _and_ ... _and_ _b__k_ is divisible by 2_v_ without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible. Here expression _x_ _and_ _y_ means applying the bitwise AND operation to numbers _x_ and _y_. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and". Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_u2009≤u2009109). Output In the first line print a single integer _k_ (_k_u2009>u20090), showing how many numbers to write out. In the second line print _k_ integers _b_1,u2009_b_2,u2009...,u2009_b__k_ — the numbers to write out. You are allowed to print numbers _b_1,u2009_b_2,u2009...,u2009_b__k_ in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them.
1,800
false
true
true
false
false
false
true
false
false
false
8,490
233B
Problem - 233B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search brute force math *1400 No tag edit access → Contest materials ·_x_u2009-u2009_n_u2009=u20090,u2009 where _x_,u2009_n_ are positive integers, _s_(_x_) is the function, equal to the sum of digits of number _x_ in the decimal number system. You are given an integer _n_, find the smallest positive integer root of equation _x_, or else determine that there are no such roots. Input A single line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091018) — the equation parameter. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. Output Print -1, if the equation doesn't have integer positive roots. Otherwise print such smallest integer _x_ (_x_u2009>u20090), that the equation given in the statement holds. Examples Input 2 Output 1 Input 110 Output 10 Input 4 Output -1 Note In the first test case _x_u2009=u20091 is the minimum root. As _s_(1)u2009=u20091 and 12u2009+u20091·1u2009-u20092u2009=u20090. In the second test case _x_u2009=u200910 is the minimum root. As _s_(10)u2009=u20091u2009+u20090u2009=u20091 and 102u2009+u20091·10u2009-u2009110u2009=u20090. In the third test case the equation has no roots.
1,400
true
false
false
false
false
false
true
true
false
false
8,905
272D
Problem - 272D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics math sortings *1600 No tag edit access → Contest materials ,u2009(_a_2,u20092),u2009...,u2009(_a__n_,u2009_n_) and sequence (_b_1,u20091),u2009(_b_2,u20092),u2009...,u2009(_b__n_,u2009_n_). Now Dima wants to count the number of distinct sequences of points of length 2·_n_ that can be assembled from these sequences, such that the _x_-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (_p_1,u2009_q_1),u2009(_p_2,u2009_q_2),u2009...,u2009(_p_2·_n_,u2009_q_2·_n_) and (_x_1,u2009_y_1),u2009(_x_2,u2009_y_2),u2009...,u2009(_x_2·_n_,u2009_y_2·_n_) distinct, if there is such _i_ (1u2009≤u2009_i_u2009≤u20092·_n_), that (_p__i_,u2009_q__i_)u2009≠u2009(_x__i_,u2009_y__i_). As the answer can be rather large, print the remainder from dividing the answer by number _m_. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). The third line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u2009109). The numbers in the lines are separated by spaces. The last line contains integer _m_ (2u2009≤u2009_m_u2009≤u2009109u2009+u20097). Output In the single line print the remainder after dividing the answer to the problem by number _m_. Examples Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 Note In the first sample you can get only one sequence: (1,u20091),u2009(2,u20091). In the second sample you can get such sequences : (1,u20091),u2009(2,u20092),u2009(2,u20091),u2009(3,u20092); (1,u20091),u2009(2,u20091),u2009(2,u20092),u2009(3,u20092). Thus, the answer is 2.
1,600
true
false
false
false
false
false
false
false
true
false
8,743
1500F
In the house where Krosh used to live, he had $$$n$$$ cupboards standing in a line, the $$$i$$$-th cupboard had the height of $$$h_i$$$. Krosh moved recently, but he wasn't able to move the cupboards with him. Now he wants to buy $$$n$$$ new cupboards so that they look as similar to old ones as possible. Krosh does not remember the exact heights of the cupboards, but for every three consecutive cupboards he remembers the height difference between the tallest and the shortest of them. In other words, if the cupboards' heights were $$$h_1, h_2, ldots, h_n$$$, then Krosh remembers the values $$$w_i = max(h_{i}, h_{i + 1}, h_{i + 2}) - min(h_{i}, h_{i + 1}, h_{i + 2})$$$ for all $$$1 leq i leq n - 2$$$. Krosh wants to buy such $$$n$$$ cupboards that all the values $$$w_i$$$ remain the same. Help him determine the required cupboards' heights, or determine that he remembers something incorrectly and there is no suitable sequence of heights. Input The first line contains two integers $$$n$$$ and $$$C$$$ ($$$3 leq n leq 10^6$$$, $$$0 leq C leq 10^{12}$$$)xa0— the number of cupboards and the limit on possible $$$w_i$$$. The second line contains $$$n - 2$$$ integers $$$w_1, w_2, ldots, w_{n - 2}$$$ ($$$0 leq w_i leq C$$$)xa0— the values defined in the statement. Output If there is no suitable sequence of $$$n$$$ cupboards, print "NO". Otherwise print "YES" in the first line, then in the second line print $$$n$$$ integers $$$h'_1, h'_2, ldots, h'_n$$$ ($$$0 le h'_i le 10^{18}$$$) — the heights of the cupboards to buy, from left to right. We can show that if there is a solution, there is also a solution satisfying the constraints on heights. If there are multiple answers, print any. Examples Output YES 4 8 8 16 20 4 0 Input 11 10 5 7 2 3 4 5 2 1 8 Output YES 1 1 6 8 6 5 2 0 0 1 8 Note Consider the first example: $$$w_1 = max(4, 8, 8) - min(4, 8, 8) = 8 - 4 = 4$$$ $$$w_2 = max(8, 8, 16) - min(8, 8, 16) = 16 - 8 = 8$$$ $$$w_3 = max(8, 16, 20) - min(8, 16, 20) = 20 - 8 = 12$$$ $$$w_4 = max(16, 20, 4) - min(16, 20, 4) = 20 - 4 = 16$$$ $$$w_5 = max(20, 4, 0) - min(20, 4, 0) = 20 - 0 = 20$$$ There are other possible solutions, for example, the following: $$$0, 1, 4, 9, 16, 25, 36$$$.
3,500
false
false
false
true
false
false
false
false
false
false
3,163
1593A
The elections in which three candidates participated have recently ended. The first candidate received $$$a$$$ votes, the second one received $$$b$$$ votes, the third one received $$$c$$$ votes. For each candidate, solve the following problem: how many votes should be added to this candidate so that he wins the election (i.e. the number of votes for this candidate was strictly greater than the number of votes for any other candidate)? Please note that for each candidate it is necessary to solve this problem independently, i.e. the added votes for any candidate do not affect the calculations when getting the answer for the other two candidates. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing three integers $$$a$$$, $$$b$$$, and $$$c$$$ ($$$0 le a,b,c le 10^9$$$). Output For each test case, output in a separate line three integers $$$A$$$, $$$B$$$, and $$$C$$$ ($$$A, B, C ge 0$$$) separated by spaces — the answers to the problem for the first, second, and third candidate, respectively. Example Input 5 0 0 0 10 75 15 13 13 17 1000 0 0 0 1000000000 0 Output 1 1 1 66 0 61 5 5 0 0 1001 1001 1000000001 0 1000000001
800
true
false
false
false
false
false
false
false
false
false
2,686
864D
Ivan has an array consisting of _n_ elements. Each of the elements is an integer from 1 to _n_. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array in such a way that his array becomes a permutation (i.e. each of the integers from 1 to _n_ was encountered in his array exactly once). If there are multiple ways to do it he wants to find the lexicographically minimal permutation among them. Thus minimizing the number of changes has the first priority, lexicographical minimizing has the second priority. In order to determine which of the two permutations is lexicographically smaller, we compare their first elements. If they are equal — compare the second, and so on. If we have two permutations _x_ and _y_, then _x_ is lexicographically smaller if _x__i_u2009<u2009_y__i_, where _i_ is the first index in which the permutations _x_ and _y_ differ. Determine the array Ivan will obtain after performing all the changes. Input The first line contains an single integer _n_ (2u2009≤u2009_n_u2009≤u2009200u2009000) — the number of elements in Ivan's array. The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_) — the description of Ivan's array. Output In the first line print _q_ — the minimum number of elements that need to be changed in Ivan's array in order to make his array a permutation. In the second line, print the lexicographically minimal permutation which can be obtained from array with _q_ changes. Examples Input 10 6 8 4 6 7 1 6 3 4 5 Output 3 2 8 4 6 7 1 9 3 10 5 Note In the first example Ivan needs to replace number three in position 1 with number one, and number two in position 3 with number four. Then he will get a permutation [1, 2, 4, 3] with only two changed numbers — this permutation is lexicographically minimal among all suitable. In the second example Ivan does not need to change anything because his array already is a permutation.
1,500
true
true
true
false
false
false
false
false
false
false
6,284
1860C
Alice and Bob are playing a game. They have a permutation $$$p$$$ of size $$$n$$$ (a permutation of size $$$n$$$ is an array of size $$$n$$$ where each element from $$$1$$$ to $$$n$$$ occurs exactly once). They also have a chip, which can be placed on any element of the permutation. Alice and Bob make alternating moves: Alice makes the first move, then Bob makes the second move, then Alice makes the third move, and so on. During the first move, Alice chooses any element of the permutation and places the chip on that element. During each of the next moves, the current player has to move the chip to any element that is simultaneously to the left and strictly less than the current element (i.u2009e. if the chip is on the $$$i$$$-th element, it can be moved to the $$$j$$$-th element if $$$j < i$$$ and $$$p_j < p_i$$$). If a player cannot make a move (it is impossible to move the chip according to the rules of the game), that player wins the game. Let's say that the $$$i$$$-th element of the permutation is lucky if the following condition holds: if Alice places the chip on the $$$i$$$-th element during her first move, she can win the game no matter how Bob plays (i.u2009e. she has a winning strategy). You have to calculate the number of lucky elements in the permutation. 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$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0– the number of elements in the permutation. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$). All $$$p_i$$$ are distinct. The sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print a single integerxa0— the number of lucky elements in the permutation. Example Input 4 3 2 1 3 2 2 1 3 1 2 3 4 2 1 4 3 Note In the first test case of the example, the $$$3$$$-rd element of the permutation is lucky. In the second test case of the example, there are no lucky elements. In the third test case of the example, the $$$2$$$-nd element of the permutation is lucky. In the fourth test case of the example, the $$$3$$$-rd and the $$$4$$$-th element of the permutation are lucky.
1,400
false
true
false
true
true
false
false
false
false
false
1,123
1702E
Polycarp was recently given a set of $$$n$$$ (number $$$n$$$xa0— even) dominoes. Each domino contains two integers from $$$1$$$ to $$$n$$$. Can he divide all the dominoes into two sets so that all the numbers on the dominoes of each set are different? Each domino must go into exactly one of the two sets. For example, if he has $$$4$$$ dominoes: $$${1, 4}$$$, $$${1, 3}$$$, $$${3, 2}$$$ and $$${4, 2}$$$, then Polycarp will be able to divide them into two sets in the required way. The first set can include the first and third dominoes ($$${1, 4}$$$ and $$${3, 2}$$$), and the second setxa0— the second and fourth ones ($$${1, 3}$$$ and $$${4, 2}$$$). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case contains a single even integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of dominoes. The next $$$n$$$ lines contain pairs of numbers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$) describing the numbers on the $$$i$$$-th domino. 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: YES, if it is possible to divide $$$n$$$ dominoes into two sets so that the numbers on the dominoes of each set are different; NO if this is not possible. You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer). Example Input 6 4 1 2 4 3 2 1 3 4 6 1 2 4 5 1 3 4 6 2 3 5 6 2 1 1 2 2 2 1 2 2 1 8 2 1 1 2 4 3 4 3 5 6 5 7 8 6 7 8 8 1 2 2 1 4 3 5 3 5 4 6 7 8 6 7 8 Output YES NO NO YES YES NO Note In the first test case, the dominoes can be divided as follows: First set of dominoes: $$$[{1, 2}, {4, 3}]$$$ Second set of dominoes: $$$[{2, 1}, {3, 4}]$$$ In other words, in the first set we take dominoes with numbers $$$1$$$ and $$$2$$$, and in the second set we take dominoes with numbers $$$3$$$ and $$$4$$$. In the second test case, there's no way to divide dominoes into $$$2$$$ sets, at least one of them will contain repeated number.
1,600
false
false
false
false
false
false
false
false
false
true
2,066
1592F2
The difference between the versions is in the costs of operations. Solution for one version won't work for another! Alice has a grid of size $$$n imes m$$$, initially all its cells are colored white. The cell on the intersection of $$$i$$$-th row and $$$j$$$-th column is denoted as $$$(i, j)$$$. Alice can do the following operations with this grid: Choose any subrectangle containing cell $$$(1, 1)$$$, and flip the colors of all its cells. (Flipping means changing its color from white to black or from black to white). This operation costs $$$1$$$ coin. Choose any subrectangle containing cell $$$(n, 1)$$$, and flip the colors of all its cells. This operation costs $$$3$$$ coins. Choose any subrectangle containing cell $$$(1, m)$$$, and flip the colors of all its cells. This operation costs $$$4$$$ coins. Choose any subrectangle containing cell $$$(n, m)$$$, and flip the colors of all its cells. This operation costs $$$2$$$ coins. As a reminder, subrectangle is a set of all cells $$$(x, y)$$$ with $$$x_1 le x le x_2$$$, $$$y_1 le y le y_2$$$ for some $$$1 le x_1 le x_2 le n$$$, $$$1 le y_1 le y_2 le m$$$. Alice wants to obtain her favorite coloring with these operations. What's the smallest number of coins that she would have to spend? It can be shown that it's always possible to transform the initial grid into any other. Input The first line of the input contains $$$2$$$ integers $$$n, m$$$ ($$$1 le n, m le 500$$$) — the dimensions of the grid. The $$$i$$$-th of the next $$$n$$$ lines contains a string $$$s_i$$$ of length $$$m$$$, consisting of letters W and B. The $$$j$$$-th character of string $$$s_i$$$ is W if the cell $$$(i, j)$$$ is colored white in the favorite coloring of Alice, and B if it's colored black. Output Output the smallest number of coins Alice would have to spend to achieve her favorite coloring. Examples Input 10 15 WWWBBBWBBBBBWWW BBBBWWWBBWWWBBB BBBWWBWBBBWWWBB BBWBWBBWWWBBWBW BBBBWWWBBBWWWBB BWBBWWBBBBBBWWW WBWWBBBBWWBBBWW WWBWWWWBBWWBWWW BWBWWBWWWWWWBWB BBBWBWBWBBBWWBW Note In the first sample, it's optimal to just apply the fourth operation once to the rectangle containing cells $$$(2, 2), (2, 3), (3, 2), (3, 3)$$$. This would cost $$$2$$$ coins.
2,800
false
true
false
false
false
true
false
false
false
false
2,687
461A
Appleman and Toastman play a game. Initially Appleman gives one group of _n_ numbers to the Toastman, then they start to complete the following tasks: Each time Toastman gets a group of numbers, he sums up all the numbers and adds this sum to the score. Then he gives the group to the Appleman. Each time Appleman gets a group consisting of a single number, he throws this group out. Each time Appleman gets a group consisting of more than one number, he splits the group into two non-empty groups (he can do it in any way) and gives each of them to Toastman. After guys complete all the tasks they look at the score value. What is the maximum possible value of score they can get? Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20093·105). The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009106) — the initial group that is given to Toastman. Output Print a single integer — the largest possible score. Note Consider the following situation in the first example. Initially Toastman gets group [3, 1, 5] and adds 9 to the score, then he give the group to Appleman. Appleman splits group [3, 1, 5] into two groups: [3, 5] and [1]. Both of them should be given to Toastman. When Toastman receives group [1], he adds 1 to score and gives the group to Appleman (he will throw it out). When Toastman receives group [3, 5], he adds 8 to the score and gives the group to Appleman. Appleman splits [3, 5] in the only possible way: [5] and [3]. Then he gives both groups to Toastman. When Toastman receives [5], he adds 5 to the score and gives the group to Appleman (he will throws it out). When Toastman receives [3], he adds 3 to the score and gives the group to Appleman (he will throws it out). Finally Toastman have added 9 + 1 + 8 + 5 + 3 = 26 to the score. This is the optimal sequence of actions.
1,200
false
true
false
false
false
false
false
false
true
false
7,996
682A
Problem - 682A - 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 math number theory *1100 No tag edit access → Contest materials ") such that 1u2009≤u2009_x_u2009≤u2009_n_, 1u2009≤u2009_y_u2009≤u2009_m_ and equals 0. As usual, Alyona has some troubles and asks you to help. Input The only line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091u2009000u2009000). Output Print the only integerxa0— the number of pairs of integers (_x_,u2009_y_) such that 1u2009≤u2009_x_u2009≤u2009_n_, 1u2009≤u2009_y_u2009≤u2009_m_ and (_x_u2009+u2009_y_) is divisible by 5. Examples Input 6 12 Output 14 Input 11 14 Output 31 Input 1 5 Output 1 Input 3 8 Output 5 Input 5 7 Output 7 Input 21 21 Output 88 Note Following pairs are suitable in the first sample case: for _x_u2009=u20091 fits _y_ equal to 4 or 9; for _x_u2009=u20092 fits _y_ equal to 3 or 8; for _x_u2009=u20093 fits _y_ equal to 2, 7 or 12; for _x_u2009=u20094 fits _y_ equal to 1, 6 or 11; for _x_u2009=u20095 fits _y_ equal to 5 or 10; for _x_u2009=u20096 fits _y_ equal to 4 or 9. Only the pair (1,u20094) is suitable in the third sample case.
1,100
true
false
false
false
false
true
false
false
false
false
7,098
2001E1
This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $$$n$$$ and $$$t$$$. You can make hacks only if both versions of the problem are solved. Consider a perfect binary tree with size $$$2^n - 1$$$, with nodes numbered from $$$1$$$ to $$$2^n-1$$$ and rooted at $$$1$$$. For each vertex $$$v$$$ ($$$1 le v le 2^{n - 1} - 1$$$), vertex $$$2v$$$ is its left child and vertex $$$2v + 1$$$ is its right child. Each node $$$v$$$ also has a value $$$a_v$$$ assigned to it. Define the operation $$$mathrm{pop}$$$ as follows: 1. initialize variable $$$v$$$ as $$$1$$$; 2. repeat the following process until vertex $$$v$$$ is a leaf (i.e. until $$$2^{n - 1} le v le 2^n - 1$$$); 1. among the children of $$$v$$$, choose the one with the larger value on it and denote such vertex as $$$x$$$; if the values on them are equal (i.e. $$$a_{2v} = a_{2v + 1}$$$), you can choose any of them; 2. assign $$$a_x$$$ to $$$a_v$$$ (i.e. $$$a_v := a_x$$$); 3. assign $$$x$$$ to $$$v$$$ (i.e. $$$v := x$$$); 3. assign $$$-1$$$ to $$$a_v$$$ (i.e. $$$a_v := -1$$$). Then we say the $$$mathrm{pop}$$$ operation is deterministic if there is a unique way to do such operation. In other words, $$$a_{2v} eq a_{2v + 1}$$$ would hold whenever choosing between them. A binary tree is called a max-heap if for every vertex $$$v$$$ ($$$1 le v le 2^{n - 1} - 1$$$), both $$$a_v ge a_{2v}$$$ and $$$a_v ge a_{2v + 1}$$$ hold. A max-heap is deterministic if the $$$mathrm{pop}$$$ operation is deterministic to the heap when we do it for the first time. Initially, $$$a_v := 0$$$ for every vertex $$$v$$$ ($$$1 le v le 2^n - 1$$$), and your goal is to count the number of different deterministic max-heaps produced by applying the following operation $$$mathrm{add}$$$ exactly $$$k$$$ times: Choose an integer $$$v$$$ ($$$1 le v le 2^n - 1$$$) and, for every vertex $$$x$$$ on the path between $$$1$$$ and $$$v$$$, add $$$1$$$ to $$$a_x$$$. Two heaps are considered different if there is a node which has different values in the heaps. Since the answer might be large, print it modulo $$$p$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n, k, p$$$ ($$$1 le n, k le 500$$$, $$$10^8 le p le 10^9$$$, $$$p$$$ is a prime). It is guaranteed that the sum of $$$n$$$ and the sum of $$$k$$$ over all test cases does not exceed $$$500$$$. Output For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $$$mathrm{add}$$$ exactly $$$k$$$ times, modulo $$$p$$$. Examples Input 7 1 13 998244353 2 1 998244353 3 2 998244853 3 3 998244353 3 4 100000037 4 2 100000039 4 3 100000037 Output 1 2 12 52 124 32 304 Input 6 87 63 100000037 77 77 100000039 100 200 998244353 200 100 998244353 32 59 998244853 1 1 998244353 Output 26831232 94573603 37147649 847564946 727060898 1 Note For the first testcase, there is only one way to generate $$$a$$$, and such sequence is a deterministic max-heap, so the answer is $$$1$$$. For the second testcase, if we choose $$$v = 1$$$ and do the operation, we would have $$$a = [1, 0, 0]$$$, and since $$$a_2 = a_3$$$, we can choose either of them when doing the first $$$mathrm{pop}$$$ operation, so such heap is not a deterministic max-heap. And if we choose $$$v = 2$$$, we would have $$$a = [1, 1, 0]$$$, during the first $$$mathrm{pop}$$$, the following would happen: initialize $$$v$$$ as $$$1$$$ since $$$a_{2v} > a_{2v + 1}$$$, choose $$$2v$$$ as $$$x$$$, then $$$x = 2$$$ assign $$$a_x$$$ to $$$a_v$$$, then $$$a = [1, 1, 0]$$$ assign $$$x$$$ to $$$v$$$, then $$$v = 2$$$ since $$$v$$$ is a leaf, assign $$$-1$$$ to $$$a_v$$$, then $$$a = [1, -1, 0]$$$ Since the first $$$mathrm{pop}$$$ operation is deterministic, this is a deterministic max-heap. Also, if we choose $$$v = 3$$$, $$$a$$$ would be a deterministic max-heap, so the answer is $$$2$$$.
2,400
true
false
false
true
false
false
false
false
false
false
251
1081B
Chouti and his classmates are going to the university soon. To say goodbye to each other, the class has planned a big farewell party in which classmates, teachers and parents sang and danced. Chouti remembered that $$$n$$$ persons took part in that party. To make the party funnier, each person wore one hat among $$$n$$$ kinds of weird hats numbered $$$1, 2, ldots n$$$. It is possible that several persons wore hats of the same kind. Some kinds of hats can remain unclaimed by anyone. After the party, the $$$i$$$-th person said that there were $$$a_i$$$ persons wearing a hat differing from his own. It has been some days, so Chouti forgot all about others' hats, but he is curious about that. Let $$$b_i$$$ be the number of hat type the $$$i$$$-th person was wearing, Chouti wants you to find any possible $$$b_1, b_2, ldots, b_n$$$ that doesn't contradict with any person's statement. Because some persons might have a poor memory, there could be no solution at all. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$), the number of persons in the party. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le n-1$$$), the statements of people. Output If there is no solution, print a single line "Impossible". Otherwise, print "Possible" and then $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$). If there are multiple answers, print any of them. Note In the answer to the first example, all hats are the same, so every person will say that there were no persons wearing a hat different from kind $$$1$$$. In the answer to the second example, the first and the second person wore the hat with type $$$1$$$ and all other wore a hat of type $$$2$$$. So the first two persons will say there were three persons with hats differing from their own. Similarly, three last persons will say there were two persons wearing a hat different from their own. In the third example, it can be shown that no solution exists. In the first and the second example, other possible configurations are possible.
1,500
false
false
true
false
false
true
false
false
false
false
5,322
698D
Bearland is a dangerous place. Limak can’t travel on foot. Instead, he has _k_ magic teleportation stones. Each stone can be used at most once. The _i_-th stone allows to teleport to a point (_ax__i_,u2009_ay__i_). Limak can use stones in any order. There are _n_ monsters in Bearland. The _i_-th of them stands at (_mx__i_,u2009_my__i_). The given _k_u2009+u2009_n_ points are pairwise distinct. After each teleportation, Limak can shoot an arrow in some direction. An arrow will hit the first monster in the chosen direction. Then, both an arrow and a monster disappear. It’s dangerous to stay in one place for long, so Limak can shoot only one arrow from one place. A monster should be afraid if it’s possible that Limak will hit it. How many monsters should be afraid of Limak? Input The first line of the input contains two integers _k_ and _n_ (1u2009≤u2009_k_u2009≤u20097, 1u2009≤u2009_n_u2009≤u20091000)xa0— the number of stones and the number of monsters. The _i_-th of following _k_ lines contains two integers _ax__i_ and _ay__i_ (u2009-u2009109u2009≤u2009_ax__i_,u2009_ay__i_u2009≤u2009109)xa0— coordinates to which Limak can teleport using the _i_-th stone. The _i_-th of last _n_ lines contains two integers _mx__i_ and _my__i_ (u2009-u2009109u2009≤u2009_mx__i_,u2009_my__i_u2009≤u2009109)xa0— coordinates of the _i_-th monster. The given _k_u2009+u2009_n_ points are pairwise distinct. Output Print the number of monsters which should be afraid of Limak. Examples Input 2 4 -2 -1 4 5 4 2 2 1 4 -1 1 -1 Input 3 8 10 20 0 0 20 40 300 600 30 60 170 340 50 100 28 56 90 180 -4 -8 -1 -2 Note In the first sample, there are two stones and four monsters. Stones allow to teleport to points (u2009-u20092,u2009u2009-u20091) and (4,u20095), marked blue in the drawing below. Monsters are at (4,u20092), (2,u20091), (4,u2009u2009-u20091) and (1,u2009u2009-u20091), marked red. A monster at (4,u2009u2009-u20091) shouldn't be afraid because it's impossible that Limak will hit it with an arrow. Other three monsters can be hit and thus the answer is 3. In the second sample, five monsters should be afraid. Safe monsters are those at (300,u2009600), (170,u2009340) and (90,u2009180).
2,600
true
false
false
false
false
false
true
false
false
false
7,030
1845F
There is a pool of length $$$l$$$ where $$$n$$$ swimmers plan to swim. People start swimming at the same time (at the time moment $$$0$$$), but you can assume that they take different lanes, so they don't interfere with each other. Each person swims along the following route: they start at point $$$0$$$ and swim to point $$$l$$$ with constant speed (which is equal to $$$v_i$$$ units per second for the $$$i$$$-th swimmer). After reaching the point $$$l$$$, the swimmer instantly (in negligible time) turns back and starts swimming to the point $$$0$$$ with the same constant speed. After returning to the point $$$0$$$, the swimmer starts swimming to the point $$$l$$$, and so on. Let's say that some real moment of time is a meeting moment if there are at least two swimmers that are in the same point of the pool at that moment of time (that point may be $$$0$$$ or $$$l$$$ as well as any other real point inside the pool). The pool will be open for $$$t$$$ seconds. You have to calculate the number of meeting moments while the pool is open. Since the answer may be very large, print it modulo $$$10^9 + 7$$$. Input The first line contains two integers $$$l$$$ and $$$t$$$ ($$$1 le l, t le 10^9$$$)xa0— the length of the pool and the duration of the process (in seconds). The second line contains the single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of swimmers. The third line contains $$$n$$$ integers $$$v_1, v_2, dots, v_n$$$ ($$$1 le v_i le 2 cdot 10^5$$$), where $$$v_i$$$ is the speed of the $$$i$$$-th swimmer. All $$$v_i$$$ are pairwise distinct. Output Print one integerxa0— the number of meeting moments (including moment $$$t$$$ if needed and excluding moment $$$0$$$), taken modulo $$$10^9 + 7$$$. Examples Input 1 1000000000 3 100000 150000 200000 Note In the first example, there are three meeting moments: moment $$$6$$$, during which both swimmers are in the point $$$6$$$; moment $$$12$$$, during which both swimmers are in the point $$$6$$$; and moment $$$18$$$, during which both swimmers are in the point $$$0$$$.
2,800
true
false
false
true
false
false
false
false
false
false
1,209
837B
The flag of Berland is such rectangular field _n_u2009×u2009_m_ that satisfies following conditions: Flag consists of three colors which correspond to letters 'R', 'G' and 'B'. Flag consists of three equal in width and height stripes, parralel to each other and to sides of the flag. Each stripe has exactly one color. Each color should be used in exactly one stripe. You are given a field _n_u2009×u2009_m_, consisting of characters 'R', 'G' and 'B'. Output "YES" (without quotes) if this field corresponds to correct flag of Berland. Otherwise, print "NO" (without quotes). Input The first line contains two integer numbers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the sizes of the field. Each of the following _n_ lines consisting of _m_ characters 'R', 'G' and 'B' — the description of the field. Output Print "YES" (without quotes) if the given field corresponds to correct flag of Berland . Otherwise, print "NO" (without quotes). Examples Input 6 5 RRRRR RRRRR BBBBB BBBBB GGGGG GGGGG Input 6 7 RRRGGGG RRRGGGG RRRGGGG RRRBBBB RRRBBBB RRRBBBB Note The field in the third example doesn't have three parralel stripes. Rows of the field in the fourth example are parralel to each other and to borders. But they have different heights — 2, 1 and 1.
1,600
false
false
true
false
false
false
true
false
false
false
6,420
216A
Problem - 216A - 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 math *1200 No tag edit access → Contest materials . Output Print a single number — the total number of tiles on the hall floor. Examples Input 2 3 4 Output 18
1,200
true
false
true
false
false
false
false
false
false
false
8,976
1217E
Let's define a balanced multiset the following way. Write down the sum of all elements of the multiset in its decimal representation. For each position of that number check if the multiset includes at least one element such that the digit of the element and the digit of the sum at that position are the same. If that holds for every position, then the multiset is balanced. Otherwise it's unbalanced. For example, multiset $$${20, 300, 10001}$$$ is balanced and multiset $$${20, 310, 10001}$$$ is unbalanced: The red digits mark the elements and the positions for which these elements have the same digit as the sum. The sum of the first multiset is $$$10321$$$, every position has the digit required. The sum of the second multiset is $$$10331$$$ and the second-to-last digit doesn't appear in any number, thus making the multiset unbalanced. You are given an array $$$a_1, a_2, dots, a_n$$$, consisting of $$$n$$$ integers. You are asked to perform some queries on it. The queries can be of two types: $$$1~i~x$$$ — replace $$$a_i$$$ with the value $$$x$$$; $$$2~l~r$$$ — find the unbalanced subset of the multiset of the numbers $$$a_l, a_{l + 1}, dots, a_r$$$ with the minimum sum, or report that no unbalanced subset exists. Note that the empty multiset is balanced. For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the number of elements in the array and the number of queries, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i < 10^9$$$). Each of the following $$$m$$$ lines contains a query of one of two types: $$$1~i~x$$$ ($$$1 le i le n$$$, $$$1 le x < 10^9$$$) — replace $$$a_i$$$ with the value $$$x$$$; $$$2~l~r$$$ ($$$1 le l le r le n$$$) — find the unbalanced subset of the multiset of the numbers $$$a_l, a_{l + 1}, dots, a_r$$$ with the lowest sum, or report that no unbalanced subset exists. It is guaranteed that there is at least one query of the second type. Output For each query of the second type print the lowest sum of the unbalanced subset. Print -1 if no unbalanced subset exists. Example Input 4 5 300 10001 20 20 2 1 3 1 1 310 2 1 3 2 3 3 2 3 4 Note All the subsets of multiset $$${20, 300, 10001}$$$ are balanced, thus the answer is -1. The possible unbalanced subsets in the third query are $$${20, 310}$$$ and $$${20, 310, 10001}$$$. The lowest sum one is $$${20, 310}$$$. Note that you are asked to choose a subset, not a subsegment, thus the chosen elements might not be adjancent in the array. The fourth query includes only the empty subset and subset $$${20}$$$. Both of them are balanced. The last query includes the empty subset and the subsets $$${20}$$$, $$${20}$$$ and $$${20, 20}$$$. Only $$${20, 20}$$$ is unbalanced, its sum is $$$40$$$. Note that you are asked to choose a multiset, thus it might include equal elements.
2,300
true
true
true
false
true
false
false
false
false
false
4,586
311D
While learning Computational Geometry, Tiny is simultaneously learning a useful data structure called segment tree or interval tree. He has scarcely grasped it when comes out a strange problem: Given an integer sequence _a_1,u2009_a_2,u2009...,u2009_a__n_. You should run _q_ queries of two types: 1. Given two integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_), ask the sum of all elements in the sequence _a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_. 2. Given two integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_), let each element _x_ in the sequence _a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_ becomes _x_3. In other words, apply an assignments _a__l_u2009=u2009_a__l_3,u2009_a__l_u2009+u20091u2009=u2009_a__l_u2009+u200913,u2009...,u2009_a__r_u2009=u2009_a__r_3. For every query of type 1, output the answer to it. Tiny himself surely cannot work it out, so he asks you for help. In addition, Tiny is a prime lover. He tells you that because the answer may be too huge, you should only output it modulo 95542721 (this number is a prime number). Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105), representing the length of the sequence. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109). The third line contains an integer _q_ (1u2009≤u2009_q_u2009≤u2009105), representing the number of queries. Then follow _q_ lines. Each line contains three integers _t__i_ (1u2009≤u2009_t__i_u2009≤u20092), _l__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_), where _t__i_ stands for the type of the query while _l__i_ and _r__i_ is the parameters of the query, correspondingly. Output For each 1-type query, print the answer to it per line. You should notice that each printed number should be non-negative and less than 95542721. Examples Input 8 1 2 3 4 5 6 7 8 5 1 2 5 2 2 5 1 2 5 2 3 6 1 4 7
2,600
true
false
false
false
true
false
false
false
false
false
8,599
6D
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced. Polycarp likes to play computer role-playing game «Lizards and Basements». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the _i_-th archer with his fire ball (they are numbered from left to right), the archer loses _a_ health points. At the same time the spell damages the archers adjacent to the _i_-th (if any) — they lose _b_ (1u2009≤u2009_b_u2009<u2009_a_u2009≤u200910) health points each. As the extreme archers (i.e. archers numbered 1 and _n_) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball. The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies? Polycarp can throw his fire ball into an archer if the latter is already killed. Input The first line of the input contains three integers _n_,u2009_a_,u2009_b_ (3u2009≤u2009_n_u2009≤u200910; 1u2009≤u2009_b_u2009<u2009_a_u2009≤u200910). The second line contains a sequence of _n_ integers — _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≤u2009_h__i_u2009≤u200915), where _h__i_ is the amount of health points the _i_-th archer has. Output In the first line print _t_ — the required minimum amount of fire balls. In the second line print _t_ numbers — indexes of the archers that Polycarp should hit to kill all the archers in _t_ shots. All these numbers should be between 2 and _n_u2009-u20091. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order.
2,600
false
false
false
true
false
false
true
false
false
false
9,962
453B
Problem - 453B - 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 brute force dp *2000 No tag edit access → Contest materials — the number of elements of the sequences _a_ and _b_. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u200930). Output Output the key — sequence _b__i_ that minimizes the sum described above. If there are multiple optimal sequences, you can output any of them. Examples Input 5 1 1 1 1 1 Output 1 1 1 1 1 Input 5 1 6 4 2 8 Output 1 5 3 1 8
2,000
false
false
false
true
false
false
true
false
false
false
8,025
417C
One day, at the "Russian Code Cup" event it was decided to play football as an out of competition event. All participants was divided into _n_ teams and played several matches, two teams could not play against each other more than once. The appointed Judge was the most experienced member — Pavel. But since he was the wisest of all, he soon got bored of the game and fell asleep. Waking up, he discovered that the tournament is over and the teams want to know the results of all the matches. Pavel didn't want anyone to discover about him sleeping and not keeping an eye on the results, so he decided to recover the results of all games. To do this, he asked all the teams and learned that the real winner was friendship, that is, each team beat the other teams exactly _k_ times. Help Pavel come up with chronology of the tournir that meets all the conditions, or otherwise report that there is no such table. Input The first line contains two integers — _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20091000). Output In the first line print an integer _m_ — number of the played games. The following _m_ lines should contain the information about all the matches, one match per line. The _i_-th line should contain two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_; _a__i_u2009≠u2009_b__i_). The numbers _a__i_ and _b__i_ mean, that in the _i_-th match the team with number _a__i_ won against the team with number _b__i_. You can assume, that the teams are numbered from 1 to _n_. If a tournir that meets the conditions of the problem does not exist, then print -1.
1,400
false
false
true
false
false
true
false
false
false
true
8,155
896E
— I... I survived. — Welcome home, Chtholly. — I kept my promise... — I made it... I really made it! After several days of fighting, Chtholly Nota Seniorious miraculously returned from the fierce battle. As promised, Willem is now baking butter cake for her. However, although Willem is skilled in making dessert, he rarely bakes butter cake. This time, Willem made a big mistake — he accidentally broke the oven! Fortunately, Chtholly decided to help him. Willem puts _n_ cakes on a roll, cakes are numbered from 1 to _n_, the _i_-th cake needs _a__i_ seconds of baking. Willem needs Chtholly to do _m_ operations to bake the cakes. Operation 1: 1 _l_ _r_ _x_ Willem asks Chtholly to check each cake in the range [_l_,u2009_r_], if the cake needs to be baked for more than _x_ seconds, he would bake it for _x_ seconds and put it back in its place. More precisely, for every _i_ in range [_l_,u2009_r_], if _a__i_ is strictly more than _x_, _a__i_ becomes equal _a__i_u2009-u2009_x_. Operation 2: 2 _l_ _r_ _x_ Willem asks Chtholly to count the number of cakes in the range [_l_,u2009_r_] that needs to be cooked for exactly _x_ seconds. More formally you should find number of such _i_ in range [_l_,u2009_r_], that _a__i_u2009=u2009_x_. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105). The second line contains _n_ integers, _i_-th of them is _a__i_ (1u2009≤u2009_a__i_u2009≤u2009105). The next _m_ lines are the _m_ operations described above. It is guaranteed that 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_ and 1u2009≤u2009_x_u2009≤u2009105.
3,100
false
false
false
false
true
false
false
false
false
false
6,161
173E
A club wants to take its members camping. In order to organize the event better the club directors decided to partition the members into several groups. Club member _i_ has a responsibility value _r__i_ and an age value _a__i_. A group is a non-empty subset of club members with one member known as group leader. A group leader should be one of the most responsible members of the group (his responsibility value is not less than responsibility of any other group member) and his age absolute difference with any other group member should not exceed _k_. Some club members are friends and want to be in the same group. They also like their group to be as large as possible. Now you should write a program that answers a series of questions like "What's the largest size of a group containing club member _x_ and club member _y_?". It's possible for _x_ or _y_ to be the group leader. Input The first line contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_k_u2009≤u2009109) — the number of club members and the age restriction for one group. The next line contains integer numbers _r_1,u2009_r_2,u2009...,u2009_r__n_ (1u2009≤u2009_r__i_u2009≤u2009109) separated by space: _r__i_ denotes the _i_-th club member's responsibility. In the same way there are integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) in the third line: _a__i_ denotes the _i_-th club member's age. The next line contains an integer _q_ denoting the number of questions that you should answer (1u2009≤u2009_q_u2009≤u2009105). The next _q_ lines describe the questions. Each line contains two space-separated integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_,u2009_x__i_u2009≠u2009_y__i_) — the indices of the club members that should end up in the same group. Output For each question print the maximum size of the group in a line. If making such a group is impossible print -1 instead. Examples Input 5 1 1 5 4 1 2 4 4 3 2 2 4 5 3 2 3 2 5 4 1 Note In the first query the largest group with members 3 and 5 is {1,u20093,u20094,u20095} where member 3 is the leader. In the second query member 2 should be the leader so the group will be {1,u20092,u20093}. In the third query the leader of the group should have age 3 so the only leader can be member 3, who is less responsible than member 2. So making a group is impossible. The group for the fourth query is the same as first query.
2,600
false
false
false
false
true
false
false
false
true
false
9,169
68C
For some experiments little Petya needs a synchrophasotron. He has already got the device, all that's left is to set the fuel supply. Fuel comes through a system of nodes numbered from 1 to _n_ and connected by pipes. Pipes go from every node with smaller number to every node with greater number. Fuel can only flow through pipes in direction from node with smaller number to node with greater number. Any amount of fuel can enter through the first node and the last node is connected directly to the synchrophasotron. It is known that every pipe has three attributes: the minimum amount of fuel that should go through it, the maximum amount of fuel that can possibly go through it and the cost of pipe activation. If _c__ij_ units of fuel (_c__ij_u2009>u20090) flow from node _i_ to node _j_, it will cost _a__ij_u2009+u2009_c__ij_2 tugriks (_a__ij_ is the cost of pipe activation), and if fuel doesn't flow through the pipe, it doesn't cost anything. Only integer number of units of fuel can flow through each pipe. Constraints on the minimal and the maximal fuel capacity of a pipe take place always, not only if it is active. You may assume that the pipe is active if and only if the flow through it is strictly greater than zero. Petya doesn't want the pipe system to be overloaded, so he wants to find the minimal amount of fuel, that, having entered the first node, can reach the synchrophasotron. Besides that he wants to impress the sponsors, so the sum of money needed to be paid for fuel to go through each pipe, must be as big as possible. Input First line contains integer _n_ (2u2009≤u2009_n_u2009≤u20096), which represents the number of nodes. Each of the next _n_(_n_u2009-u20091)u2009/u20092 lines contains five integers _s_,u2009_f_,u2009_l_,u2009_h_,u2009_a_ that describe pipes — the first node of the pipe, the second node of the pipe, the minimum and the maximum amount of fuel that can flow through the pipe and the the activation cost, respectively. (1u2009≤u2009_s_u2009<u2009_f_u2009≤u2009_n_,u20090u2009≤u2009_l_u2009≤u2009_h_u2009≤u20095,u20090u2009≤u2009_a_u2009≤u20096). It is guaranteed that for each pair of nodes with distinct numbers there will be exactly one pipe between them described in the input. Output Output in the first line two space-separated numbers: the minimum possible amount of fuel that can flow into the synchrophasotron, and the maximum possible sum that needs to be paid in order for that amount of fuel to reach synchrophasotron. If there is no amount of fuel that can reach synchrophasotron, output "-1 -1". The amount of fuel which will flow into synchrophasotron is not neccessary positive. It could be equal to zero if the minimum constraint of every pipe is equal to zero. Examples Input 3 1 2 1 2 3 1 3 0 0 0 2 3 3 4 5 Input 4 1 2 0 2 1 2 3 0 2 1 1 3 0 2 6 1 4 0 0 1 2 4 0 0 0 3 4 2 3 0 Input 3 1 2 0 2 1 1 3 1 2 1 2 3 1 2 1 Note In the first test, we can either pass 1 or 2 units of fuel from node 1 to node 2. The minimum possible amount is 1, it costs _a_12u2009+u200912u2009=u20094. In the second test, you can pass at most 2 units from node 1 to node 2, and at you have to pass at least 3 units from node 2 to node 3. It is impossible. In the third test, the minimum possible amount is 2. You can pass each unit of fuel through two different paths: either 1->2->3->4 or 1->3->4. If you use the first path twice, it will cost _a_12u2009+u200922u2009+u2009_a_23u2009+u200922u2009+u2009_a_34u2009+u200922=14. If you use the second path twice, it will cost _a_13u2009+u200922u2009+u2009_a_34u2009+u200922=14. However, if you use each path (allowing one unit of fuel go through pipes 1->2, 2->3, 1->3, and two units go through 3->4) it will cost _a_12u2009+u200912u2009+u2009_a_23u2009+u200912u2009+u2009_a_13u2009+u200912u2009+u2009_a_34u2009+u200922=15 and it is the maximum possible cost. Also note that since no fuel flows from node 1 to node 4, activation cost for that pipe is not added to the answer.
2,200
false
false
false
false
false
false
true
false
false
false
9,629
835B
Problem - 835B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy *1100 No tag edit access → Contest materials . Announcement") . The second line contains integer _n_ (1u2009≤u2009_n_u2009<u200910100000). There are no leading zeros in _n_. It's guaranteed that this situation is possible. Output Print the minimum number of digits in which the initial number and _n_ can differ. Examples Input 3 11 Output 1 Input 3 99 Output 0 Note In the first example, the initial number could be 12. In the second example the sum of the digits of _n_ is not less than _k_. The initial number could be equal to _n_.
1,100
false
true
false
false
false
false
false
false
false
false
6,426
1498E
This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: [ is a city in which Dixit lives. In the city, there are $$$n$$$ houses. There is exactly one directed road between every pair of houses. For example, consider two houses A and B, then there is a directed road either from A to B or from B to A but not both. The number of roads leading to the $$$i$$$-th house is $$$k_i$$$. Two houses A and B are bi-reachable if A is reachable from B and B is reachable from A. We say that house B is reachable from house A when there is a path from house A to house B. Dixit wants to buy two houses in the city, that is, one for living and one for studying. Of course, he would like to travel from one house to another. So, he wants to find a pair of bi-reachable houses A and B. Among all such pairs, he wants to choose one with the maximum value of $$$k_A - k_B$$$, where $$$k_i$$$ is the number of roads leading to the house $$$i$$$. If more than one optimal pair exists, any of them is suitable. Since Dixit is busy preparing CodeCraft, can you help him find the desired pair of houses, or tell him that no such houses exist? In the problem input, you are not given the direction of each road. You are given — for each house — only the number of incoming roads to that house ($$$k_i$$$). You are allowed to ask only one type of query from the judge: give two houses A and B, and the judge answers whether B is reachable from A. There is no upper limit on the number of queries. But, you cannot ask more queries after the judge answers "Yes" to any of your queries. Also, you cannot ask the same query twice. Once you have exhausted all your queries (or the judge responds "Yes" to any of your queries), your program must output its guess for the two houses and quit. See the Interaction section below for more details. Input The first line contains a single integer $$$n$$$ ($$$3 le n le 500$$$) denoting the number of houses in the city. The next line contains $$$n$$$ space-separated integers $$$k_1, k_2, dots, k_n$$$ ($$$0 le k_i le n - 1$$$), the $$$i$$$-th of them represents the number of incoming roads to the $$$i$$$-th house. Interaction To ask a query, print "? A B" $$$(1 leq A,B leq N, A eq B)$$$. The judge will respond "Yes" if house B is reachable from house A, or "No" otherwise. To output the final answer, print "! A B", where A and B are bi-reachable with the maximum possible value of $$$k_A - k_B$$$. If there does not exist such pair of houses A and B, output "! 0 0". After outputting the final answer, your program must terminate immediately, otherwise you will receive Wrong Answer verdict. You cannot ask the same query twice. There is no upper limit to the number of queries you ask, but, you cannot ask more queries after the judge answers "Yes" to any of your queries. Your program must now output the final answer ("! A B" or "! 0 0") and terminate. If you ask a query in incorrect format or repeat a previous query, you will get Wrong Answer verdict. After printing a query do not forget to output the end of the line and flush the output. Otherwise, you will get the Idleness limit exceeded error. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Examples Input 4 1 2 0 3 No No No No No No Output ? 2 1 ? 1 3 ? 4 1 ? 2 3 ? 4 2 ? 4 3 ! 0 0 Note In the first sample input, we are given a city of three houses with one incoming road each. The user program asks one query: "? 1 2": asking whether we can reach from house $$$1$$$ to house $$$2$$$. The judge responds with "Yes". The user program now concludes that this is sufficient information to determine the correct answer. So, it outputs "! 1 2" and quits. In the second sample input, the user program queries for six different pairs of houses, and finally answers "! 0 0" as it is convinced that no two houses as desired in the question exist in this city.
2,200
false
true
false
false
false
false
true
false
true
true
3,177
300D
Vasily the bear has got a large square white table of _n_ rows and _n_ columns. The table has got a black border around this table. The example of the initial table at _n_ = 5. Vasily the bear wants to paint his square table in exactly _k_ moves. Each move is sequence of actions: 1. The bear chooses some square inside his table. At that the square must have a black border painted around it. Also, the square shouldn't contain a black cell. The number of cells in the square shouldn't be less than 2. 2. The bear chooses some row and some column inside the chosen square. Then he paints each cell of this row and this column inside the chosen square. After that the rectangles, formed by the square's border and the newly painted cells, must be squares of a non-zero area. An example of correct painting at _n_ = 7 и _k_ = 2. The bear already knows numbers _n_ and _k_. Help him — find the number of ways to paint the square in exactly _k_ moves. Two ways to paint are called distinct if the resulting tables will differ in at least one cell. As the answer can be rather large, print the remainder after dividing it by 7340033. Input The first line contains integer _q_ (1u2009≤u2009_q_u2009≤u2009105) — the number of test data. Each of the following _q_ lines contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009109,u20090u2009≤u2009_k_u2009≤u20091000) — the size of the initial table and the number of moves for the corresponding test. Output For each test from the input print the answer to the problem modulo 7340033. Print the answers to the tests in the order in which the tests are given in the input. Examples Input 8 1 0 1 1 3 0 3 1 2 0 2 1 3 2 7 2 Note All possible painting ways for the test _n_u2009=u20097 and _k_u2009=u20092 are:
2,300
false
false
false
true
false
false
false
false
false
false
8,632
924E
Olya wants to buy a custom wardrobe. It should have _n_ boxes with heights _a_1,u2009_a_2,u2009...,u2009_a__n_, stacked one on another in some order. In other words, we can represent each box as a vertical segment of length _a__i_, and all these segments should form a single segment from 0 to without any overlaps. Some of the boxes are important (in this case _b__i_u2009=u20091), others are not (then _b__i_u2009=u20090). Olya defines the convenience of the wardrobe as the number of important boxes such that their bottom edge is located between the heights _l_ and _r_, inclusive. You are given information about heights of the boxes and their importance. Compute the maximum possible convenience of the wardrobe if you can reorder the boxes arbitrarily. Input The first line contains three integers _n_, _l_ and _r_ (1u2009≤u2009_n_u2009≤u200910u2009000, 0u2009≤u2009_l_u2009≤u2009_r_u2009≤u200910u2009000)xa0— the number of boxes, the lowest and the highest heights for a bottom edge of an important box to be counted in convenience. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u200910u2009000)xa0— the heights of the boxes. It is guaranteed that the sum of height of all boxes (i.xa0e. the height of the wardrobe) does not exceed 10u2009000: Olya is not very tall and will not be able to reach any higher. The second line contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (0u2009≤u2009_b__i_u2009≤u20091), where _b__i_ equals 1 if the _i_-th box is important, and 0 otherwise. Output Print a single integerxa0— the maximum possible convenience of the wardrobe. Examples Input 5 3 6 3 2 5 1 2 1 1 0 1 0 Note In the first example you can, for example, first put an unimportant box of height 2, then put an important boxes of sizes 1, 3 and 2, in this order, and then the remaining unimportant boxes. The convenience is equal to 2, because the bottom edges of important boxes of sizes 3 and 2 fall into the range [3,u20096]. In the second example you have to put the short box under the tall box.
2,700
false
true
false
true
false
false
false
false
false
false
6,013
106A
There is a card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly — you can find them later yourselves if you want. To play durak you need a pack of 36 cards. Each card has a suit ("S", "H", "D" and "C") and a rank (in the increasing order "6", "7", "8", "9", "T", "J", "Q", "K" and "A"). At the beginning of the game one suit is arbitrarily chosen as trump. The players move like that: one player puts one or several of his cards on the table and the other one should beat each of them with his cards. A card beats another one if both cards have similar suits and the first card has a higher rank then the second one. Besides, a trump card can beat any non-trump card whatever the cards’ ranks are. In all other cases you can not beat the second card with the first one. You are given the trump suit and two different cards. Determine whether the first one beats the second one or not. Input The first line contains the tramp suit. It is "S", "H", "D" or "C". The second line contains the description of the two different cards. Each card is described by one word consisting of two symbols. The first symbol stands for the rank ("6", "7", "8", "9", "T", "J", "Q", "K" and "A"), and the second one stands for the suit ("S", "H", "D" and "C"). Output Print "YES" (without the quotes) if the first cards beats the second one. Otherwise, print "NO" (also without the quotes).
1,000
false
false
true
false
false
false
false
false
false
false
9,461
626D
Andrew and Jerry are playing a game with Harry as the scorekeeper. The game consists of three rounds. In each round, Andrew and Jerry draw randomly without replacement from a jar containing _n_ balls, each labeled with a distinct positive integer. Without looking, they hand their balls to Harry, who awards the point to the player with the larger number and returns the balls to the jar. The winner of the game is the one who wins at least two of the three rounds. Andrew wins rounds 1 and 2 while Jerry wins round 3, so Andrew wins the game. However, Jerry is unhappy with this system, claiming that he will often lose the match despite having the higher overall total. What is the probability that the sum of the three balls Jerry drew is strictly higher than the sum of the three balls Andrew drew? Input The first line of input contains a single integer _n_ (2u2009≤u2009_n_u2009≤u20092000) — the number of balls in the jar. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u20095000) — the number written on the _i_th ball. It is guaranteed that no two balls have the same number. Output Print a single real value — the probability that Jerry has a higher total, given that Andrew wins the first two rounds and Jerry wins the third. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Note In the first case, there are only two balls. In the first two rounds, Andrew must have drawn the 2 and Jerry must have drawn the 1, and vice versa in the final round. Thus, Andrew's sum is 5 and Jerry's sum is 4, so Jerry never has a higher total. In the second case, each game could've had three outcomes — 10u2009-u20092, 10u2009-u20091, or 2u2009-u20091. Jerry has a higher total if and only if Andrew won 2u2009-u20091 in both of the first two rounds, and Jerry drew the 10 in the last round. This has probability .
1,800
false
false
false
true
false
false
true
false
false
false
7,325
436E
Everyone who has played Cut the Rope knows full well how the gameplay is organized. All levels in the game are divided into boxes. Initially only one box with some levels is available. Player should complete levels to earn stars, collecting stars opens new box with levels. Imagine that you are playing Cut the Rope for the first time. Currently you have only the levels of the first box (by the way, it is called "Cardboard Box"). Each level is characterized by two integers: _a__i_ — how long it takes to complete the level for one star, _b__i_ — how long it takes to complete the level for two stars (_a__i_u2009<u2009_b__i_). You want to open the next box as quickly as possible. So, you need to earn at least _w_ stars. How do make it happen? Note that the level can be passed only once: either for one star or for two. You do not necessarily need to pass all the levels. Input The first line contains two integers _n_ and _w_ (1u2009≤u2009_n_u2009≤u20093·105;xa01u2009≤u2009_w_u2009≤u20092_n_) — the number of levels in the first box and the number of stars you need to open another box. Each of the following _n_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_u2009<u2009_b__i_u2009≤u2009109) — the attributes of the _i_-th level. Output In the first line print integer _t_ — the minimum time you need to open the next box. In the next line, print _n_ digits without spaces — the description of the optimal scenario: if you need to pass the _i_-th level for one star, the _i_-th digit should equal 1; if you need to pass the _i_-th level for two stars, the _i_-th digit should equal 2; if you do not need to pass the _i_-th level at all, the _i_-th digit should equal 0. Examples Input 5 3 10 20 5 10 10 20 6 9 25 30 Note In the first test sample, answer 21 is also assumed correct.
2,600
false
true
false
false
true
false
false
false
false
false
8,093
1301B
Dark is going to attend Motarack's birthday. Dark decided that the gift he is going to give to Motarack is an array $$$a$$$ of $$$n$$$ non-negative integers. Dark created that array $$$1000$$$ years ago, so some elements in that array disappeared. Dark knows that Motarack hates to see an array that has two adjacent elements with a high absolute difference between them. He doesn't have much time so he wants to choose an integer $$$k$$$ ($$$0 leq k leq 10^{9}$$$) and replaces all missing elements in the array $$$a$$$ with $$$k$$$. Let $$$m$$$ be the maximum absolute difference between all adjacent elements (i.e. the maximum value of $$$a_i - a_{i+1}$$$ for all $$$1 leq i leq n - 1$$$) in the array $$$a$$$ after Dark replaces all missing elements with $$$k$$$. Dark should choose an integer $$$k$$$ so that $$$m$$$ is minimized. Can you help him? Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 10^{5}$$$)xa0— the size of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-1 leq a_i leq 10 ^ {9}$$$). If $$$a_i = -1$$$, then the $$$i$$$-th integer is missing. It is guaranteed that at least one integer is missing in every test case. It is guaranteed, that the sum of $$$n$$$ for all test cases does not exceed $$$4 cdot 10 ^ {5}$$$. Output Print the answers for each test case in the following format: You should print two integers, the minimum possible value of $$$m$$$ and an integer $$$k$$$ ($$$0 leq k leq 10^{9}$$$) that makes the maximum absolute difference between adjacent elements in the array $$$a$$$ equal to $$$m$$$. Make sure that after replacing all the missing elements with $$$k$$$, the maximum absolute difference between adjacent elements becomes $$$m$$$. If there is more than one possible $$$k$$$, you can print any of them. Example Input 7 5 -1 10 -1 12 -1 5 -1 40 35 -1 35 6 -1 -1 9 -1 3 -1 2 -1 -1 2 0 -1 4 1 -1 3 -1 7 1 -1 7 5 2 -1 5 Output 1 11 5 35 3 6 0 42 0 0 1 2 3 4 Note In the first test case after replacing all missing elements with $$$11$$$ the array becomes $$$[11, 10, 11, 12, 11]$$$. The absolute difference between any adjacent elements is $$$1$$$. It is impossible to choose a value of $$$k$$$, such that the absolute difference between any adjacent element will be $$$leq 0$$$. So, the answer is $$$1$$$. In the third test case after replacing all missing elements with $$$6$$$ the array becomes $$$[6, 6, 9, 6, 3, 6]$$$. $$$a_1 - a_2 = 6 - 6 = 0$$$; $$$a_2 - a_3 = 6 - 9 = 3$$$; $$$a_3 - a_4 = 9 - 6 = 3$$$; $$$a_4 - a_5 = 6 - 3 = 3$$$; $$$a_5 - a_6 = 3 - 6 = 3$$$. So, the maximum difference between any adjacent elements is $$$3$$$.
1,500
false
true
false
false
false
false
false
true
false
false
4,193
1468M
You are given $$$n$$$ sets of integers. The $$$i$$$-th set contains $$$k_i$$$ integers. Two sets are called similar if they share at least two common elements, i.u2009e. there exist two integers $$$x$$$ and $$$y$$$ such that $$$x e y$$$, and they both belong to each of the two sets. Your task is to find two similar sets among the given ones, or report that there is no such pair of sets. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 50000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$) the number of given sets. The following $$$n$$$ lines describe the sets. The $$$i$$$-th line starts with an integer $$$k_i$$$ ($$$2 le k_i le 10^5$$$) — the number of integers in the $$$i$$$-th set. Then $$$k_i$$$ integers $$$a_{i,1}$$$, $$$a_{i,2}$$$, ..., $$$a_{i,k_i}$$$ ($$$1 le a_{i,j} le 10^9$$$) follow — the elements of the $$$i$$$-th set. It is guaranteed that all elements in each set are different. The total number of elements in all sets in all test cases is not greater than $$$2cdot 10^5$$$. Output For each test case, print the answer on a single line. If there is no pair of similar sets, print -1. Otherwise, print two different integers — the indices of the similar sets. The sets are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there are multiple answers, print any of them. Example Input 3 4 2 1 10 3 1 3 5 5 5 4 3 2 1 3 10 20 30 3 4 1 2 3 4 4 2 3 4 5 4 3 4 5 6 2 3 1 3 5 3 4 3 2
2,300
false
false
true
false
true
false
false
false
false
true
3,340
120I
Vasya has been collecting transport tickets for quite a while now. His collection contains several thousands of tram, trolleybus and bus tickets. Vasya is already fed up with the traditional definition of what a lucky ticket is. Thus, he's looking for new perspectives on that. Besides, Vasya cannot understand why all tickets are only divided into lucky and unlucky ones. He thinks that all tickets are lucky but in different degrees. Having given the matter some thought, Vasya worked out the definition of a ticket's degree of luckiness. Let a ticket consist of 2_n_ digits. Let's regard each digit as written as is shown on the picture: You have seen such digits on electronic clocks: seven segments are used to show digits. Each segment can either be colored or not. The colored segments form a digit. Vasya regards the digits as written in this very way and takes the right half of the ticket and puts it one the left one, so that the first digit coincides with the _n_u2009+u20091-th one, the second digit coincides with the _n_u2009+u20092-th one, ..., the _n_-th digit coincides with the 2_n_-th one. For each pair of digits, put one on another, he counts the number of segments colored in both digits and summarizes the resulting numbers. The resulting value is called the degree of luckiness of a ticket. For example, the degree of luckiness of ticket 03 equals four and the degree of luckiness of ticket 2345 equals six. You are given the number of a ticket containing 2_n_ digits. Your task is to find among the tickets whose number exceeds the number of this ticket but also consists of 2_n_ digits such ticket, whose degree of luckiness exceeds the degrees of luckiness of the given ticket. Moreover, if there are several such tickets, you should only choose the one with the smallest number.
2,200
false
true
false
false
false
false
false
false
false
false
9,398
859F
It's another Start[c]up, and that means there are T-shirts to order. In order to make sure T-shirts are shipped as soon as possible, we've decided that this year we're going to order all of the necessary T-shirts before the actual competition. The top _C_ contestants are going to be awarded T-shirts, but we obviously don't know which contestants that will be. The plan is to get the T-Shirt sizes of all contestants before the actual competition, and then order enough T-shirts so that no matter who is in the top _C_ we'll have T-shirts available in order to award them. In order to get the T-shirt sizes of the contestants, we will send out a survey. The survey will allow contestants to either specify a single desired T-shirt size, or two adjacent T-shirt sizes. If a contestant specifies two sizes, it means that they can be awarded either size. As you can probably tell, this plan could require ordering a lot of unnecessary T-shirts. We'd like your help to determine the minimum number of T-shirts we'll need to order to ensure that we'll be able to award T-shirts no matter the outcome of the competition. Input Input will begin with two integers _N_ and _C_ (1u2009≤u2009_N_u2009≤u20092·105, 1u2009≤u2009_C_), the number of T-shirt sizes and number of T-shirts to be awarded, respectively. Following this is a line with 2·_N_u2009-u20091 integers, _s_1 through _s_2·_N_u2009-u20091 (0u2009≤u2009_s__i_u2009≤u2009108). For odd _i_, _s__i_ indicates the number of contestants desiring T-shirt size ((_i_u2009+u20091)u2009/u20092). For even _i_, _s__i_ indicates the number of contestants okay receiving either of T-shirt sizes (_i_u2009/u20092) and (_i_u2009/u20092u2009+u20091). _C_ will not exceed the total number of contestants. Output Print the minimum number of T-shirts we need to buy. Examples Input 4 160 88 69 62 29 58 52 44 Note In the first example, we can buy 100 of each size.
2,800
false
true
false
false
false
false
false
false
false
false
6,303
1266C
Let $$$a$$$ be a matrix of size $$$r imes c$$$ containing positive integers, not necessarily distinct. Rows of the matrix are numbered from $$$1$$$ to $$$r$$$, columns are numbered from $$$1$$$ to $$$c$$$. We can construct an array $$$b$$$ consisting of $$$r + c$$$ integers as follows: for each $$$i in [1, r]$$$, let $$$b_i$$$ be the greatest common divisor of integers in the $$$i$$$-th row, and for each $$$j in [1, c]$$$ let $$$b_{r+j}$$$ be the greatest common divisor of integers in the $$$j$$$-th column. We call the matrix diverse if all $$$r + c$$$ numbers $$$b_k$$$ ($$$k in [1, r + c]$$$) are pairwise distinct. The magnitude of a matrix equals to the maximum of $$$b_k$$$. For example, suppose we have the following matrix: $$$begin{pmatrix} 2 & 9 & 7 4 & 144 & 84 end{pmatrix}$$$ We construct the array $$$b$$$: 1. $$$b_1$$$ is the greatest common divisor of $$$2$$$, $$$9$$$, and $$$7$$$, that is $$$1$$$; 2. $$$b_2$$$ is the greatest common divisor of $$$4$$$, $$$144$$$, and $$$84$$$, that is $$$4$$$; 3. $$$b_3$$$ is the greatest common divisor of $$$2$$$ and $$$4$$$, that is $$$2$$$; 4. $$$b_4$$$ is the greatest common divisor of $$$9$$$ and $$$144$$$, that is $$$9$$$; 5. $$$b_5$$$ is the greatest common divisor of $$$7$$$ and $$$84$$$, that is $$$7$$$. So $$$b = [1, 4, 2, 9, 7]$$$. All values in this array are distinct, so the matrix is diverse. The magnitude is equal to $$$9$$$. For a given $$$r$$$ and $$$c$$$, find a diverse matrix that minimises the magnitude. If there are multiple solutions, you may output any of them. If there are no solutions, output a single integer $$$0$$$. Output If there is no solution, output a single integer $$$0$$$. Otherwise, output $$$r$$$ rows. The $$$i$$$-th of them should contain $$$c$$$ space-separated integers, the $$$j$$$-th of which is $$$a_{i,j}$$$ — the positive integer in the $$$i$$$-th row and $$$j$$$-th column of a diverse matrix minimizing the magnitude. Furthermore, it must hold that $$$1 leq a_{i,j} leq 10^9$$$. It can be shown that if a solution exists, there is also a solution with this additional constraint (still having minimum possible magnitude). Note In the first example, the GCDs of rows are $$$b_1 = 4$$$ and $$$b_2 = 1$$$, and the GCDs of columns are $$$b_3 = 2$$$ and $$$b_4 = 3$$$. All GCDs are pairwise distinct and the maximum of them is $$$4$$$. Since the GCDs have to be distinct and at least $$$1$$$, it is clear that there are no diverse matrices of size $$$2 imes 2$$$ with magnitude smaller than $$$4$$$. In the second example, no matter what $$$a_{1,1}$$$ is, $$$b_1 = b_2$$$ will always hold, so there are no diverse matrices.
1,400
true
true
false
false
false
true
false
false
false
false
4,369
1433G
You are a mayor of Berlyatov. There are $$$n$$$ districts and $$$m$$$ two-way roads between them. The $$$i$$$-th road connects districts $$$x_i$$$ and $$$y_i$$$. The cost of travelling along this road is $$$w_i$$$. There is some path between each pair of districts, so the city is connected. There are $$$k$$$ delivery routes in Berlyatov. The $$$i$$$-th route is going from the district $$$a_i$$$ to the district $$$b_i$$$. There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district $$$a_i$$$ to the district $$$b_i$$$ to deliver products. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently). You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with $$$0$$$). Let $$$d(x, y)$$$ be the cheapest cost of travel between districts $$$x$$$ and $$$y$$$. Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with $$$0$$$. In other words, you have to find the minimum possible value of $$$sumlimits_{i = 1}^{k} d(a_i, b_i)$$$ after applying the operation described above optimally. Input The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 1000$$$; $$$n - 1 le m le min(1000, frac{n(n-1)}{2})$$$; $$$1 le k le 1000$$$) — the number of districts, the number of roads and the number of courier routes. The next $$$m$$$ lines describe roads. The $$$i$$$-th road is given as three integers $$$x_i$$$, $$$y_i$$$ and $$$w_i$$$ ($$$1 le x_i, y_i le n$$$; $$$x_i e y_i$$$; $$$1 le w_i le 1000$$$), where $$$x_i$$$ and $$$y_i$$$ are districts the $$$i$$$-th road connects and $$$w_i$$$ is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts. The next $$$k$$$ lines describe courier routes. The $$$i$$$-th route is given as two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$) — the districts of the $$$i$$$-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently). Output Print one integer — the minimum total courier routes cost you can achieve (i.e. the minimum value $$$sumlimits_{i=1}^{k} d(a_i, b_i)$$$, where $$$d(x, y)$$$ is the cheapest cost of travel between districts $$$x$$$ and $$$y$$$) if you can make some (at most one) road cost zero. Examples Input 6 5 2 1 2 5 2 3 7 2 4 4 4 5 2 4 6 8 1 6 5 3 Input 5 5 4 1 2 5 2 3 4 1 4 3 4 3 7 3 5 2 1 5 1 3 3 3 1 5 Note The picture corresponding to the first example: There, you can choose either the road $$$(2, 4)$$$ or the road $$$(4, 6)$$$. Both options lead to the total cost $$$22$$$. The picture corresponding to the second example: There, you can choose the road $$$(3, 4)$$$. This leads to the total cost $$$13$$$.
2,100
false
false
false
false
false
false
true
false
false
true
3,493
1450F
To help those contestants who struggle a lot in contests, the headquarters of Codeforces are planning to introduce Division 5. In this new division, the tags of all problems will be announced prior to the round to help the contestants. The contest consists of $$$n$$$ problems, where the tag of the $$$i$$$-th problem is denoted by an integer $$$a_i$$$. You want to AK (solve all problems). To do that, you must solve the problems in some order. To make the contest funnier, you created extra limitations on yourself. You do not want to solve two problems consecutively with the same tag since it is boring. Also, you are afraid of big jumps in difficulties while solving them, so you want to minimize the number of times that you solve two problems consecutively that are not adjacent in the contest order. Formally, your solve order can be described by a permutation $$$p$$$ of length $$$n$$$. The cost of a permutation is defined as the number of indices $$$i$$$ ($$$1le i<n$$$) where $$$p_{i+1}-p_i>1$$$. You have the requirement that $$$a_{p_i} e a_{p_{i+1}}$$$ for all $$$1le i< n$$$. You want to know the minimum possible cost of permutation that satisfies the requirement. If no permutations meet this requirement, you should report about it. Input The first line contains a single integer $$$t$$$ ($$$1leq tleq 10^4$$$) — the number of test cases. The first line of the description of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the number of problems in the contest. The next line contains $$$n$$$ integers $$$a_1,a_2,ldots a_n$$$ ($$$1 le a_i le n$$$) — the tags of the problems. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, if there are no permutations that satisfy the required condition, print $$$-1$$$. Otherwise, print the minimum possible cost of a permutation that satisfies the required condition. Example Input 4 6 2 1 2 3 1 1 5 1 1 1 2 2 8 7 7 2 7 7 1 8 7 10 1 2 3 4 1 1 2 3 4 1 Note In the first test case, let $$$p=[5, 4, 3, 2, 1, 6]$$$. The cost is $$$1$$$ because we jump from $$$p_5=1$$$ to $$$p_6=6$$$, and $$$6-1>1$$$. This permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than $$$1$$$. In the second test case, let $$$p=[1,5,2,4,3]$$$. The cost is $$$3$$$ because $$$p_2-p_1>1$$$, $$$p_3-p_2>1$$$, and $$$p_4-p_3>1$$$. The permutation is valid because we don't solve problems with the same tag twice in a row. We cannot find a permutation with a cost smaller than $$$3$$$. In the third test case, for any order of solving the problems, we will solve two problems with the same tag consecutively, so the answer is $$$-1$$$.
2,400
false
true
false
false
false
true
false
false
false
false
3,432
1726D
You are given a connected, undirected and unweighted graph with $$$n$$$ vertices and $$$m$$$ edges. Notice the limit on the number of edges: $$$m le n + 2$$$. Let's say we color some of the edges red and the remaining edges blue. Now consider only the red edges and count the number of connected components in the graph. Let this value be $$$c_1$$$. Similarly, consider only the blue edges and count the number of connected components in the graph. Let this value be $$$c_2$$$. Find an assignment of colors to the edges such that the quantity $$$c_1+c_2$$$ is minimised. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$n-1 leq m leq min{left(n+2,frac{n cdot (n-1)}{2} ight)}$$$)xa0— the number of vertices and the number of edges respectively. $$$m$$$ lines follow. The $$$i$$$-th line contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i,v_i le n$$$, $$$u_i e v_i$$$) denoting that the $$$i$$$-th edge goes between vertices $$$u_i$$$ and $$$v_i$$$. The input is guaranteed to have no multiple edges or self loops. The graph is also guaranteed to be connected. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2 cdot 10^6$$$. Output For each test case, output a binary string of length $$$m$$$. The $$$i$$$-th character of the string should be 1 if the $$$i$$$-th edge should be colored red, and 0 if it should be colored blue. If there are multiple ways to assign colors to edges that give the minimum answer, you may output any. Example Input 4 5 7 1 2 2 3 3 4 4 5 5 1 1 3 3 5 4 4 1 2 2 3 1 4 3 4 6 7 1 2 1 3 3 4 4 5 1 4 5 6 6 2 2 1 1 2 Output 0111010 1001 0001111 0 Note The corresponding graph of the first test case is: $$$c_1 + c_2 = 1 + 2 = 3$$$ The corresponding graph of the second test case is: $$$c_1 + c_2 = 2 + 2 = 4$$$
2,000
false
false
false
false
false
true
true
false
false
true
1,930
919E
Problem - 919E - 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 chinese remainder theorem math number theory *2100 No tag edit access → Contest materials ") Editorial") satisfy $$$$$$n cdot a^n equiv b quad ( extrm{mod};p),$$$$$$ where $$$a, b, p$$$ are all known constants. Input The only line contains four integers $$$a,b,p,x$$$ ($$$2 leq p leq 10^6+3$$$, $$$1 leq a,b < p$$$, $$$1 leq x leq 10^{12}$$$). It is guaranteed that $$$p$$$ is a prime. Output Print a single integer: the number of possible answers $$$n$$$. Examples Input 2 3 5 8 Output 2 Input 4 6 7 13 Output 1 Input 233 233 10007 1 Output 1 Note In the first sample, we can see that $$$n=2$$$ and $$$n=8$$$ are possible answers.
2,100
true
false
false
false
false
false
false
false
false
false
6,052
1942C1
This is the easy version of the problem. The only difference between the two versions is the constraint on $$$y$$$. In this version $$$y = 0$$$. You can make hacks only if both versions are solved. Bessie has received a birthday cake from her best friend Elsie, and it came in the form of a regular polygon with $$$n$$$ sides. The vertices of the cake are numbered from $$$1$$$ to $$$n$$$ clockwise. You and Bessie are going to choose some of those vertices to cut non-intersecting diagonals into the cake. In other words, the endpoints of the diagonals must be part of the chosen vertices. Bessie would only like to give out pieces of cake which result in a triangle to keep consistency. The size of the pieces doesn't matter, and the whole cake does not have to be separated into all triangles (other shapes are allowed in the cake, but those will not be counted). Bessie has already chosen $$$x$$$ of those vertices that can be used to form diagonals. She wants you to choose no more than $$$y$$$ other vertices such that the number of triangular pieces of cake she can give out is maximized. What is the maximum number of triangular pieces of cake Bessie can give out? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case consists of three integers, $$$n$$$, $$$x$$$, and $$$y$$$ ($$$4 leq n leq 10^9$$$, $$$2 leq x leq min(n, 2 cdot 10^5)$$$, $$$y = 0$$$)xa0— the number of sides of the polygon, number of vertices Bessie has chosen, and the maximum number of other vertices you can choose. The second line consists of $$$x$$$ distinct integers from $$$1$$$ to $$$n$$$, representing the vertices Bessie has chosen. It is guaranteed the sum of $$$x$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer: the maximum number of non-intersecting triangular pieces of cake she can give out. Example Input 3 8 4 0 1 6 2 5 8 8 0 1 3 2 5 4 6 7 8 4 2 0 1 3 Note In test cases $$$1$$$, $$$2$$$ and $$$3$$$, you can get $$$2$$$, $$$6$$$ and $$$2$$$ non-intersecting triangular pieces of cake, respectively. A possible construction is shown in the following pictures: The green dots represent vertices that can be used, the blue lines represent diagonals that are drawn, and the red numbers represent triangles that are counted.
1,300
true
true
false
false
false
false
false
false
false
false
630
1551A
Polycarp must pay exactly $$$n$$$ burles at the checkout. He has coins of two nominal values: $$$1$$$ burle and $$$2$$$ burles. Polycarp likes both kinds of coins equally. So he doesn't want to pay with more coins of one type than with the other. Thus, Polycarp wants to minimize the difference between the count of coins of $$$1$$$ burle and $$$2$$$ burles being used. Help him by determining two non-negative integer values $$$c_1$$$ and $$$c_2$$$ which are the number of coins of $$$1$$$ burle and $$$2$$$ burles, respectively, so that the total value of that number of coins is exactly $$$n$$$ (i.u2009e. $$$c_1 + 2 cdot c_2 = n$$$), and the absolute value of the difference between $$$c_1$$$ and $$$c_2$$$ is as little as possible (i.u2009e. you must minimize $$$c_1-c_2$$$). Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line. This line contains one integer $$$n$$$ ($$$1 le n le 10^9$$$) — the number of burles to be paid by Polycarp. Output For each test case, output a separate line containing two integers $$$c_1$$$ and $$$c_2$$$ ($$$c_1, c_2 ge 0$$$) separated by a space where $$$c_1$$$ is the number of coins of $$$1$$$ burle and $$$c_2$$$ is the number of coins of $$$2$$$ burles. If there are multiple optimal solutions, print any one. Example Input 6 1000 30 1 32 1000000000 5 Output 334 333 10 10 1 0 10 11 333333334 333333333 1 2 Note The answer for the first test case is "334 333". The sum of the nominal values of all coins is $$$334 cdot 1 + 333 cdot 2 = 1000$$$, whereas $$$334 - 333 = 1$$$. One can't get the better value because if $$$c_1 - c_2 = 0$$$, then $$$c_1 = c_2$$$ and $$$c_1 cdot 1 + c_1 cdot 2 = 1000$$$, but then the value of $$$c_1$$$ isn't an integer. The answer for the second test case is "10 10". The sum of the nominal values is $$$10 cdot 1 + 10 cdot 2 = 30$$$ and $$$10 - 10 = 0$$$, whereas there's no number having an absolute value less than $$$0$$$.
800
true
true
false
false
false
false
false
false
false
false
2,892