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
988E
Problem - 988E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force greedy *2100 No tag edit access → Contest materials ") . It is guaranteed that the first (left) digit of the number $$$n$$$ is not a zero. Output If it is impossible to obtain a number that is divisible by $$$25$$$, print -1. Otherwise print the minimum number of moves required to obtain such number. Note that you can swap only adjacent digits in the given number. Examples Input 5071 Output 4 Input 705 Output 1 Input 1241367 Output -1 Note In the first example one of the possible sequences of moves is 5071 $$$ ightarrow$$$ 5701 $$$ ightarrow$$$ 7501 $$$ ightarrow$$$ 7510 $$$ ightarrow$$$ 7150.
2,100
false
true
false
false
false
false
true
false
false
false
5,759
1697E
You are given $$$n$$$ points on the plane, the coordinates of the $$$i$$$-th point are $$$(x_i, y_i)$$$. No two points have the same coordinates. The distance between points $$$i$$$ and $$$j$$$ is defined as $$$d(i,j) = x_i - x_j + y_i - y_j$$$. For each point, you have to choose a color, represented by an integer from $$$1$$$ to $$$n$$$. For every ordered triple of different points $$$(a,b,c)$$$, the following constraints should be met: if $$$a$$$, $$$b$$$ and $$$c$$$ have the same color, then $$$d(a,b) = d(a,c) = d(b,c)$$$; if $$$a$$$ and $$$b$$$ have the same color, and the color of $$$c$$$ is different from the color of $$$a$$$, then $$$d(a,b) < d(a,c)$$$ and $$$d(a,b) < d(b,c)$$$. Calculate the number of different ways to choose the colors that meet these constraints. Input The first line contains one integer $$$n$$$ ($$$2 le n le 100$$$)xa0— the number of points. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 le x_i, y_i le 10^8$$$). No two points have the same coordinates (i.u2009e. if $$$i e j$$$, then either $$$x_i e x_j$$$ or $$$y_i e y_j$$$). Output Print one integerxa0— the number of ways to choose the colors for the points. Since it can be large, print it modulo $$$998244353$$$. Examples Input 5 1 2 2 4 3 4 4 4 1 3 Note In the first test, the following ways to choose the colors are suitable: $$$[1, 1, 1]$$$; $$$[2, 2, 2]$$$; $$$[3, 3, 3]$$$; $$$[1, 2, 3]$$$; $$$[1, 3, 2]$$$; $$$[2, 1, 3]$$$; $$$[2, 3, 1]$$$; $$$[3, 1, 2]$$$; $$$[3, 2, 1]$$$.
2,400
true
true
true
true
false
true
true
false
false
true
2,096
1253C
Tsumugi brought $$$n$$$ delicious sweets to the Light Music Club. They are numbered from $$$1$$$ to $$$n$$$, where the $$$i$$$-th sweet has a sugar concentration described by an integer $$$a_i$$$. Yui loves sweets, but she can eat at most $$$m$$$ sweets each day for health reasons. Days are $$$1$$$-indexed (numbered $$$1, 2, 3, ldots$$$). Eating the sweet $$$i$$$ at the $$$d$$$-th day will cause a sugar penalty of $$$(d cdot a_i)$$$, as sweets become more sugary with time. A sweet can be eaten at most once. The total sugar penalty will be the sum of the individual penalties of each sweet eaten. Suppose that Yui chooses exactly $$$k$$$ sweets, and eats them in any order she wants. What is the minimum total sugar penalty she can get? Since Yui is an undecided girl, she wants you to answer this question for every value of $$$k$$$ between $$$1$$$ and $$$n$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le m le n le 200 000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 200 000$$$). Output You have to output $$$n$$$ integers $$$x_1, x_2, ldots, x_n$$$ on a single line, separed by spaces, where $$$x_k$$$ is the minimum total sugar penalty Yui can get if she eats exactly $$$k$$$ sweets. Examples Input 9 2 6 19 3 4 4 2 6 7 8 Output 2 5 11 18 30 43 62 83 121 Note Let's analyze the answer for $$$k = 5$$$ in the first example. Here is one of the possible ways to eat $$$5$$$ sweets that minimize total sugar penalty: Day $$$1$$$: sweets $$$1$$$ and $$$4$$$ Day $$$2$$$: sweets $$$5$$$ and $$$3$$$ Day $$$3$$$ : sweet $$$6$$$ Total penalty is $$$1 cdot a_1 + 1 cdot a_4 + 2 cdot a_5 + 2 cdot a_3 + 3 cdot a_6 = 6 + 4 + 8 + 6 + 6 = 30$$$. We can prove that it's the minimum total sugar penalty Yui can achieve if she eats $$$5$$$ sweets, hence $$$x_5 = 30$$$.
1,500
true
true
false
true
false
false
false
false
true
false
4,420
1110C
Can the greatest common divisor and bitwise operations have anything in common? It is time to answer this question. Suppose you are given a positive integer $$$a$$$. You want to choose some integer $$$b$$$ from $$$1$$$ to $$$a - 1$$$ inclusive in such a way that the = max_{0 < b < a}{gcd(a oplus b, a > & > b)}.$$$$$$ Here $$$oplus$$$ denotes the . Input The first line contains an integer $$$q$$$ ($$$1 le q le 10^3$$$)xa0— the number of integers you need to compute the answer for. After that $$$q$$$ integers are given, one per line: $$$a_1, a_2, ldots, a_q$$$ ($$$2 le a_i le 2^{25} - 1$$$)xa0— the integers you need to compute the answer for. Output For each integer, print the answer in the same order as the integers are given in input. Note For the first integer the optimal choice is $$$b = 1$$$, then $$$a oplus b = 3$$$, $$$a > & > b = 0$$$, and the greatest common divisor of $$$3$$$ and $$$0$$$ is $$$3$$$. For the second integer one optimal choice is $$$b = 2$$$, then $$$a oplus b = 1$$$, $$$a > & > b = 2$$$, and the greatest common divisor of $$$1$$$ and $$$2$$$ is $$$1$$$. For the third integer the optimal choice is $$$b = 2$$$, then $$$a oplus b = 7$$$, $$$a > & > b = 0$$$, and the greatest common divisor of $$$7$$$ and $$$0$$$ is $$$7$$$.
1,500
true
false
false
false
false
true
false
false
false
false
5,147
227B
Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array. According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is. Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to _n_) and ending with the _n_-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the _n_-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent. To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to _n_, and generated _m_ queries of the form: find element with value _b__i_ in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand. But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of elements in the array. The second line contains _n_ distinct space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_) — the elements of array. The third line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of queries. The last line contains _m_ space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (1u2009≤u2009_b__i_u2009≤u2009_n_) — the search queries. Note that the queries can repeat. Output Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces. 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. Note In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element). In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).
1,100
false
false
true
false
false
false
false
false
false
false
8,929
988F
Polycarp lives on a coordinate line at the point $$$x = 0$$$. He goes to his friend that lives at the point $$$x = a$$$. Polycarp can move only from left to right, he can pass one unit of length each second. Now it's raining, so some segments of his way are in the rain. Formally, it's raining on $$$n$$$ non-intersecting segments, the $$$i$$$-th segment which is in the rain is represented as $$$[l_i, r_i]$$$ ($$$0 le l_i < r_i le a$$$). There are $$$m$$$ umbrellas lying on the line, the $$$i$$$-th umbrella is located at point $$$x_i$$$ ($$$0 le x_i le a$$$) and has weight $$$p_i$$$. When Polycarp begins his journey, he doesn't have any umbrellas. During his journey from $$$x = 0$$$ to $$$x = a$$$ Polycarp can pick up and throw away umbrellas. Polycarp picks up and throws down any umbrella instantly. He can carry any number of umbrellas at any moment of time. Because Polycarp doesn't want to get wet, he must carry at least one umbrella while he moves from $$$x$$$ to $$$x + 1$$$ if a segment $$$[x, x + 1]$$$ is in the rain (i.e. if there exists some $$$i$$$ such that $$$l_i le x$$$ and $$$x + 1 le r_i$$$). The condition above is the only requirement. For example, it is possible to go without any umbrellas to a point where some rain segment starts, pick up an umbrella at this point and move along with an umbrella. Polycarp can swap umbrellas while he is in the rain. Each unit of length passed increases Polycarp's fatigue by the sum of the weights of umbrellas he carries while moving. Can Polycarp make his way from point $$$x = 0$$$ to point $$$x = a$$$? If yes, find the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally. Input The first line contains three integers $$$a$$$, $$$n$$$ and $$$m$$$ ($$$1 le a, m le 2000, 1 le n le lceilfrac{a}{2} ceil$$$) — the point at which Polycarp's friend lives, the number of the segments in the rain and the number of umbrellas. Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 le l_i < r_i le a$$$) — the borders of the $$$i$$$-th segment under rain. It is guaranteed that there is no pair of intersecting segments. In other words, for each pair of segments $$$i$$$ and $$$j$$$ either $$$r_i < l_j$$$ or $$$r_j < l_i$$$. Each of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$0 le x_i le a$$$, $$$1 le p_i le 10^5$$$) — the location and the weight of the $$$i$$$-th umbrella. Output Print "-1" (without quotes) if Polycarp can't make his way from point $$$x = 0$$$ to point $$$x = a$$$. Otherwise print one integer — the minimum total fatigue after reaching $$$x = a$$$, if Polycarp picks up and throws away umbrellas optimally. Note In the first example the only possible strategy is to take the fourth umbrella at the point $$$x = 1$$$, keep it till the point $$$x = 7$$$ (the total fatigue at $$$x = 7$$$ will be equal to $$$12$$$), throw it away, move on from $$$x = 7$$$ to $$$x = 8$$$ without an umbrella, take the third umbrella at $$$x = 8$$$ and keep it till the end (the total fatigue at $$$x = 10$$$ will be equal to $$$14$$$). In the second example the only possible strategy is to take the first umbrella, move with it till the point $$$x = 9$$$, throw it away and proceed without an umbrella till the end.
2,100
false
false
false
true
false
false
false
false
false
false
5,758
146A
Problem - 146A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials ") . Petya calls a ticket lucky if the ticket's number is a lucky number and the sum of digits in the first half (the sum of the first _n_u2009/u20092 digits) equals the sum of digits in the second half (the sum of the last _n_u2009/u20092 digits). Check if the given ticket is lucky. Input The first line contains an even integer _n_ (2u2009≤u2009_n_u2009≤u200950) — the length of the ticket number that needs to be checked. The second line contains an integer whose length equals exactly _n_ — the ticket number. The number may contain leading zeros. Output On the first line print "YES" if the given ticket number is lucky. Otherwise, print "NO" (without the quotes). Examples Input 2 47 Output NO Input 4 4738 Output NO Input 4 4774 Output YES Note In the first sample the sum of digits in the first half does not equal the sum of digits in the second half (4u2009≠u20097). In the second sample the ticket number is not the lucky number.
800
false
false
true
false
false
false
false
false
false
false
9,290
1066D
Maksim has $$$n$$$ objects and $$$m$$$ boxes, each box has size exactly $$$k$$$. Objects are numbered from $$$1$$$ to $$$n$$$ in order from left to right, the size of the $$$i$$$-th object is $$$a_i$$$. Maksim wants to pack his objects into the boxes and he will pack objects by the following algorithm: he takes one of the empty boxes he has, goes from left to right through the objects, and if the $$$i$$$-th object fits in the current box (the remaining size of the box is greater than or equal to $$$a_i$$$), he puts it in the box, and the remaining size of the box decreases by $$$a_i$$$. Otherwise he takes the new empty box and continues the process above. If he has no empty boxes and there is at least one object not in some box then Maksim cannot pack the chosen set of objects. Maksim wants to know the maximum number of objects he can pack by the algorithm above. To reach this target, he will throw out the leftmost object from the set until the remaining set of objects can be packed in boxes he has. Your task is to say the maximum number of objects Maksim can pack in boxes he has. Each time when Maksim tries to pack the objects into the boxes, he will make empty all the boxes he has before do it (and the relative order of the remaining set of objects will not change). Input The first line of the input contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 le n, m le 2 cdot 10^5$$$, $$$1 le k le 10^9$$$) — the number of objects, the number of boxes and the size of each box. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le k$$$), where $$$a_i$$$ is the size of the $$$i$$$-th object. Output Print the maximum number of objects Maksim can pack using the algorithm described in the problem statement. Note In the first example Maksim can pack only $$$4$$$ objects. Firstly, he tries to pack all the $$$5$$$ objects. Distribution of objects will be $$$[5], [2, 1]$$$. Maxim cannot pack the next object in the second box and he has no more empty boxes at all. Next he will throw out the first object and the objects distribution will be $$$[2, 1], [4, 2]$$$. So the answer is $$$4$$$. In the second example it is obvious that Maksim cannot pack all the objects starting from first, second, third and fourth (in all these cases the distribution of objects is $$$[4]$$$), but he can pack the last object ($$$[1]$$$). In the third example Maksim can pack all the objects he has. The distribution will be $$$[1, 2], [3], [1, 1]$$$.
1,800
false
false
true
false
false
false
false
true
false
false
5,378
1875D
You are given an array of $$$n$$$ nonnegative integers $$$a_1, a_2, dots, a_n$$$. Let $$$m$$$ be a variable that is initialized to $$$0$$$, Jellyfish will perform the following operation $$$n$$$ times: select an index $$$i$$$ ($$$1 leq i leq a$$$) and delete $$$a_i$$$ from $$$a$$$. add $$$operatorname{MEX}(a)^{dagger}$$$ to $$$m$$$. Now Jellyfish wants to know the minimum possible final value of $$$m$$$ if he performs all the operations optimally. $$$^{dagger}$$$ The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of $$$[2,2,1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. The MEX of $$$[3,1,0,1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. The MEX of $$$[0,3,1,2]$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$, and $$$3$$$ belong to the array, but $$$4$$$ does not. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 5000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 5000$$$)xa0— the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 leq a_i leq 10^9$$$)xa0— the integers in the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Note In the first test case, we delete elements from $$$a$$$ in the following order: $$$[5,2,color{red}{1},0,3,0,4,0] o [5,2,0,3,color{red}{0},4,0] o [5,2,color{red}{0},3,4,0] o [5,2,3,4,color{red}{0}] o [5,2,color{red}{3},4] o [color{red}{5},2,4] o [color{red}{2},4] o [color{red}{4}] o [~]$$$. The value of $$$m$$$ will be $$$1+1+1+0+0+0+0+0=3$$$.
1,600
false
false
false
true
false
false
false
false
false
false
1,027
1051G
Suppose you are given a sequence $$$S$$$ of $$$k$$$ pairs of integers $$$(a_1, b_1), (a_2, b_2), dots, (a_k, b_k)$$$. You can perform the following operations on it: 1. Choose some position $$$i$$$ and increase $$$a_i$$$ by $$$1$$$. That can be performed only if there exists at least one such position $$$j$$$ that $$$i e j$$$ and $$$a_i = a_j$$$. The cost of this operation is $$$b_i$$$; 2. Choose some position $$$i$$$ and decrease $$$a_i$$$ by $$$1$$$. That can be performed only if there exists at least one such position $$$j$$$ that $$$a_i = a_j + 1$$$. The cost of this operation is $$$-b_i$$$. Each operation can be performed arbitrary number of times (possibly zero). Let $$$f(S)$$$ be minimum possible $$$x$$$ such that there exists a sequence of operations with total cost $$$x$$$, after which all $$$a_i$$$ from $$$S$$$ are pairwise distinct. Now for the task itself ... You are given a sequence $$$P$$$ consisting of $$$n$$$ pairs of integers $$$(a_1, b_1), (a_2, b_2), dots, (a_n, b_n)$$$. All $$$b_i$$$ are pairwise distinct. Let $$$P_i$$$ be the sequence consisting of the first $$$i$$$ pairs of $$$P$$$. Your task is to calculate the values of $$$f(P_1), f(P_2), dots, f(P_n)$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of pairs in sequence $$$P$$$. Next $$$n$$$ lines contain the elements of $$$P$$$: $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i le 2 cdot 10^5$$$, $$$1 le b_i le n$$$). It is guaranteed that all values of $$$b_i$$$ are pairwise distinct.
2,900
false
true
false
false
true
false
false
false
false
false
5,449
613C
Ivan wants to make a necklace as a present to his beloved girl. A necklace is a cyclic sequence of beads of different colors. Ivan says that necklace is beautiful relative to the cut point between two adjacent beads, if the chain of beads remaining after this cut is a palindrome (reads the same forward and backward). Ivan has beads of _n_ colors. He wants to make a necklace, such that it's beautiful relative to as many cuts as possible. He certainly wants to use all the beads. Help him to make the most beautiful necklace. Input The first line of the input contains a single number _n_ (1u2009≤u2009_n_u2009≤u200926) — the number of colors of beads. The second line contains after _n_ positive integers _a__i_ xa0 — the quantity of beads of _i_-th color. It is guaranteed that the sum of _a__i_ is at least 2 and does not exceed 100u2009000. Output In the first line print a single numberxa0— the maximum number of beautiful cuts that a necklace composed from given beads may have. In the second line print any example of such necklace. Each color of the beads should be represented by the corresponding lowercase English letter (starting with a). As the necklace is cyclic, print it starting from any point. Note In the first sample a necklace can have at most one beautiful cut. The example of such a necklace is shown on the picture. In the second sample there is only one way to compose a necklace.
2,500
true
false
false
false
false
true
false
false
false
false
7,385
1369F
# BareLee Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Lee is used to finish his stories in a stylish way, this time he barely failed it, but Ice Bear came and helped him. Lee is so grateful for it, so he decided to show Ice Bear his new game called “Critic”. . . The game is a one versus one game. It has t rounds, each round has two integers si and ei (which are determined and are known before the game begins, si and ei may differ from round to round). The integer si is written on the board at the beginning of the corresponding round. The players will take turns. Each player will erase the number on the board (let’s say it was a) and will choose to write either 2 · a or a + 1 instead. Whoever writes a number strictly greater than ei loses that round and the other one wins that round. Now Lee wants to play “Critic” against Ice Bear, for each round he has chosen the round’s si and ei in advance. Lee will start the first round, the loser of each round will start the next round. The winner of the last round is the winner of the game, and the loser of the last round is the loser of the game. Determine if Lee can be the winner independent of Ice Bear’s moves or not. Also, determine if Lee can be the loser independent of Ice Bear’s moves or not. # Input The first line contains the integer t (1 ≤ t ≤ 10 5) x16 the number of rounds the game has. Then t lines follow, each contains two integers si and ei (1 ≤ si ≤ ei ≤ 10 18 ) x16 the i-th round’s information. The rounds are played in the same order as given in input, si and ei for all rounds are known to everyone before the game starts. # Output Print two integers. The first one should be 1 if Lee can be the winner independent of Ice Bear’s moves, and 0 otherwise. The second one should be 1 if Lee can be the loser independent of Ice Bear’s moves, and 0 otherwise. Page 1 of 2 Examples standard input standard output 35 8 1 4 3 10 1 1 41 2 2 3 3 4 4 5 0 0 11 1 0 1 21 9 4 5 0 0 21 2 2 8 1 0 6216986951114298167 235031205335543871 148302405431848579 455670351549314242 506251128322958430 575521452907339082 1 768614336404564650 189336074809158272 622104412002885672 588320087414024192 662540324268197150 1 0 # Note Remember, whoever writes an integer greater than ei loses. Page 2 of 2
2,700
false
false
false
true
false
false
false
false
false
false
3,822
696A
Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. There exists a bidirectional road between intersections _i_ and 2_i_ and another road between _i_ and 2_i_u2009+u20091 for every positive integer _i_. You can clearly see that there exists a unique shortest path between any two intersections. Initially anyone can pass any road for free. But since SlapsGiving is ahead of us, there will _q_ consecutive events happen soon. There are two types of events: 1. Government makes a new rule. A rule can be denoted by integers _v_, _u_ and _w_. As the result of this action, the passing fee of all roads on the shortest path from _u_ to _v_ increases by _w_ dollars. 2. Barney starts moving from some intersection _v_ and goes to intersection _u_ where there's a girl he wants to cuddle (using his fake name Lorenzo Von Matterhorn). He always uses the shortest path (visiting minimum number of intersections or roads) between two intersections. Government needs your calculations. For each time Barney goes to cuddle a girl, you need to tell the government how much money he should pay (sum of passing fee of all roads he passes). Input The first line of input contains a single integer _q_ (1u2009≤u2009_q_u2009≤u20091u2009000). The next _q_ lines contain the information about the events in chronological order. Each event is described in form 1 _v_ _u_ _w_ if it's an event when government makes a new rule about increasing the passing fee of all roads on the shortest path from _u_ to _v_ by _w_ dollars, or in form 2 _v_ _u_ if it's an event when Barnie goes to cuddle from the intersection _v_ to the intersection _u_. 1u2009≤u2009_v_,u2009_u_u2009≤u20091018,u2009_v_u2009≠u2009_u_,u20091u2009≤u2009_w_u2009≤u2009109 states for every description line. Output For each event of second type print the sum of passing fee of all roads Barney passes in this event, in one line. Print the answers in chronological order of corresponding events. Example Input 7 1 3 4 30 1 4 1 2 1 3 6 8 2 4 3 1 6 1 40 2 3 7 2 2 4 Note In the example testcase: Here are the intersections used: 1. Intersections on the path are 3, 1, 2 and 4. 2. Intersections on the path are 4, 2 and 1. 3. Intersections on the path are only 3 and 6. 4. Intersections on the path are 4, 2, 1 and 3. Passing fee of roads on the path are 32, 32 and 30 in order. So answer equals to 32u2009+u200932u2009+u200930u2009=u200994. 5. Intersections on the path are 6, 3 and 1. 6. Intersections on the path are 3 and 7. Passing fee of the road between them is 0. 7. Intersections on the path are 2 and 4. Passing fee of the road between them is 32 (increased by 30 in the first event and by 2 in the second).
1,500
false
false
true
false
true
false
true
false
false
false
7,041
92A
There are _n_ walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number 2 sits to the left of the walrus number 1, the walrus number 3 sits to the left of the walrus number 2, ..., the walrus number 1 sits to the left of the walrus number _n_. The presenter has _m_ chips. The presenter stands in the middle of the circle and starts giving the chips to the walruses starting from walrus number 1 and moving clockwise. The walrus number _i_ gets _i_ chips. If the presenter can't give the current walrus the required number of chips, then the presenter takes the remaining chips and the process ends. Determine by the given _n_ and _m_ how many chips the presenter will get in the end. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u200950, 1u2009≤u2009_m_u2009≤u2009104) — the number of walruses and the number of chips correspondingly. Output Print the number of chips the presenter ended up with. Note In the first sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, four chips to the walrus number 4, then again one chip to the walrus number 1. After that the presenter runs out of chips. He can't give anything to the walrus number 2 and the process finishes. In the third sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, then again one chip to the walrus number 1. The presenter has one chip left and he can't give two chips to the walrus number 2, that's why the presenter takes the last chip.
800
true
false
true
false
false
false
false
false
false
false
9,517
1968G1
This is the easy version of the problem. In this version $$$l=r$$$. You are given a string $$$s$$$. For a fixed $$$k$$$, consider a division of $$$s$$$ into exactly $$$k$$$ continuous substrings $$$w_1,dots,w_k$$$. Let $$$f_k$$$ be the maximal possible $$$LCP(w_1,dots,w_k)$$$ among all divisions. $$$LCP(w_1,dots,w_m)$$$ is the length of the Longest Common Prefix of the strings $$$w_1,dots,w_m$$$. For example, if $$$s=abababcab$$$ and $$$k=4$$$, a possible division is $$$color{red}{ab}color{blue}{ab}color{orange}{abc}color{green}{ab}$$$. The $$$LCP(color{red}{ab},color{blue}{ab},color{orange}{abc},color{green}{ab})$$$ is $$$2$$$, since $$$ab$$$ is the Longest Common Prefix of those four strings. Note that each substring consists of a continuous segment of characters and each character belongs to exactly one substring. Your task is to find $$$f_l,f_{l+1},dots,f_r$$$. In this version $$$l=r$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$l$$$, $$$r$$$ ($$$1 le l = r le n le 2 cdot 10^5$$$)xa0— the length of the string and the given range. The second line of each test case contains string $$$s$$$ of length $$$n$$$, all characters are lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output $$$r-l+1$$$ values: $$$f_l,dots,f_r$$$. Note In the first sample $$$n=k$$$, so the only division of $$$aba$$$ is $$$color{red}acolor{blue}bcolor{orange}a$$$. The answer is zero, because those strings do not have a common prefix. In the second sample, the only division is $$$color{red}acolor{blue}acolor{orange}a$$$. Their longest common prefix is one.
1,900
false
false
false
true
true
false
false
true
false
false
494
1436F
Problem - 1436F - 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 number theory *2800 No tag edit access → Contest materials based on Final Andersen Programming Contest 2020") . Editorial") : the number of different values in the multiset $$$S$$$. Each of the next $$$m$$$ lines contains two integers $$$a_i$$$, $$$freq_i$$$ ($$$1 le a_i le 10^5, 1 le freq_i le 10^9$$$). Element $$$a_i$$$ appears in the multiset $$$S$$$ $$$freq_i$$$ times. All $$$a_i$$$ are different. Output Print the required sum, modulo $$$998,244,353$$$. Examples Input 2 1 1 2 1 Output 9 Input 4 1 1 2 1 3 1 6 1 Output 1207 Input 1 1 5 Output 560 Note A multiset is a set where elements are allowed to coincide. $$$X$$$ is the cardinality of a set $$$X$$$, the number of elements in it. $$$A subset B$$$: Set $$$A$$$ is a subset of a set $$$B$$$. In the first example $$$B={1}, A={1,2}$$$ and $$$B={2}, A={1, 2}$$$ have a product equal to $$$1cdot3 + 2cdot3=9$$$. Other pairs of $$$A$$$ and $$$B$$$ don't satisfy the given constraints.
2,800
true
false
false
false
false
false
false
false
false
false
3,486
250B
An IPv6-address is a 128-bit number. For convenience, this number is recorded in blocks of 16 bits in hexadecimal record, the blocks are separated by colons — 8 blocks in total, each block has four hexadecimal digits. Here is an example of the correct record of a IPv6 address: "0124:5678:90ab:cdef:0124:5678:90ab:cdef". We'll call such format of recording an IPv6-address full. Besides the full record of an IPv6 address there is a short record format. The record of an IPv6 address can be shortened by removing one or more leading zeroes at the beginning of each block. However, each block should contain at least one digit in the short format. For example, the leading zeroes can be removed like that: "a56f:00d3:0000:0124:0001:f19a:1000:0000" u2009→u2009 "a56f:d3:0:0124:01:f19a:1000:00". There are more ways to shorten zeroes in this IPv6 address. Some IPv6 addresses contain long sequences of zeroes. Continuous sequences of 16-bit zero blocks can be shortened to "::". A sequence can consist of one or several consecutive blocks, with all 16 bits equal to 0. You can see examples of zero block shortenings below: "a56f:00d3:0000:0124:0001:0000:0000:0000" u2009→u2009 "a56f:00d3:0000:0124:0001::"; "a56f:0000:0000:0124:0001:0000:1234:0ff0" u2009→u2009 "a56f::0124:0001:0000:1234:0ff0"; "a56f:0000:0000:0000:0001:0000:1234:0ff0" u2009→u2009 "a56f:0000::0000:0001:0000:1234:0ff0"; "a56f:00d3:0000:0124:0001:0000:0000:0000" u2009→u2009 "a56f:00d3:0000:0124:0001::0000"; "0000:0000:0000:0000:0000:0000:0000:0000" u2009→u2009 "::". It is not allowed to shorten zero blocks in the address more than once. This means that the short record can't contain the sequence of characters "::" more than once. Otherwise, it will sometimes be impossible to determine the number of zero blocks, each represented by a double colon. The format of the record of the IPv6 address after removing the leading zeroes and shortening the zero blocks is called short. You've got several short records of IPv6 addresses. Restore their full record. Input The first line contains a single integer _n_ — the number of records to restore (1u2009≤u2009_n_u2009≤u2009100). Each of the following _n_ lines contains a string — the short IPv6 addresses. Each string only consists of string characters "0123456789abcdef:". It is guaranteed that each short address is obtained by the way that is described in the statement from some full IPv6 address.
1,500
false
false
true
false
false
false
false
false
false
false
8,835
1981B
There is a sequence $$$a_0, a_1, a_2, ldots$$$ of infinite length. Initially $$$a_i = i$$$ for every non-negative integer $$$i$$$. After every second, each element of the sequence will simultaneously change. $$$a_i$$$ will change to $$$a_{i - 1} mid a_i mid a_{i + 1}$$$ for every positive integer $$$i$$$. $$$a_0$$$ will change to $$$a_0 mid a_1$$$. Here, $$$$$$ denotes . The description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$0 le n, m le 10^9$$$). Output For each test case, output a single integer — the value of $$$a_n$$$ after $$$m$$$ seconds. Example Input 9 0 0 0 1 0 2 1 0 5 2 10 1 20 3 1145 14 19198 10 Output 0 1 3 1 7 11 23 1279 19455 Note After $$$1$$$ second, $$$[a_0, a_1, a_2, a_3, a_4, a_5]$$$ will become $$$[1, 3, 3, 7, 7, 7]$$$. After $$$2$$$ seconds, $$$[a_0, a_1, a_2, a_3, a_4, a_5]$$$ will become $$$[3, 3, 7, 7, 7, 7]$$$.
1,300
true
false
false
false
false
false
false
false
false
false
401
274C
Problem - 274C - 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 geometry *2600 No tag edit access → Contest materials . At the time zero circles start to grow simultaneously. In other words, the radius of each circle at time _t_ (_t_u2009>u20090) is equal to _t_. The circles are drawn as black discs on an infinite white plane. So at each moment the plane consists of several black and white regions. Note that the circles may overlap while growing. We define a hole as a closed, connected white region. For instance, the figure contains two holes shown by red border. During growing some holes may be created and it is easy to see that each created hole will disappear eventually. Luyi asks you to find moment of time such that the last hole disappears. In other words, you should find the first moment such that no hole can be seen after that. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100). Each of the next _n_ lines contains two integers _x__i_ and _y__i_ (u2009-u2009104u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009104), indicating the location of _i_-th circle. It's guaranteed that no two circles are centered at the same point. Output Print the moment where the last hole disappears. If there exists no moment in which we can find holes print -1. The answer will be considered correct if the absolute or relative error does not exceed 10u2009-u20094. Examples Input 3 0 0 1 1 2 2 Output -1 Input 4 0 0 0 2 2 2 2 0 Output 1.414214 Input 4 0 1 0 -1 -2 0 4 0 Output 2.125000
2,600
false
false
false
false
false
false
true
false
false
false
8,737
1674A
You are given two integers $$$x$$$ and $$$y$$$. You want to choose two strictly positive (greater than zero) integers $$$a$$$ and $$$b$$$, and then apply the following operation to $$$x$$$ exactly $$$a$$$ times: replace $$$x$$$ with $$$b cdot x$$$. You want to find two positive integers $$$a$$$ and $$$b$$$ such that $$$x$$$ becomes equal to $$$y$$$ after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it. For example: if $$$x = 3$$$ and $$$y = 75$$$, you may choose $$$a = 2$$$ and $$$b = 5$$$, so that $$$x$$$ becomes equal to $$$3 cdot 5 cdot 5 = 75$$$; if $$$x = 100$$$ and $$$y = 100$$$, you may choose $$$a = 3$$$ and $$$b = 1$$$, so that $$$x$$$ becomes equal to $$$100 cdot 1 cdot 1 cdot 1 = 100$$$; if $$$x = 42$$$ and $$$y = 13$$$, there is no answer since you cannot decrease $$$x$$$ with the given operations. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of one line containing two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le 100$$$). Output If it is possible to choose a pair of positive integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$ after the aforementioned process, print these two integers. The integers you print should be not less than $$$1$$$ and not greater than $$$10^9$$$ (it can be shown that if the answer exists, there is a pair of integers $$$a$$$ and $$$b$$$ meeting these constraints). If there are multiple such pairs, print any of them. If it is impossible to choose a pair of integers $$$a$$$ and $$$b$$$ so that $$$x$$$ becomes $$$y$$$, print the integer $$$0$$$ twice.
800
true
false
false
false
false
true
false
false
false
false
2,229
1764B
Everybody! Doremy's Perfect Math Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's math class, Doremy is teaching everyone subtraction. Now she gives you a quiz to prove that you are paying attention in class. You are given a set $$$S$$$ containing positive integers. You may perform the following operation some (possibly zero) number of times: choose two integers $$$x$$$ and $$$y$$$ from the set $$$S$$$ such that $$$x > y$$$ and $$$x - y$$$ is not in the set $$$S$$$. add $$$x-y$$$ into the set $$$S$$$. You need to tell Doremy the maximum possible number of integers in $$$S$$$ if the operations are performed optimally. It can be proven that this number is finite. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line contains an integer $$$n$$$ ($$$2 le nle 10^5$$$)xa0— the size of the set $$$S$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,dots,a_n$$$ ($$$1le a_1 < a_2 < cdots < a_n le 10^9$$$)xa0— the positive integers in $$$S$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, you need to output the maximum possible number of integers in $$$S$$$. It can be proven that this value is finite. Note In the first test case, no such $$$x$$$ and $$$y$$$ exist. The maximum possible number of integers in $$$S$$$ is $$$2$$$. In the second test case, $$$S={5,10,25}$$$ at first. You can choose $$$x=25$$$, $$$y=10$$$, then add $$$x-y=15$$$ to the set. $$$S={5,10,15,25}$$$ now. You can choose $$$x=25$$$, $$$y=5$$$, then add $$$x-y=20$$$ to the set. $$$S={5,10,15,20,25}$$$ now. You can not perform any operation now. After performing all operations, the number of integers in $$$S$$$ is $$$5$$$. It can be proven that no other sequence of operations allows $$$S$$$ to contain more than $$$5$$$ integers.
900
true
false
false
false
false
false
false
false
false
false
1,726
1951C
As the CEO of a startup company, you want to reward each of your $$$k$$$ employees with a ticket to the upcoming concert. The tickets will be on sale for $$$n$$$ days, and by some time travelling, you have predicted that the price per ticket at day $$$i$$$ will be $$$a_i$$$. However, to prevent ticket hoarding, the concert organizers have implemented the following measures: A person may purchase no more than $$$m$$$ tickets per day. If a person purchases $$$x$$$ tickets on day $$$i$$$, all subsequent days (i.e. from day $$$i+1$$$ onwards) will have their prices per ticket increased by $$$x$$$. For example, if $$$a = [1, 3, 8, 4, 5]$$$ and you purchase $$$2$$$ tickets on day $$$1$$$, they will cost $$$2$$$ in total, and the prices from day $$$2$$$ onwards will become $$$[5, 10, 6, 7]$$$. If you then purchase $$$3$$$ more tickets on day $$$2$$$, they will cost in total an additional $$$15$$$, and the prices from day $$$3$$$ onwards will become $$$[13, 9, 10]$$$. Find the minimum spending to purchase $$$k$$$ tickets. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 le n le 3 cdot 10^5, 1 le m le 10^9, 1 le k le min(nm, 10^9)$$$)xa0— the number of sale days, the maximum amount of ticket purchasable each day, and the number of tickets to be bought at the end. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the price per ticket for each of the upcoming $$$n$$$ days. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print one integer: the minimum amount of money needed to purchase exactly $$$k$$$ tickets. Example Input 4 4 2 3 8 6 4 2 4 2 8 8 6 4 2 5 100 1 10000 1 100 10 1000 6 3 9 5 5 5 5 5 5 Note In the first test case, one optimal way to buy $$$3$$$ tickets is as follows: Buy $$$0$$$ tickets on the first day. The prices per ticket for the remaining days are $$$[6, 4, 2]$$$. Buy $$$0$$$ tickets on the second day. The prices per ticket for the remaining days are $$$[4, 2]$$$. Buy $$$1$$$ ticket on the third day with cost $$$4$$$. The price per ticket for the remaining day is $$$[3]$$$. Buy $$$2$$$ tickets on the fourth day with cost $$$6$$$. In the second test case, there is only one way to buy $$$8$$$ tickets: Buy $$$2$$$ tickets on the first day with cost $$$16$$$. The prices per ticket for the remaining days are $$$[8, 6, 4]$$$. Buy $$$2$$$ tickets on the second day with cost $$$16$$$. The prices per ticket for the remaining days are $$$[8, 6]$$$. Buy $$$2$$$ tickets on the third day with cost $$$16$$$. The price per ticket for the remaining day is $$$[8]$$$. Buy $$$2$$$ tickets on the fourth day with cost $$$16$$$.
1,400
true
true
false
false
false
false
false
false
true
false
572
721D
Recently Maxim has found an array of _n_ integers, needed by no one. He immediately come up with idea of changing it: he invented positive integer _x_ and decided to add or subtract it from arbitrary array elements. Formally, by applying single operation Maxim chooses integer _i_ (1u2009≤u2009_i_u2009≤u2009_n_) and replaces the _i_-th element of array _a__i_ either with _a__i_u2009+u2009_x_ or with _a__i_u2009-u2009_x_. Please note that the operation may be applied more than once to the same position. Maxim is a curious minimalis, thus he wants to know what is the minimum value that the product of all array elements (i.e. ) can reach, if Maxim would apply no more than _k_ operations to it. Please help him in that. Input The first line of the input contains three integers _n_,u2009_k_ and _x_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009200u2009000,u20091u2009≤u2009_x_u2009≤u2009109)xa0— the number of elements in the array, the maximum number of operations and the number invented by Maxim, respectively. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ ()xa0— the elements of the array found by Maxim. Output Print _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ in the only linexa0— the array elements after applying no more than _k_ operations to the array. In particular, should stay true for every 1u2009≤u2009_i_u2009≤u2009_n_, but the product of all array elements should be minimum possible. If there are multiple answers, print any of them.
2,000
true
true
false
false
true
true
false
false
false
false
6,928
71B
A progress bar is an element of graphical interface that displays the progress of a process for this very moment before it is completed. Let's take a look at the following form of such a bar. A bar is represented as _n_ squares, located in line. To add clarity, let's number them with positive integers from 1 to _n_ from the left to the right. Each square has saturation (_a__i_ for the _i_-th square), which is measured by an integer from 0 to _k_. When the bar for some _i_ (1u2009≤u2009_i_u2009≤u2009_n_) is displayed, squares 1,u20092,u2009... ,u2009_i_u2009-u20091 has the saturation _k_, squares _i_u2009+u20091,u2009_i_u2009+u20092,u2009... ,u2009_n_ has the saturation 0, and the saturation of the square _i_ can have any value from 0 to _k_. So some first squares of the progress bar always have the saturation _k_. Some last squares always have the saturation 0. And there is no more than one square that has the saturation different from 0 and _k_. The degree of the process's completion is measured in percents. Let the process be _t_% completed. Then the following inequation is fulfilled: An example of such a bar can be seen on the picture. For the given _n_, _k_, _t_ determine the measures of saturation for all the squares _a__i_ of the progress bar. Input We are given 3 space-separated integers _n_, _k_, _t_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009100, 0u2009≤u2009_t_u2009≤u2009100).
1,300
true
false
true
false
false
false
false
false
false
false
9,615
275A
Problem - 275A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *900 No tag edit access → Contest materials
900
false
false
true
false
false
false
false
false
false
false
8,734
1054A
Masha lives in a multi-storey building, where floors are numbered with positive integers. Two floors are called adjacent if their numbers differ by one. Masha decided to visit Egor. Masha lives on the floor $$$x$$$, Egor on the floor $$$y$$$ (not on the same floor with Masha). The house has a staircase and an elevator. If Masha uses the stairs, it takes $$$t_1$$$ seconds for her to walk between adjacent floors (in each direction). The elevator passes between adjacent floors (in each way) in $$$t_2$$$ seconds. The elevator moves with doors closed. The elevator spends $$$t_3$$$ seconds to open or close the doors. We can assume that time is not spent on any action except moving between adjacent floors and waiting for the doors to open or close. If Masha uses the elevator, it immediately goes directly to the desired floor. Coming out of the apartment on her floor, Masha noticed that the elevator is now on the floor $$$z$$$ and has closed doors. Now she has to choose whether to use the stairs or use the elevator. If the time that Masha needs to get to the Egor's floor by the stairs is strictly less than the time it will take her using the elevator, then she will use the stairs, otherwise she will choose the elevator. Help Mary to understand whether to use the elevator or the stairs. Input The only line contains six integers $$$x$$$, $$$y$$$, $$$z$$$, $$$t_1$$$, $$$t_2$$$, $$$t_3$$$ ($$$1 leq x, y, z, t_1, t_2, t_3 leq 1000$$$)xa0— the floor Masha is at, the floor Masha wants to get to, the floor the elevator is located on, the time it takes Masha to pass between two floors by stairs, the time it takes the elevator to pass between two floors and the time it takes for the elevator to close or open the doors. It is guaranteed that $$$x e y$$$. Output If the time it will take to use the elevator is not greater than the time it will take to use the stairs, print «YES» (without quotes), otherwise print «NO> (without quotes). You can print each letter in any case (upper or lower). Note In the first example: If Masha goes by the stairs, the time she spends is $$$4 cdot 4 = 16$$$, because she has to go $$$4$$$ times between adjacent floors and each time she spends $$$4$$$ seconds. If she chooses the elevator, she will have to wait $$$2$$$ seconds while the elevator leaves the $$$4$$$-th floor and goes to the $$$5$$$-th. After that the doors will be opening for another $$$1$$$ second. Then Masha will enter the elevator, and she will have to wait for $$$1$$$ second for the doors closing. Next, the elevator will spend $$$4 cdot 2 = 8$$$ seconds going from the $$$5$$$-th floor to the $$$1$$$-st, because the elevator has to pass $$$4$$$ times between adjacent floors and spends $$$2$$$ seconds each time. And finally, it will take another $$$1$$$ second before the doors are open and Masha can come out. Thus, all the way by elevator will take $$$2 + 1 + 1 + 8 + 1 = 13$$$ seconds, which is less than $$$16$$$ seconds, so Masha has to choose the elevator. In the second example, it is more profitable for Masha to use the stairs, because it will take $$$13$$$ seconds to use the elevator, that is more than the $$$10$$$ seconds it will takes to go by foot. In the third example, the time it takes to use the elevator is equal to the time it takes to walk up by the stairs, and is equal to $$$12$$$ seconds. That means Masha will take the elevator.
800
false
false
true
false
false
false
false
false
false
false
5,447
314D
Problem - 314D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search data structures geometry sortings two pointers *2500 No tag edit access → Contest materials and (_x_2,u2009_y_2) equal _x_1u2009-u2009_x_2u2009+u2009_y_1u2009-u2009_y_2. The distance between the point and the straight lines is the minimum distance from the point to some point belonging to one of the lines. Help Sereja, find the maximum distance from the points to the optimally located straight lines. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). Next _n_ lines contain the coordinates of the lines. The _i_-th line contains two integers _x__i_,u2009_y__i_ (_x__i_,u2009_y__i_u2009≤u2009109). Output In a single line print a real number — the answer to the problem. Your answer will be considered correct iff its absolute or relative error doesn't exceed 10u2009-u20096. Examples Input 4 0 0 2 0 0 2 2 2 Output 0.000000000000000 Input 4 1 0 0 1 2 1 1 2 Output 1.000000000000000
2,500
false
false
false
false
true
false
false
true
true
false
8,587
1228B
Suppose there is a $$$h imes w$$$ grid consisting of empty or full cells. Let's make some definitions: $$$r_{i}$$$ is the number of consecutive full cells connected to the left side in the $$$i$$$-th row ($$$1 le i le h$$$). In particular, $$$r_i=0$$$ if the leftmost cell of the $$$i$$$-th row is empty. $$$c_{j}$$$ is the number of consecutive full cells connected to the top end in the $$$j$$$-th column ($$$1 le j le w$$$). In particular, $$$c_j=0$$$ if the topmost cell of the $$$j$$$-th column is empty. In other words, the $$$i$$$-th row starts exactly with $$$r_i$$$ full cells. Similarly, the $$$j$$$-th column starts exactly with $$$c_j$$$ full cells. These are the $$$r$$$ and $$$c$$$ values of some $$$3 imes 4$$$ grid. Black cells are full and white cells are empty. You have values of $$$r$$$ and $$$c$$$. Initially, all cells are empty. Find the number of ways to fill grid cells to satisfy values of $$$r$$$ and $$$c$$$. Since the answer can be very large, find the answer modulo $$$1000000007,(10^{9} + 7)$$$. In other words, find the remainder after division of the answer by $$$1000000007,(10^{9} + 7)$$$. Input The first line contains two integers $$$h$$$ and $$$w$$$ ($$$1 le h, w le 10^{3}$$$)xa0— the height and width of the grid. The second line contains $$$h$$$ integers $$$r_{1}, r_{2}, ldots, r_{h}$$$ ($$$0 le r_{i} le w$$$)xa0— the values of $$$r$$$. The third line contains $$$w$$$ integers $$$c_{1}, c_{2}, ldots, c_{w}$$$ ($$$0 le c_{j} le h$$$)xa0— the values of $$$c$$$. Output Print the answer modulo $$$1000000007,(10^{9} + 7)$$$. Examples Input 19 16 16 16 16 16 15 15 0 5 0 4 9 9 1 4 4 0 8 16 12 6 12 19 15 8 6 19 19 14 6 9 16 10 11 15 4 Note In the first example, this is the other possible case. In the second example, it's impossible to make a grid to satisfy such $$$r$$$, $$$c$$$ values. In the third example, make sure to print answer modulo $$$(10^9 + 7)$$$.
1,400
true
false
true
false
false
false
false
false
false
false
4,534
704C
Natalia Romanova is trying to test something on the new gun S.H.I.E.L.D gave her. In order to determine the result of the test, she needs to find the number of answers to a certain equation. The equation is of form: Where represents logical OR and represents logical exclusive OR (XOR), and _v__i_,u2009_j_ are some boolean variables or their negations. Natalia calls the left side of the equation a XNF formula. Each statement in brackets is called a clause, and _v__i_,u2009_j_ are called literals. In the equation Natalia has, the left side is actually a 2-XNF-2 containing variables _x_1,u2009_x_2,u2009...,u2009_x__m_ and their negations. An XNF formula is 2-XNF-2 if: 1. For each 1u2009≤u2009_i_u2009≤u2009_n_, _k__i_u2009≤u20092, i.e. the size of each clause doesn't exceed two. 2. Each variable occurs in the formula at most two times (with negation and without negation in total). Please note that it's possible that a variable occurs twice but its negation doesn't occur in any clause (or vice versa). Natalia is given a formula of _m_ variables, consisting of _n_ clauses. Please, make sure to check the samples in order to properly understand how the formula looks like. Natalia is more into fight than theory, so she asked you to tell her the number of answers to this equation. More precisely, you need to find the number of ways to set _x_1,u2009...,u2009_x__m_ with _true_ and _false_ (out of total of 2_m_ ways) so that the equation is satisfied. Since this number can be extremely large, you need to print the answer modulo 109u2009+u20097. Please, note that some variable may appear twice in one clause, or not appear in the equation at all (but still, setting it to _false_ or _true_ gives different ways to set variables). Input The first line of input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100u2009000)xa0— the number of clauses and the number of variables respectively. The next _n_ lines contain the formula. The _i_-th of them starts with an integer _k__i_xa0— the number of literals in the _i_-th clause. It is followed by _k__i_ non-zero integers _a__i_,u20091,u2009...,u2009_a__i_,u2009_k__i_. If _a__i_,u2009_j_u2009>u20090 then _v__i_,u2009_j_ is _x__a__i_,u2009_j_ otherwise it's negation of _x_u2009-u2009_a__i_,u2009_j_ (1u2009≤u2009_k__i_u2009≤u20092, u2009-u2009_m_u2009≤u2009_a__i_,u2009_j_u2009≤u2009_m_, _a__i_,u2009_j_u2009≠u20090). Note The equation in the first sample is: The equation in the second sample is: The equation in the third sample is:
2,900
true
false
true
true
false
false
false
false
false
true
7,004
1389B
You are given an array $$$a_1, a_2, dots, a_n$$$, consisting of $$$n$$$ positive integers. Initially you are standing at index $$$1$$$ and have a score equal to $$$a_1$$$. You can perform two kinds of moves: 1. move rightxa0— go from your current index $$$x$$$ to $$$x+1$$$ and add $$$a_{x+1}$$$ to your score. This move can only be performed if $$$x<n$$$. 2. move leftxa0— go from your current index $$$x$$$ to $$$x-1$$$ and add $$$a_{x-1}$$$ to your score. This move can only be performed if $$$x>1$$$. Also, you can't perform two or more moves to the left in a row. You want to perform exactly $$$k$$$ moves. Also, there should be no more than $$$z$$$ moves to the left among them. What is the maximum score you can achieve? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains three integers $$$n, k$$$ and $$$z$$$ ($$$2 le n le 10^5$$$, $$$1 le k le n - 1$$$, $$$0 le z le min(5, k)$$$)xa0— the number of elements in the array, the total number of moves you should perform and the maximum number of moves to the left you can perform. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^4$$$)xa0— the given array. The sum of $$$n$$$ over all testcases does not exceed $$$3 cdot 10^5$$$. Output Print $$$t$$$ integersxa0— for each testcase output the maximum score you can achieve if you make exactly $$$k$$$ moves in total, no more than $$$z$$$ of them are to the left and there are no two or more moves to the left in a row. Example Input 4 5 4 0 1 5 4 3 2 5 4 1 1 5 4 3 2 5 4 4 10 20 30 40 50 10 7 3 4 6 8 2 9 9 7 4 10 9 Note In the first testcase you are not allowed to move left at all. So you make four moves to the right and obtain the score $$$a_1 + a_2 + a_3 + a_4 + a_5$$$. In the second example you can move one time to the left. So we can follow these moves: right, right, left, right. The score will be $$$a_1 + a_2 + a_3 + a_2 + a_3$$$. In the third example you can move four times to the left but it's not optimal anyway, you can just move four times to the right and obtain the score $$$a_1 + a_2 + a_3 + a_4 + a_5$$$.
1,600
false
true
false
true
false
false
true
false
false
false
3,727
2006C
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $$$x eq y$$$ from the set such that their average $$$frac{x+y}2$$$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $$$x$$$ and $$$y$$$ remain in the set. Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $$$1$$$. For example, sets $$${2}$$$, $$${2, 5, 4, 3}$$$, $$${5, 6, 8, 7}$$$ are consecutive, while $$${2, 4, 5, 6}$$$, $$${9, 7}$$$ are not. Eri likes consecutive sets. Suppose there is an array $$$b$$$, then Eri puts all elements in $$$b$$$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $$$b$$$ will be called brilliant. Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers. Eri has an array $$$a$$$ of $$$n$$$ positive integers. Please help him to count the number of pairs of integers $$$(l,r)$$$ such that $$$1 leq l leq r leq n$$$ and the subarray $$$a_l, a_{l+1}, ldots, a_r$$$ is brilliant. Input Each test 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 a single integer $$$n$$$ ($$$1 leq n leq 4 cdot 10^5$$$)xa0— length 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$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$4 cdot 10^5$$$. Note In the first test case, the array $$$a = [2, 2]$$$ has $$$3$$$ subarrays: $$$[2]$$$, $$$[2]$$$, and $$$[2, 2]$$$. For all of them, the set only contains a single integer $$$2$$$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $$$3$$$. In the second test case, let's consider the subarray $$$[3, 6, 10]$$$. We can do operations as follows: $$$$$${3,6,10} xrightarrow{x=6,y=10} {3,6,8,10} xrightarrow{x=6,y=8} {3,6,7,8,10} xrightarrow{x=3,y=7} {3,5,6,7,8,10}$$$$$$ $$$$$$xrightarrow{x=3,y=5} {3,4,5,6,7,8,10} xrightarrow{x=8,y=10} {3,4,5,6,7,8,9,10}$$$$$$ $$${3,4,5,6,7,8,9,10}$$$ is a consecutive set, so the subarray $$$[3, 6, 10]$$$ is brilliant.
2,300
true
false
false
false
true
false
false
false
false
false
215
467E
Problem - 467E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures dp greedy *2300 No tag edit access → Contest materials . And finally... Alex had given this complicated task to George, and George gave it to you. Help George to cope with the task. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095·105). The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Output In the first line print a single integer 4_m_ — the maximal possible length of required sequence _b_. In the second line print 4_m_ integers _b_1,u2009_b_2,u2009...,u2009_b_4_m_, that is required sequence. If there are multiple optimal answers you may print any of them. Examples Input 4 3 5 3 5 Output 4 3 5 3 5 Input 10 35 1 2 1 2 35 100 200 100 200 Output 8 1 2 1 2 100 200 100 200
2,300
false
true
false
true
true
false
false
false
false
false
7,968
1808E2
This is the medium version of the problem. The only difference between the three versions is the constraints on $$$n$$$ and $$$k$$$. You can make hacks only if all versions of the problem are solved. Maxim is a minibus driver on Venus. To ride on Maxim's minibus, you need a ticket. Each ticket has a number consisting of $$$n$$$ digits. However, as we know, the residents of Venus use a numeral system with base $$$k$$$, rather than the decimal system. Therefore, the ticket number can be considered as a sequence of $$$n$$$ integers from $$$0$$$ to $$$k-1$$$, inclusive. The residents of Venus consider a ticket to be lucky if there is a digit on it that is equal to the sum of the remaining digits, modulo $$$k$$$. For example, if $$$k=10$$$, then the ticket $$$7135$$$ is lucky because $$$7 + 1 + 5 equiv 3 pmod{10}$$$. On the other hand, the ticket $$$7136$$$ is not lucky because no digit is equal to the sum of the others modulo $$$10$$$. Once, while on a trip, Maxim wondered: how many lucky tickets exist? At the same time, Maxim understands that this number can be very large, so he is interested only in the answer modulo some prime number $$$m$$$. Input The only line of the input contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 le n le 10^{18}$$$, $$$1 le k le 100$$$, $$$10^8 le m le 10^9 + 7$$$, $$$m$$$ is a prime number)xa0— the number of digits on the ticket, the base of the numeral system on Venus, and the module for answer calculation. Note In the first example, there are only four lucky tickets: $$$000$$$, $$$011$$$, $$$101$$$, and $$$110$$$.
2,500
false
false
false
true
false
false
false
false
false
false
1,422
522A
One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on. These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed. Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive. We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user. Output Print a single integer — the maximum length of a repost chain. Examples Input 5 tourist reposted Polycarp Petr reposted Tourist WJMZBMR reposted Petr sdya reposted wjmzbmr vepifanov reposted sdya Input 6 Mike reposted Polycarp Max reposted Polycarp EveryOne reposted Polycarp 111 reposted Polycarp VkCup reposted Polycarp Codeforces reposted Polycarp Input 1 SoMeStRaNgEgUe reposted PoLyCaRp
1,200
false
false
false
true
false
false
false
false
false
true
7,750
1389G
You are given an undirected connected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. $$$k$$$ vertices of this graph are special. You have to direct each edge of this graph or leave it undirected. If you leave the $$$i$$$-th edge undirected, you pay $$$w_i$$$ coins, and if you direct it, you don't have to pay for it. Let's call a vertex saturated if it is reachable from each special vertex along the edges of the graph (if an edge is undirected, it can be traversed in both directions). After you direct the edges of the graph (possibly leaving some of them undirected), you receive $$$c_i$$$ coins for each saturated vertex $$$i$$$. Thus, your total profit can be calculated as $$$sum limits_{i in S} c_i - sum limits_{j in U} w_j$$$, where $$$S$$$ is the set of saturated vertices, and $$$U$$$ is the set of edges you leave undirected. For each vertex $$$i$$$, calculate the maximum possible profit you can get if you have to make the vertex $$$i$$$ saturated. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 3 cdot 10^5$$$, $$$n - 1 le m le min(3 cdot 10^5, frac{n(n-1)}{2})$$$, $$$1 le k le n$$$). The second line contains $$$k$$$ pairwise distinct integers $$$v_1$$$, $$$v_2$$$, ..., $$$v_k$$$ ($$$1 le v_i le n$$$) — the indices of the special vertices. The third line contains $$$n$$$ integers $$$c_1$$$, $$$c_2$$$, ..., $$$c_n$$$ ($$$0 le c_i le 10^9$$$). The fourth line contains $$$m$$$ integers $$$w_1$$$, $$$w_2$$$, ..., $$$w_m$$$ ($$$0 le w_i le 10^9$$$). Then $$$m$$$ lines follow, the $$$i$$$-th line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$) — the endpoints of the $$$i$$$-th edge. There is at most one edge between each pair of vertices. Output Print $$$n$$$ integers, where the $$$i$$$-th integer is the maximum profit you can get if you have to make the vertex $$$i$$$ saturated. Examples Input 3 2 2 1 3 11 1 5 10 10 1 2 2 3 Input 4 4 4 1 2 3 4 1 5 7 8 100 100 100 100 1 2 2 3 3 4 1 4 Note Consider the first example: the best way to make vertex $$$1$$$ saturated is to direct the edges as $$$2 o 1$$$, $$$3 o 2$$$; $$$1$$$ is the only saturated vertex, so the answer is $$$11$$$; the best way to make vertex $$$2$$$ saturated is to leave the edge $$$1-2$$$ undirected and direct the other edge as $$$3 o 2$$$; $$$1$$$ and $$$2$$$ are the saturated vertices, and the cost to leave the edge $$$1-2$$$ undirected is $$$10$$$, so the answer is $$$2$$$; the best way to make vertex $$$3$$$ saturated is to direct the edges as $$$2 o 3$$$, $$$1 o 2$$$; $$$3$$$ is the only saturated vertex, so the answer is $$$5$$$. The best course of action in the second example is to direct the edges along the cycle: $$$1 o 2$$$, $$$2 o 3$$$, $$$3 o 4$$$ and $$$4 o 1$$$. That way, all vertices are saturated.
2,800
false
false
false
true
false
false
false
false
false
true
3,722
1553A
Problem - 1553A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math number theory *800 No tag edit access → Contest materials ") Editorial") $$$ to be the sum of digits of number $$$x$$$ written in decimal system. For example, $$$S(5) = 5$$$, $$$S(10) = 1$$$, $$$S(322) = 7$$$. We will call an integer $$$x$$$ interesting if $$$S(x + 1) < S(x)$$$. In each test you will be given one integer $$$n$$$. Your task is to calculate the number of integers $$$x$$$ such that $$$1 le x le n$$$ and $$$x$$$ is interesting. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) xa0— number of test cases. Then $$$t$$$ lines follow, the $$$i$$$-th line contains one integer $$$n$$$ ($$$1 le n le 10^9$$$) for the $$$i$$$-th test case. Output Print $$$t$$$ integers, the $$$i$$$-th should be the answer for the $$$i$$$-th test case. Example Input 5 1 9 10 34 880055535 Output 0 1 1 3 88005553 Note The first interesting number is equal to $$$9$$$.
800
true
false
false
false
false
false
false
false
false
false
2,875
802N
The marmots need to prepare _k_ problems for HC2 over _n_ days. Each problem, once prepared, also has to be printed. The preparation of a problem on day _i_ (at most one per day) costs _a__i_ CHF, and the printing of a problem on day _i_ (also at most one per day) costs _b__i_ CHF. Of course, a problem cannot be printed before it has been prepared (but doing both on the same day is fine). What is the minimum cost of preparation and printing? Input The first line of input contains two space-separated integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20092200). The second line contains _n_ space-separated integers _a_1,u2009...,u2009_a__n_ () — the preparation costs. The third line contains _n_ space-separated integers _b_1,u2009...,u2009_b__n_ () — the printing costs. Output Output the minimum cost of preparation and printing _k_ problems — that is, the minimum possible sum _a__i_1u2009+u2009_a__i_2u2009+u2009...u2009+u2009_a__i__k_u2009+u2009_b__j_1u2009+u2009_b__j_2u2009+u2009...u2009+u2009_b__j__k_, where 1u2009≤u2009_i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__k_u2009≤u2009_n_, 1u2009≤u2009_j_1u2009<u2009_j_2u2009<u2009...u2009<u2009_j__k_u2009≤u2009_n_ and _i_1u2009≤u2009_j_1, _i_2u2009≤u2009_j_2, ..., _i__k_u2009≤u2009_j__k_. Example Input 8 4 3 8 7 9 9 4 6 8 2 5 9 4 3 8 9 1 Note In the sample testcase, one optimum solution is to prepare the first problem on day 1 and print it on day 1, prepare the second problem on day 2 and print it on day 4, prepare the third problem on day 3 and print it on day 5, and prepare the fourth problem on day 6 and print it on day 8.
2,400
false
false
false
false
false
false
false
true
false
true
6,554
1054B
Initially Ildar has an empty array. He performs $$$n$$$ steps. On each step he takes a subset of integers already added to the array and appends the mex of this subset to the array. The mex of an multiset of integers is the smallest non-negative integer not presented in the multiset. For example, the mex of the multiset $$$[0, 2, 3]$$$ is $$$1$$$, while the mex of the multiset $$$[1, 2, 1]$$$ is $$$0$$$. More formally, on the step $$$m$$$, when Ildar already has an array $$$a_1, a_2, ldots, a_{m-1}$$$, he chooses some subset of indices $$$1 leq i_1 < i_2 < ldots < i_k < m$$$ (possibly, empty), where $$$0 leq k < m$$$, and appends the $$$mex(a_{i_1}, a_{i_2}, ldots a_{i_k})$$$ to the end of the array. After performing all the steps Ildar thinks that he might have made a mistake somewhere. He asks you to determine for a given array $$$a_1, a_2, ldots, a_n$$$ the minimum step $$$t$$$ such that he has definitely made a mistake on at least one of the steps $$$1, 2, ldots, t$$$, or determine that he could have obtained this array without mistakes. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 100,000$$$)xa0— the number of steps Ildar made. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$)xa0— the array Ildar obtained. Output If Ildar could have chosen the subsets on each step in such a way that the resulting array is $$$a_1, a_2, ldots, a_n$$$, print $$$-1$$$. Otherwise print a single integer $$$t$$$xa0— the smallest index of a step such that a mistake was made on at least one step among steps $$$1, 2, ldots, t$$$. Note In the first example it is possible that Ildar made no mistakes. Here is the process he could have followed. $$$1$$$-st step. The initial array is empty. He can choose an empty subset and obtain $$$0$$$, because the mex of an empty set is $$$0$$$. Appending this value to the end he gets the array $$$[0]$$$. $$$2$$$-nd step. The current array is $$$[0]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1]$$$. $$$3$$$-rd step. The current array is $$$[0,1]$$$. He can choose a subset $$$[0,1]$$$ and obtain an integer $$$2$$$, because $$$mex(0,1) = 2$$$. Appending this value to the end he gets the array $$$[0,1,2]$$$. $$$4$$$-th step. The current array is $$$[0,1,2]$$$. He can choose a subset $$$[0]$$$ and obtain an integer $$$1$$$, because $$$mex(0) = 1$$$. Appending this value to the end he gets the array $$$[0,1,2,1]$$$. Thus, he can get the array without mistakes, so the answer is $$$-1$$$. In the second example he has definitely made a mistake on the very first step, because he could not have obtained anything different from $$$0$$$. In the third example he could have obtained $$$[0, 1, 2]$$$ without mistakes, but $$$239$$$ is definitely wrong.
1,000
false
false
true
false
false
false
false
false
false
false
5,446
685E
Gerda is travelling to the palace of the Snow Queen. The road network consists of _n_ intersections and _m_ bidirectional roads. Roads are numbered from 1 to _m_. Snow Queen put a powerful spell on the roads to change the weather conditions there. Now, if Gerda steps on the road _i_ at the moment of time less or equal to _i_, she will leave the road exactly at the moment _i_. In case she steps on the road _i_ at the moment of time greater than _i_, she stays there forever. Gerda starts at the moment of time _l_ at the intersection number _s_ and goes to the palace of the Snow Queen, located at the intersection number _t_. Moreover, she has to be there at the moment _r_ (or earlier), before the arrival of the Queen. Given the description of the road network, determine for _q_ queries _l__i_, _r__i_, _s__i_ and _t__i_ if it's possible for Gerda to get to the palace on time. Input The first line of the input contains integers _n_, _m_ and _q_ (2u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_m_,u2009_q_u2009≤u2009200u2009000)xa0— the number of intersections in the road network of Snow Queen's Kingdom, the number of roads and the number of queries you have to answer. The _i_-th of the following _m_ lines contains the description of the road number _i_. The description consists of two integers _v__i_ and _u__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_, _v__i_u2009≠u2009_u__i_)xa0— the indices of the intersections connected by the _i_-th road. It's possible to get both from _v__i_ to _u__i_ and from _u__i_ to _v__i_ using only this road. Each pair of intersection may appear several times, meaning there are several roads connecting this pair. Last _q_ lines contain the queries descriptions. Each of them consists of four integers _l__i_, _r__i_, _s__i_ and _t__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_m_, 1u2009≤u2009_s__i_,u2009_t__i_u2009≤u2009_n_, _s__i_u2009≠u2009_t__i_)xa0— the moment of time Gerda starts her journey, the last moment of time she is allowed to arrive to the palace, the index of the starting intersection and the index of the intersection where palace is located. Output For each query print "Yes" (without quotes) if Gerda can be at the Snow Queen palace on time (not later than _r__i_) or "No" (without quotes) otherwise. Example Input 5 4 6 1 2 2 3 3 4 3 5 1 3 1 4 1 3 2 4 1 4 4 5 1 4 4 1 2 3 1 4 2 2 2 3
2,800
false
false
false
false
false
false
true
false
false
true
7,079
1139E
There are $$$n$$$ students and $$$m$$$ clubs in a college. The clubs are numbered from $$$1$$$ to $$$m$$$. Each student has a potential $$$p_i$$$ and is a member of the club with index $$$c_i$$$. Initially, each student is a member of exactly one club. A technical fest starts in the college, and it will run for the next $$$d$$$ days. There is a coding competition every day in the technical fest. Every day, in the morning, exactly one student of the college leaves their club. Once a student leaves their club, they will never join any club again. Every day, in the afternoon, the director of the college will select one student from each club (in case some club has no members, nobody is selected from that club) to form a team for this day's coding competition. The strength of a team is the mex of potentials of the students in the team. The director wants to know the maximum possible strength of the team for each of the coming $$$d$$$ days. Thus, every day the director chooses such team, that the team strength is maximized. The mex of the multiset $$$S$$$ is the smallest non-negative integer that is not present in $$$S$$$. For example, the mex of the $$${0, 1, 1, 2, 4, 5, 9}$$$ is $$$3$$$, the mex of $$${1, 2, 3}$$$ is $$$0$$$ and the mex of $$$varnothing$$$ (empty set) is $$$0$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq m leq n leq 5000$$$), the number of students and the number of clubs in college. The second line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$0 leq p_i < 5000$$$), where $$$p_i$$$ is the potential of the $$$i$$$-th student. The third line contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 leq c_i leq m$$$), which means that $$$i$$$-th student is initially a member of the club with index $$$c_i$$$. The fourth line contains an integer $$$d$$$ ($$$1 leq d leq n$$$), number of days for which the director wants to know the maximum possible strength of the team. Each of the next $$$d$$$ lines contains an integer $$$k_i$$$ ($$$1 leq k_i leq n$$$), which means that $$$k_i$$$-th student lefts their club on the $$$i$$$-th day. It is guaranteed, that the $$$k_i$$$-th student has not left their club earlier. Output For each of the $$$d$$$ days, print the maximum possible strength of the team on that day. Examples Input 5 3 0 1 2 2 0 1 2 2 3 2 5 3 2 4 5 1 Input 5 3 0 1 2 2 1 1 3 2 3 2 5 4 2 3 5 1 Input 5 5 0 1 2 4 5 1 2 3 4 5 4 2 3 5 4 Note Consider the first example: On the first day, student $$$3$$$ leaves their club. Now, the remaining students are $$$1$$$, $$$2$$$, $$$4$$$ and $$$5$$$. We can select students $$$1$$$, $$$2$$$ and $$$4$$$ to get maximum possible strength, which is $$$3$$$. Note, that we can't select students $$$1$$$, $$$2$$$ and $$$5$$$, as students $$$2$$$ and $$$5$$$ belong to the same club. Also, we can't select students $$$1$$$, $$$3$$$ and $$$4$$$, since student $$$3$$$ has left their club. On the second day, student $$$2$$$ leaves their club. Now, the remaining students are $$$1$$$, $$$4$$$ and $$$5$$$. We can select students $$$1$$$, $$$4$$$ and $$$5$$$ to get maximum possible strength, which is $$$1$$$. On the third day, the remaining students are $$$1$$$ and $$$5$$$. We can select students $$$1$$$ and $$$5$$$ to get maximum possible strength, which is $$$1$$$. On the fourth day, the remaining student is $$$1$$$. We can select student $$$1$$$ to get maximum possible strength, which is $$$1$$$. On the fifth day, no club has students and so the maximum possible strength is $$$0$$$.
2,400
false
false
false
false
false
false
false
false
false
true
5,030
1278B
You are given two integers $$$a$$$ and $$$b$$$. You can perform a sequence of operations: during the first operation you choose one of these numbers and increase it by $$$1$$$; during the second operation you choose one of these numbers and increase it by $$$2$$$, and so on. You choose the number of these operations yourself. For example, if $$$a = 1$$$ and $$$b = 3$$$, you can perform the following sequence of three operations: 1. add $$$1$$$ to $$$a$$$, then $$$a = 2$$$ and $$$b = 3$$$; 2. add $$$2$$$ to $$$b$$$, then $$$a = 2$$$ and $$$b = 5$$$; 3. add $$$3$$$ to $$$a$$$, then $$$a = 5$$$ and $$$b = 5$$$. Calculate the minimum number of operations required to make $$$a$$$ and $$$b$$$ equal. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^9$$$). Output For each test case print one integer — the minimum numbers of operations required to make $$$a$$$ and $$$b$$$ equal. Note First test case considered in the statement. In the second test case integers $$$a$$$ and $$$b$$$ are already equal, so you don't need to perform any operations. In the third test case you have to apply the first, the second, the third and the fourth operation to $$$b$$$ ($$$b$$$ turns into $$$20 + 1 + 2 + 3 + 4 = 30$$$).
1,500
true
true
false
false
false
false
false
false
false
false
4,305
1721A
You have an image file of size $$$2 imes 2$$$, consisting of $$$4$$$ pixels. Each pixel can have one of $$$26$$$ different colors, denoted by lowercase Latin letters. You want to recolor some of the pixels of the image so that all $$$4$$$ pixels have the same color. In one move, you can choose no more than two pixels of the same color and paint them into some other color (if you choose two pixels, both should be painted into the same color). What is the minimum number of moves you have to make in order to fulfill your goal? Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Each test case consists of two lines. Each of these lines contains two lowercase letters of Latin alphabet without any separators, denoting a row of pixels in the image. Output For each test case, print one integer — the minimum number of moves you have to make so that all $$$4$$$ pixels of the image have the same color. Example Input 5 rb br cc wb aa aa ab cd yy xx Note Let's analyze the test cases of the example. In the first test case, you can paint the bottom left pixel and the top right pixel (which share the same color) into the color r, so all pixels have this color. In the second test case, two moves are enough: paint both top pixels, which have the same color c, into the color b; paint the bottom left pixel into the color b. In the third test case, all pixels already have the same color. In the fourth test case, you may leave any of the pixels unchanged, and paint all three other pixels into the color of that pixel in three moves. In the fifth test case, you can paint both top pixels into the color x.
800
false
true
true
false
false
false
false
false
false
false
1,959
119B
Vasya is about to take his first university exam in about several minutes. And it's not just some ordinary exam, it's on mathematical analysis. Of course, right now Vasya can only think of one thing: what the result of his talk with the examiner will be... To prepare for the exam, one has to study proofs of _n_ theorems. It is known that there will be _k_ examination cards on the exam and each card contains distinct theorems. Besides, no theorem is mentioned in more than one card (that is, theorems won't be mentioned in any card). During the exam several students may get the same card. We do not know the exact way theorems are distributed by cards, however the students that took the exam before Vasya told him what theorems their cards contained. Vasya evaluates his level of proficiency in the _i_-th theorem by some number _a__i_. The level of proficiency in some card is the average of the levels of proficiency in the theorems that are included in the card. Now Vasya wants to know the minimally and maximally possible levels of his proficiency in the card he gets on the exam. Vasya wants to determine it by the data he has collected from other students. Unfortunately, Vasya has no time left to do the math and he asked you to help him. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009100) — the number of theorems and the number of cards correspondingly. The second line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009100), the _i_-th number (1u2009≤u2009_i_u2009≤u2009_n_) corresponds to Vasya's proficiency in the _i_-th theorem. The third line contains number _q_ (0u2009≤u2009_q_u2009≤u2009100) — the number of people that have taken the exam before Vasya. Each of the following _q_ lines contains the description of a student's card: integers from 1 to _n_ inclusive. They are the numbers of theorems included in the card in the order in which they are enumerated in the input data. The numbers are given in an arbitrary order. It is guaranteed that the given cards are valid (that is, that all theorems in one card are different and that different people get cards that either don't contain the same theorems or coincide up to the theorems' permutation). Output Print two real numbers, representing Vasya's minimum and maximum proficiency in the card he will get on the exam. The absolute or relative error should not exceed 10u2009-u20096. Examples Input 7 3 7 15 0 19 10 5 12 2 1 6 7 4 Output 5.0000000000 15.5000000000 Output 4.5000000000 13.5000000000 Note Let's analyze the first sample. Vasya's proficiency in the cards whose content he already knows equals 6 and 15.5 correspondingly. The three theorems that are left are only enough to make one exam card. If we consider all possible variants of theorems included in the card we can see that in the best case scenario Vasya gets the card that contains theorems 4 and 7 (his proficiency would equal 15.5) and in the worst case scenario he gets theorems 3 and 5 (his proficiency would equal 5). The ⌊ _x_⌋ operation denotes taking integer part of real number _x_ (rounding down).
1,900
false
false
true
false
false
true
false
false
true
false
9,410
1519F
Alice and Bob play a game. Alice has got $$$n$$$ treasure chests (the $$$i$$$-th of which contains $$$a_i$$$ coins) and $$$m$$$ keys (the $$$j$$$-th of which she can sell Bob for $$$b_j$$$ coins). Firstly, Alice puts some locks on the chests. There are $$$m$$$ types of locks, the locks of the $$$j$$$-th type can only be opened with the $$$j$$$-th key. To put a lock of type $$$j$$$ on the $$$i$$$-th chest, Alice has to pay $$$c_{i,j}$$$ dollars. Alice can put any number of different types of locks on each chest (possibly, zero). Then, Bob buys some of the keys from Alice (possibly none, possibly all of them) and opens each chest he can (he can open a chest if he has the keys for all of the locks on this chest). Bob's profit is the difference between the total number of coins in the opened chests and the total number of coins he spends buying keys from Alice. If Bob's profit is strictly positive (greater than zero), he wins the game. Otherwise, Alice wins the game. Alice wants to put some locks on some chests so no matter which keys Bob buys, she always wins (Bob cannot get positive profit). Of course, she wants to spend the minimum possible number of dollars on buying the locks. Help her to determine whether she can win the game at all, and if she can, how many dollars she has to spend on the locks. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 6$$$)xa0— the number of chests and the number of keys, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 4$$$), where $$$a_i$$$ is the number of coins in the $$$i$$$-th chest. The third line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 le b_j le 4$$$), where $$$b_j$$$ is the number of coins Bob has to spend to buy the $$$j$$$-th key from Alice. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains $$$m$$$ integers $$$c_{i,1}, c_{i,2}, dots, c_{i,m}$$$ ($$$1 le c_{i,j} le 10^7$$$), where $$$c_{i,j}$$$ is the number of dollars Alice has to spend to put a lock of the $$$j$$$-th type on the $$$i$$$-th chest. Output If Alice cannot ensure her victory (no matter which locks she puts on which chests, Bob always has a way to gain positive profit), print $$$-1$$$. Otherwise, print one integerxa0— the minimum number of dollars Alice has to spend to win the game regardless of Bob's actions. Examples Input 2 3 3 3 1 1 4 10 20 100 20 15 80 Input 2 3 3 3 2 1 4 10 20 100 20 15 80 Input 2 3 3 4 1 1 4 10 20 100 20 15 80 Note In the first example, Alice should put locks of types $$$1$$$ and $$$3$$$ on the first chest, and locks of type $$$2$$$ and $$$3$$$ on the second chest. In the second example, Alice should put locks of types $$$1$$$ and $$$2$$$ on the first chest, and a lock of type $$$3$$$ on the second chest.
3,200
false
false
false
true
false
false
true
false
false
false
3,064
1488J
Your friend is running a flower shop. In order to be prepared for the next holidays (when, as usual, sales skyrocket) she asked you to write her a special program that will help to analyze the stocks she has. There are $$$n$$$ different types of flowers she can order and each flower of the type $$$i$$$ costs $$$w_i$$$. The last holidays were a great success, she sold all flowers she had, so right now all her stocks are empty. From this point, she starts routine operations of ordering and selling flowers, while trying to analyze what she has at hand. All of this can be represented as $$$m$$$ queries of three types: "$$$1$$$ $$$i$$$ $$$c$$$"xa0— she bought $$$c$$$ flowers of type $$$i$$$; "$$$2$$$ $$$i$$$ $$$c$$$"xa0— she disposed of $$$c$$$ flowers of type $$$i$$$; "$$$3$$$ $$$l$$$ $$$r$$$ $$$k$$$"xa0— how many variants of bouquets she can make using only flowers of types $$$l, l + 1, dots, r$$$ with the total cost no more than $$$k$$$. For simplicity, you can think that a bouquet is a multiset of flowers, and two bouquets are different if they are different as multisets. The cost of a bouquet is the sum of all flowers it has. Help your friend and write the program that can process all these queries. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 1000$$$; $$$1 le m le 1000$$$)xa0— the number of flower types and the number of queries. The second line contains $$$n$$$ integers $$$w_1, w_2, dots, w_n$$$ ($$$1 le w_i le 1000$$$)xa0— the cost of one flower of each type. The next $$$m$$$ lines contains queriesxa0— one per line. Each query has one of three types: $$$1$$$ $$$i$$$ $$$c$$$ ($$$1 le i le n$$$; $$$1 le c le 5000$$$); $$$2$$$ $$$i$$$ $$$c$$$ ($$$1 le i le n$$$; $$$1 le c le 5000$$$). It's guaranteed that there are at least $$$c$$$ flowers of type $$$i$$$ at this moment; $$$3$$$ $$$l$$$ $$$r$$$ $$$k$$$ ($$$1 le l le r le n$$$; $$$1 le k le 5000$$$) It's guaranteed that the total cost of all flowers in stock after each query doesn't exceed $$$5000$$$. Output For each query of the third type, print how many variants of bouquets she can make using only flowers of types $$$l, l + 1, dots, r$$$ with the total cost no more than $$$k$$$. Since the answer may be too large, print it modulo $$$998,244,353$$$. Example Input 5 12 1 2 3 2 1 1 1 5 1 2 3 1 3 1 3 1 5 10 3 4 5 100 1 4 4 1 5 1 3 2 5 7 3 1 1 3 3 1 5 100 2 1 5 3 1 5 100
3,100
true
false
false
false
true
false
false
false
false
false
3,230
1834B
Fedya is playing a new game called "The Legend of Link", in which one of the character's abilities is to combine two materials into one weapon. Each material has its own strength, which can be represented by a positive integer $$$x$$$. The strength of the resulting weapon is determined as the sum of the absolute differences of the digits in the decimal representation of the integers at each position. Formally, let the first material have strength $$$X = overline{x_{1}x_{2} ldots x_{n}}$$$, and the second material have strength $$$Y = overline{y_{1}y_{2} ldots y_{n}}$$$. Then the strength of the weapon is calculated as $$$x_{1} - y_{1} + x_{2} - y_{2} + ldots + x_{n} - y_{n}$$$. If the integers have different lengths, then the shorter integer is padded with leading zeros. Fedya has an unlimited supply of materials with all possible strengths from $$$L$$$ to $$$R$$$, inclusive. Help him find the maximum possible strength of the weapon he can obtain. An integer $$$C = overline{c_{1}c_{2} ldots c_{k}}$$$ is defined as an integer obtained by sequentially writing the digits $$$c_1, c_2, ldots, c_k$$$ from left to right, i.e. $$$10^{k-1} cdot c_1 + 10^{k-2} cdot c_2 + ldots + c_k$$$. 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 two integers $$$L$$$ and $$$R$$$ ($$$1 le L le R < 10^{100}$$$)xa0— the decimal representation of the integers representing the minimum and maximum strength of the materials that Fedya has. It is guaranteed that the integers $$$L$$$ and $$$R$$$ do not contain leading zeros. Note that the input data may not fit into standard $$$32$$$-bit or $$$64$$$-bit integer data types. Output For each test case print one integerxa0— the maximum possible strength of the weapon that Fedya can obtain from the given materials. Example Input 6 53 57 179 239 13 37 132228 132228 54943329752812629795 55157581939688863366 88 1914 Note In the first test case, the weapon made from materials with strengths $$$53$$$ and $$$57$$$ will have the maximum possible strength: $$$5 - 5 + 3 - 7 = 4$$$. In the second test case, the maximum strength is achieved with materials with strengths $$$190$$$ and $$$209$$$: $$$1 - 2 + 9 - 0 + 0 - 9 = 19$$$. In the fourth test case, there is only one valid strength, so the answer is $$$0$$$. In the sixth test case, the maximum strength is achieved with materials with strengths $$$1909$$$ and $$$90$$$: $$$1 - 0 + 9 - 0 + 0 - 9 + 9 - 0 = 28$$$. Note that the shorter integer was padded with leading zeros.
1,000
true
true
false
false
false
false
false
false
false
false
1,283
671D
Mayor of Yusland just won the lottery and decided to spent money on something good for town. For example, repair all the roads in the town. Yusland consists of _n_ intersections connected by _n_u2009-u20091 bidirectional roads. One can travel from any intersection to any other intersection using only these roads. There is only one road repairing company in town, named "RC company". Company's center is located at the intersection 1. RC company doesn't repair roads you tell them. Instead, they have workers at some intersections, who can repair only some specific paths. The _i_-th worker can be paid _c__i_ coins and then he repairs all roads on a path from _u__i_ to some _v__i_ that lies on the path from _u__i_ to intersection 1. Mayor asks you to choose the cheapest way to hire some subset of workers in order to repair all the roads in Yusland. It's allowed that some roads will be repaired more than once. If it's impossible to repair all roads print u2009-u20091. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009300u2009000)xa0— the number of cities in Yusland and the number of workers respectively. Then follow _n_−1 line, each of them contains two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_)xa0— indices of intersections connected by the _i_-th road. Last _m_ lines provide the description of workers, each line containing three integers _u__i_, _v__i_ and _c__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, 1u2009≤u2009_c__i_u2009≤u2009109). This means that the _i_-th worker can repair all roads on the path from _v__i_ to _u__i_ for _c__i_ coins. It's guaranteed that _v__i_ lies on the path from _u__i_ to 1. Note that _v__i_ and _u__i_ may coincide. Output If it's impossible to repair all roads then print u2009-u20091. Otherwise print a single integerxa0— minimum cost required to repair all roads using "RC company" workers. Example Input 6 5 1 2 1 3 3 4 4 5 4 6 2 1 2 3 1 4 4 1 3 5 3 1 6 3 2 Note In the first sample, we should choose workers with indices 1, 3, 4 and 5, some roads will be repaired more than once but it is OK. The cost will be equal to 2u2009+u20093u2009+u20091u2009+u20092u2009=u20098 coins.
2,900
false
true
false
true
true
false
false
false
false
false
7,137
1761B
Define a cyclic sequence of size $$$n$$$ as an array $$$s$$$ of length $$$n$$$, in which $$$s_n$$$ is adjacent to $$$s_1$$$. Muxii has a ring represented by a cyclic sequence $$$a$$$ of size $$$n$$$. However, the ring itself hates equal adjacent elements. So if two adjacent elements in the sequence are equal at any time, one of them will be erased immediately. The sequence doesn't contain equal adjacent elements initially. Muxii can perform the following operation until the sequence becomes empty: Choose an element in $$$a$$$ and erase it. For example, if ring is $$$[1, 2, 4, 2, 3, 2]$$$, and Muxii erases element $$$4$$$, then ring would erase one of the elements equal to $$$2$$$, and the ring will become $$$[1, 2, 3, 2]$$$. Muxii wants to find the maximum number of operations he could perform. Note that in a ring of size $$$1$$$, its only element isn't considered adjacent to itself (so it's not immediately erased). Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1leq tleq 100$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1leq nleq 100$$$)xa0— the size of the cyclic sequence. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq a_ileq n$$$)xa0— the sequence itself. It's guaranteed that $$$a_i e a_{i+1}$$$ for $$$1leq i<n$$$. It's guaranteed that $$$a_n e a_1$$$ when $$$n>1$$$. Output For each test case, output a single integerxa0— the maximum number of operations Muxii can perform. Note In the first test case, you can erase the second element first, then erase the remaining elements one by one in any order. In total, you can perform the operation $$$4$$$ times. Note that if you erase the first element first, then the sequence will be turned into $$$[2,3,2]$$$ and then immediately become $$$[2,3]$$$. In the second test case, you can erase the first element first, then the sequence becomes $$$[2,1]$$$. Then you can erase all remaining elements one by one in any order.
1,000
false
true
true
false
false
true
false
false
false
false
1,747
1408A
You are given three sequences: $$$a_1, a_2, ldots, a_n$$$; $$$b_1, b_2, ldots, b_n$$$; $$$c_1, c_2, ldots, c_n$$$. For each $$$i$$$, $$$a_i eq b_i$$$, $$$a_i eq c_i$$$, $$$b_i eq c_i$$$. Find a sequence $$$p_1, p_2, ldots, p_n$$$, that satisfy the following conditions: $$$p_i in {a_i, b_i, c_i}$$$ $$$p_i eq p_{(i mod n) + 1}$$$. In other words, for each element, you need to choose one of the three possible values, such that no two adjacent elements (where we consider elements $$$i,i+1$$$ adjacent for $$$i<n$$$ and also elements $$$1$$$ and $$$n$$$) will have equal value. It can be proved that in the given constraints solution always exists. You don't need to minimize/maximize anything, you need to find any proper sequence. Input The first line of input contains one integer $$$t$$$ ($$$1 leq t leq 100$$$): the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$3 leq n leq 100$$$): the number of elements in the given sequences. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 100$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq 100$$$). The fourth line contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 leq c_i leq 100$$$). It is guaranteed that $$$a_i eq b_i$$$, $$$a_i eq c_i$$$, $$$b_i eq c_i$$$ for all $$$i$$$. Output For each test case, print $$$n$$$ integers: $$$p_1, p_2, ldots, p_n$$$ ($$$p_i in {a_i, b_i, c_i}$$$, $$$p_i eq p_{i mod n + 1}$$$). If there are several solutions, you can print any. Example Input 5 3 1 1 1 2 2 2 3 3 3 4 1 2 1 2 2 1 2 1 3 4 3 4 7 1 3 3 1 1 1 1 2 4 4 3 2 2 4 4 2 2 2 4 4 2 3 1 2 1 2 3 3 3 1 2 10 1 1 1 2 2 2 3 3 3 1 2 2 2 3 3 3 1 1 1 2 3 3 3 1 1 1 2 2 2 3 Output 1 2 3 1 2 1 2 1 3 4 3 2 4 2 1 3 2 1 2 3 1 2 3 1 2 3 2 Note In the first test case $$$p = [1, 2, 3]$$$. It is a correct answer, because: $$$p_1 = 1 = a_1$$$, $$$p_2 = 2 = b_2$$$, $$$p_3 = 3 = c_3$$$ $$$p_1 eq p_2 $$$, $$$p_2 eq p_3 $$$, $$$p_3 eq p_1$$$ All possible correct answers to this test case are: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$. In the second test case $$$p = [1, 2, 1, 2]$$$. In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = a_3$$$, $$$p_4 = a_4$$$. Also we can see, that no two adjacent elements of the sequence are equal. In the third test case $$$p = [1, 3, 4, 3, 2, 4, 2]$$$. In this sequence $$$p_1 = a_1$$$, $$$p_2 = a_2$$$, $$$p_3 = b_3$$$, $$$p_4 = b_4$$$, $$$p_5 = b_5$$$, $$$p_6 = c_6$$$, $$$p_7 = c_7$$$. Also we can see, that no two adjacent elements of the sequence are equal.
800
false
false
false
false
false
true
false
false
false
false
3,636
1395B
Boboniu likes playing chess with his employees. As we know, no employee can beat the boss in the chess game, so Boboniu has never lost in any round. You are a new applicant for his company. Boboniu will test you with the following chess question: Consider a $$$n imes m$$$ grid (rows are numbered from $$$1$$$ to $$$n$$$, and columns are numbered from $$$1$$$ to $$$m$$$). You have a chess piece, and it stands at some cell $$$(S_x,S_y)$$$ which is not on the border (i.e. $$$2 le S_x le n-1$$$ and $$$2 le S_y le m-1$$$). From the cell $$$(x,y)$$$, you can move your chess piece to $$$(x,y')$$$ ($$$1le y'le m, y' eq y$$$) or $$$(x',y)$$$ ($$$1le x'le n, x' eq x$$$). In other words, the chess piece moves as a rook. From the cell, you can move to any cell on the same row or column. Your goal is to visit each cell exactly once. Can you find a solution? Note that cells on the path between two adjacent cells in your route are not counted as visited, and it is not required to return to the starting point. Input The only line of the input contains four integers $$$n$$$, $$$m$$$, $$$S_x$$$ and $$$S_y$$$ ($$$3le n,mle 100$$$, $$$2 le S_x le n-1$$$, $$$2 le S_y le m-1$$$) — the number of rows, the number of columns, and the initial position of your chess piece, respectively. Output You should print $$$ncdot m$$$ lines. The $$$i$$$-th line should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 leq x_i leq n$$$, $$$1 leq y_i leq m$$$), denoting the $$$i$$$-th cell that you visited. You should print exactly $$$nm$$$ pairs $$$(x_i, y_i)$$$, they should cover all possible pairs $$$(x_i, y_i)$$$, such that $$$1 leq x_i leq n$$$, $$$1 leq y_i leq m$$$. We can show that under these constraints there always exists a solution. If there are multiple answers, print any. Examples Output 2 2 1 2 1 3 2 3 3 3 3 2 3 1 2 1 1 1 Output 2 2 2 1 2 3 2 4 1 4 3 4 3 3 3 2 3 1 1 1 1 2 1 3 Note Possible routes for two examples:
1,100
false
false
false
false
false
true
false
false
false
false
3,695
88A
Vasya studies music. He has learned lots of interesting stuff. For example, he knows that there are 12 notes: C, C#, D, D#, E, F, F#, G, G#, A, B, H. He also knows that the notes are repeated cyclically: after H goes C again, and before C stands H. We will consider the C note in the row's beginning and the C note after the H similar and we will identify them with each other. The distance between the notes along the musical scale is measured in tones: between two consecutive notes there's exactly one semitone, that is, 0.5 tone. The distance is taken from the lowest tone to the uppest one, that is, the distance between C and E is 4 semitones and between E and C is 8 semitones Vasya also knows what a chord is. A chord is an unordered set of no less than three notes. However, for now Vasya only works with triads, that is with the chords that consist of exactly three notes. He can already distinguish between two types of triads — major and minor. Let's define a major triad. Let the triad consist of notes _X_, _Y_ and _Z_. If we can order the notes so as the distance along the musical scale between _X_ and _Y_ equals 4 semitones and the distance between _Y_ and _Z_ is 3 semitones, then the triad is major. The distance between _X_ and _Z_, accordingly, equals 7 semitones. A minor triad is different in that the distance between _X_ and _Y_ should be 3 semitones and between _Y_ and _Z_ — 4 semitones. For example, the triad "C E G" is major: between C and E are 4 semitones, and between E and G are 3 semitones. And the triplet "C# B F" is minor, because if we order the notes as "B C# F", than between B and C# will be 3 semitones, and between C# and F — 4 semitones. Help Vasya classify the triad the teacher has given to him. Output Print "major" if the chord is major, "minor" if it is minor, and "strange" if the teacher gave Vasya some weird chord which is neither major nor minor. Vasya promises you that the answer will always be unambiguous. That is, there are no chords that are both major and minor simultaneously.
1,200
false
false
true
false
false
false
true
false
false
false
9,531
1521C
This is an interactive problem! Nastia has a hidden permutation $$$p$$$ of length $$$n$$$ consisting of integers from $$$1$$$ to $$$n$$$. You, for some reason, want to figure out the permutation. To do that, you can give her an integer $$$t$$$ ($$$1 le t le 2$$$), two different indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$, $$$i eq j$$$), and an integer $$$x$$$ ($$$1 le x le n - 1$$$). Depending on $$$t$$$, she will answer: $$$t = 1$$$: $$$max{(min{(x, p_i)}, min{(x + 1, p_j)})}$$$; $$$t = 2$$$: $$$min{(max{(x, p_i)}, max{(x + 1, p_j)})}$$$. You can ask Nastia at most $$$lfloor frac {3 cdot n} { 2} floor + 30$$$ times. It is guaranteed that she will not change her permutation depending on your queries. Can you guess the permutation? Input The input consists of several test cases. In the beginning, you receive the integer $$$T$$$ ($$$1 le T le 10,000$$$)xa0— the number of test cases. At the beginning of each test case, you receive an integer $$$n$$$ ($$$3 le n le 10^4$$$)xa0— the length of the permutation $$$p$$$. It's guaranteed that the permutation is fixed beforehand and that the sum of $$$n$$$ in one test doesn't exceed $$$2 cdot 10^4$$$. Interaction To ask a question, print "? $$$t$$$ $$$i$$$ $$$j$$$ $$$x$$$" ($$$t = 1$$$ or $$$t = 2$$$, $$$1 le i, j le n$$$, $$$i eq j$$$, $$$1 le x le n - 1$$$) Then, you should read the answer. If we answer with $$$−1$$$ instead of a valid answer, that means you exceeded the number of queries or made an invalid query. Exit immediately after receiving $$$−1$$$ and you will see the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. To print the answer, print "! $$$p_1$$$ $$$p_2$$$ $$$ldots$$$ $$$p_{n}$$$ (without quotes). Note that answering doesn't count as one of the $$$lfloor frac {3 cdot n} {2} floor + 30$$$ queries. After printing a query or printing the answer, 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 the documentation for other languages. Hacks To hack the solution, use the following test format. The first line should contain a single integer $$$T$$$ ($$$1 le T le 10,000$$$)xa0— the number of test cases. For each test case in the first line print a single integer $$$n$$$ ($$$3 le n le 10^4$$$)xa0— the length of the hidden permutation $$$p$$$. In the second line print $$$n$$$ space-separated integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$), where $$$p$$$ is permutation. Note that the sum of $$$n$$$ over all test cases should not exceed $$$2 cdot 10^4$$$. Example Output ? 2 4 1 3 ? 1 2 4 2 ! 3 1 4 2 ? 2 3 4 2 ! 2 5 3 4 1 Note Consider the first test case. The hidden permutation is $$$[3, 1, 4, 2]$$$. We print: "? $$$2$$$ $$$4$$$ $$$1$$$ $$$3$$$" and get back $$$min{(max{(3, p_4}), max{(4, p_1)})} = 3$$$. We print: "? $$$1$$$ $$$2$$$ $$$4$$$ $$$2$$$" and get back $$$max{(min{(2, p_2)}, min{(3, p_4)})} = 2$$$. Consider the second test case. The hidden permutation is $$$[2, 5, 3, 4, 1]$$$. We print: "? $$$2$$$ $$$3$$$ $$$4$$$ $$$2$$$" and get back $$$min{(max{(2, p_3}), max{(3, p_4)})} = 3$$$.
2,000
false
false
false
false
false
true
false
false
false
false
3,053
1732D1
This is the easy version of the problem. The only difference is that in this version there are no "remove" queries. Initially you have a set containing one element — $$$0$$$. You need to handle $$$q$$$ queries of the following types: + $$$x$$$ — add the integer $$$x$$$ to the set. It is guaranteed that this integer is not contained in the set; ? $$$k$$$ — find the $$$k ext{-mex}$$$ of the set. In our problem, we define the $$$k ext{-mex}$$$ of a set of integers as the smallest non-negative integer $$$x$$$ that is divisible by $$$k$$$ and which is not contained in the set. Input The first line contains an integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$) — the number of queries. The following $$$q$$$ lines describe the queries. An addition query of integer $$$x$$$ is given in the format + $$$x$$$ ($$$1 leq x leq 10^{18}$$$). It is guaranteed that $$$x$$$ was not contained in the set. A search query of $$$k ext{-mex}$$$ is given in the format ? $$$k$$$ ($$$1 leq k leq 10^{18}$$$). It is guaranteed that there will be at least one query of type ?. Output For each query of type ? output a single integer — the $$$k ext{-mex}$$$ of the set. Examples Input 15 + 1 + 2 ? 1 + 4 ? 2 + 6 ? 3 + 7 + 8 ? 1 ? 2 + 5 ? 1 + 1000000000000000000 ? 1000000000000000000 Output 3 6 3 3 10 3 2000000000000000000 Input 6 + 100 ? 100 + 200 ? 100 + 50 ? 50 Note In the first example: After the first and second queries, the set will contain elements $$${0, 1, 2}$$$. The smallest non-negative number that is divisible by $$$1$$$ and is not contained in the set is $$$3$$$. After the fourth query, the set will contain the elements $$${0, 1, 2, 4}$$$. The smallest non-negative number that is divisible by $$$2$$$ and is not contained in the set is $$$6$$$. In the second example: Initially, the set contains only the element $$${0}$$$. After adding an integer $$$100$$$ the set contains elements $$${0, 100}$$$. $$$100 ext{-mex}$$$ of the set is $$$200$$$. After adding an integer $$$200$$$ the set contains elements $$${0, 100, 200}$$$. $$$100 ext{-mex}$$$ of the set is $$$300$$$. After adding an integer $$$50$$$ the set contains elements $$${0, 50, 100, 200}$$$. $$$50 ext{-mex}$$$ of the set is $$$150$$$.
1,500
false
false
true
false
true
false
true
false
false
false
1,895
924B
An atom of element X can exist in _n_ distinct states with energies _E_1u2009<u2009_E_2u2009<u2009...u2009<u2009_E__n_. Arkady wants to build a laser on this element, using a three-level scheme. Here is a simplified description of the scheme. Three distinct states _i_, _j_ and _k_ are selected, where _i_u2009<u2009_j_u2009<u2009_k_. After that the following process happens: 1. initially the atom is in the state _i_, 2. we spend _E__k_u2009-u2009_E__i_ energy to put the atom in the state _k_, 3. the atom emits a photon with useful energy _E__k_u2009-u2009_E__j_ and changes its state to the state _j_, 4. the atom spontaneously changes its state to the state _i_, losing energy _E__j_u2009-u2009_E__i_, 5. the process repeats from step 1. Let's define the energy conversion efficiency as , i.xa0e. the ration between the useful energy of the photon and spent energy. Due to some limitations, Arkady can only choose such three states that _E__k_u2009-u2009_E__i_u2009≤u2009_U_. Help Arkady to find such the maximum possible energy conversion efficiency within the above constraints. Input The first line contains two integers _n_ and _U_ (3u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_U_u2009≤u2009109) — the number of states and the maximum possible difference between _E__k_ and _E__i_. The second line contains a sequence of integers _E_1,u2009_E_2,u2009...,u2009_E__n_ (1u2009≤u2009_E_1u2009<u2009_E_2...u2009<u2009_E__n_u2009≤u2009109). It is guaranteed that all _E__i_ are given in increasing order. Output If it is not possible to choose three states that satisfy all constraints, print -1. Otherwise, print one real number ηxa0— the maximum possible energy conversion efficiency. Your answer is considered correct its absolute or relative error does not exceed 10u2009-u20099. Formally, let your answer be _a_, and the jury's answer be _b_. Your answer is considered correct if . Examples Input 10 8 10 13 15 16 17 19 20 22 24 25 Note In the first example choose states 1, 2 and 3, so that the energy conversion efficiency becomes equal to . In the second example choose states 4, 5 and 9, so that the energy conversion efficiency becomes equal to .
1,600
false
true
false
false
false
false
false
true
false
false
6,016
576C
Problem - 576C - 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 divide and conquer geometry greedy sortings *2100 No tag edit access → Contest materials with integer coordinates between 0 and 106. The distance between the two points with numbers _a_ and _b_ is said to be the following value: (the distance calculated by such formula is called Manhattan distance). We call a hamiltonian path to be some permutation _p__i_ of numbers from 1 to _n_. We say that the length of this path is value . Find some hamiltonian path with a length of no more than 25u2009×u2009108. Note that you do not have to minimize the path length. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009106). The _i_u2009+u20091-th line contains the coordinates of the _i_-th point: _x__i_ and _y__i_ (0u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009106). It is guaranteed that no two points coincide. Output Print the permutation of numbers _p__i_ from 1 to _n_ — the sought Hamiltonian path. The permutation must meet the inequality . If there are multiple possible answers, print any of them. It is guaranteed that the answer exists. Examples Input 5 0 7 8 10 3 4 5 0 9 12 Output 4 3 1 2 5 Note In the sample test the total distance is: (5u2009-u20093u2009+u20090u2009-u20094)u2009+u2009(3u2009-u20090u2009+u20094u2009-u20097)u2009+u2009(0u2009-u20098u2009+u20097u2009-u200910)u2009+u2009(8u2009-u20099u2009+u200910u2009-u200912)u2009=u20092u2009+u20094u2009+u20093u2009+u20093u2009+u20098u2009+u20093u2009+u20091u2009+u20092u2009=u200926
2,100
false
true
false
false
false
true
false
false
true
false
7,541
1332A
Alice has a cute cat. To keep her cat fit, Alice wants to design an exercising walk for her cat! Initially, Alice's cat is located in a cell $$$(x,y)$$$ of an infinite grid. According to Alice's theory, cat needs to move: exactly $$$a$$$ steps left: from $$$(u,v)$$$ to $$$(u-1,v)$$$; exactly $$$b$$$ steps right: from $$$(u,v)$$$ to $$$(u+1,v)$$$; exactly $$$c$$$ steps down: from $$$(u,v)$$$ to $$$(u,v-1)$$$; exactly $$$d$$$ steps up: from $$$(u,v)$$$ to $$$(u,v+1)$$$. Note that the moves can be performed in an arbitrary order. For example, if the cat has to move $$$1$$$ step left, $$$3$$$ steps right and $$$2$$$ steps down, then the walk right, down, left, right, right, down is valid. Alice, however, is worrying that her cat might get lost if it moves far away from her. So she hopes that her cat is always in the area $$$[x_1,x_2] imes [y_1,y_2]$$$, i.e. for every cat's position $$$(u,v)$$$ of a walk $$$x_1 le u le x_2$$$ and $$$y_1 le v le y_2$$$ holds. Also, note that the cat can visit the same cell multiple times. Can you help Alice find out if there exists a walk satisfying her wishes? Formally, the walk should contain exactly $$$a+b+c+d$$$ unit moves ($$$a$$$ to the left, $$$b$$$ to the right, $$$c$$$ to the down, $$$d$$$ to the up). Alice can do the moves in any order. Her current position $$$(u, v)$$$ should always satisfy the constraints: $$$x_1 le u le x_2$$$, $$$y_1 le v le y_2$$$. The staring point is $$$(x, y)$$$. You are required to answer $$$t$$$ test cases independently. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$) — the number of testcases. The first line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$0 le a,b,c,d le 10^8$$$, $$$a+b+c+d ge 1$$$). The second line of the test case contains six integers $$$x$$$, $$$y$$$, $$$x_1$$$, $$$y_1$$$, $$$x_2$$$, $$$y_2$$$ ($$$-10^8 le x_1le x le x_2 le 10^8$$$, $$$-10^8 le y_1 le y le y_2 le 10^8$$$). Output For each test case, output "YES" in a separate line, if there exists a walk satisfying her wishes. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower). Note In the first test case, one valid exercising walk is $$$$$$(0,0) ightarrow (-1,0) ightarrow (-2,0) ightarrow (-2,1) ightarrow (-2,2) ightarrow (-1,2) ightarrow(0,2) ightarrow (0,1) ightarrow (0,0) ightarrow (-1,0)$$$$$$
1,100
true
true
true
false
false
false
false
false
false
false
4,050
1419B
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases. A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has $$$n$$$ stairs, then it is made of $$$n$$$ columns, the first column is $$$1$$$ cell high, the second column is $$$2$$$ cells high, $$$ldots$$$, the $$$n$$$-th column if $$$n$$$ cells high. The lowest cells of all stairs must be in the same row. A staircase with $$$n$$$ stairs is called nice, if it may be covered by $$$n$$$ disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with $$$7$$$ stairs looks like: Find out the maximal number of different nice staircases, that can be built, using no more than $$$x$$$ cells, in total. No cell can be used more than once. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 1000)$$$ xa0— the number of test cases. The description of each test case contains a single integer $$$x$$$ $$$(1 le x le 10^{18})$$$ xa0— the number of cells for building staircases. Output For each test case output a single integer xa0— the number of different nice staircases, that can be built, using not more than $$$x$$$ cells, in total. Example Input 4 1 8 6 1000000000000000000 Note In the first test case, it is possible to build only one staircase, that consists of $$$1$$$ stair. It's nice. That's why the answer is $$$1$$$. In the second test case, it is possible to build two different nice staircases: one consists of $$$1$$$ stair, and another consists of $$$3$$$ stairs. This will cost $$$7$$$ cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is $$$2$$$. In the third test case, it is possible to build only one of two nice staircases: with $$$1$$$ stair or with $$$3$$$ stairs. In the first case, there will be $$$5$$$ cells left, that may be used only to build a staircase with $$$2$$$ stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is $$$1$$$. If Jett builds a staircase with $$$3$$$ stairs, then there are no more cells left, so the answer is $$$1$$$ again.
1,200
true
true
true
false
false
true
true
false
false
false
3,586
1419C
A new agent called Killjoy invented a virus COVID-2069 that infects accounts on Codeforces. Each account has a rating, described by an integer (it can possibly be negative or very large). Killjoy's account is already infected and has a rating equal to $$$x$$$. Its rating is constant. There are $$$n$$$ accounts except hers, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th account's initial rating is $$$a_i$$$. Any infected account (initially the only infected account is Killjoy's) instantly infects any uninfected account if their ratings are equal. This can happen at the beginning (before any rating changes) and after each contest. If an account is infected, it can not be healed. Contests are regularly held on Codeforces. In each contest, any of these $$$n$$$ accounts (including infected ones) can participate. Killjoy can't participate. After each contest ratings are changed this way: each participant's rating is changed by an integer, but the sum of all changes must be equal to zero. New ratings can be any integer. Find out the minimal number of contests needed to infect all accounts. You can choose which accounts will participate in each contest and how the ratings will change. It can be proven that all accounts can be infected in some finite number of contests. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 100)$$$xa0— the number of test cases. The next $$$2t$$$ lines contain the descriptions of all test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$2 le n le 10^3$$$, $$$-4000 le x le 4000$$$)xa0— the number of accounts on Codeforces and the rating of Killjoy's account. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ $$$(-4000 le a_i le 4000)$$$xa0— the ratings of other accounts. Output For each test case output the minimal number of contests needed to infect all accounts. Example Input 3 2 69 68 70 6 4 4 4 4 4 4 4 9 38 -21 83 50 -59 -77 15 -71 -78 20 Note In the first test case it's possible to make all ratings equal to $$$69$$$. First account's rating will increase by $$$1$$$, and second account's rating will decrease by $$$1$$$, so the sum of all changes will be equal to zero. In the second test case all accounts will be instantly infected, because all ratings (including Killjoy's account's rating) are equal to $$$4$$$.
1,500
true
true
true
false
false
false
false
false
false
false
3,585
1383C
Note that the only difference between String Transformation 1 and String Transformation 2 is in the move Koa does. In this version the letter $$$y$$$ Koa selects can be any letter from the first $$$20$$$ lowercase letters of English alphabet (read statement for better understanding). You can make hacks in these problems independently. Koa the Koala has two strings $$$A$$$ and $$$B$$$ of the same length $$$n$$$ ($$$A=B=n$$$) consisting of the first $$$20$$$ lowercase English alphabet letters (ie. from a to t). In one move Koa: 1. selects some subset of positions $$$p_1, p_2, ldots, p_k$$$ ($$$k ge 1; 1 le p_i le n; p_i eq p_j$$$ if $$$i eq j$$$) of $$$A$$$ such that $$$A_{p_1} = A_{p_2} = ldots = A_{p_k} = x$$$ (ie. all letters on this positions are equal to some letter $$$x$$$). 2. selects any letter $$$y$$$ (from the first $$$20$$$ lowercase letters in English alphabet). 3. sets each letter in positions $$$p_1, p_2, ldots, p_k$$$ to letter $$$y$$$. More formally: for each $$$i$$$ ($$$1 le i le k$$$) Koa sets $$$A_{p_i} = y$$$.Note that you can only modify letters in string $$$A$$$. Koa wants to know the smallest number of moves she has to do to make strings equal to each other ($$$A = B$$$) or to determine that there is no way to make them equal. Help her! Input Each test contains multiple test cases. The first line contains $$$t$$$ ($$$1 le t le 10$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of strings $$$A$$$ and $$$B$$$. The second line of each test case contains string $$$A$$$ ($$$A=n$$$). The third line of each test case contains string $$$B$$$ ($$$B=n$$$). Both strings consists of the first $$$20$$$ lowercase English alphabet letters (ie. from a to t). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case: Print on a single line the smallest number of moves she has to do to make strings equal to each other ($$$A = B$$$) or $$$-1$$$ if there is no way to make them equal. Example Input 5 3 aab bcc 4 cabc abcb 3 abc tsr 4 aabd cccd 5 abcbd bcdda Note In the $$$1$$$-st test case Koa: 1. selects positions $$$1$$$ and $$$2$$$ and sets $$$A_1 = A_2 = $$$ b ($$$color{red}{aa}b ightarrow color{blue}{bb}b$$$). 2. selects positions $$$2$$$ and $$$3$$$ and sets $$$A_2 = A_3 = $$$ c ($$$bcolor{red}{bb} ightarrow bcolor{blue}{cc}$$$). In the $$$2$$$-nd test case Koa: 1. selects positions $$$1$$$ and $$$4$$$ and sets $$$A_1 = A_4 = $$$ a ($$$color{red}{c}abcolor{red}{c} ightarrow color{blue}{a}abcolor{blue}{a}$$$). 2. selects positions $$$2$$$ and $$$4$$$ and sets $$$A_2 = A_4 = $$$ b ($$$acolor{red}{a}bcolor{red}{a} ightarrow acolor{blue}{b}bcolor{blue}{b}$$$). 3. selects position $$$3$$$ and sets $$$A_3 = $$$ c ($$$abcolor{red}{b}b ightarrow abcolor{blue}{c}b$$$). In the $$$3$$$-rd test case Koa: 1. selects position $$$1$$$ and sets $$$A_1 = $$$ t ($$$color{red}{a}bc ightarrow color{blue}{t}bc$$$). 2. selects position $$$2$$$ and sets $$$A_2 = $$$ s ($$$tcolor{red}{b}c ightarrow tcolor{blue}{s}c$$$). 3. selects position $$$3$$$ and sets $$$A_3 = $$$ r ($$$tscolor{red}{c} ightarrow tscolor{blue}{r}$$$).
3,100
false
false
false
true
false
false
false
false
false
true
3,754
497D
, # Problem A. Gears Input file: stdin Output file: stdout Time limit: 2 seconds Memory limit: 256 megabytes There are two polygons on the plane, A and B. Polygon A rotates around point P , and polygon B rotates around point Q. Each polygon rotates with the constant rotational speed in the clockwise direction around its point, the rotational speed values of the polygons’ rotation are equal. Your task is to determine if there will be a collision between polygons. A collision is a situation when the polygons have at least one common point. It is guaranteed that at the moment 0 the polygons A and B do not intersect and no polygon is fully contained inside another one. Note that: • the polygons are not necessarily convex; • points P and Q can be located on the border of or outside their polygons. # Input The first line contains space-separated coordinates of point P .The second line contains a single integer n (3 ≤ n ≤ 1000 ) — the number of vertices of polygon A.Each of the next n lines contains two space-separated integers — the coordinates of the corresponding vertex of polygon A.The next line is empty. Then follow space-separated coordinates of point Q.The next line contains a single integer m (3 ≤ m ≤ 1000 ) — the number of vertices of polygon B. Next m lines contain the coordinates of the vertices of the polygon B.The vertices of both polygons are listed in the counterclockwise order. Coordinates of all points are integers, their absolute values don’t exceed 10 4. # Output Print “ YES ”, if the collision takes place and “ NO ” otherwise (don’t print the quotes). Page 1 of 2 , # Examples stdin stdout 1 0 40 0 1 0 1 5 0 5 9 0 49 0 9 -5 10 -5 10 0 YES 0 0 31 0 2 -1 2 1 0 0 3-1 0 -2 1 -2 -1 NO # Note A polygon is a closed polyline that doesn’t intersect itself and doesn’t touch itself. Picture to the first sample: Page 2 of 2
2,900
true
false
false
false
false
false
true
false
false
false
7,846
70D
Once a walrus professor Plato asked his programming students to perform the following practical task. The students had to implement such a data structure that would support a convex hull on some set of points _S_. The input to the program had _q_ queries of two types: 1. Add a point with coordinates (_x_,u2009_y_) into the set _S_. Note that in this case the convex hull of _S_ could have changed, and could have remained the same. 2. Say whether a point with coordinates (_x_,u2009_y_) belongs to an area limited by the convex hull, including the border. All the students coped with the task. What about you? Input The first line contains an integer _q_ (4u2009≤u2009_q_u2009≤u2009105). Then follow _q_ lines in the following way: "_t_ _x_ _y_", where _t_ is the query type (1 or 2), and (_x_,u2009_y_) are the coordinates of the point (u2009-u2009106u2009≤u2009_x_,u2009_y_u2009≤u2009106, _x_ and _y_ are integers). There is at least one query of type 2. It is guaranteed that the three queries of the first type follow first and the points given in the queries form a non-degenerative triangle. Also all the points added in _S_ are distinct. Output For each query of the second type print one string containing "YES", if the point lies inside the convex hull or on its border. Otherwise, print "NO". Examples Input 8 1 0 0 1 2 0 1 2 2 2 1 0 1 0 2 2 1 1 2 2 1 2 20 -1
2,700
false
false
false
false
true
false
false
false
false
false
9,618
1033D
You are given $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$. Each of $$$a_i$$$ has between $$$3$$$ and $$$5$$$ divisors. Consider $$$a = prod a_i$$$xa0— the product of all input integers. Find the number of divisors of $$$a$$$. As this number may be very large, print it modulo prime number $$$998244353$$$. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 500$$$)xa0— the number of numbers. Each of the next $$$n$$$ lines contains an integer $$$a_i$$$ ($$$1 leq a_i leq 2cdot 10^{18}$$$). It is guaranteed that the number of divisors of each $$$a_i$$$ is between $$$3$$$ and $$$5$$$. Output Print a single integer $$$d$$$xa0— the number of divisors of the product $$$a_1 cdot a_2 cdot dots cdot a_n$$$ modulo $$$998244353$$$. Hacks input For hacks, the input needs to be provided in a special format. The first line contains an integer $$$n$$$xa0($$$1 leq n leq 500$$$)xa0— the number of numbers. Each of the next $$$n$$$ lines contains a prime factorization of $$$a_i$$$. The line contains an integer $$$k_i$$$xa0($$$2 leq k_i leq 4$$$)xa0— the number of prime factors of $$$a_i$$$ and $$$k_i$$$ integers $$$p_{i,j}$$$xa0($$$2 leq p_{i,j} leq 2 cdot 10^{18}$$$) where $$$p_{i,j}$$$ is the $$$j$$$-th prime factor of $$$a_i$$$. Before supplying the input to the contestant, $$$a_i = prod p_{i,j}$$$ are calculated. Note that each $$$p_{i,j}$$$ must be prime, each computed $$$a_i$$$ must satisfy $$$a_i leq 2cdot10^{18}$$$ and must have between $$$3$$$ and $$$5$$$ divisors. The contestant will be given only $$$a_i$$$, and not its prime factorization. For example, you need to use this test to get the first sample: 3 2 3 3 2 3 5 2 11 13 Interaction From the technical side, this problem is interactive. Therefore, do not forget to output end of line and flush the output. Also, do not read more than you need. To flush the output, 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 8 4606061759128693 4606066102679989 4606069767552943 4606063116488033 4606063930903637 4606064745319241 4606063930904021 4606065559735517 Note In the first case, $$$a = 19305$$$. Its divisors are $$$1, 3, 5, 9, 11, 13, 15, 27, 33, 39, 45, 55, 65, 99, 117, 135, 143, 165, 195, 297, 351, 429, 495, 585, 715, 1287, 1485, 1755, 2145, 3861, 6435, 19305$$$xa0— a total of $$$32$$$. In the second case, $$$a$$$ has four divisors: $$$1$$$, $$$86028121$$$, $$$86028157$$$, and $$$7400840699802997 $$$. In the third case $$$a = 202600445671925364698739061629083877981962069703140268516570564888699 375209477214045102253766023072401557491054453690213483547$$$. In the fourth case, $$$a=512=2^9$$$, so answer equals to $$$10$$$.
2,000
true
false
false
false
false
false
false
false
false
false
5,532
803E
Each evening Roma plays online poker on his favourite website. The rules of poker on this website are a bit strange: there are always two players in a hand, there are no bets, and the winner takes 1 virtual bourle from the loser. Last evening Roma started to play poker. He decided to spend no more than _k_ virtual bourles — he will stop immediately if the number of his loses exceeds the number of his wins by _k_. Also Roma will leave the game if he wins enough money for the evening, i.e. if the number of wins exceeds the number of loses by _k_. Next morning Roma found a piece of paper with a sequence on it representing his results. Roma doesn't remember the results exactly, and some characters in the sequence are written in a way such that it's impossible to recognize this character, so Roma can't recall whether he won _k_ bourles or he lost. The sequence written by Roma is a string _s_ consisting of characters W (Roma won the corresponding hand), L (Roma lost), D (draw) and ? (unknown result). Roma wants to restore any valid sequence by changing all ? characters to W, L or D. The sequence is called valid if all these conditions are met: In the end the absolute difference between the number of wins and loses is equal to _k_; There is no hand such that the absolute difference before this hand was equal to _k_. Help Roma to restore any such sequence. Input The first line contains two numbers _n_ (the length of Roma's sequence) and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20091000). The second line contains the sequence _s_ consisting of characters W, L, D and ?. There are exactly _n_ characters in this sequence. Output If there is no valid sequence that can be obtained from _s_ by replacing all ? characters by W, L or D, print NO. Otherwise print this sequence. If there are multiple answers, print any of them. Examples Input 20 5 ?LLLLLWWWWW????????? Output WLLLLLWWWWWWWWLWLWDW
2,000
false
false
false
true
false
false
false
false
false
true
6,548
1400E
Problem - 1400E - 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 divide and conquer dp greedy *2200 No tag edit access → Contest materials ") , then remove one occurrence of $$$l$$$, one occurrence of $$$l + 1$$$, ..., one occurrence of $$$r$$$ from the multiset. This operation can be applied only if each number from $$$l$$$ to $$$r$$$ occurs at least once in the multiset; choose two integers $$$i$$$ and $$$x$$$ ($$$x ge 1$$$), then remove $$$x$$$ occurrences of $$$i$$$ from the multiset. This operation can be applied only if the multiset contains at least $$$x$$$ occurrences of $$$i$$$. What is the minimum number of operations required to delete all elements from the multiset? Input The first line contains one integer $$$n$$$ ($$$1 le n le 5000$$$). The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 le a_i le 10^9$$$). Output Print one integer — the minimum number of operations required to delete all elements from the multiset. Examples Input 4 1 4 1 1 Output 2 Input 5 1 0 1 0 1 Output 3
2,200
false
true
false
true
true
false
false
false
false
false
3,668
276C
The little girl loves the problems on array queries very much. One day she came across a rather well-known problem: you've got an array of $$$n$$$ elements (the elements of the array are indexed starting from 1); also, there are $$$q$$$ queries, each one is defined by a pair of integers $$$l_i$$$, $$$r_i$$$ $$$(1 le l_i le r_i le n)$$$. You need to find for each query the sum of elements of the array with indexes from $$$l_i$$$ to $$$r_i$$$, inclusive. The little girl found the problem rather boring. She decided to reorder the array elements before replying to the queries in a way that makes the sum of query replies maximum possible. Your task is to find the value of this maximum sum. Input The first line contains two space-separated integers $$$n$$$ ($$$1 le n le 2cdot10^5$$$) and $$$q$$$ ($$$1 le q le 2cdot10^5$$$) — the number of elements in the array and the number of queries, correspondingly. The next line contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$1 le a_i le 2cdot10^5$$$) — the array elements. Each of the following $$$q$$$ lines contains two space-separated integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) — the $$$i$$$-th query. Output In a single line print, a single integer — the maximum sum of query replies after the array elements are reordered. Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 5 3 5 2 4 1 3 1 5 2 3 2 3
1,500
false
true
true
false
true
false
false
false
true
false
8,730
1937B
You are given a $$$2 imes n$$$ grid filled with zeros and ones. Let the number at the intersection of the $$$i$$$-th row and the $$$j$$$-th column be $$$a_{ij}$$$. There is a grasshopper at the top-left cell $$$(1, 1)$$$ that can only jump one cell right or downwards. It wants to reach the bottom-right cell $$$(2, n)$$$. Consider the binary string of length $$$n+1$$$ consisting of numbers written in cells of the path without changing their order. Your goal is to: 1. Find the lexicographically smallest$$$^dagger$$$ string you can attain by choosing any available path; 2. Find the number of paths that yield this lexicographically smallest string. $$$^dagger$$$ If two strings $$$s$$$ and $$$t$$$ have the same length, then $$$s$$$ is lexicographically smaller than $$$t$$$ if and only if in the first position where $$$s$$$ and $$$t$$$ differ, the string $$$s$$$ has a smaller element than the corresponding element in $$$t$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line of each test case contains a binary string $$$a_{11} a_{12} ldots a_{1n}$$$ ($$$a_{1i}$$$ is either $$$0$$$ or $$$1$$$). The third line of each test case contains a binary string $$$a_{21} a_{22} ldots a_{2n}$$$ ($$$a_{2i}$$$ is either $$$0$$$ or $$$1$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output two lines: 1. The lexicographically smallest string you can attain by choosing any available path; 2. The number of paths that yield this string. Example Input 3 2 00 00 4 1101 1100 8 00100111 11101101 Output 000 2 11000 1 001001101 4 Note In the first test case, the lexicographically smallest string is $$$mathtt{000}$$$. There are two paths that yield this string: In the second test case, the lexicographically smallest string is $$$mathtt{11000}$$$. There is only one path that yields this string:
1,300
false
true
true
true
false
false
false
false
false
false
661
1485C
Problem - 1485C - 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 number theory *1700 No tag edit access → Contest materials ") ") $$$ is called special if $$$lfloor frac{a}{b} floor = a bmod b$$$. Here, $$$lfloor frac{a}{b} floor$$$ is the result of the integer division between $$$a$$$ and $$$b$$$, while $$$a bmod b$$$ is its remainder. You are given two integers $$$x$$$ and $$$y$$$. Find the number of special pairs $$$(a,b)$$$ such that $$$1leq a leq x$$$ and $$$1 leq b leq y$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The only line of the description of each test case contains two integers $$$x$$$, $$$y$$$ ($$$1 le x,y le 10^9$$$). Output For each test case print the answer on a single line. Example Input 9 3 4 2 100 4 3 50 3 12 4 69 420 12345 6789 123456 789 12345678 9 Output 1 0 2 3 5 141 53384 160909 36 Note In the first test case, the only special pair is $$$(3, 2)$$$. In the second test case, there are no special pairs. In the third test case, there are two special pairs: $$$(3, 2)$$$ and $$$(4, 3)$$$.
1,700
true
false
false
false
false
false
true
true
false
false
3,257
152B
One day Vasya went out for a walk in the yard but there weren't any of his friends outside and he had no one to play touch and run. But the boy didn't lose the high spirits and decided to play touch and run with himself. You may ask: "How did he do that?" The answer is simple. Vasya noticed that the yard is a rectangular _n_u2009×u2009_m_ field. The squares have coordinates (_x_,u2009_y_) (1u2009≤u2009_x_u2009≤u2009_n_,u20091u2009≤u2009_y_u2009≤u2009_m_), where _x_ is the index of the row and _y_ is the index of the column. Initially Vasya stands in the square with coordinates (_x__c_,u2009_y__c_). To play, he has got a list of _k_ vectors (_dx__i_,u2009_dy__i_) of non-zero length. The game goes like this. The boy considers all vectors in the order from 1 to _k_, and consecutively chooses each vector as the current one. After the boy has chosen a current vector, he makes the maximally possible number of valid steps in the vector's direction (it is possible that he makes zero steps). A step is defined as one movement from the square where the boy is standing now, in the direction of the current vector. That is, if Vasya is positioned in square (_x_,u2009_y_), and the current vector is (_dx_,u2009_dy_), one step moves Vasya to square (_x_u2009+u2009_dx_,u2009_y_u2009+u2009_dy_). A step is considered valid, if the boy does not go out of the yard if he performs the step. Vasya stepped on and on, on and on until he ran out of vectors in his list. Ha had been stepping for so long that he completely forgot how many steps he had made. Help the boy and count how many steps he had made. Input The first input line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009109) — the yard's sizes. The second line contains integers _x__c_ and _y__c_ — the initial square's coordinates (1u2009≤u2009_x__c_u2009≤u2009_n_,u20091u2009≤u2009_y__c_u2009≤u2009_m_). The third line contains an integer _k_ (1u2009≤u2009_k_u2009≤u2009104) — the number of vectors. Then follow _k_ lines, each of them contains two integers _dx__i_ and _dy__i_ (_dx__i_,u2009_dy__i_u2009≤u2009109,u2009_dx_u2009+u2009_dy_u2009≥u20091). Output Print the single number — the number of steps Vasya had made. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Note In the first sample Vasya is initially positioned at square (1,u20091) and makes 3 steps by the first vector (1,u20091). So, he consecutively visits the squares (2,u20092),u2009(3,u20093),u2009(4,u20094). Then he makes 0 steps by the second vector (1,u20091). He makes 1 more step by the third vector (0,u2009u2009-u20092) and he ends up in square (4,u20092). Overall, Vasya makes 4 steps. In the second sample Vasya is initially positioned in square (1,u20092) and makes 0 steps by vector (u2009-u20091,u20090), as the square with coordinates (0,u20092) is located outside the yard.
1,300
false
false
true
false
false
false
false
true
false
false
9,268
2023B
It is already the year $$$3024$$$, ideas for problems have long run out, and the olympiad now takes place in a modified individual format. The olympiad consists of $$$n$$$ problems, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th problem has its own score $$$a_i$$$ and a certain parameter $$$b_i$$$ ($$$1 le b_i le n$$$). Initially, the testing system gives the participant the first problem. When the participant is given the $$$i$$$-th problem, they have two options: They can submit the problem and receive $$$a_i$$$ points; They can skip the problem, in which case they will never be able to submit it. Then, the testing system selects the next problem for the participant from problems with indices $$$j$$$, such that: If he submitted the $$$i$$$-th problem, it looks at problems with indices $$$j < i$$$; If he skipped the $$$i$$$-th problem, it looks at problems with indices $$$j leq b_i$$$. Among these problems, it selects the problem with the maximum index that it has not previously given to the participant (he has neither submitted nor skipped it before). If there is no such problem, then the competition for the participant ends, and their result is equal to the sum of points for all submitted problems. In particular, if the participant submits the first problem, then the competition for them ends. Note that the participant receives each problem at most once. Prokhor has prepared thoroughly for the olympiad, and now he can submit any problem. Help him determine the maximum number of points he can achieve. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) — 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 4 cdot 10^5$$$) — the number of problems in the olympiad. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) — the scores of the problems. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq n$$$) — the parameters of the problems. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Output For each test case, output a single integer — the maximum number of points that Prokhor can achieve. Example Input 4 2 15 16 2 1 5 10 10 100 100 1000 3 4 1 1 1 3 100 49 50 3 2 2 4 100 200 300 1000 2 3 4 1 Note In the first test case, Prokhor can skip the first problem; then he will receive the problem with index $$$b_1 = 2$$$. Prokhor can submit it and receive $$$a_2 = 16$$$ points. After that, the competition will end because Prokhor has already received all problems. Note that if Prokhor submits the first problem, he will receive $$$a_1 = 15$$$ points, but the competition will end immediately. In the second test case, Prokhor can skip the first problem; then he will receive the problem with index $$$b_1 = 3$$$. Prokhor can submit it and receive $$$a_3 = 100$$$ points. After that, Prokhor will receive the second problem, which he can skip to receive the problem with index $$$b_2 = 4$$$. Prokhor can submit the fourth problem and receive another $$$a_4 = 100$$$ points. After that, the competition ends because Prokhor has already received all problems with indices not exceeding $$$4$$$. Thus, Prokhor will receive a total of $$$200$$$ points. In the third test case, Prokhor can submit the first problem and receive $$$100$$$ points, after which the competition will end immediately.
1,700
false
false
false
true
false
false
false
true
false
true
130
818G
Author note: I think some of you might remember the problem "Two Melodies" from Eductational Codeforces Round 22. Now it's time to make it a bit more difficult! Alice is a composer, and recently she had recorded two tracks that became very popular. Now she has got a lot of fans who are waiting for new tracks. This time Alice wants to form four melodies for her tracks. Alice has a sheet with _n_ notes written on it. She wants to take four such non-empty non-intersecting subsequences that all of them form a melody and sum of their lengths is maximal. Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Subsequence forms a melody when each two adjacent notes either differ by 1 or are congruent modulo 7. You should write a program which will calculate maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody. Input The first line contains one integer number _n_ (4u2009≤u2009_n_u2009≤u20093000). The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105) — notes written on a sheet. Output Print maximum sum of lengths of such four non-empty non-intersecting subsequences that all of them form a melody. Note In the first example it is possible to compose 4 melodies by choosing any 4 notes (and each melody will consist of only one note). In the second example it is possible to compose one melody with 2 notes — {1,u20092}. Remaining notes are used in other three melodies (one note per each melody).
2,600
false
false
false
false
false
false
false
false
false
true
6,481
1311E
You are given two integers $$$n$$$ and $$$d$$$. You need to construct a rooted binary tree consisting of $$$n$$$ vertices with a root at the vertex $$$1$$$ and the sum of depths of all vertices equals to $$$d$$$. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex $$$v$$$ is the last different from $$$v$$$ vertex on the path from the root to the vertex $$$v$$$. The depth of the vertex $$$v$$$ is the length of the path from the root to the vertex $$$v$$$. Children of vertex $$$v$$$ are all vertices for which $$$v$$$ is the parent. The binary tree is such a tree that no vertex has more than $$$2$$$ children. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The only line of each test case contains two integers $$$n$$$ and $$$d$$$ ($$$2 le n, d le 5000$$$) — the number of vertices in the tree and the required sum of depths of all vertices. It is guaranteed that the sum of $$$n$$$ and the sum of $$$d$$$ both does not exceed $$$5000$$$ ($$$sum n le 5000, sum d le 5000$$$). Output For each test case, print the answer. If it is impossible to construct such a tree, print "NO" (without quotes) in the first line. Otherwise, print "{YES}" in the first line. Then print $$$n-1$$$ integers $$$p_2, p_3, dots, p_n$$$ in the second line, where $$$p_i$$$ is the parent of the vertex $$$i$$$. Note that the sequence of parents you print should describe some binary tree. Example Output YES 1 2 1 3 YES 1 2 3 3 9 9 2 1 6 NO Note Pictures corresponding to the first and the second test cases of the example:
2,200
false
false
false
false
false
true
true
false
false
false
4,139
1508F
Touko's favorite sequence of numbers is a permutation $$$a_1, a_2, dots, a_n$$$ of $$$1, 2, dots, n$$$, and she wants some collection of permutations that are similar to her favorite permutation. She has a collection of $$$q$$$ intervals of the form $$$[l_i, r_i]$$$ with $$$1 le l_i le r_i le n$$$. To create permutations that are similar to her favorite permutation, she coined the following definition: A permutation $$$b_1, b_2, dots, b_n$$$ allows an interval $$$[l', r']$$$ to holds its shape if for any pair of integers $$$(x, y)$$$ such that $$$l' le x < y le r'$$$, we have $$$b_x < b_y$$$ if and only if $$$a_x < a_y$$$. A permutation $$$b_1, b_2, dots, b_n$$$ is $$$k$$$-similar if $$$b$$$ allows all intervals $$$[l_i, r_i]$$$ for all $$$1 le i le k$$$ to hold their shapes. Yuu wants to figure out all $$$k$$$-similar permutations for Touko, but it turns out this is a very hard task; instead, Yuu will encode the set of all $$$k$$$-similar permutations with directed acylic graphs (DAG). Yuu also coined the following definitions for herself: A permutation $$$b_1, b_2, dots, b_n$$$ satisfies a DAG $$$G'$$$ if for all edge $$$u o v$$$ in $$$G'$$$, we must have $$$b_u < b_v$$$. A $$$k$$$-encoding is a DAG $$$G_k$$$ on the set of vertices $$$1, 2, dots, n$$$ such that a permutation $$$b_1, b_2, dots, b_n$$$ satisfies $$$G_k$$$ if and only if $$$b$$$ is $$$k$$$-similar. Since Yuu is free today, she wants to figure out the minimum number of edges among all $$$k$$$-encodings for each $$$k$$$ from $$$1$$$ to $$$q$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 25,000$$$, $$$1 le q le 100,000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ which form a permutation of $$$1, 2, dots, n$$$. The $$$i$$$-th of the following $$$q$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$. ($$$1 le l_i le r_i le n$$$). Output Print $$$q$$$ lines. The $$$k$$$-th of them should contain a single integer xa0— The minimum number of edges among all $$$k$$$-encodings. Examples Input 4 3 2 4 1 3 1 3 2 4 1 4 Input 8 4 3 7 4 8 1 5 2 6 3 6 1 6 3 8 1 8 Input 10 10 10 5 1 2 7 3 9 4 6 8 2 2 4 5 6 8 4 10 4 4 2 7 2 2 7 8 3 7 2 10 Output 0 1 3 6 6 9 9 9 9 8 Note For the first test case: All $$$1$$$-similar permutations must allow the interval $$$[1, 3]$$$ to hold its shape. Therefore, the set of all $$$1$$$-similar permutations is $$${[3, 4, 2, 1], [3, 4, 1, 2], [2, 4, 1, 3], [2, 3, 1, 4]}$$$. The optimal encoding of these permutations is All $$$2$$$-similar permutations must allow the intervals $$$[1, 3]$$$ and $$$[2, 4]$$$ to hold their shapes. Therefore, the set of all $$$2$$$-similar permutations is $$${[3, 4, 1, 2], [2, 4, 1, 3]}$$$. The optimal encoding of these permutations is All $$$3$$$-similar permutations must allow the intervals $$$[1, 3]$$$, $$$[2, 4]$$$, and $$$[1, 4]$$$ to hold their shapes. Therefore, the set of all $$$3$$$-similar permutations only includes $$$[2, 4, 1, 3]$$$. The optimal encoding of this permutation is
3,500
false
false
false
false
true
false
true
false
false
false
3,131
468E
Problem - 468E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp graph matchings math meet-in-the-middle *3100 No tag edit access → Contest materials . The special property of matrix _A_ is almost all its elements equal to 1. Only _k_ elements have specified value. You can find the definition of permanent at the link: first line contains two space-separated integers _n_,u2009_k_ (1u2009≤u2009_n_u2009≤u2009105;xa01u2009≤u2009_k_u2009≤u200950). The next _k_ lines contain the description of the matrix. The _i_-th line contains three space-separated integers _x__i_,u2009_y__i_,u2009_w__i_ (1u2009≤u2009_x__i_,u2009u2009_y__i_u2009≤u2009u2009_n_;xa00u2009u2009≤u2009u2009_w__i_u2009u2009≤u2009109). These numbers denote that _A__x__i_,u2009_y__i_u2009=u2009_w__i_. All the elements of the matrix except of the given elements are equal to 1. It's guaranteed that all the positions (_x__i_,u2009_y__i_) are distinct. Output Print the permanent of the matrix modulo 1000000007xa0(109u2009u2009+u2009u20097). Examples Input 3 1 1 1 2 Output 8 Input 10 10 3 3 367056794 6 2 124561273 1 3 46718146 6 9 415916869 10 5 985968336 3 1 526792265 1 4 386357058 10 4 349304187 2 7 102032499 3 6 502679075 Output 233333333
3,100
true
false
false
true
false
false
false
false
false
false
7,963
949D
Instructors of Some Informatics School make students go to bed. The house contains _n_ rooms, in each room exactly _b_ students were supposed to sleep. However, at the time of curfew it happened that many students are not located in their assigned rooms. The rooms are arranged in a row and numbered from 1 to _n_. Initially, in _i_-th room there are _a__i_ students. All students are currently somewhere in the house, therefore _a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__n_u2009=u2009_nb_. Also 2 instructors live in this house. The process of curfew enforcement is the following. One instructor starts near room 1 and moves toward room _n_, while the second instructor starts near room _n_ and moves toward room 1. After processing current room, each instructor moves on to the next one. Both instructors enter rooms and move simultaneously, if _n_ is odd, then only the first instructor processes the middle room. When all rooms are processed, the process ends. When an instructor processes a room, she counts the number of students in the room, then turns off the light, and locks the room. Also, if the number of students inside the processed room is not equal to _b_, the instructor writes down the number of this room into her notebook (and turns off the light, and locks the room). Instructors are in a hurry (to prepare the study plan for the next day), so they don't care about who is in the room, but only about the number of students. While instructors are inside the rooms, students can run between rooms that are not locked and not being processed. A student can run by at most _d_ rooms, that is she can move to a room with number that differs my at most _d_. Also, after (or instead of) running each student can hide under a bed in a room she is in. In this case the instructor will not count her during the processing. In each room any number of students can hide simultaneously. Formally, here is what's happening: A curfew is announced, at this point in room _i_ there are _a__i_ students. Each student can run to another room but not further than _d_ rooms away from her initial room, or stay in place. After that each student can optionally hide under a bed. Instructors enter room 1 and room _n_, they count students there and lock the room (after it no one can enter or leave this room). Each student from rooms with numbers from 2 to _n_u2009-u20091 can run to another room but not further than _d_ rooms away from her current room, or stay in place. Each student can optionally hide under a bed. Instructors move from room 1 to room 2 and from room _n_ to room _n_u2009-u20091. This process continues until all rooms are processed. Let _x_1 denote the number of rooms in which the first instructor counted the number of non-hidden students different from _b_, and _x_2 be the same number for the second instructor. Students know that the principal will only listen to one complaint, therefore they want to minimize the maximum of numbers _x__i_. Help them find this value if they use the optimal strategy. Input The first line contains three integers _n_, _d_ and _b_ (2u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_d_u2009≤u2009_n_u2009-u20091, 1u2009≤u2009_b_u2009≤u200910u2009000), number of rooms in the house, running distance of a student, official number of students in a room. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109), _i_-th of which stands for the number of students in the _i_-th room before curfew announcement. It is guaranteed that _a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__n_u2009=u2009_nb_. Note In the first sample the first three rooms are processed by the first instructor, and the last two are processed by the second instructor. One of the optimal strategies is the following: firstly three students run from room 5 to room 4, on the next stage two of them run to room 3, and one of those two hides under a bed. This way, the first instructor writes down room 2, and the second writes down nothing. In the second sample one of the optimal strategies is the following: firstly all students in room 1 hide, all students from room 2 run to room 3. On the next stage one student runs from room 3 to room 4, and 5 students hide. This way, the first instructor writes down rooms 1 and 2, the second instructor writes down rooms 5 and 6.
2,300
false
true
false
false
false
false
true
true
true
false
5,921
512E
Fox Ciel just designed a puzzle game called "Polygon"! It is played using triangulations of a regular _n_-edge polygon. The goal is to transform one triangulation to another by some tricky rules. Triangulation of an _n_-edge poylgon is a set of _n_u2009-u20093 diagonals satisfying the condition that no two diagonals share a common internal point. For example, the initial state of the game may look like (a) in above figure. And your goal may look like (c). In each step you can choose a diagonal inside the polygon (but not the one of edges of the polygon) and flip this diagonal. Suppose you are going to flip a diagonal _a_u2009–u2009_b_. There always exist two triangles sharing _a_u2009–u2009_b_ as a side, let's denote them as _a_u2009–u2009_b_u2009–u2009_c_ and _a_u2009–u2009_b_u2009–u2009_d_. As a result of this operation, the diagonal _a_u2009–u2009_b_ is replaced by a diagonal _c_u2009–u2009_d_. It can be easily proven that after flip operation resulting set of diagonals is still a triangulation of the polygon. So in order to solve above case, you may first flip diagonal 6u2009–u20093, it will be replaced by diagonal 2u2009–u20094. Then you flip diagonal 6u2009–u20094 and get figure (c) as result. Ciel just proved that for any starting and destination triangulations this game has a solution. She wants you to solve it in no more than 20u2009000 steps for any puzzle satisfying _n_u2009≤u20091000. Input The first line contain an integer _n_ (4u2009≤u2009_n_u2009≤u20091000), number of edges of the regular polygon. Then follows two groups of (_n_u2009-u20093) lines describing the original triangulation and goal triangulation. Description of each triangulation consists of (_n_u2009-u20093) lines. Each line contains 2 integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_), describing a diagonal _a__i_u2009–u2009_b__i_. It is guaranteed that both original and goal triangulations are correct (i. e. no two diagonals share a common internal point in both of these triangulations). Output First, output an integer _k_ (0u2009≤u2009_k_u2009≤u200920,u2009000): number of steps. Then output _k_ lines, each containing 2 integers _a__i_ and _b__i_: the endpoints of a diagonal you are going to flip at step _i_. You may output _a__i_ and _b__i_ in any order. If there are several possible solutions, output any of them. Examples Input 8 7 1 2 7 7 3 6 3 4 6 6 1 6 2 6 3 6 4 6 8 Note Sample test 2 is discussed above and shown on the picture.
2,900
false
false
false
false
false
true
false
false
false
false
7,794
1183H
The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. In one move you can take any subsequence $$$t$$$ of the given string and add it to the set $$$S$$$. The set $$$S$$$ can't contain duplicates. This move costs $$$n - t$$$, where $$$t$$$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $$$S$$$ of size $$$k$$$ or report that it is impossible to do so. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100, 1 le k le 10^{12}$$$) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set $$$S$$$ of size $$$k$$$, print -1. Otherwise, print the minimum possible total cost to do it. Note In the first example we can generate $$$S$$$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $$$S$$$ is $$$0$$$ and the cost of the others is $$$1$$$. So the total cost of $$$S$$$ is $$$4$$$.
1,900
false
false
false
true
false
false
false
false
false
false
4,795
513D2
You need to find a binary tree of size _n_ that satisfies a given set of _c_ constraints. Suppose that the nodes of the unknown binary tree are labeled using a pre-order traversal starting with 1. For the _i_-th constraint you are given two labels, _a__i_ and _b__i_ and a direction, left or right. In case of left direction, _b__i_ is an element of the subtree rooted at _a__i_'s left child. Similarly in the case of right direction _b__i_ is an element of the subtree rooted at _a__i_'s right child. Input The first line of input contains two integers _n_ and _c_. The next _c_ lines contain 2 integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_) and either "LEFT" or "RIGHT" denoting whether _b_ is in the subtree rooted at _a__i_'s left child or in the subtree rooted at _a__i_'s right child. The problem consists of multiple subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem D1 (9 points), the constraints 1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_c_u2009≤u200950 will hold. In subproblem D2 (8 points), the constraints 1u2009≤u2009_n_u2009≤u20091000000, 1u2009≤u2009_c_u2009≤u2009100000 will hold. Output Output will be on a single line. Any binary tree that satisfies the constraints will be accepted. The tree's nodes should be printed out as _n_ space separated labels representing an in-order traversal, using the pre-order numbers as labels of vertices. If there are no trees that satisfy the constraints, print "IMPOSSIBLE" (without quotes). Examples Input 3 2 1 2 LEFT 1 3 RIGHT Input 3 2 1 2 RIGHT 1 3 LEFT Note Consider the first sample test. We need to find a tree with 3 nodes that satisfies the following two constraints. The node labeled 2 with pre-order traversal should be in the left subtree of the node labeled 1 with pre-order traversal; the node labeled 3 with pre-order traversal should be in the right subtree of the node labeled 1. There is only one tree with three nodes that satisfies these constraints and its in-order traversal is (2,u20091,u20093). Pre-order is the "root – left subtree – right subtree" order. In-order is the "left subtree – root – right subtree" order. For other information regarding in-order and pre-order, see [
2,600
false
false
false
false
true
true
false
false
false
false
7,788
1383E
Koa the Koala has a binary string $$$s$$$ of length $$$n$$$. Koa can perform no more than $$$n-1$$$ (possibly zero) operations of the following form: In one operation Koa selects positions $$$i$$$ and $$$i+1$$$ for some $$$i$$$ with $$$1 le i < s$$$ and sets $$$s_i$$$ to $$$max(s_i, s_{i+1})$$$. Then Koa deletes position $$$i+1$$$ from $$$s$$$ (after the removal, the remaining parts are concatenated). Note that after every operation the length of $$$s$$$ decreases by $$$1$$$. How many different binary strings can Koa obtain by doing no more than $$$n-1$$$ (possibly zero) operations modulo $$$10^9+7$$$ ($$$1000000007$$$)? Input The only line of input contains binary string $$$s$$$ ($$$1 le s le 10^6$$$). For all $$$i$$$ ($$$1 le i le s$$$) $$$s_i = 0$$$ or $$$s_i = 1$$$. Output On a single line print the answer to the problem modulo $$$10^9+7$$$ ($$$1000000007$$$). Note In the first sample Koa can obtain binary strings: $$$0$$$, $$$00$$$ and $$$000$$$. In the second sample Koa can obtain binary strings: $$$1$$$, $$$01$$$, $$$11$$$, $$$011$$$, $$$101$$$ and $$$0101$$$. For example: to obtain $$$01$$$ from $$$0101$$$ Koa can operate as follows: $$$0101 ightarrow 0(10)1 ightarrow 011 ightarrow 0(11) ightarrow 01$$$. to obtain $$$11$$$ from $$$0101$$$ Koa can operate as follows: $$$0101 ightarrow (01)01 ightarrow 101 ightarrow 1(01) ightarrow 11$$$. Parentheses denote the two positions Koa selected in each operation.
2,800
false
false
false
true
true
false
false
false
false
false
3,752
1902F
You are given a tree consisting of $$$n$$$ vertices. There is an integer written on each vertex; the $$$i$$$-th vertex has integer $$$a_i$$$ written on it. You have to process $$$q$$$ queries. The $$$i$$$-th query consists of three integers $$$x_i$$$, $$$y_i$$$ and $$$k_i$$$. For this query, you have to answer if it is possible to choose a set of vertices $$$v_1, v_2, dots, v_m$$$ (possibly empty) such that: every vertex $$$v_j$$$ is on the simple path between $$$x_i$$$ and $$$y_i$$$ (endpoints can be used as well); $$$a_{v_1} oplus a_{v_2} oplus dots oplus a_{v_m} = k_i$$$, where $$$oplus$$$ denotes the bitwise XOR operator. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 2^{20} - 1$$$). Then $$$n-1$$$ lines follow. Each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$; $$$u e v$$$) denoting an edge of the tree. The next line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow. The $$$i$$$-th of them contains three integers $$$x_i$$$, $$$y_i$$$ and $$$k_i$$$ ($$$1 le x_i, y_i le n$$$; $$$0 le k_i le 2^{20} - 1$$$). Output For each query, print YES if it is possible to form a set of vertices meeting the constraints. Otherwise, print NO. You can print each letter in any case. Example Input 4 0 1 2 10 2 1 3 2 4 2 8 3 3 0 3 4 1 3 4 7 1 3 1 1 3 2 1 3 10 1 4 10 1 4 11 Output YES YES NO YES YES NO YES YES
2,400
true
false
true
false
true
false
false
false
false
true
898
325E
Problem - 325E - Codeforces =============== xa0 modulo _n_ or node (2·_i_)u2009+u20091 modulo _n_. The last node to be disarmed must be node 0. Node 0 must be disarmed twice, but all other nodes must be disarmed exactly once. Your task is to find any such order and print it. If there is no such order, print -1. Input Input consists of a single integer _n_ (2u2009≤u2009_n_u2009≤u2009105). Output Print an order in which you can to disarm all nodes. If it is impossible, print -1 instead. If there are multiple orders, print any one of them. Examples Input 2 Output 0 1 0 Input 3 Output -1 Input 4 Output 0 1 3 2 0 Input 16 Output 0 1 2 4 9 3 6 13 10 5 11 7 15 14 12 8 0
2,800
false
true
false
false
false
false
false
false
false
true
8,537
1870B
You are given two arrays of integers — $$$a_1, ldots, a_n$$$ of length $$$n$$$, and $$$b_1, ldots, b_m$$$ of length $$$m$$$. You can choose any element $$$b_j$$$ from array $$$b$$$ ($$$1 leq j leq m$$$), and for all $$$1 leq i leq n$$$ perform $$$a_i = a_i b_j$$$. You can perform any number of such operations. After all the operations, the value of $$$x = a_1 oplus a_2 oplus ldots oplus a_n$$$ will be calculated. Find the minimum and maximum values of $$$x$$$ that could be obtained after performing any set of operations. Above, $$$$$$ is the — the number of test cases. This is followed by the description of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 2 cdot 10^5$$$) — the sizes of arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$) — the array $$$a$$$. The third line of each test case contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$0 leq b_i leq 10^9$$$) — the array $$$b$$$. It is guaranteed that the sum of values of $$$n$$$ and $$$m$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$2$$$ numbers: the minimum and maximum possible values of $$$x$$$ after performing any set of operations. Example Input 2 2 3 0 1 1 2 3 3 1 1 1 2 1 Note In the first test case, if we apply the operation with element $$$b_1 = 1$$$, the array $$$a$$$ will become $$$[1, 1]$$$, and $$$x$$$ will be $$$0$$$. If no operations are applied, then $$$x = 1$$$. In the second test case, if no operations are applied, then $$$x = 2$$$. If we apply the operation with $$$b_1 = 1$$$, then $$$a = [1, 1, 3]$$$, and $$$x = 3$$$.
1,200
true
true
false
false
false
false
false
false
false
false
1,058
328B
Problem - 328B - Codeforces =============== xa0 . Similary, 2 and 5 work the same. There is no other pair of digits with similar effect. He called this effect "Digital Mimicry". Sheldon favourite number is _t_. He wants to have as many instances of _t_ as possible. How many instances he can construct using the given sequence of ice pieces. He can use any piece at most once. Input The first line contains integer _t_ (1u2009≤u2009_t_u2009≤u200910000). The second line contains the sequence of digits on the pieces. The length of line is equal to the number of pieces and between 1 and 200, inclusive. It contains digits between 0 and 9. Output Print the required number of instances. Examples Input 42 23454 Output 2 Input 169 12118999 Output 1 Note This problem contains very weak pretests.
1,500
false
true
false
false
false
false
false
false
false
false
8,530
1610H
After watching the new over-rated series Squid Game, Mashtali and Soroush decided to hold their own Squid Games! Soroush agreed to be the host and will provide money for the winner's prize, and Mashtali became the Front Man! $$$m$$$ players registered to play in the games to win the great prize, but when Mashtali found out how huge the winner's prize is going to be, he decided to kill eliminate all the players so he could take the money for himself! Here is how evil Mashtali is going to eliminate players: There is an unrooted tree with $$$n$$$ vertices. Every player has $$$2$$$ special vertices $$$x_i$$$ and $$$y_i$$$. In one operation, Mashtali can choose any vertex $$$v$$$ of the tree. Then, for each remaining player $$$i$$$ he finds a vertex $$$w$$$ on the simple path from $$$x_i$$$ to $$$y_i$$$, which is the closest to $$$v$$$. If $$$w e x_i$$$ and $$$w e y_i$$$, player $$$i$$$ will be eliminated. Now Mashtali wondered: "What is the minimum number of operations I should perform so that I can remove every player from the game and take the money for myself?" Since he was only thinking about the money, he couldn't solve the problem by himself and asked for your help! Input The first line contains $$$2$$$ integer $$$n$$$ and $$$m$$$ $$$(1 le n, m le 3 cdot 10^5)$$$xa0— the number of vertices of the tree and the number of players. The second line contains $$$n-1$$$ integers $$$par_2, par_3, ldots, par_n$$$ $$$(1 le par_i < i)$$$xa0— denoting an edge between node $$$i$$$ and $$$par_i$$$. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ $$$(1 le x_i, y_i le n, x_i e y_i)$$$xa0— the special vertices of the $$$i$$$-th player. Output Print the minimum number of operations Mashtali has to perform. If there is no way for Mashtali to eliminate all the players, print $$$-1$$$. Note Explanation for the first sample: In the first operation, Mashtali can choose vertex $$$1$$$ and eliminate players with colors red and blue. In the second operation, he can choose vertex $$$6$$$ and eliminate the player with orange color. In the second sample, Mashtali can't eliminate the first player.
3,100
false
true
false
false
true
false
false
false
false
false
2,593
1986E
You are given an array of integers $$$a_1, a_2, ldots, a_n$$$ and an integer $$$k$$$. You need to make it beautiful with the least amount of operations. Before applying operations, you can shuffle the array elements as you like. For one operation, you can do the following: Choose an index $$$1 leq i leq n$$$, Make $$$a_i = a_i + k$$$. The array $$$b_1, b_2, ldots, b_n$$$ is beautiful if $$$b_i = b_{n - i + 1}$$$ for all $$$1 leq i leq n$$$. Find the minimum number of operations needed to make the array beautiful, or report that it is impossible. Input Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of sets of input data. Then follows their description. The first line of each set of input data contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^5$$$, $$$1 leq k leq 10^9$$$)xa0— the size of the array $$$a$$$ and the number $$$k$$$ from the problem statement. The second line of each set of input data contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all sets of input data does not exceed $$$2 cdot 10^5$$$. Output For each set of input data, output the minimum number of operations needed to make the array beautiful, or $$$-1$$$ if it is impossible. Example Input 11 1 1000000000 1 2 1 624323799 708290323 3 1 3 2 1 4 1 7 1 5 3 5 1 11 2 15 7 10 7 1 1 8 2 16 8 16 31 13 1 2 1 1 3 3 11 12 22 45 777 777 1500 74 10 2 1 2 1 2 1 2 1 2 1 2 11 2 1 2 1 2 1 2 1 2 1 2 1 13 3 2 3 9 14 17 10 22 20 18 30 1 4 28 5 1 2 3 5 3 5 Output 0 83966524 1 4 6 1 48 -1 0 14 0 Note In the first set of input data, the array is already beautiful. In the second set of input data, you can shuffle the array before the operations and perform the operation with index $$$i = 1$$$ for $$$83966524$$$ times. In the third set of input data, you can shuffle the array $$$a$$$ and make it equal to $$$[2, 3, 1]$$$. Then apply the operation with index $$$i = 3$$$ to get the array $$$[2, 3, 2]$$$, which is beautiful. In the eighth set of input data, there is no set of operations and no way to shuffle the elements to make the array beautiful. In the ninth set of input data, the array is already beautiful.
1,700
true
true
false
false
false
false
false
false
true
false
361
1904E
Those who don't work don't eat. Get the things you want with your own power. But believe, the earnest and serious people are the ones who have the last laugh... But even then, I won't give you a present. —Santa, Hayate no Gotoku! Since Hayate didn't get any Christmas presents from Santa, he is instead left solving a tree query problem. Hayate has a tree with $$$n$$$ nodes. Hayate now wants you to answer $$$q$$$ queries. Each query consists of a node $$$x$$$ and $$$k$$$ other additional nodes $$$a_1,a_2,ldots,a_k$$$. These $$$k+1$$$ nodes are guaranteed to be all distinct. For each query, you must find the length of the longest simple path starting at node $$$x^dagger$$$ after removing nodes $$$a_1,a_2,ldots,a_k$$$ along with all edges connected to at least one of nodes $$$a_1,a_2,ldots,a_k$$$. $$$^dagger$$$ A simple path of length $$$k$$$ starting at node $$$x$$$ is a sequence of distinct nodes $$$x=u_0,u_1,ldots,u_k$$$ such that there exists a edge between nodes $$$u_{i-1}$$$ and $$$u_i$$$ for all $$$1 leq i leq k$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$)xa0— the number of nodes of the tree and the number of queries. The following $$$n - 1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u e v$$$)xa0— denoting an edge between nodes $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree. The following $$$q$$$ lines describe the queries. Each line contains the integers $$$x$$$, $$$k$$$ and $$$a_1,a_2,ldots,a_k$$$ ($$$1 leq x leq n$$$, $$$0 leq k < n$$$, $$$1 leq a_i leq n$$$)xa0— the starting node, the number of removed nodes and the removed nodes. It is guaranteed that for each query, $$$x,a_1,a_2,ldots,a_k$$$ are all distinct. It is guaranteed that the sum of $$$k$$$ over all queries will not exceed $$$2 cdot 10^5$$$. Output For each query, output a single integer denoting the answer for that query. Examples Input 7 7 1 2 1 3 3 4 2 5 2 6 6 7 2 0 2 1 1 2 2 1 6 3 1 4 3 1 1 5 0 5 2 1 6 Input 4 4 1 2 1 3 2 4 2 1 3 3 1 4 2 1 4 2 3 1 3 4 Note In the first example, the tree is as follows: In the first query, no nodes are missing. The longest simple path starting from node $$$2$$$ is $$$2 o 1 o 3 o 4$$$. Thus, the answer is $$$3$$$. In the third query, nodes $$$1$$$ and $$$6$$$ are missing and the tree is shown below. The longest simple path starting from node $$$2$$$ is $$$2 o 5$$$. Thus, the answer is $$$1$$$.
2,500
false
false
true
false
true
false
false
false
false
true
885
770B
Problem - 770B - Codeforces =============== xa0 ]( "Анонс") — the integer which Anton has. Output Print the positive integer which doesn't exceed _x_ and has the maximum sum of digits. If there are several such integers, print the biggest of them. Printed integer must not contain leading zeros. Examples Input 100 Output 99 Input 48 Output 48 Input 521 Output 499
1,300
true
false
true
false
false
false
false
false
false
false
6,701
863B
Vadim is really keen on travelling. Recently he heard about kayaking activity near his town and became very excited about it, so he joined a party of kayakers. Now the party is ready to start its journey, but firstly they have to choose kayaks. There are 2·_n_ people in the group (including Vadim), and they have exactly _n_u2009-u20091 tandem kayaks (each of which, obviously, can carry two people) and 2 single kayaks. _i_-th person's weight is _w__i_, and weight is an important matter in kayaking — if the difference between the weights of two people that sit in the same tandem kayak is too large, then it can crash. And, of course, people want to distribute their seats in kayaks in order to minimize the chances that kayaks will crash. Formally, the instability of a single kayak is always 0, and the instability of a tandem kayak is the absolute difference between weights of the people that are in this kayak. Instability of the whole journey is the total instability of all kayaks. Help the party to determine minimum possible total instability! Input The first line contains one number _n_ (2u2009≤u2009_n_u2009≤u200950). The second line contains 2·_n_ integer numbers _w_1, _w_2, ..., _w_2_n_, where _w__i_ is weight of person _i_ (1u2009≤u2009_w__i_u2009≤u20091000). Output Print minimum possible total instability. Examples Input 4 1 3 4 6 3 4 100 200
1,500
false
true
false
false
false
false
true
false
true
false
6,293
1674G
You are given a directed acyclic graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. The vertices are numbered from $$$1$$$ to $$$n$$$. There are no multiple edges and self-loops. Let $$$mathit{in}_v$$$ be the number of incoming edges (indegree) and $$$mathit{out}_v$$$ be the number of outgoing edges (outdegree) of vertex $$$v$$$. You are asked to remove some edges from the graph. Let the new degrees be $$$mathit{in'}_v$$$ and $$$mathit{out'}_v$$$. You are only allowed to remove the edges if the following conditions hold for every vertex $$$v$$$: $$$mathit{in'}_v < mathit{in}_v$$$ or $$$mathit{in'}_v = mathit{in}_v = 0$$$; $$$mathit{out'}_v < mathit{out}_v$$$ or $$$mathit{out'}_v = mathit{out}_v = 0$$$. Let's call a set of vertices $$$S$$$ cute if for each pair of vertices $$$v$$$ and $$$u$$$ ($$$v eq u$$$) such that $$$v in S$$$ and $$$u in S$$$, there exists a path either from $$$v$$$ to $$$u$$$ or from $$$u$$$ to $$$v$$$ over the non-removed edges. What is the maximum possible size of a cute set $$$S$$$ after you remove some edges from the graph and both indegrees and outdegrees of all vertices either decrease or remain equal to $$$0$$$? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$0 le m le 2 cdot 10^5$$$)xa0— the number of vertices and the number of edges of the graph. Each of the next $$$m$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$; $$$v eq u$$$)xa0— the description of an edge. The given edges form a valid directed acyclic graph. There are no multiple edges. Note In the first example, you can remove edges $$$(1, 2)$$$ and $$$(2, 3)$$$. $$$mathit{in} = [0, 1, 2]$$$, $$$mathit{out} = [2, 1, 0]$$$. $$$mathit{in'} = [0, 0, 1]$$$, $$$mathit{out'} = [1, 0, 0]$$$. You can see that for all $$$v$$$ the conditions hold. The maximum cute set $$$S$$$ is formed by vertices $$$1$$$ and $$$3$$$. They are still connected directly by an edge, so there is a path between them. In the second example, there are no edges. Since all $$$mathit{in}_v$$$ and $$$mathit{out}_v$$$ are equal to $$$0$$$, leaving a graph with zero edges is allowed. There are $$$5$$$ cute sets, each contains a single vertex. Thus, the maximum size is $$$1$$$. In the third example, you can remove edges $$$(7, 1)$$$, $$$(2, 4)$$$, $$$(1, 3)$$$ and $$$(6, 2)$$$. The maximum cute set will be $$$S = {7, 3, 2}$$$. You can remove edge $$$(7, 3)$$$ as well, and the answer won't change. Here is the picture of the graph from the third example:
2,000
false
false
false
true
false
false
false
false
false
true
2,223
1907D
Polycarp is designing a level for a game. The level consists of $$$n$$$ segments on the number line, where the $$$i$$$-th segment starts at the point with coordinate $$$l_i$$$ and ends at the point with coordinate $$$r_i$$$. The player starts the level at the point with coordinate $$$0$$$. In one move, they can move to any point that is within a distance of no more than $$$k$$$. After their $$$i$$$-th move, the player must land within the $$$i$$$-th segment, that is, at a coordinate $$$x$$$ such that $$$l_i le x le r_i$$$. This means: After the first move, they must be inside the first segment (from $$$l_1$$$ to $$$r_1$$$); After the second move, they must be inside the second segment (from $$$l_2$$$ to $$$r_2$$$); ... After the $$$n$$$-th move, they must be inside the $$$n$$$-th segment (from $$$l_n$$$ to $$$r_n$$$). The level is considered completed if the player reaches the $$$n$$$-th segment, following the rules described above. After some thought, Polycarp realized that it is impossible to complete the level with some values of $$$k$$$. Polycarp does not want the level to be too easy, so he asks you to determine the minimum integer $$$k$$$ with which it is possible to complete the level. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)—the number of test cases. Descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)—the number of segments in the level. The following $$$n$$$ lines. The $$$i$$$-th line contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 le l_i le r_i le 10^9$$$)—the boundaries of the $$$i$$$-th segment. Segments may intersect. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer—the minimum value of $$$k$$$ with which it is possible to complete the level. Example Input 4 5 1 5 3 4 5 6 8 10 0 1 3 0 2 0 1 0 3 3 3 8 10 18 6 11 4 10 20 0 5 15 17 2 2 Note In the third example, the player can make the following moves: Move from point $$$0$$$ to point $$$5$$$ ($$$3 le 5 le 8$$$); Move from point $$$5$$$ to point $$$10$$$ ($$$10 le 10 le 18$$$); Move from point $$$10$$$ to point $$$7$$$ ($$$6 le 7 le 11$$$). Note that for the last move, the player could have chosen not to move and still complete the level.
1,400
false
false
false
false
false
true
false
true
false
false
861
582E
In this problem we consider Boolean functions of four variables _A_,u2009_B_,u2009_C_,u2009_D_. Variables _A_,u2009_B_,u2009_C_ and _D_ are logical and can take values 0 or 1. We will define a function using the following grammar: <expression> ::= <variable> (<expression>) <operator> (<expression>) <variable> ::= 'A' 'B' 'C' 'D' 'a' 'b' 'c' 'd' <operator> ::= '&' '' Here large letters _A_,u2009_B_,u2009_C_,u2009_D_ represent variables, and small letters represent their negations. For example, if _A_u2009=u20091, then character 'A' corresponds to value 1, and value character 'a' corresponds to value 0. Here character '&' corresponds to the operation of logical AND, character '' corresponds to the operation of logical OR. You are given expression _s_, defining function _f_, where some operations and variables are missing. Also you know the values of the function _f_(_A_,u2009_B_,u2009_C_,u2009_D_) for some _n_ distinct sets of variable values. Count the number of ways to restore the elements that are missing in the expression so that the resulting expression corresponded to the given information about function _f_ in the given variable sets. As the value of the result can be rather large, print its remainder modulo 109u2009+u20097. Input The first line contains expression _s_ (1u2009≤u2009_s_u2009≤u2009500), where some characters of the operators and/or variables are replaced by character '?'. The second line contains number _n_ (0u2009≤u2009_n_u2009≤u200924) — the number of integers sets for which we know the value of function _f_(_A_,u2009_B_,u2009_C_,u2009_D_). Next _n_ lines contain the descriptions of the sets: the _i_-th of them contains five integers _a__i_,u2009_b__i_,u2009_c__i_,u2009_d__i_,u2009_e__i_ (0u2009≤u2009_a__i_,u2009_b__i_,u2009_c__i_,u2009_d__i_,u2009_e__i_u2009≤u20091), separated by spaces and meaning that _f_(_a__i_,u2009_b__i_,u2009_c__i_,u2009_d__i_)u2009=u2009_e__i_. It is guaranteed that all the tuples (_a__i_,u2009_b__i_,u2009_c__i_,u2009_d__i_) are distinct. Output In a single line print the answer to the problem. Examples Input ? 2 1 0 1 0 1 0 1 1 0 1 Input ((?)&(?))((?)&(?)) 0 Note In the first sample the two valid expressions are 'C' and 'd'. In the second sample the expressions look as follows: '(A)&(a)', '(A)&(b)', '(A)&(C)', '(A)&(D)'.
3,000
false
false
false
true
false
false
false
false
false
false
7,513
1558C
You have a permutation: an array $$$a = [a_1, a_2, ldots, a_n]$$$ of distinct integers from $$$1$$$ to $$$n$$$. The length of the permutation $$$n$$$ is odd. You need to sort the permutation in increasing order. In one step, you can choose any prefix of the permutation with an odd length and reverse it. Formally, if $$$a = [a_1, a_2, ldots, a_n]$$$, you can choose any odd integer $$$p$$$ between $$$1$$$ and $$$n$$$, inclusive, and set $$$a$$$ to $$$[a_p, a_{p-1}, ldots, a_1, a_{p+1}, a_{p+2}, ldots, a_n]$$$. Find a way to sort $$$a$$$ using no more than $$$frac{5n}{2}$$$ reversals of the above kind, or determine that such a way doesn't exist. The number of reversals doesn't have to be minimized. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2021$$$; $$$n$$$ is odd)xa0— the length of the permutation. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the permutation itself. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2021$$$. Output For each test case, if it's impossible to sort the given permutation in at most $$$frac{5n}{2}$$$ reversals, print a single integer $$$-1$$$. Otherwise, print an integer $$$m$$$ ($$$0 le m le frac{5n}{2}$$$), denoting the number of reversals in your sequence of steps, followed by $$$m$$$ integers $$$p_i$$$ ($$$1 le p_i le n$$$; $$$p_i$$$ is odd), denoting the lengths of the prefixes of $$$a$$$ to be reversed, in chronological order. Note that $$$m$$$ doesn't have to be minimized. If there are multiple answers, print any. Example Input 3 3 1 2 3 5 3 4 5 2 1 3 2 1 3 Note In the first test case, the permutation is already sorted. Any even number of reversals of the length $$$3$$$ prefix doesn't change that fact. In the second test case, after reversing the prefix of length $$$3$$$ the permutation will change to $$$[5, 4, 3, 2, 1]$$$, and then after reversing the prefix of length $$$5$$$ the permutation will change to $$$[1, 2, 3, 4, 5]$$$. In the third test case, it's impossible to sort the permutation.
2,000
false
true
false
false
false
true
false
false
false
false
2,840
2028F
Note that the memory limit is unusual. The Cheshire Cat has a riddle for Alice: given $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ and a target $$$m$$$, is there a way to insert $$$+$$$ and $$$ imes$$$ into the circles of the expression $$$$$$a_1 circ a_2 circ cdots circ a_n = m$$$$$$ to make it true? We follow the usual order of operations: $$$ imes$$$ is done before $$$+$$$. Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland! Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1le nle 2cdot 10^5$$$; $$$1le mle 10^4$$$)xa0— the number of integers and the target, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0le a_ile 10^4$$$)xa0— the elements of the array $$$a$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output "YES" without quotes if it is possible to get the target by inserting $$$+$$$ or $$$ imes$$$ and "NO" otherwise. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 6 5 4 2 1 1 1 2 5 5 2 1 1 1 2 5 6 2 1 1 1 2 5 7 2 1 1 1 2 5 8 2 1 1 1 2 5 6 2 0 2 2 3 Output YES YES YES YES NO YES Note Possible solutions for the first four test cases are shown below. $$$$$$begin{align*} 2 imes 1 + 1 imes 1 imes 2 &= 4 2 imes 1 + 1 + 1 imes 2 &= 5 2 imes 1 + 1 + 1 + 2 &= 6 2 + 1 + 1 + 1 + 2 &= 7 end{align*}$$$$$$ It is impossible to get a result of $$$8$$$ in the fifth test case.
2,700
false
false
true
true
false
false
true
false
false
false
98
1225E
You are at the top left cell $$$(1, 1)$$$ of an $$$n imes m$$$ labyrinth. Your goal is to get to the bottom right cell $$$(n, m)$$$. You can only move right or down, one cell per step. Moving right from a cell $$$(x, y)$$$ takes you to the cell $$$(x, y + 1)$$$, while moving down takes you to the cell $$$(x + 1, y)$$$. Some cells of the labyrinth contain rocks. When you move to a cell with rock, the rock is pushed to the next cell in the direction you're moving. If the next cell contains a rock, it gets pushed further, and so on. The labyrinth is surrounded by impenetrable walls, thus any move that would put you or any rock outside of the labyrinth is illegal. Count the number of different legal paths you can take from the start to the goal modulo $$$10^9 + 7$$$. Two paths are considered different if there is at least one cell that is visited in one path, but not visited in the other. Input The first line contains two integers $$$n, m$$$xa0— dimensions of the labyrinth ($$$1 leq n, m leq 2000$$$). Next $$$n$$$ lines describe the labyrinth. Each of these lines contains $$$m$$$ characters. The $$$j$$$-th character of the $$$i$$$-th of these lines is equal to "R" if the cell $$$(i, j)$$$ contains a rock, or "." if the cell $$$(i, j)$$$ is empty. It is guaranteed that the starting cell $$$(1, 1)$$$ is empty. Output Print a single integerxa0— the number of different legal paths from $$$(1, 1)$$$ to $$$(n, m)$$$ modulo $$$10^9 + 7$$$. Examples Input 4 4 ...R .RR. .RR. R... Note In the first sample case we can't (and don't have to) move, hence the only path consists of a single cell $$$(1, 1)$$$. In the second sample case the goal is blocked and is unreachable. Illustrations for the third sample case can be found here: [
2,200
false
false
false
true
false
false
false
true
false
false
4,547
1339A
You have integer $$$n$$$. Calculate how many ways are there to fully cover belt-like area of $$$4n-2$$$ triangles with diamond shapes. Diamond shape consists of two triangles. You can move, rotate or flip the shape, but you cannot scale it. $$$2$$$ coverings are different if some $$$2$$$ triangles are covered by the same diamond shape in one of them and by different diamond shapes in the other one. Please look at pictures below for better understanding. On the left you can see the diamond shape you will use, and on the right you can see the area you want to fill.These are the figures of the area you want to fill for $$$n = 1, 2, 3, 4$$$. You have to answer $$$t$$$ independent test cases. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^{4}$$$)xa0— the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$1 le n le 10^{9}$$$). Output For each test case, print the number of ways to fully cover belt-like area of $$$4n-2$$$ triangles using diamond shape. It can be shown that under given constraints this number of ways doesn't exceed $$$10^{18}$$$. Note In the first test case, there are the following $$$2$$$ ways to fill the area: In the second test case, there is a unique way to fill the area:
900
true
false
true
true
false
false
true
false
false
false
4,009
1515I
Phoenix wonders what it is like to rob diamonds from a jewelry store! There are $$$n$$$ types of diamonds. The $$$i$$$-th type has weight $$$w_i$$$ and value $$$v_i$$$. The store initially has $$$a_i$$$ diamonds of the $$$i$$$-th type. Each day, for $$$q$$$ days, one of the following will happen: 1. A new shipment of $$$k_i$$$ diamonds of type $$$d_i$$$ arrive. 2. The store sells $$$k_i$$$ diamonds of type $$$d_i$$$. 3. Phoenix wonders what will happen if he robs the store using a bag that can fit diamonds with total weight not exceeding $$$c_i$$$. If he greedily takes diamonds of the largest value that fit, how much value would be taken? If there are multiple diamonds with the largest value, he will take the one with minimum weight. If, of the diamonds with the largest value, there are multiple with the same minimum weight, he will take any of them. Of course, since Phoenix is a law-abiding citizen, this is all a thought experiment and he never actually robs any diamonds from the store. This means that queries of type $$$3$$$ do not affect the diamonds in the store. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le q le 10^5$$$)xa0— the number of types of diamonds and number of days, respectively. The next $$$n$$$ lines describe each type of diamond. The $$$i$$$-th line will contain three integers $$$a_i$$$, $$$w_i$$$, and $$$v_i$$$ ($$$0 le a_i le 10^5$$$; $$$1 le w_i, v_i le 10^5$$$)xa0— the initial number of diamonds of the $$$i$$$-th type, the weight of diamonds of the $$$i$$$-th type, and the value of diamonds of the $$$i$$$-th type, respectively. The next $$$q$$$ lines contain the queries. For each query, the first integer of each line is $$$t$$$ ($$$1 le t le 3$$$)xa0— the type of query. If $$$t=1$$$, then two integers $$$k_i$$$, $$$d_i$$$ follow ($$$1 le k_i le 10^5$$$; $$$1 le d_i le n$$$). This means that a new shipment of $$$k_i$$$ diamonds arrived, each of type $$$d_i$$$. If $$$t=2$$$, then two integers $$$k_i$$$, $$$d_i$$$ follow ($$$1 le k_i le 10^5$$$; $$$1 le d_i le n$$$). This means that the store has sold $$$k_i$$$ diamonds, each of type $$$d_i$$$. It is guaranteed that the store had the diamonds before they sold them. If $$$t=3$$$, an integer $$$c_i$$$ will follow ($$$1 le c_i le 10^{18}$$$)xa0— the weight capacity of Phoenix's bag. It is guaranteed that there is at least one query where $$$t=3$$$. Note For the first query where $$$t=3$$$, Phoenix can fit $$$2$$$ diamonds of type $$$1$$$, with total weight $$$6$$$ and value $$$8$$$. For the second query where $$$t=3$$$, Phoenix will first fit in $$$3$$$ diamonds of type $$$3$$$, then one diamond of type $$$1$$$ for a total weight of $$$9$$$ and a value of $$$16$$$. Note that diamonds of type $$$3$$$ are prioritized over type $$$1$$$ because type $$$3$$$ has equal value but less weight. For the final query where $$$t=3$$$, Phoenix can fit every diamond for a total value of $$$13$$$.
3,400
false
false
false
false
true
false
false
true
true
false
3,083
1621B
The integers shop sells $$$n$$$ segments. The $$$i$$$-th of them contains all integers from $$$l_i$$$ to $$$r_i$$$ and costs $$$c_i$$$ coins. Tomorrow Vasya will go to this shop and will buy some segments there. He will get all integers that appear in at least one of bought segments. The total cost of the purchase is the sum of costs of all segments in it. After shopping, Vasya will get some more integers as a gift. He will get integer $$$x$$$ as a gift if and only if all of the following conditions are satisfied: Vasya hasn't bought $$$x$$$. Vasya has bought integer $$$l$$$ that is less than $$$x$$$. Vasya has bought integer $$$r$$$ that is greater than $$$x$$$. Vasya can get integer $$$x$$$ as a gift only once so he won't have the same integers after receiving a gift. For example, if Vasya buys segment $$$[2, 4]$$$ for $$$20$$$ coins and segment $$$[7, 8]$$$ for $$$22$$$ coins, he spends $$$42$$$ coins and receives integers $$$2, 3, 4, 7, 8$$$ from these segments. He also gets integers $$$5$$$ and $$$6$$$ as a gift. Due to the technical issues only the first $$$s$$$ segments (that is, segments $$$[l_1, r_1], [l_2, r_2], ldots, [l_s, r_s]$$$) will be available tomorrow in the shop. Vasya wants to get (to buy or to get as a gift) as many integers as possible. If he can do this in differents ways, he selects the cheapest of them. For each $$$s$$$ from $$$1$$$ to $$$n$$$, find how many coins will Vasya spend if only the first $$$s$$$ segments will be available. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$1 leq n leq 10^5$$$)xa0— the number of segments in the shop. Each of next $$$n$$$ lines contains three integers $$$l_i$$$, $$$r_i$$$, $$$c_i$$$ ($$$1 leq l_i leq r_i leq 10^9, 1 leq c_i leq 10^9$$$)xa0— the ends of the $$$i$$$-th segments and its cost. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Note In the first test case if $$$s = 1$$$ then Vasya can buy only the segment $$$[2, 4]$$$ for $$$20$$$ coins and get $$$3$$$ integers. The way to get $$$7$$$ integers for $$$42$$$ coins in case $$$s = 2$$$ is described in the statement. In the second test case note, that there can be the same segments in the shop.
1,500
false
true
true
false
true
false
false
false
false
false
2,519
128B
One day in the IT lesson Anna and Maria learned about the lexicographic order. String _x_ is lexicographically less than string _y_, if either _x_ is a prefix of _y_ (and _x_u2009≠u2009_y_), or there exists such _i_ (1u2009≤u2009_i_u2009≤u2009_min_(_x_,u2009_y_)), that _x__i_u2009<u2009_y__i_, and for any _j_ (1u2009≤u2009_j_u2009<u2009_i_) _x__j_u2009=u2009_y__j_. Here _a_ denotes the length of the string _a_. The lexicographic comparison of strings is implemented by operator < in modern programming languagesu200bu200b. The teacher gave Anna and Maria homework. She gave them a string of length _n_. They should write out all substrings of the given string, including the whole initial string, and the equal substrings (for example, one should write out the following substrings from the string "aab": "a", "a", "aa", "ab", "aab", "b"). The resulting strings should be sorted in the lexicographical order. The cunning teacher doesn't want to check all these strings. That's why she said to find only the _k_-th string from the list. Help Anna and Maria do the homework. Input The first line contains a non-empty string that only consists of small Latin letters ("a"-"z"), whose length does not exceed 105. The second line contains the only integer _k_ (1u2009≤u2009_k_u2009≤u2009105). Output Print the string Anna and Maria need — the _k_-th (in the lexicographical order) substring of the given string. If the total number of substrings is less than _k_, print a string saying "No such line." (without the quotes). Note In the second sample before string "bc" follow strings "a", "ab", "abc", "b".
2,100
false
false
true
false
false
true
true
false
false
false
9,369
1450G
Please pay attention to the unusual memory limit in this problem. In a parallel universe, Satan is called "Trygub". For that reason, the letters of his namesake were deleted from the alphabet in ancient times. The government has $$$n$$$ workers standing in a row and numbered with integers from $$$1$$$ to $$$n$$$ from left to right. Their job categories can be represented as a string $$$s$$$ of length $$$n$$$, where the character $$$s_i$$$ represents the job category of the $$$i$$$-th worker. A new law will be approved to increase the equality between the workers. The government decided to make everyone have the same job category by performing the following operation any number of times (possibly zero). There is a fixed rational parameter $$$k=frac ab$$$ describing how easy it is to convince the public, and it will be used to determine the success of an operation. In an operation, the government first selects a job category $$$x$$$ with at least one worker at the current moment. Suppose $$$i_1,ldots, i_m$$$ ($$$i_1<ldots<i_m$$$) are the positions of all the workers with job category $$$x$$$. If $$$kcdot (i_m-i_1+1)le m$$$, the government is able to choose any job category $$$y$$$ with at least one worker at the current moment and change the job category of all workers with job category $$$x$$$ to job category $$$y$$$. If it is possible to make all workers have job category $$$x$$$, we say that $$$x$$$ is obtainable. Can you tell the government the set of obtainable job categories? Input The first line contains three integers $$$n, a, b$$$ ($$$1 le n le 5000$$$, $$$1le ale ble 10^5$$$) — the number of workers and the numerator and denominator of the parameter $$$k$$$, respectively. The second line contains a string $$$s$$$ of length $$$n$$$, consisting of lowercase English characters — the job categories of each worker. The characters 't', 'r', 'y', 'g', 'u', and 'b' do not appear in the string $$$s$$$. Note The first operation must select the job category 'i' because all other job categories cannot satisfy the condition, therefore 'i' is not obtainable. Below is showed how to obtain 'c', 'm', and 'o'. The square brackets denote the segment containing all workers of the selected category, the red color denotes this category and the blue color denotes the new category after the change. Get 'c': 1. ($$$ exttt{com}color{red}{ exttt{[i]}} exttt{com} ightarrow exttt{com}color{#1E90FF}{ exttt{[o]}} exttt{com}$$$) 2. ($$$ exttt{c}color{red}{ exttt{[o}} exttt{m}color{red}{ exttt{o}} exttt{c}color{red}{ exttt{o]}} exttt{m} ightarrow exttt{c}color{#1E90FF}{ exttt{[m}} exttt{m}color{#1E90FF}{ exttt{m}} exttt{c}color{#1E90FF}{ exttt{m]}} exttt{m}$$$) 3. ($$$ exttt{c}color{red}{ exttt{[mmm}} exttt{c}color{red}{ exttt{mm]}} ightarrow exttt{c}color{#1E90FF}{ exttt{[ccc}} exttt{c}color{#1E90FF}{ exttt{cc]}}$$$) Get 'm': 1. ($$$ exttt{com}color{red}{ exttt{[i]}} exttt{com} ightarrow exttt{com}color{#1E90FF}{ exttt{[o]}} exttt{com}$$$) 2. ($$$ exttt{c}color{red}{ exttt{[o}} exttt{m}color{red}{ exttt{o}} exttt{c}color{red}{ exttt{o]}} exttt{m} ightarrow exttt{c}color{#1E90FF}{ exttt{[c}} exttt{m}color{#1E90FF}{ exttt{c}} exttt{c}color{#1E90FF}{ exttt{c]}} exttt{m}$$$) 3. ($$$color{red}{ exttt{[cc}} exttt{m}color{red}{ exttt{ccc]}} exttt{m} ightarrow color{#1E90FF}{ exttt{[mm}} exttt{m}color{#1E90FF}{ exttt{mmm]}} exttt{m}$$$) Get 'o': 1. ($$$ exttt{com}color{red}{ exttt{[i]}} exttt{com} ightarrow exttt{com}color{#1E90FF}{ exttt{[c]}} exttt{com}$$$) 2. ($$$color{red}{ exttt{[c}} exttt{om}color{red}{ exttt{cc]}} exttt{om} ightarrow color{#1E90FF}{ exttt{[m}} exttt{om}color{#1E90FF}{ exttt{mm]}} exttt{om}$$$) 3. ($$$color{red}{ exttt{[m}} exttt{o}color{red}{ exttt{mmm}} exttt{o}color{red}{ exttt{m]}} ightarrow color{#1E90FF}{ exttt{[o}} exttt{o}color{#1E90FF}{ exttt{ooo}} exttt{o}color{#1E90FF}{ exttt{o]}}$$$)
3,500
false
false
false
true
false
false
false
false
false
false
3,431
303D
Problem - 303D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math number theory *2600 No tag edit access → Contest materials . Rotating a number means putting its last several digit into first. For example, by rotating number 12345 you can obtain any numbers: 12345,u200951234,u200945123,u200934512,u200923451. It's worth mentioning that leading-zeroes are allowed. So both 4500123 and 0123450 can be obtained by rotating 0012345. You can see why 142857 satisfies the condition. All of the 6 equations are under base 10. 142857·1u2009=u2009142857; 142857·2u2009=u2009285714; 142857·3u2009=u2009428571; 142857·4u2009=u2009571428; 142857·5u2009=u2009714285; 142857·6u2009=u2009857142. Now, Bike has a problem. He extends "Rotatable Number" under any base _b_. As is mentioned above, 142857 is a "Rotatable Number" under base 10. Another example is 0011 under base 2. All of the 4 equations are under base 2. 0011·1u2009=u20090011; 0011·10u2009=u20090110; 0011·11u2009=u20091001; 0011·100u2009=u20091100. So, he wants to find the largest _b_ (1u2009<u2009_b_u2009<u2009_x_) so that there is a positive "Rotatable Number" (leading-zeroes allowed) of length _n_ under base _b_. Note that any time you multiply a rotatable number by numbers from 1 to its length you should get a rotating of that number. Input The only line contains two space-separated integers _n_,u2009_x_ (1u2009≤u2009_n_u2009≤u20095·106,u20092u2009≤u2009_x_u2009≤u2009109). Output Print a single integer — the largest _b_ you found. If no such _b_ exists, print -1 instead. Examples Input 6 11 Output 10 Input 5 8 Output -1
2,600
true
false
false
false
false
false
false
false
false
false
8,620
727B
Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. The bill is a string in which the names of the purchases and their prices are printed in a row without any spaces. Check has the format "_name_1_price_1_name_2_price_2..._name__n__price__n_", where _name__i_ (name of the _i_-th purchase) is a non-empty string of length not more than 10, consisting of lowercase English letters, and _price__i_ (the price of the _i_-th purchase) is a non-empty string, consisting of digits and dots (decimal points). It is possible that purchases with equal names have different prices. The price of each purchase is written in the following format. If the price is an integer number of dollars then cents are not written. Otherwise, after the number of dollars a dot (decimal point) is written followed by cents in a two-digit format (if number of cents is between 1 and 9 inclusively, there is a leading zero). Also, every three digits (from less significant to the most) in dollars are separated by dot (decimal point). No extra leading zeroes are allowed. The price always starts with a digit and ends with a digit. For example: "234", "1.544", "149.431.10", "0.99" and "123.05" are valid prices, ".333", "3.33.11", "12.00", ".33", "0.1234" and "1.2" are not valid. Write a program that will find the total price of all purchases in the given bill. Input The only line of the input contains a non-empty string _s_ with length not greater than 1000xa0— the content of the bill. It is guaranteed that the bill meets the format described above. It is guaranteed that each price in the bill is not less than one cent and not greater than 106 dollars. Output Print the total price exactly in the same format as prices given in the input. Examples Input chipsy48.32televizor12.390
1,600
false
false
true
false
false
false
false
false
false
false
6,899