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
645D
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot _i_ will beat robot _j_ if and only if robot _i_ has a higher skill level than robot _j_. And if robot _i_ beats robot _j_ and robot _j_ beats robot _k_, then robot _i_ will beat robot _k_. Since rapping is such a subtle art, two robots can never have the same skill level. Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level. Input The first line of the input consists of two integers, the number of robots _n_ (2u2009≤u2009_n_u2009≤u2009100u2009000) and the number of rap battles _m_ (). The next _m_ lines describe the results of the rap battles in the order they took place. Each consists of two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_), indicating that robot _u__i_ beat robot _v__i_ in the _i_-th rap battle. No two rap battles involve the same pair of robots. It is guaranteed that at least one ordering of the robots satisfies all _m_ relations. Output Print the minimum _k_ such that the ordering of the robots by skill level is uniquely defined by the first _k_ rap battles. If there exists more than one ordering that satisfies all _m_ relations, output -1. Note In the first sample, the robots from strongest to weakest must be (4,u20092,u20091,u20093), which Bessie can deduce after knowing the results of the first four rap battles. In the second sample, both (1,u20093,u20092) and (3,u20091,u20092) are possible orderings of the robots from strongest to weakest after both rap battles.
1,800
false
false
false
true
false
false
false
true
false
true
7,231
1290D
This is the hard version of the problem. You can find the easy version in the Div. 2 contest. Both versions only differ in the number of times you can ask your friend to taste coffee. This is an interactive problem. You're considering moving to another city, where one of your friends already lives. There are $$$n$$$ cafés in this city, where $$$n$$$ is a power of two. The $$$i$$$-th café produces a single variety of coffee $$$a_i$$$. As you're a coffee-lover, before deciding to move or not, you want to know the number $$$d$$$ of distinct varieties of coffees produced in this city. You don't know the values $$$a_1, ldots, a_n$$$. Fortunately, your friend has a memory of size $$$k$$$, where $$$k$$$ is a power of two. Once per day, you can ask him to taste a cup of coffee produced by the café $$$c$$$, and he will tell you if he tasted a similar coffee during the last $$$k$$$ days. You can also ask him to take a medication that will reset his memory. He will forget all previous cups of coffee tasted. You can reset his memory at most $$$30 000$$$ times. More formally, the memory of your friend is a queue $$$S$$$. Doing a query on café $$$c$$$ will: Tell you if $$$a_c$$$ is in $$$S$$$; Add $$$a_c$$$ at the back of $$$S$$$; If $$$S > k$$$, pop the front element of $$$S$$$. Doing a reset request will pop all elements out of $$$S$$$. Your friend can taste at most $$$dfrac{3n^2}{2k}$$$ cups of coffee in total. Find the diversity $$$d$$$ (number of distinct values in the array $$$a$$$). Note that asking your friend to reset his memory does not count towards the number of times you ask your friend to taste a cup of coffee. In some test cases the behavior of the interactor is adaptive. It means that the array $$$a$$$ may be not fixed before the start of the interaction and may depend on your queries. It is guaranteed that at any moment of the interaction, there is at least one array $$$a$$$ consistent with all the answers given so far. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 1024$$$, $$$k$$$ and $$$n$$$ are powers of two). It is guaranteed that $$$dfrac{3n^2}{2k} le 15 000$$$. Interaction You begin the interaction by reading $$$n$$$ and $$$k$$$. To ask your friend to taste a cup of coffee produced by the café $$$c$$$, in a separate line output? $$$c$$$ Where $$$c$$$ must satisfy $$$1 le c le n$$$. Don't forget to flush, to get the answer. In response, you will receive a single letter Y (yes) or N (no), telling you if variety $$$a_c$$$ is one of the last $$$k$$$ varieties of coffee in his memory. To reset the memory of your friend, in a separate line output the single letter R in upper case. You can do this operation at most $$$30 000$$$ times. When you determine the number $$$d$$$ of different coffee varieties, output! $$$d$$$ In case your query is invalid, you asked more than $$$frac{3n^2}{2k}$$$ queries of type ? or you asked more than $$$30 000$$$ queries of type R, the program will print the letter E and will finish interaction. You will receive a Wrong Answer verdict. Make sure to exit immediately to avoid getting other verdicts. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hack format The first line should contain the word fixed The second line should contain two integers $$$n$$$ and $$$k$$$, separated by space ($$$1 le k le n le 1024$$$, $$$k$$$ and $$$n$$$ are powers of two). It must hold that $$$dfrac{3n^2}{2k} le 15 000$$$. The third line should contain $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$, separated by spaces ($$$1 le a_i le n$$$). Examples Output ? 1 ? 2 ? 3 ? 4 R ? 4 ? 1 ? 2 ! 3 Output ? 2 ? 6 ? 4 ? 5 ? 2 ? 5 ! 6 Note In the first example, the array is $$$a = [1, 4, 1, 3]$$$. The city produces $$$3$$$ different varieties of coffee ($$$1$$$, $$$3$$$ and $$$4$$$). The successive varieties of coffee tasted by your friend are $$$1, 4, extbf{1}, 3, 3, 1, 4$$$ (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a reset memory request R, so the answer to the second ? 4 ask is N. Had there been no reset memory request, the answer to the second ? 4 ask is Y. In the second example, the array is $$$a = [1, 2, 3, 4, 5, 6, 6, 6]$$$. The city produces $$$6$$$ different varieties of coffee. The successive varieties of coffee tasted by your friend are $$$2, 6, 4, 5, extbf{2}, extbf{5}$$$.
3,000
false
false
false
false
false
true
false
false
false
true
4,243
350E
Valera conducts experiments with algorithms that search for shortest paths. He has recently studied the Floyd's algorithm, so it's time to work with it. Valera's already written the code that counts the shortest distance between any pair of vertexes in a non-directed connected graph from _n_ vertexes and _m_ edges, containing no loops and multiple edges. Besides, Valera's decided to mark part of the vertexes. He's marked exactly _k_ vertexes _a_1,u2009_a_2,u2009...,u2009_a__k_. Valera's code is given below. ans[i][j] // the shortest distance for a pair of vertexes _i_,u2009_j_ a[i] // vertexes, marked by Valerafor(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { if (i == j) ans[i][j] = 0; else ans[i][j] = INF; //INF is a very large number } } for(i = 1; i <= m; i++) { read a pair of vertexes u, v that have a non-directed edge between them; ans[u][v] = 1; ans[v][u] = 1; } for (i = 1; i <= k; i++) { v = a[i]; for(j = 1; j <= n; j++) for(r = 1; r <= n; r++) ans[j][r] = min(ans[j][r], ans[j][v] + ans[v][r]); } Valera has seen that his code is wrong. Help the boy. Given the set of marked vertexes _a_1,u2009_a_2,u2009...,u2009_a__k_, find such non-directed connected graph, consisting of _n_ vertexes and _m_ edges, for which Valera's code counts the wrong shortest distance for at least one pair of vertexes (_i_,u2009_j_). Valera is really keen to get a graph without any loops and multiple edges. If no such graph exists, print -1. Input The first line of the input contains three integers _n_,u2009_m_,u2009_k_ (3u2009≤u2009_n_u2009≤u2009300, 2u2009≤u2009_k_u2009≤u2009_n_ , ) — the number of vertexes, the number of edges and the number of marked vertexes. The second line of the input contains _k_ space-separated integers _a_1,u2009_a_2,u2009... _a__k_ (1u2009≤u2009_a__i_u2009≤u2009_n_) — the numbers of the marked vertexes. It is guaranteed that all numbers _a__i_ are distinct. Output If the graph doesn't exist, print -1 on a single line. Otherwise, print _m_ lines, each containing two integers _u_,u2009_v_ — the description of the edges of the graph Valera's been looking for.
2,200
false
false
false
false
false
true
true
false
false
true
8,431
2037D
Mualani loves surfing on her sharky surfboard! Mualani's surf path can be modeled by a number line. She starts at position $$$1$$$, and the path ends at position $$$L$$$. When she is at position $$$x$$$ with a jump power of $$$k$$$, she can jump to any integer position in the interval $$$[x, x+k]$$$. Initially, her jump power is $$$1$$$. However, her surf path isn't completely smooth. There are $$$n$$$ hurdles on her path. Each hurdle is represented by an interval $$$[l, r]$$$, meaning she cannot jump to any position in the interval $$$[l, r]$$$. There are also $$$m$$$ power-ups at certain positions on the path. Power-up $$$i$$$ is located at position $$$x_i$$$ and has a value of $$$v_i$$$. When Mualani is at position $$$x_i$$$, she has the option to collect the power-up to increase her jump power by $$$v_i$$$. There may be multiple power-ups at the same position. When she is at a position with some power-ups, she may choose to take or ignore each individual power-up. No power-up is in the interval of any hurdle. What is the minimum number of power-ups she must collect to reach position $$$L$$$ to finish the path? If it is not possible to finish the surf path, output $$$-1$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$L$$$ ($$$1 leq n, m leq 2 cdot 10^5, 3 leq L leq 10^9$$$) — the number of hurdles, the number of power-ups, and the position of the end. The following $$$n$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$2 leq l_i leq r_i leq L-1$$$) — the bounds of the interval for the $$$i$$$'th hurdle. It is guaranteed that $$$r_i + 1 < l_{i+1}$$$ for all $$$1 leq i < n$$$ (i.e. all hurdles are non-overlapping, sorted by increasing positions, and the end point of a previous hurdle is not consecutive with the start point of the next hurdle). The following $$$m$$$ lines contain two integers $$$x_i$$$ and $$$v_i$$$ ($$$1 leq x_i, v_i leq L$$$) — the position and the value for the $$$i$$$'th power-up. There may be multiple power-ups with the same $$$x$$$. It is guaranteed that $$$x_i leq x_{i+1}$$$ for all $$$1 leq i < m$$$ (i.e. the power-ups are sorted by non-decreasing position) and no power-up is in the interval of any hurdle. It is guaranteed the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the minimum number of power-ups she must collect to reach position $$$L$$$. If it is not possible, output $$$-1$$$. Example Input 4 2 5 50 7 14 30 40 2 2 3 1 3 5 18 2 22 32 4 3 50 4 6 15 18 20 26 34 38 1 2 8 2 10 2 1 4 17 10 14 1 6 1 2 1 2 16 9 1 2 10 5 9 2 3 2 2 Note In the first test case, she can collect power-ups $$$1$$$, $$$2$$$, $$$3$$$, and $$$5$$$ to clear all hurdles. In the second test case, she cannot jump over the first hurdle. In the fourth test case, by collecting both power-ups, she can jump over the hurdle.
1,300
false
true
false
false
true
false
false
false
false
false
42
1182F
Problem - 1182F - 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 number theory *2700 No tag edit access → Contest materials = ext{abs}( ext{sin}(frac{p}{q} pi x))$$$. Find minimum possible integer $$$x$$$ that maximizes $$$f(x)$$$ where $$$a le x le b$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains four integers $$$a$$$, $$$b$$$, $$$p$$$, and $$$q$$$ ($$$0 le a le b le 10^{9}$$$, $$$1 le p$$$, $$$q le 10^{9}$$$). Output Print the minimum possible integer $$$x$$$ for each test cases, separated by newline. Example Input 2 0 3 1 3 17 86 389 995 Output 1 55 Note In the first test case, $$$f(0) = 0$$$, $$$f(1) = f(2) approx 0.866$$$, $$$f(3) = 0$$$. In the second test case, $$$f(55) approx 0.999969$$$, which is the largest among all possible values.
2,700
false
false
false
false
true
false
false
true
false
false
4,803
518E
After bracket sequences Arthur took up number theory. He has got a new favorite sequence of length _n_ (_a_1,u2009_a_2,u2009...,u2009_a__n_), consisting of integers and integer _k_, not exceeding _n_. This sequence had the following property: if you write out the sums of all its segments consisting of _k_ consecutive elements (_a_1xa0u2009+u2009xa0_a_2xa0...xa0u2009+u2009xa0_a__k_,u2009xa0_a_2xa0u2009+u2009xa0_a_3xa0u2009+u2009xa0...xa0u2009+u2009xa0_a__k_u2009+u20091,u2009xa0...,u2009xa0_a__n_u2009-u2009_k_u2009+u20091xa0u2009+u2009xa0_a__n_u2009-u2009_k_u2009+u20092xa0u2009+u2009xa0...xa0u2009+u2009xa0_a__n_), then those numbers will form strictly increasing sequence. For example, for the following sample: _n_u2009=u20095,u2009xa0_k_u2009=u20093,u2009xa0_a_u2009=u2009(1,u2009xa02,u2009xa04,u2009xa05,u2009xa06) the sequence of numbers will look as follows: (1xa0u2009+u2009xa02xa0u2009+u2009xa04,u2009xa02xa0u2009+u2009xa04xa0u2009+u2009xa05,u2009xa04xa0u2009+u2009xa05xa0u2009+u2009xa06)xa0=xa0(7,u2009xa011,u2009xa015), that means that sequence _a_ meets the described property. Obviously the sequence of sums will have _n_u2009-u2009_k_u2009+u20091 elements. Somebody (we won't say who) replaced some numbers in Arthur's sequence by question marks (if this number is replaced, it is replaced by exactly one question mark). We need to restore the sequence so that it meets the required property and also minimize the sum _a__i_, where _a__i_ is the absolute value of _a__i_. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105), showing how many numbers are in Arthur's sequence and the lengths of segments respectively. The next line contains _n_ space-separated elements _a__i_ (1u2009≤u2009_i_u2009≤u2009_n_). If _a__i_xa0u2009=u2009xa0?, then the _i_-th element of Arthur's sequence was replaced by a question mark. Otherwise, _a__i_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109) is the _i_-th element of Arthur's sequence. Output If Arthur is wrong at some point and there is no sequence that could fit the given information, print a single string "Incorrect sequence" (without the quotes). Otherwise, print _n_ integers — Arthur's favorite sequence. If there are multiple such sequences, print the sequence with the minimum sum _a__i_, where _a__i_ is the absolute value of _a__i_. If there are still several such sequences, you are allowed to print any of them. Print the elements of the sequence without leading zeroes.
2,200
true
true
true
false
false
false
false
false
false
false
7,764
1214E
It's Petya's birthday party and his friends have presented him a brand new "Electrician-$$$n$$$" construction set, which they are sure he will enjoy as he always does with weird puzzles they give him. Construction set "Electrician-$$$n$$$" consists of $$$2n - 1$$$ wires and $$$2n$$$ light bulbs. Each bulb has its own unique index that is an integer from $$$1$$$ to $$$2n$$$, while all wires look the same and are indistinguishable. In order to complete this construction set one has to use each of the wires to connect two distinct bulbs. We define a chain in a completed construction set as a sequence of distinct bulbs of length at least two, such that every two consecutive bulbs in this sequence are directly connected by a wire. Completed construction set configuration is said to be correct if a resulting network of bulbs and wires has a tree structure, i.e. any two distinct bulbs are the endpoints of some chain. Petya was assembling different configurations for several days, and he noticed that sometimes some of the bulbs turn on. After a series of experiments he came up with a conclusion that bulbs indexed $$$2i$$$ and $$$2i - 1$$$ turn on if the chain connecting them consists of exactly $$$d_i$$$ wires. Moreover, the following important condition holds: the value of $$$d_i$$$ is never greater than $$$n$$$. Petya did his best but was not able to find a configuration that makes all bulbs to turn on, so he seeks your assistance. Please, find out a configuration that makes all bulbs shine. It is guaranteed that such configuration always exists. Input The first line of the input contains a single integer $$$n$$$ ($$$1 leq n leq 100,000$$$)xa0— the parameter of a construction set that defines the number of bulbs and the number of wires. Next line contains $$$n$$$ integers $$$d_1, d_2, ldots, d_n$$$ ($$$1 leq d_i leq n$$$), where $$$d_i$$$ stands for the number of wires the chain between bulbs $$$2i$$$ and $$$2i - 1$$$ should consist of. Output Print $$$2n - 1$$$ lines. The $$$i$$$-th of them should contain two distinct integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq 2n$$$, $$$a_i e b_i$$$)xa0— indices of bulbs connected by a wire. If there are several possible valid answer you can print any of them. Examples Output 1 6 2 6 3 5 3 6 4 5 Output 1 6 1 7 2 6 3 5 3 6 4 5 7 8 Output 1 3 2 3 3 5 4 5 5 7 6 7 7 12 8 12 9 11 9 12 10 11 Note Answer for the first sample test. Answer for the second sample test.
2,000
true
false
false
false
false
true
false
false
true
true
4,607
518B
Little Tanya decided to present her dad a postcard on his Birthday. She has already created a message — string _s_ of length _n_, consisting of uppercase and lowercase English letters. Tanya can't write yet, so she found a newspaper and decided to cut out the letters and glue them into the postcard to achieve string _s_. The newspaper contains string _t_, consisting of uppercase and lowercase English letters. We know that the length of string _t_ greater or equal to the length of the string _s_. The newspaper may possibly have too few of some letters needed to make the text and too many of some other letters. That's why Tanya wants to cut some _n_ letters out of the newspaper and make a message of length exactly _n_, so that it looked as much as possible like _s_. If the letter in some position has correct value and correct letter case (in the string _s_ and in the string that Tanya will make), then she shouts joyfully "YAY!", and if the letter in the given position has only the correct value but it is in the wrong case, then the girl says "WHOOPS". Tanya wants to make such message that lets her shout "YAY!" as much as possible. If there are multiple ways to do this, then her second priority is to maximize the number of times she says "WHOOPS". Your task is to help Tanya make the message. Input The first line contains line _s_ (1u2009≤u2009_s_u2009≤u20092·105), consisting of uppercase and lowercase English letters — the text of Tanya's message. The second line contains line _t_ (_s_u2009≤u2009_t_u2009≤u20092·105), consisting of uppercase and lowercase English letters — the text written in the newspaper. Here _a_ means the length of the string _a_. Output Print two integers separated by a space: the first number is the number of times Tanya shouts "YAY!" while making the message, the second number is the number of times Tanya says "WHOOPS" while making the message.
1,400
false
true
true
false
false
false
false
false
false
false
7,767
1497E2
This is the hard version of the problem. The only difference is that in this version $$$0 leq k leq 20$$$. There is an array $$$a_1, a_2, ldots, a_n$$$ of $$$n$$$ positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most $$$k$$$ such operations before the division: choose a number in the array and change its value to any positive integer. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer $$$t$$$ $$$(1 le t le 1000)$$$ xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 leq k leq 20$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^7$$$). It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case print a single integer xa0— the answer to the problem. Example Input 3 5 2 18 6 2 4 1 11 4 6 2 2 8 9 1 3 6 3 9 7 1 0 1 Note In the first test case it is possible to change the array this way: $$$[underline{3}, 6, 2, 4, underline{5}]$$$ (changed elements are underlined). After that the array does not need to be divided, so the answer is $$$1$$$. In the second test case it is possible to change the array this way: $$$[6, 2, underline{3}, 8, 9, underline{5}, 3, 6, underline{10}, underline{11}, 7]$$$. After that such division is optimal: $$$[6, 2, 3]$$$ $$$[8, 9, 5, 3, 6, 10, 11, 7]$$$
2,500
true
true
false
true
true
false
false
false
false
false
3,182
990C
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()", "(())" are regular (the resulting expressions are: "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not. You are given $$$n$$$ bracket sequences $$$s_1, s_2, dots , s_n$$$. Calculate the number of pairs $$$i, j , (1 le i, j le n)$$$ such that the bracket sequence $$$s_i + s_j$$$ is a regular bracket sequence. Operation $$$+$$$ means concatenation i.e. "()(" + ")()" = "()()()". If $$$s_i + s_j$$$ and $$$s_j + s_i$$$ are regular bracket sequences and $$$i e j$$$, then both pairs $$$(i, j)$$$ and $$$(j, i)$$$ must be counted in the answer. Also, if $$$s_i + s_i$$$ is a regular bracket sequence, the pair $$$(i, i)$$$ must be counted in the answer. Input The first line contains one integer $$$n , (1 le n le 3 cdot 10^5)$$$ — the number of bracket sequences. The following $$$n$$$ lines contain bracket sequences — non-empty strings consisting only of characters "(" and ")". The sum of lengths of all bracket sequences does not exceed $$$3 cdot 10^5$$$. Output In the single line print a single integer — the number of pairs $$$i, j , (1 le i, j le n)$$$ such that the bracket sequence $$$s_i + s_j$$$ is a regular bracket sequence. Note In the first example, suitable pairs are $$$(3, 1)$$$ and $$$(2, 2)$$$. In the second example, any pair is suitable, namely $$$(1, 1), (1, 2), (2, 1), (2, 2)$$$.
1,500
false
false
true
false
false
false
false
false
false
false
5,750
570C
Daniel has a string _s_, consisting of lowercase English letters and period signs (characters '.'). Let's define the operation of replacement as the following sequence of steps: find a substring ".." (two consecutive periods) in string _s_, of all occurrences of the substring let's choose the first one, and replace this substring with string ".". In other words, during the replacement operation, the first two consecutive periods are replaced by one. If string _s_ contains no two consecutive periods, then nothing happens. Let's define _f_(_s_) as the minimum number of operations of replacement to perform, so that the string does not have any two consecutive periods left. You need to process _m_ queries, the _i_-th results in that the character at position _x__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_) of string _s_ is assigned value _c__i_. After each operation you have to calculate and output the value of _f_(_s_). Help Daniel to process all queries. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009300u2009000) the length of the string and the number of queries. The second line contains string _s_, consisting of _n_ lowercase English letters and period signs. The following _m_ lines contain the descriptions of queries. The _i_-th line contains integer _x__i_ and _c__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_, _c__i_ — a lowercas English letter or a period sign), describing the query of assigning symbol _c__i_ to position _x__i_. Output Print _m_ numbers, one per line, the _i_-th of these numbers must be equal to the value of _f_(_s_) after performing the _i_-th assignment. Examples Input 10 3 .b..bz.... 1 h 3 c 9 f Note Note to the first sample test (replaced periods are enclosed in square brackets). The original string is ".b..bz....". after the first query _f_(hb..bz....) = 4xa0xa0xa0xa0("hb[..]bz...." u2009→u2009 "hb.bz[..].." u2009→u2009 "hb.bz[..]." u2009→u2009 "hb.bz[..]" u2009→u2009 "hb.bz.") after the second query _f_(hbс.bz....) = 3xa0xa0xa0xa0("hbс.bz[..].." u2009→u2009 "hbс.bz[..]." u2009→u2009 "hbс.bz[..]" u2009→u2009 "hbс.bz.") after the third query _f_(hbс.bz..f.) = 1xa0xa0xa0xa0("hbс.bz[..]f." u2009→u2009 "hbс.bz.f.") Note to the second sample test. The original string is ".cc.". after the first query: _f_(..c.) = 1xa0xa0xa0xa0("[..]c." u2009→u2009 ".c.") after the second query: _f_(....) = 3xa0xa0xa0xa0("[..].." u2009→u2009 "[..]." u2009→u2009 "[..]" u2009→u2009 ".") after the third query: _f_(.a..) = 1xa0xa0xa0xa0(".a[..]" u2009→u2009 ".a.") after the fourth query: _f_(aa..) = 1xa0xa0xa0xa0("aa[..]" u2009→u2009 "aa.")
1,600
false
false
true
false
true
true
false
false
false
false
7,569
2022D2
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 978 (Div. 2) 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 dp interactive *2700 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST D2. Asesino (Hard Version) time limit per test2.5 seconds memory limit per test256 megabytes This is the hard version of the problem. In this version, you must use the minimum number of queries possible. You can make hacks only if both versions of the problem are solved. This is an interactive problem. It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia". Today, $$$n$$$ players, numbered from $$$1$$$ to $$$n$$$, will play "Asesino" with the following three roles: Knight: a Knight is someone who always tells the truth. Knave: a Knave is someone who always lies. Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave. Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves. As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor. To determine the Impostor, you will ask some questions. In each question, you will pick two players $$$i$$$ and $$$j$$$ ($$$1 leq i, j leq n$$$; $$$i eq j$$$) and ask if player $$$i$$$ thinks that player $$$j$$$ is a Knight. The results of the question is shown in the table below. Knight Knave Impostor Knight Yes No Yes Knave No Yes No Impostor No Yes — The response of the cell in row $$$a$$$ and column $$$b$$$ is the result of asking a question when $$$i$$$ has role $$$a$$$ and $$$j$$$ has row $$$b$$$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $$$i$$$ is a Knight and $$$j$$$ is an Impostor. Find the Impostor in the minimum number of queries possible. That is, let $$$f(n)$$$ be the minimum integer such that for $$$n$$$ players, there exists a strategy that can determine the Impostor using at most $$$f(n)$$$ questions. Then, you should use at most $$$f(n)$$$ questions to determine the Impostor. Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem. Input The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 10^5$$$)xa0— the number of people playing the game. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Interaction To ask a question, output a line in the following format: "? i j" ($$$1 leq i,j leq n$$$; $$$i eq j$$$)xa0— to ask player $$$i$$$ if they think player $$$j$$$ is a Knight. The jury will output a "1" if player $$$i$$$ thinks player $$$j$$$ is a Knight, and "0" otherwise. When you have determined which player the Impostor is, output a line in the following format: "! i" ($$$1 leq i leq n$$$)xa0— the Impostor is player $$$i$$$. Note that answering does not count to your limit of $$$f(n)$$$ questions. If you have made an invalid output, used more than $$$f(n)$$$ questions or wrongly determined the Impostor, the jury will respond with "-1" and you will receive a Wrong Answer verdict. Upon receiving "-1", your program must terminate immediately. Otherwise, you may receive an arbitrary verdict because your solution might be reading from a closed stream. After printing a query do not forget to output the end of the 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; sys.stdout.flush() in Python; std::io::stdout().flush() in Rust; see the documentation for other languages. Hack format For hacks, use the following format. The first line should contain a single integer $$$t$$$xa0— the number of test cases. The first line of each test case should contain the integer $$$n$$$ followed by the string "manual". The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-1 le a_i le 1$$$)xa0— the roles of each player. $$$1$$$ denotes a Knight, $$$0$$$ denotes a Knave, and $$$-1$$$ dentoes an Impostor. There must be exactly one Impostor, that is there must be exactly one index $$$i$$$ such that $$$a_i=-1$$$. As an example, the hack format for the example input is: 2 7 manual 0 1 0 -1 0 1 0 4 manual 0 1 -1 0 Example input 2 7 1 0 0 1 1 0 0 4 0 1 1 1 output ? 1 3 ? 7 6 ? 2 5 ? 6 2 ? 4 5 ? 4 6 ? 1 4 ! 4 ? 1 2 ? 2 3 ? 3 4 ? 4 1 ! 3 Note Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples. In the first test case of the example, players at indices $$$2$$$ and $$$6$$$ are Knights, players at indices $$$1$$$, $$$3$$$, $$$5$$$, and $$$7$$$ are Knaves, and the Impostor is at index $$$4$$$. The following is an explanation of the questions asked: In the first query, player $$$i$$$ is a Knave and player $$$j$$$ is a Knave. The answer is "yes" since Knaves always lie. In the second query, player $$$i$$$ is a Knave and player $$$j$$$ is a Knight. The answer is "no" since Knaves always lie. In the third query, player $$$i$$$ is a Knight and player $$$j$$$ is a Knave. The answer is "no" since Knights always tell the truth. In the fourth query, player $$$i$$$ is a Knight and player $$$j$$$ is a Knight. The answer is "yes" since Knights always tell the truth. In the fifth query, player $$$i$$$ is a Impostor and player $$$j$$$ is a Knave. The answer is "yes" since the Impostor always lies. In the sixth query, player $$$i$$$ is a Impostor and player $$$j$$$ is a Knight. The answer is "no" since the Impostor always lies. In the seventh query, player $$$i$$$ is a Knave and player $$$j$$$ is a Impostor. The answer is "no" since Knaves always lie and Knaves thinks that the Impostor is a Knight. Codeforces (c)
2,700
false
false
false
true
false
true
false
false
false
false
134
1006F
There is a rectangular grid of size $$$n imes m$$$. Each cell has a number written on it; the number on the cell ($$$i, j$$$) is $$$a_{i, j}$$$. Your task is to calculate the number of paths from the upper-left cell ($$$1, 1$$$) to the bottom-right cell ($$$n, m$$$) meeting the following constraints: You can move to the right or to the bottom only. Formally, from the cell ($$$i, j$$$) you may move to the cell ($$$i, j + 1$$$) or to the cell ($$$i + 1, j$$$). The target cell can't be outside of the grid. The xor of all the numbers on the path from the cell ($$$1, 1$$$) to the cell ($$$n, m$$$) must be equal to $$$k$$$ (xor operation is the bitwise exclusive OR, it is represented as '^' in Java or C++ and "xor" in Pascal). Find the number of such paths in the given grid. Input The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le n, m le 20$$$, $$$0 le k le 10^{18}$$$) — the height and the width of the grid, and the number $$$k$$$. The next $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line is $$$a_{i, j}$$$ ($$$0 le a_{i, j} le 10^{18}$$$). Output Print one integer — the number of paths from ($$$1, 1$$$) to ($$$n, m$$$) with xor sum equal to $$$k$$$. Examples Input 3 3 11 2 1 5 7 10 0 12 6 4 Input 3 4 2 1 3 3 3 0 3 3 2 3 0 1 1 Input 3 4 1000000000000000000 1 3 3 3 0 3 3 2 3 0 1 1 Note All the paths from the first example: $$$(1, 1) ightarrow (2, 1) ightarrow (3, 1) ightarrow (3, 2) ightarrow (3, 3)$$$; $$$(1, 1) ightarrow (2, 1) ightarrow (2, 2) ightarrow (2, 3) ightarrow (3, 3)$$$; $$$(1, 1) ightarrow (1, 2) ightarrow (2, 2) ightarrow (3, 2) ightarrow (3, 3)$$$. All the paths from the second example: $$$(1, 1) ightarrow (2, 1) ightarrow (3, 1) ightarrow (3, 2) ightarrow (3, 3) ightarrow (3, 4)$$$; $$$(1, 1) ightarrow (2, 1) ightarrow (2, 2) ightarrow (3, 2) ightarrow (3, 3) ightarrow (3, 4)$$$; $$$(1, 1) ightarrow (2, 1) ightarrow (2, 2) ightarrow (2, 3) ightarrow (2, 4) ightarrow (3, 4)$$$; $$$(1, 1) ightarrow (1, 2) ightarrow (2, 2) ightarrow (2, 3) ightarrow (3, 3) ightarrow (3, 4)$$$; $$$(1, 1) ightarrow (1, 2) ightarrow (1, 3) ightarrow (2, 3) ightarrow (3, 3) ightarrow (3, 4)$$$.
2,100
false
false
false
true
false
false
true
false
false
false
5,650
18B
Problem - 18B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math *1700 No tag edit access → Contest materials ") Tutorial") is a segment with coordinates
1,700
true
false
false
false
false
false
true
false
false
false
9,904
1559D1
This is the easy version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from $$$1$$$ to $$$n$$$, and they would like to add edges to their forests such that: After adding edges, both of their graphs are still forests. They add the same edges. That is, if an edge $$$(u, v)$$$ is added to Mocha's forest, then an edge $$$(u, v)$$$ is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers $$$n$$$, $$$m_1$$$ and $$$m_2$$$ ($$$1 le n le 1000$$$, $$$0 le m_1, m_2 < n$$$) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next $$$m_1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) — the edges in Mocha's forest. Each of the next $$$m_2$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) — the edges in Diana's forest. Output The first line contains only one integer $$$h$$$, the maximum number of edges Mocha and Diana can add (in each forest). Each of the next $$$h$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) — the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. We can add an edge $$$(2, 4)$$$.
1,400
false
true
false
false
false
true
true
false
false
true
2,833
514E
When Darth Vader gets bored, he sits down on the sofa, closes his eyes and thinks of an infinite rooted tree where each node has exactly _n_ sons, at that for each node, the distance between it an its _i_-th left child equals to _d__i_. The Sith Lord loves counting the number of nodes in the tree that are at a distance at most _x_ from the root. The distance is the sum of the lengths of edges on the path between nodes. But he has got used to this activity and even grew bored of it. 'Why does he do that, then?' — you may ask. It's just that he feels superior knowing that only he can solve this problem. Do you want to challenge Darth Vader himself? Count the required number of nodes. As the answer can be rather large, find it modulo 109u2009+u20097. Input The first line contains two space-separated integers _n_ and _x_ (1u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_x_u2009≤u2009109) — the number of children of each node and the distance from the root within the range of which you need to count the nodes. The next line contains _n_ space-separated integers _d__i_ (1u2009≤u2009_d__i_u2009≤u2009100) — the length of the edge that connects each node with its _i_-th child. Output Print a single number — the number of vertexes in the tree at distance from the root equal to at most _x_. Note Pictures to the sample (the yellow color marks the nodes the distance to which is at most three)
2,200
false
false
false
true
false
false
false
false
false
false
7,776
1451C
Ashish has two strings $$$a$$$ and $$$b$$$, each of length $$$n$$$, and an integer $$$k$$$. The strings only contain lowercase English letters. He wants to convert string $$$a$$$ into string $$$b$$$ by performing some (possibly zero) operations on $$$a$$$. In one move, he can either choose an index $$$i$$$ ($$$1 leq ileq n-1$$$) and swap $$$a_i$$$ and $$$a_{i+1}$$$, or choose an index $$$i$$$ ($$$1 leq i leq n-k+1$$$) and if $$$a_i, a_{i+1}, ldots, a_{i+k-1}$$$ are all equal to some character $$$c$$$ ($$$c eq$$$ 'z'), replace each one with the next character $$$(c+1)$$$, that is, 'a' is replaced by 'b', 'b' is replaced by 'c' and so on. Note that he can perform any number of operations, and the operations can only be performed on string $$$a$$$. Help Ashish determine if it is possible to convert string $$$a$$$ into $$$b$$$ after performing some (possibly zero) operations on it. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$)xa0— the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $$$n$$$ ($$$2 leq n leq 10^6$$$) and $$$k$$$ ($$$1 leq k leq n$$$). The second line of each test case contains the string $$$a$$$ of length $$$n$$$ consisting of lowercase English letters. The third line of each test case contains the string $$$b$$$ of length $$$n$$$ consisting of lowercase English letters. It is guaranteed that the sum of values $$$n$$$ among all test cases does not exceed $$$10^6$$$. Output For each test case, print "Yes" if Ashish can convert $$$a$$$ into $$$b$$$ after some moves, else print "No". You may print the letters of the answer in any case (upper or lower). Example Input 4 3 3 abc bcd 4 2 abba azza 2 1 zz aa 6 2 aaabba ddddcc Note In the first test case it can be shown that it is impossible to convert $$$a$$$ into $$$b$$$. In the second test case, "abba" $$$xrightarrow{ ext{inc}}$$$ "acca" $$$xrightarrow{ ext{inc}}$$$ $$$ldots$$$ $$$xrightarrow{ ext{inc}}$$$ "azza". Here "swap" denotes an operation of the first type, and "inc" denotes an operation of the second type. In the fourth test case, "aaabba" $$$xrightarrow{ ext{swap}}$$$ "aaabab" $$$xrightarrow{ ext{swap}}$$$ "aaaabb" $$$xrightarrow{ ext{inc}}$$$ $$$ldots$$$ $$$xrightarrow{ ext{inc}}$$$ "ddaabb" $$$xrightarrow{ ext{inc}}$$$ $$$ldots$$$ $$$xrightarrow{ ext{inc}}$$$ "ddddbb" $$$xrightarrow{ ext{inc}}$$$ $$$ldots$$$ $$$xrightarrow{ ext{inc}}$$$ "ddddcc".
1,400
false
true
true
true
false
false
false
false
false
false
3,426
1838F
This is an interactive problem. There is an $$$n$$$ by $$$n$$$ grid of conveyor belts, in positions $$$(1, 1)$$$ through $$$(n, n)$$$ of a coordinate plane. Every other square in the plane is empty. Each conveyor belt can be configured to move boxes up ('^'), down ('v'), left ('<'), or right ('>'). If a box moves onto an empty square, it stops moving. However, one of the $$$n^2$$$ belts is stuck, and will always move boxes in the same direction, no matter how it is configured. Your goal is to perform a series of tests to determine which conveyor belt is stuck, and the direction in which it sends items. To achieve this, you can perform up to $$$25$$$ tests. In each test, you assign a direction to all $$$n^2$$$ belts, place a box on top of one of them, and then turn all of the conveyors on. One possible result of a query with $$$n=4$$$. In this case, the box starts at $$$(2, 2)$$$. If there were no stuck conveyor, it would end up at $$$(5, 4)$$$, but because of the stuck '>' conveyor at $$$(3, 3)$$$, it enters an infinite loop. The conveyors move the box around too quickly for you to see, so the only information you receive from a test is whether the box eventually stopped moving, and if so, the coordinates of its final position. Interaction You begin the interaction by reading a single integer $$$n$$$ ($$$2 le nle 100$$$)xa0— the number of rows and columns in the grid. Then, you can make at most $$$25$$$ queries. Each query should begin with a line of the form ? r c, where r and c are the initial row and column of the box, respectively. The next $$$n$$$ lines of the query should contain $$$n$$$ characters each. The $$$j$$$th character of the $$$i$$$th row should be one of '^', 'v', '<', or '>', indicating the direction of conveyor $$$(i, j)$$$ for this query. After each query, you will receive two integers $$$x$$$ and $$$y$$$. If $$$x = y = -1$$$, then the box entered an infinite loop. Otherwise, its final position was $$$(x, y)$$$. If you make too many queries or make an invalid query, you will receive the Wrong Answer verdict. After you have found the stuck conveyor and its direction, print a single line ! r c dir, where r and c are the row and column of the stuck conveyor, respectively, and dir is one of '^', 'v', '<', or '>', indicating the direction of the stuck conveyor. Note that printing this answer does not count towards your total of $$$25$$$ queries. After printing this line, your program should terminate. The interactor is non-adaptive. This means that the location and direction of the stuck belt is fixed at the start of the interaction, and does not change after the queries. After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To make a hack, use the following format. The first line should contain a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the number of rows and columns in the grid. The next line should contain two integers $$$r$$$ and $$$c$$$ ($$$1 le r, c le n$$$), as well as a character $$$mathrm{dir}$$$ ($$$mathrm{dir}$$$ is one of '^', 'v', '<', '>')xa0— the position of the stuck conveyor and its fixed direction. These values should be separated by spaces. Note For the first query of the first sample input, the box starts on $$$(2, 2)$$$ and enters an infinite loop containing rows $$$2$$$ and $$$3$$$. Because the stuck conveyor is in row $$$1$$$, it does not affect the outcome of the query. For the second query of the first sample input, the conveyors are configured in the same way, but the box starts on $$$(1, 1)$$$. If there were no stuck conveyor, it would enter an infinite loop between $$$(1, 2)$$$ and $$$(1, 3)$$$. However, the stuck conveyor redirects it to $$$(0, 2)$$$. After these two queries, the program is able to determine that the stuck conveyor is at $$$(1, 2)$$$ and directs items upward. The query for the second sample input corresponds to the picture above. After asking the query, there are many possibilities for the stuck conveyor, but the program correctly guesses that it is at $$$(3, 3)$$$ and directs items to the right.
3,000
false
false
false
false
false
true
false
true
false
false
1,259
351A
Jeff got 2_n_ real numbers _a_1,u2009_a_2,u2009...,u2009_a_2_n_ as a birthday present. The boy hates non-integer numbers, so he decided to slightly "adjust" the numbers he's got. Namely, Jeff consecutively executes _n_ operations, each of them goes as follows: choose indexes _i_ and _j_ (_i_u2009≠u2009_j_) that haven't been chosen yet; round element _a__i_ to the nearest integer that isn't more than _a__i_ (assign to _a__i_: ⌊ _a__i_xa0⌋); round element _a__j_ to the nearest integer that isn't less than _a__j_ (assign to _a__j_: ⌈ _a__j_xa0⌉). Nevertheless, Jeff doesn't want to hurt the feelings of the person who gave him the sequence. That's why the boy wants to perform the operations so as to make the absolute value of the difference between the sum of elements before performing the operations and the sum of elements after performing the operations as small as possible. Help Jeff find the minimum absolute value of the difference. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092000). The next line contains 2_n_ real numbers _a_1, _a_2, ..., _a_2_n_ (0u2009≤u2009_a__i_u2009≤u200910000), given with exactly three digits after the decimal point. The numbers are separated by spaces. Output In a single line print a single real number — the required difference with exactly three digits after the decimal point. Examples Input 3 0.000 0.500 0.750 1.000 2.000 3.000 Input 3 4469.000 6526.000 4864.000 9356.383 7490.000 995.896 Note In the first test case you need to perform the operations as follows: (_i_u2009=u20091,u2009_j_u2009=u20094), (_i_u2009=u20092,u2009_j_u2009=u20093), (_i_u2009=u20095,u2009_j_u2009=u20096). In this case, the difference will equal (0u2009+u20090.5u2009+u20090.75u2009+u20091u2009+u20092u2009+u20093)u2009-u2009(0u2009+u20090u2009+u20091u2009+u20091u2009+u20092u2009+u20093)u2009=u20090.25.
1,800
true
true
true
true
false
false
false
false
false
false
8,430
961E
One day Polycarp decided to rewatch his absolute favourite episode of well-known TV series "Tufurama". He was pretty surprised when he got results only for season 7 episode 3 with his search query of "Watch Tufurama season 3 episode 7 online full hd free". This got Polycarp confused — what if he decides to rewatch the entire series someday and won't be able to find the right episodes to watch? Polycarp now wants to count the number of times he will be forced to search for an episode using some different method. TV series have _n_ seasons (numbered 1 through _n_), the _i_-th season has _a__i_ episodes (numbered 1 through _a__i_). Polycarp thinks that if for some pair of integers _x_ and _y_ (_x_u2009<u2009_y_) exist both season _x_ episode _y_ and season _y_ episode _x_ then one of these search queries will include the wrong results. Help Polycarp to calculate the number of such pairs! Input The first line contains one integer _n_ (1u2009u2009≤u2009_n_u2009u2009≤u2009u20092·105) — the number of seasons. The second line contains _n_ integers separated by space _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — number of episodes in each season. Output Print one integer — the number of pairs _x_ and _y_ (_x_u2009<u2009_y_) such that there exist both season _x_ episode _y_ and season _y_ episode _x_. Note Possible pairs in the second example: 1. _x_u2009=u20091, _y_u2009=u20092 (season 1 episode 2 season 2 episode 1); 2. _x_u2009=u20092, _y_u2009=u20093 (season 2 episode 3 season 3 episode 2); 3. _x_u2009=u20091, _y_u2009=u20093 (season 1 episode 3 season 3 episode 1). In the third example: 1. _x_u2009=u20091, _y_u2009=u20092 (season 1 episode 2 season 2 episode 1); 2. _x_u2009=u20091, _y_u2009=u20093 (season 1 episode 3 season 3 episode 1).
1,900
false
false
false
false
true
false
false
false
false
false
5,859
1935C
In the new messenger for the students of the Master's Assistance Center, Keftemerum, an update is planned, in which developers want to optimize the set of messages shown to the user. There are a total of $$$n$$$ messages. Each message is characterized by two integers $$$a_i$$$ and $$$b_i$$$. The time spent reading the set of messages with numbers $$$p_1, p_2, ldots, p_k$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct) is calculated by the formula: $$$$$$Large sum_{i=1}^{k} a_{p_i} + sum_{i=1}^{k - 1} b_{p_i} - b_{p_{i+1}}$$$$$$ Note that the time to read a set of messages consisting of one message with number $$$p_1$$$ is equal to $$$a_{p_1}$$$. Also, the time to read an empty set of messages is considered to be $$$0$$$. The user can determine the time $$$l$$$ that he is willing to spend in the messenger. The messenger must inform the user of the maximum possible size of the set of messages, the reading time of which does not exceed $$$l$$$. Note that the maximum size of the set of messages can be equal to $$$0$$$. The developers of the popular messenger failed to implement this function, so they asked you to solve this problem. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5 cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$l$$$ ($$$1 leq n leq 2000$$$, $$$1 leq l leq 10^9$$$) — the number of messages and the time the user is willing to spend in the messenger. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 10^9$$$) — characteristics of the $$$i$$$-th message. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$4 cdot 10^6$$$. Output For each test case, output a single integer — the maximum possible size of a set of messages, the reading time of which does not exceed $$$l$$$. Example Input 5 5 8 4 3 1 5 2 4 4 3 2 3 1 6 4 10 3 12 4 8 2 1 2 12 5 26 24 7 8 28 30 22 3 8 17 17 5 14 15 3 1000000000 998244353 179 239 228 1337 993 1007 Note In the first test case, you can take a set of three messages with numbers $$$p_1 = 3$$$, $$$p_2 = 2$$$, and $$$p_3 = 5$$$. The time spent reading this set is equal to $$$a_3 + a_2 + a_5 + b_3 - b_2 + b_2 - b_5 = 2 + 1 + 2 + 4 - 5 + 5 - 3 = 8$$$. In the second test case, you can take a set of one message with number $$$p_1 = 1$$$. The time spent reading this set is equal to $$$a_1 = 4$$$. In the fifth test case, it can be shown that there is no such non-empty set of messages, the reading time of which does not exceed $$$l$$$.
1,800
false
true
false
true
true
true
true
true
true
false
672
22C
Bob got a job as a system administrator in X corporation. His first task was to connect _n_ servers with the help of _m_ two-way direct connection so that it becomes possible to transmit data from one server to any other server via these connections. Each direct connection has to link two different servers, each pair of servers should have at most one direct connection. Y corporation, a business rival of X corporation, made Bob an offer that he couldn't refuse: Bob was asked to connect the servers in such a way, that when server with index _v_ fails, the transmission of data between some other two servers becomes impossible, i.e. the system stops being connected. Help Bob connect the servers. Input The first input line contains 3 space-separated integer numbers _n_, _m_, _v_ (3u2009≤u2009_n_u2009≤u2009105,u20090u2009≤u2009_m_u2009≤u2009105,u20091u2009≤u2009_v_u2009≤u2009_n_), _n_ — amount of servers, _m_ — amount of direct connections, _v_ — index of the server that fails and leads to the failure of the whole system. Output If it is impossible to connect the servers in the required way, output -1. Otherwise output _m_ lines with 2 numbers each — description of all the direct connections in the system. Each direct connection is described by two numbers — indexes of two servers, linked by this direct connection. The servers are numbered from 1. If the answer is not unique, output any.
1,700
false
false
false
false
false
false
false
false
false
true
9,886
1787F
A permutation scientist is studying a self-transforming permutation $$$a$$$ consisting of $$$n$$$ elements $$$a_1,a_2,ldots,a_n$$$. A permutation is a sequence of integers from $$$1$$$ to $$$n$$$ of length $$$n$$$ containing each number exactly once. For example, $$$[1]$$$, $$$[4, 3, 5, 1, 2]$$$ are permutations, while $$$[1, 1]$$$, $$$[4, 3, 1]$$$ are not. The permutation transforms day by day. On each day, each element $$$x$$$ becomes $$$a_x$$$, that is, $$$a_x$$$ becomes $$$a_{a_x}$$$. Specifically: on the first day, the permutation becomes $$$b$$$, where $$$b_x = a_{a_x}$$$; on the second day, the permutation becomes $$$c$$$, where $$$c_x = b_{b_x}$$$; $$$ldots$$$ For example, consider permutation $$$a = [2,3,1]$$$. On the first day, it becomes $$$[3,1,2]$$$. On the second day, it becomes $$$[2,3,1]$$$. You're given the permutation $$$a'$$$ on the $$$k$$$-th day. Define $$$sigma(x) = a_x$$$, and define $$$f(x)$$$ as the minimal positive integer $$$m$$$ such that $$$sigma^m(x) = x$$$, where $$$sigma^m(x)$$$ denotes $$$underbrace{sigma(sigma(ldots sigma}_{m ext{ times}}(x) ldots))$$$. For example, if $$$a = [2,3,1]$$$, then $$$sigma(1) = 2$$$, $$$sigma^2(1) = sigma(sigma(1)) = sigma(2) = 3$$$, $$$sigma^3(1) = sigma(sigma(sigma(1))) = sigma(3) = 1$$$, so $$$f(1) = 3$$$. And if $$$a=[4,2,1,3]$$$, $$$sigma(2) = 2$$$ so $$$f(2) = 1$$$; $$$sigma(3) = 1$$$, $$$sigma^2(3) = 4$$$, $$$sigma^3(3) = 3$$$ so $$$f(3) = 3$$$. Find the initial permutation $$$a$$$ such that $$$sumlimits^n_{i=1} dfrac{1}{f(i)}$$$ is minimum possible. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le k le 10^9$$$) — the length of $$$a$$$, and the last day. The second line contains $$$n$$$ integers $$$a'_1,a'_2,ldots,a'_n$$$ ($$$1 le a'_i le n$$$) — the permutation on the $$$k$$$-th day. It's guaranteed that the sum of $$$n$$$ does not exceed $$$2 cdot 10^5$$$. Output For each test case, if at least one initial $$$a$$$ consistent with the given $$$a'$$$ exists, print "YES", then print $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ — the initial permutation with the smallest sum $$$sumlimits^n_{i=1} dfrac{1}{f(i)}$$$. If there are multiple answers with the smallest sum, print any. If there are no valid permutations, print "NO". Example Input 10 5 3 1 2 3 4 5 7 2 1 2 3 4 5 6 7 8 998 1 2 3 4 5 6 7 8 6 1 6 3 5 4 1 2 4 8 4 2 1 3 9 1 1 5 4 8 7 6 3 2 9 5 9999999 2 3 4 5 1 7 97843220 4 6 1 2 7 5 3 3 1000000000 2 1 3 12 3 8 9 10 1 5 3 11 4 7 6 12 2 Output YES 2 3 4 1 5 YES 6 2 5 7 1 3 4 YES 2 3 4 5 6 7 8 1 YES 3 1 6 4 2 5 YES 4 2 1 3 NO YES 3 4 5 1 2 YES 2 5 4 6 3 7 1 NO YES 3 7 8 6 5 1 12 10 11 4 2 9 Note In the second test case, the initial permutation can be $$$a = [6,2,5,7,1,3,4]$$$, which becomes $$$[3,2,1,4,6,5,7]$$$ on the first day and $$$a' = [1,2,3,4,5,6,7]$$$ on the second day (the $$$k$$$-th day). Also, among all the permutations satisfying that, it has the minimum $$$sumlimits^n_{i=1} dfrac{1}{f(i)}$$$, which is $$$dfrac{1}{4}+dfrac{1}{1}+dfrac{1}{4}+dfrac{1}{2}+dfrac{1}{4}+dfrac{1}{4}+dfrac{1}{2}=3$$$. In the fifth test case, the initial permutation can be $$$a = [4,2,1,3]$$$, which becomes $$$[3,2,4,1]$$$ on the first day, $$$[4,2,1,3]$$$ on the second day, and so on. So it finally becomes $$$a' = [4,2,1,3]$$$ on the $$$8$$$-th day (the $$$k$$$-th day). And it has the minimum $$$sumlimits^n_{i=1} dfrac{1}{f(i)} = dfrac{1}{3} + dfrac{1}{1} + dfrac{1}{3} + dfrac{1}{3} = 2$$$.
2,500
true
false
true
false
false
true
false
false
false
false
1,558
1175D
Problem - 1175D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy sortings *1900 No tag edit access → Contest materials $$$ be the index of subarray the $$$i$$$-th element belongs to. Subarrays are numbered from left to right and from $$$1$$$ to $$$k$$$. Let the cost of division be equal to $$$sumlimits_{i=1}^{n} (a_i cdot f(i))$$$. For example, if $$$a =
1,900
false
true
false
false
false
false
false
false
true
false
4,842
1487D
A Pythagorean triple is a triple of integer numbers $$$(a, b, c)$$$ such that it is possible to form a right triangle with the lengths of the first cathetus, the second cathetus and the hypotenuse equal to $$$a$$$, $$$b$$$ and $$$c$$$, respectively. An example of the Pythagorean triple is $$$(3, 4, 5)$$$. Vasya studies the properties of right triangles, and he uses a formula that determines if some triple of integers is Pythagorean. Unfortunately, he has forgotten the exact formula; he remembers only that the formula was some equation with squares. So, he came up with the following formula: $$$c = a^2 - b$$$. Obviously, this is not the right formula to check if a triple of numbers is Pythagorean. But, to Vasya's surprise, it actually worked on the triple $$$(3, 4, 5)$$$: $$$5 = 3^2 - 4$$$, so, according to Vasya's formula, it is a Pythagorean triple. When Vasya found the right formula (and understood that his formula is wrong), he wondered: how many are there triples of integers $$$(a, b, c)$$$ with $$$1 le a le b le c le n$$$ such that they are Pythagorean both according to his formula and the real definition? He asked you to count these triples. Output For each test case, print one integer — the number of triples of integers $$$(a, b, c)$$$ with $$$1 le a le b le c le n$$$ such that they are Pythagorean according both to the real definition and to the formula Vasya came up with. Note The only Pythagorean triple satisfying $$$c = a^2 - b$$$ with $$$1 le a le b le c le 9$$$ is $$$(3, 4, 5)$$$; that's why the answer for $$$n = 3$$$ is $$$0$$$, and the answer for $$$n = 6$$$ (and for $$$n = 9$$$) is $$$1$$$.
1,500
true
false
false
false
false
false
true
true
false
false
3,243
1879D
Problem - 1879D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks combinatorics divide and conquer dp math *1700 No tag edit access → Contest materials cdot (r - l + 1)$$$, where $$$f(l, r)$$$ is $$$a_l oplus a_{l+1} oplus dots oplus a_{r-1} oplus a_r$$$ (the character $$$oplus$$$ denotes bitwise XOR). Since the answer can be very large, print it modulo $$$998244353$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9)$$$. Output Print the one integerxa0— the value of $$$sum_{l=1}^{n} sum_{r=l}^{n} f(l, r) cdot (r - l + 1)$$$, taken modulo $$$998244353$$$. Examples Input 3 1 3 2 Output 12 Input 4 39 68 31 80 Output 1337 Input 7 313539461 779847196 221612534 488613315 633203958 394620685 761188160 Output 257421502 Note In the first example, the answer is equal to $$$f(1, 1) + 2 cdot f(1, 2) + 3 cdot f(1, 3) + f(2, 2) + 2 cdot f(2, 3) + f(3, 3) = $$$ $$$= 1 + 2 cdot 2 + 3 cdot 0 + 3 + 2 cdot 1 + 2 = 12$$$.
1,700
true
false
false
true
false
false
false
false
false
false
1,007
1614D1
This is the easy version of the problem. The only difference is maximum value of $$$a_i$$$. Once in Kostomuksha Divan found an array $$$a$$$ consisting of positive integers. Now he wants to reorder the elements of $$$a$$$ to maximize the value of the following function: $$$$$$sum_{i=1}^n operatorname{gcd}(a_1, , a_2, , dots, , a_i),$$$$$$ where $$$operatorname{gcd}(x_1, x_2, ldots, x_k)$$$ denotes the = x$$$ for any integer $$$x$$$. Reordering elements of an array means changing the order of elements in the array arbitrary, or leaving the initial order. Of course, Divan can solve this problem. However, he found it interesting, so he decided to share it with you. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the size of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_{1}, , a_{2}, , dots, , a_{n}$$$ ($$$1 le a_{i} le 5 cdot 10^6$$$) — the array $$$a$$$. Note In the first example, it's optimal to rearrange the elements of the given array in the following order: $$$[6, , 2, , 2, , 2, , 3, , 1]$$$: $$$$$$operatorname{gcd}(a_1) + operatorname{gcd}(a_1, , a_2) + operatorname{gcd}(a_1, , a_2, , a_3) + operatorname{gcd}(a_1, , a_2, , a_3, , a_4) + operatorname{gcd}(a_1, , a_2, , a_3, , a_4, , a_5) + operatorname{gcd}(a_1, , a_2, , a_3, , a_4, , a_5, , a_6) = 6 + 2 + 2 + 2 + 1 + 1 = 14.$$$$$$ It can be shown that it is impossible to get a better answer. In the second example, it's optimal to rearrange the elements of a given array in the following order: $$$[100, , 10, , 10, , 5, , 1, , 3, , 3, , 7, , 42, , 54]$$$.
2,100
false
false
false
true
false
false
false
false
false
false
2,567
1038E
You are given $$$n$$$ blocks, each of them is of the form [color$$$_1$$$valuecolor$$$_2$$$], where the block can also be flipped to get [color$$$_2$$$valuecolor$$$_1$$$]. A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C. The value of the sequence is defined as the sum of the values of the blocks in this sequence. Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence. Input The first line of input contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the number of given blocks. Each of the following $$$n$$$ lines describes corresponding block and consists of $$$mathrm{color}_{1,i}$$$, $$$mathrm{value}_i$$$ and $$$mathrm{color}_{2,i}$$$ ($$$1 le mathrm{color}_{1,i}, mathrm{color}_{2,i} le 4$$$, $$$1 le mathrm{value}_i le 100,000$$$). Output Print exactly one integerxa0— the maximum total value of the subset of blocks, which makes a valid sequence. Examples Input 6 2 1 4 1 2 4 3 4 4 2 8 3 3 16 3 1 32 2 Input 7 1 100000 1 1 100000 2 1 100000 2 4 50000 3 3 50000 4 4 50000 4 3 50000 3 Input 4 1 1000 1 2 500 2 3 250 3 4 125 4 Note In the first example, it is possible to form a valid sequence from all blocks. One of the valid sequences is the following: [421] [1322] [283] [3163] [344] [412] The first block from the input ([214] $$$ o$$$ [412]) and second ([124] $$$ o$$$ [421]) are flipped. In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped): [21000001] [11000001] [11000002] In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
2,400
false
false
false
true
false
false
true
false
false
true
5,504
1431J
Problem - 1431J - Codeforces =============== xa0 ]( "Kotlin Heroes 5: ICPC Round Announcement") ; array $$$b$$$ is non-decreasing; $$$b_1 oplus b_2 oplus dots oplus b_{2n-1} = 0$$$ ($$$oplus$$$ denotes bitwise XOR operation:
3,400
false
false
false
true
false
false
false
false
false
false
3,500
623D
A MIPT student named Misha has a birthday today, and he decided to celebrate it in his country house in suburban Moscow. _n_ friends came by, and after a typical party they decided to play blind man's buff. The birthday boy gets blindfolded and the other players scatter around the house. The game is played in several rounds. In each round, Misha catches exactly one of his friends and has to guess who it is. The probability of catching the _i_-th friend does not change between rounds and is equal to _p__i_ percent (as we know, it is directly proportional to the amount of alcohol consumed by the _i_-th friend) and _p_1u2009+u2009_p_2u2009+u2009...u2009+u2009_p__n_u2009=u2009100 holds. Misha has no information about who he caught. After Misha makes an attempt to guess the caught person, the round ends. Even then, Misha isn't told whether he guessed correctly, and a new round begins. The game ends when Misha guesses every friend at least once, that is, there exists such set of rounds _k_1,u2009_k_2,u2009...,u2009_k__n_, that during round number _k__i_ Misha caught the _i_-th friend and guessed him. Misha wants to minimize the expectation of the number of rounds of the game. Despite the fact that at any point in the game Misha has no information about who he has already guessed, his friends are honest, and if they see that the condition for the end of the game is fulfilled, the game ends immediately. Find the expectation of the number of rounds in the game if Misha plays optimally. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of Misha's friends. The second line contains _n_ integers _p__i_ (), giving the probability to catch the _i_-th friend in one particular round in percent. Output Print a single real valuexa0— the expectation of the number of rounds provided that Misha plays optimally. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if .
2,700
true
true
false
false
false
false
false
false
false
false
7,337
249E
A Russian space traveller Alisa Selezneva, like any other schoolgirl of the late 21 century, is interested in science. She has recently visited the MIT (Moscow Institute of Time), where its chairman and the co-inventor of the time machine academician Petrov told her about the construction of a time machine. During the demonstration of the time machine performance Alisa noticed that the machine does not have high speed and the girl got interested in the reason for such disadvantage. As it turns out on closer examination, one of the problems that should be solved for the time machine isn't solved by an optimal algorithm. If you find a way to solve this problem optimally, the time machine will run faster and use less energy. A task that none of the staff can solve optimally is as follows. There exists a matrix _a_, which is filled by the following rule: The cells are consecutive positive integers, starting with one. Besides, _a__i_,u2009_j_u2009<u2009_a__t_,u2009_k_ (_i_,u2009_j_,u2009_t_,u2009_k_u2009≥u20091), if: 1. _max_(_i_,u2009_j_)u2009<u2009_max_(_t_,u2009_k_); 2. _max_(_i_,u2009_j_)u2009=u2009_max_(_t_,u2009_k_) and _j_u2009<u2009_k_; 3. _max_(_i_,u2009_j_)u2009=u2009_max_(_t_,u2009_k_), _j_u2009=u2009_k_ and _i_u2009>u2009_t_. So, after the first 36 numbers are inserted, matrix _a_ will look as follows: To solve the problem, you should learn to find rather quickly for the given values of _x_1,u2009_y_1,u2009_x_2 and _y_2 (_x_1u2009≤u2009_x_2,u2009_y_1u2009≤u2009_y_2) the meaning of expression: As the meaning of this expression can be large enough, it is sufficient to know only the last 10 digits of the sought value. So, no one in MTI can solve the given task. Alice was brave enough to use the time machine and travel the past to help you. Your task is to write a program that uses the given values _x_1,u2009_y_1,u2009_x_2 and _y_2 finds the last 10 digits of the given expression. Input The first input line contains a single integer _t_ (1u2009≤u2009_t_u2009≤u2009105) — the number of test sets for which you should solve the problem. Each of the next _t_ lines contains the description of a test — four positive integers _x_1,u2009_y_1,u2009_x_2 and _y_2 (1u2009≤u2009_x_1u2009≤u2009_x_2u2009≤u2009109,u20091u2009≤u2009_y_1u2009≤u2009_y_2u2009≤u2009109), separated by spaces. Output For each query print the meaning of the expression if it contains at most 10 characters. Otherwise, print three characters "." (without the quotes), and then ten last digits of the time expression. Print the answer to each query on a single line. Follow the format, given in the sample as closely as possible.
2,600
true
false
false
false
false
false
false
false
false
false
8,837
1098E
Fedya loves problems involving data structures. Especially ones about different queries on subsegments. Fedya had a nice array $$$a_1, a_2, ldots a_n$$$ and a beautiful data structure. This data structure, given $$$l$$$ and $$$r$$$, $$$1 le l le r le n$$$, could find the greatest integer $$$d$$$, such that $$$d$$$ divides each of $$$a_l$$$, $$$a_{l+1}$$$, ..., $$$a_{r}$$$. Fedya really likes this data structure, so he applied it to every non-empty contiguous subarray of array $$$a$$$, put all answers into the array and sorted it. He called this array $$$b$$$. It's easy to see that array $$$b$$$ contains $$$n(n+1)/2$$$ elements. After that, Fedya implemented another cool data structure, that allowed him to find sum $$$b_l + b_{l+1} + ldots + b_r$$$ for given $$$l$$$ and $$$r$$$, $$$1 le l le r le n(n+1)/2$$$. Surely, Fedya applied this data structure to every contiguous subarray of array $$$b$$$, called the result $$$c$$$ and sorted it. Help Fedya find the lower median of array $$$c$$$. Recall that for a sorted array of length $$$k$$$ the lower median is an element at position $$$lfloor frac{k + 1}{2} floor$$$, if elements of the array are enumerated starting from $$$1$$$. For example, the lower median of array $$$(1, 1, 2, 3, 6)$$$ is $$$2$$$, and the lower median of $$$(0, 17, 23, 96)$$$ is $$$17$$$. Input First line contains a single integer $$$n$$$xa0— number of elements in array $$$a$$$ ($$$1 le n le 50,000$$$). Second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0— elements of the array ($$$1 le a_i le 100,000$$$). Note In the first sample array $$$b$$$ is equal to $$${3, 3, 6}$$$, then array $$$c$$$ is equal to $$${3, 3, 6, 6, 9, 12}$$$, so the lower median is $$$6$$$. In the second sample $$$b$$$ is $$${8, 8, 8}$$$, $$$c$$$ is $$${8, 8, 8, 16, 16, 24}$$$, so the lower median is $$$8$$$.
3,400
true
false
true
false
false
false
false
true
false
false
5,212
1903A
Theofanis is busy after his and changing the array $$$a_1, a_2, ldots, a_n$$$ to $$$a_1, a_2, ldots, a_{i-1}, ; a_j, a_{j-1}, ldots, a_i, ; a_{j+1}, ldots, a_{n-1}, a_n$$$. The length of the subarray is then $$$j - i + 1$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 100$$$)xa0— the number of boxes and the length of the maximum reverse that Theofanis can make. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{9}$$$)xa0— the number written on each box. Output For each test case, print YES (case-insensitive), if the array can be sorted in non-decreasing order, or NO (case-insensitive) otherwise. Example Input 5 3 2 1 2 3 3 1 9 9 9 4 4 6 4 2 1 4 3 10 3 830 14 2 1 3 1 Note In the first two test cases, the boxes are already sorted in non-decreasing order. In the third test case, we can reverse the whole array. In the fourth test case, we can reverse the first two boxes and the last two boxes. In the fifth test case, it can be shown that it's impossible to sort the boxes.
800
false
true
false
false
false
false
true
false
true
false
897
1776M
The "Parmigiana di melanzane" is a typical Italian dish. Alessandro and Bianca have very different tastes when it comes to it: Alessandro loves to eat Parmigiana with seafood, but Bianca thinks it is an atrocity! To decide which ingredients to include in the dish they prepare, they play the following game. There are $$$n$$$ possible ingredients, labeled from $$$1$$$ to $$$n$$$. The higher the label, the closer the ingredient is to being seafood. The ingredients are connected by $$$n - 1$$$ edges, in such a way as to form a tree. Alessandro and Bianca take turns, with Alessandro going first. They alternately choose a terminal ingredient $$$x$$$, that is an ingredient currently connected to at most one other ingredient, and remove it from the tree. If the terminal ingredient $$$x$$$ was chosen by Alessandro, it goes in the recipe; if it was chosen by Bianca, it is discarded. The taste of the Parmigiana is measured as the maximum label of an ingredient in the recipe. Alessandro wants to maximize the taste, while Bianca wants to minimize the taste. If both play optimally, what is the taste of the Parmigiana? Input The first line contains an integer $$$n$$$ ($$$2le n le 100,000$$$) — the number of ingredients. Each of the following $$$n-1$$$ lines contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$) — the ingredients that the $$$i$$$-th edge connects. It is guaranteed that the edges form a tree (i.e., any pair of ingredients is connected by the edges, possibly indirectly). Output Print the value of the taste if both Alessandro and Bianca play optimally. Note In the first sample, Alessandro can choose terminal ingredient $$$4$$$ in the first turn. This ingredient is added to the recipe. Since $$$4$$$ is the maximum label of an ingredient, the taste is $$$4$$$ regardless of the choices that follow. In the second sample, Bianca can make sure that neither ingredient $$$4$$$ nor $$$5$$$ are included in the recipe, in which case Alessandro can include $$$3$$$. Thus, the taste is $$$3$$$.
3,000
false
true
false
true
false
false
false
true
false
false
1,616
601C
Kleofáš is participating in an _n_-thlon - a tournament consisting of _n_ different competitions in _n_ different disciplines (numbered 1 through _n_). There are _m_ participants in the _n_-thlon and each of them participates in all competitions. In each of these _n_ competitions, the participants are given ranks from 1 to _m_ in such a way that no two participants are given the same rank - in other words, the ranks in each competition form a permutation of numbers from 1 to _m_. The score of a participant in a competition is equal to his/her rank in it. The overall score of each participant is computed as the sum of that participant's scores in all competitions. The overall rank of each participant is equal to 1u2009+u2009_k_, where _k_ is the number of participants with strictly smaller overall score. The _n_-thlon is over now, but the results haven't been published yet. Kleofáš still remembers his ranks in each particular competition; however, he doesn't remember anything about how well the other participants did. Therefore, Kleofáš would like to know his expected overall rank. All competitors are equally good at each discipline, so all rankings (permutations of ranks of everyone except Kleofáš) in each competition are equiprobable. Input The first line of the input contains two space-separated integers _n_ (1u2009≤u2009_n_u2009≤u2009100) and _m_ (1u2009≤u2009_m_u2009≤u20091000)xa0— the number of competitions and the number of participants respectively. Then, _n_ lines follow. The _i_-th of them contains one integer _x__i_ (1u2009≤u2009_x__i_u2009≤u2009_m_)xa0— the rank of Kleofáš in the _i_-th competition. Output Output a single real number – the expected overall rank of Kleofáš. Your answer will be considered correct if its relative or absolute error doesn't exceed 10u2009-u20099. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Note In the first sample, Kleofáš has overall score 6. Nobody else can have overall score less than 6 (but it's possible for one other person to have overall score 6 as well), so his overall rank must be 1.
2,300
true
false
false
true
false
false
false
false
false
false
7,438
1510C
There was no problem about a cactus at the NERC 2020 online round. That's a bad mistake, so judges decided to fix it. You shall not pass to the World Finals 2021 without solving a problem about a cactus! A cactus is a connected undirected graph in which every edge lies on at most one simple cycle. Intuitively, a cactus is a generalization of a tree where some cycles are allowed. Multiedges (multiple edges between a pair of vertices) and loops (edges that connect a vertex to itself) are not allowed in a cactus. Cher has got a cactus. She calls cactus strong if it is impossible to add an edge to it in such a way that it still remains a cactus. But Cher thinks her cactus is not strong enough. She wants to add the smallest possible number of edges to it to make it strong, i.xa0e.xa0to create a new cactus with the same vertices, so that the original cactus is a subgraph of the new one, and it is impossible to add another edge to it so that the graph remains a cactus. Cher hired you to do this job for her. So... it's on you! Input The input consists of one or more independent test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5$$$, $$$0 le m le 10^5$$$), where $$$n$$$ is the number of vertices in the graph. Vertices are numbered from $$$1$$$ to $$$n$$$. Edges of the graph are represented by a set of edge-distinct paths, where $$$m$$$ is the number of such paths. Each of the following $$$m$$$ lines contains a path in the graph. A path starts with an integer number $$$s_i$$$ ($$$2 le s_i le 1000$$$) followed by $$$s_i$$$ integers from $$$1$$$ to $$$n$$$. These $$$s_i$$$ integers represent vertices of a path. Adjacent vertices in a path are distinct. The path can go through the same vertex multiple times, but every edge is traversed exactly once in the whole test case. There are no multiedges in the graph (there is at most one edge between any two vertices). The last line of the input after all test cases always contains two zeros. It does not define a test case. It just marks the end of the input and does not require any output. All graphs in the input are cacti. The total sum of all values of $$$n$$$ and the total sum of all values of $$$m$$$ throughout the input both do not exceed $$$10^5$$$. Output For each test case, first output the line with the minimal possible number of additional edges $$$A$$$. Then output $$$A$$$ lines, each describing one edge as $$$u_i$$$ $$$v_i$$$, where $$$u_i$$$ and $$$v_i$$$ are the numbers of vertices to connect. After adding these edges, the resulting graph must be a strong cactus. Example Input 6 1 7 1 2 5 6 2 3 4 3 1 4 1 2 3 1 5 2 3 1 3 5 3 1 2 4 7 2 6 1 2 3 4 5 3 3 6 5 7 0 0 Output 1 1 4 0 1 5 4 2 1 3 6 7
2,900
false
false
false
false
false
false
false
false
false
true
3,125
1223F
Let's look at the following process: initially you have an empty stack and an array $$$s$$$ of the length $$$l$$$. You are trying to push array elements to the stack in the order $$$s_1, s_2, s_3, dots s_{l}$$$. Moreover, if the stack is empty or the element at the top of this stack is not equal to the current element, then you just push the current element to the top of the stack. Otherwise, you don't push the current element to the stack and, moreover, pop the top element of the stack. If after this process the stack remains empty, the array $$$s$$$ is considered stack exterminable. There are samples of stack exterminable arrays: $$$[1, 1]$$$; $$$[2, 1, 1, 2]$$$; $$$[1, 1, 2, 2]$$$; $$$[1, 3, 3, 1, 2, 2]$$$; $$$[3, 1, 3, 3, 1, 3]$$$; $$$[3, 3, 3, 3, 3, 3]$$$; $$$[5, 1, 2, 2, 1, 4, 4, 5]$$$; Let's consider the changing of stack more details if $$$s = [5, 1, 2, 2, 1, 4, 4, 5]$$$ (the top of stack is highlighted). 1. after pushing $$$s_1 = 5$$$ the stack turn into $$$[ extbf{5}]$$$; 2. after pushing $$$s_2 = 1$$$ the stack turn into $$$[5, extbf{1}]$$$; 3. after pushing $$$s_3 = 2$$$ the stack turn into $$$[5, 1, extbf{2}]$$$; 4. after pushing $$$s_4 = 2$$$ the stack turn into $$$[5, extbf{1}]$$$; 5. after pushing $$$s_5 = 1$$$ the stack turn into $$$[ extbf{5}]$$$; 6. after pushing $$$s_6 = 4$$$ the stack turn into $$$[5, extbf{4}]$$$; 7. after pushing $$$s_7 = 4$$$ the stack turn into $$$[ extbf{5}]$$$; 8. after pushing $$$s_8 = 5$$$ the stack is empty. You are given an array $$$a_1, a_2, ldots, a_n$$$. You have to calculate the number of its subarrays which are stack exterminable. Note, that you have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$)xa0— the number of queries. The first line of each query contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the length of array $$$a$$$. The second line of each query contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the elements. It is guaranteed that the sum of all $$$n$$$ over all queries does not exceed $$$3 cdot 10^5$$$. Output For each test case print one integer in single line — the number of stack exterminable subarrays of the array $$$a$$$. Example Input 3 5 2 1 1 2 2 6 1 2 1 1 3 2 9 3 1 2 2 1 6 6 3 3 Note In the first query there are four stack exterminable subarrays: $$$a_{1 ldots 4} = [2, 1, 1, 2], a_{2 ldots 3} = [1, 1], a_{2 ldots 5} = [1, 1, 2, 2], a_{4 ldots 5} = [2, 2]$$$. In the second query, only one subarray is exterminable subarrayxa0— $$$a_{3 ldots 4}$$$. In the third query, there are eight stack exterminable subarrays: $$$a_{1 ldots 8}, a_{2 ldots 5}, a_{2 ldots 7}, a_{2 ldots 9}, a_{3 ldots 4}, a_{6 ldots 7}, a_{6 ldots 9}, a_{8 ldots 9}$$$.
2,600
false
false
false
true
true
false
false
false
false
false
4,554
353A
Valera has got _n_ domino pieces in a row. Each piece consists of two halves — the upper one and the lower one. Each of the halves contains a number from 1 to 6. Valera loves even integers very much, so he wants the sum of the numbers on the upper halves and the sum of the numbers on the lower halves to be even. To do that, Valera can rotate the dominoes by 180 degrees. After the rotation the upper and the lower halves swap places. This action takes one second. Help Valera find out the minimum time he must spend rotating dominoes to make his wish come true. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100), denoting the number of dominoes Valera has. Next _n_ lines contain two space-separated integers _x__i_,u2009_y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u20096). Number _x__i_ is initially written on the upper half of the _i_-th domino, _y__i_ is initially written on the lower half. Output Print a single number — the minimum required number of seconds. If Valera can't do the task in any time, print u2009-u20091. Note In the first test case the sum of the numbers on the upper halves equals 10 and the sum of the numbers on the lower halves equals 6. Both numbers are even, so Valera doesn't required to do anything. In the second sample Valera has only one piece of domino. It is written 3 on the one of its halves, therefore one of the sums will always be odd. In the third case Valera can rotate the first piece, and after that the sum on the upper halves will be equal to 10, and the sum on the lower halves will be equal to 8.
1,200
true
false
true
false
false
false
false
false
false
false
8,423
1933B
You are given an array $$$a_1, a_2, ldots, a_n$$$. In one move, you can perform either of the following two operations: Choose an element from the array and remove it from the array. As a result, the length of the array decreases by $$$1$$$; Choose an element from the array and increase its value by $$$1$$$. You can perform any number of moves. If the current array becomes empty, then no more moves can be made. Your task is to find the minimum number of moves required to make the sum of the elements of the array $$$a$$$ divisible by $$$3$$$. It is possible that you may need $$$0$$$ moves. Note that the sum of the elements of an empty array (an array of length $$$0$$$) is equal to $$$0$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^4$$$). 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 number of moves. Example Input 8 4 2 2 5 4 3 1 3 2 4 3 7 6 8 1 1 4 2 2 4 2 2 5 5 7 2 4 8 1 9 3 4 2 4 10 Note In the first test case, initially the array $$$a = [2, 2, 5, 4]$$$. One of the optimal ways to make moves is: remove the current $$$4$$$th element and get $$$a = [2, 2, 5]$$$; As a result, the sum of the elements of the array $$$a$$$ will be divisible by $$$3$$$ (indeed, $$$a_1 + a_2 + a_3 = 2 + 2 + 5 = 9$$$). In the second test case, initially, the sum of the array is $$$1+3+2 = 6$$$, which is divisible by $$$3$$$. Therefore, no moves are required. Hence, the answer is $$$0$$$. In the fourth test case, initially, the sum of the array is $$$1$$$, which is not divisible by $$$3$$$. By removing its only element, you will get an empty array, so its sum is $$$0$$$. Hence, the answer is $$$1$$$.
800
true
false
true
false
false
false
false
false
false
false
686
1185G2
The only difference between easy and hard versions is constraints. Polycarp loves to listen to music, so he never leaves the player, even on the way home from the university. Polycarp overcomes the distance from the university to the house in exactly $$$T$$$ minutes. In the player, Polycarp stores $$$n$$$ songs, each of which is characterized by two parameters: $$$t_i$$$ and $$$g_i$$$, where $$$t_i$$$ is the length of the song in minutes ($$$1 le t_i le 50$$$), $$$g_i$$$ is its genre ($$$1 le g_i le 3$$$). Polycarp wants to create such a playlist so that he can listen to music all the time on the way from the university to his home, and at the time of his arrival home, the playlist is over. Polycarp never interrupts songs and always listens to them from beginning to end. Thus, if he started listening to the $$$i$$$-th song, he would spend exactly $$$t_i$$$ minutes on its listening. Polycarp also does not like when two songs of the same genre play in a row (i.e. successively/adjacently) or when the songs in his playlist are repeated. Help Polycarpus count the number of different sequences of songs (their order matters), the total duration is exactly $$$T$$$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Input The first line of the input contains two integers $$$n$$$ and $$$T$$$ ($$$1 le n le 50, 1 le T le 2500$$$) — the number of songs in the player and the required total duration, respectively. Next, the $$$n$$$ lines contain descriptions of songs: the $$$i$$$-th line contains two integers $$$t_i$$$ and $$$g_i$$$ ($$$1 le t_i le 50, 1 le g_i le 3$$$) — the duration of the $$$i$$$-th song and its genre, respectively. Output Output one integer — the number of different sequences of songs, the total length of exactly $$$T$$$, such that there are no two consecutive songs of the same genre in them and all the songs in the playlist are different. Since the answer may be huge, output it modulo $$$10^9 + 7$$$ (that is, the remainder when dividing the quantity by $$$10^9 + 7$$$). Note In the first example, Polycarp can make any of the $$$6$$$ possible playlist by rearranging the available songs: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$ and $$$[3, 2, 1]$$$ (indices of the songs are given). In the second example, the first and second songs cannot go in succession (since they have the same genre). Thus, Polycarp can create a playlist in one of $$$2$$$ possible ways: $$$[1, 3, 2]$$$ and $$$[2, 3, 1]$$$ (indices of the songs are given). In the third example, Polycarp can make the following playlists: $$$[1, 2, 3]$$$, $$$[1, 3, 2]$$$, $$$[2, 1, 3]$$$, $$$[2, 3, 1]$$$, $$$[3, 1, 2]$$$, $$$[3, 2, 1]$$$, $$$[1, 4]$$$, $$$[4, 1]$$$, $$$[2, 3, 4]$$$ and $$$[4, 3, 2]$$$ (indices of the songs are given).
2,600
false
false
false
true
false
false
false
false
false
false
4,772
1320A
Tanya wants to go on a journey across the cities of Berland. There are $$$n$$$ cities situated along the main railroad line of Berland, and these cities are numbered from $$$1$$$ to $$$n$$$. Tanya plans her journey as follows. First of all, she will choose some city $$$c_1$$$ to start her journey. She will visit it, and after that go to some other city $$$c_2 > c_1$$$, then to some other city $$$c_3 > c_2$$$, and so on, until she chooses to end her journey in some city $$$c_k > c_{k - 1}$$$. So, the sequence of visited cities $$$[c_1, c_2, dots, c_k]$$$ should be strictly increasing. There are some additional constraints on the sequence of cities Tanya visits. Each city $$$i$$$ has a beauty value $$$b_i$$$ associated with it. If there is only one city in Tanya's journey, these beauty values imply no additional constraints. But if there are multiple cities in the sequence, then for any pair of adjacent cities $$$c_i$$$ and $$$c_{i + 1}$$$, the condition $$$c_{i + 1} - c_i = b_{c_{i + 1}} - b_{c_i}$$$ must hold. For example, if $$$n = 8$$$ and $$$b = [3, 4, 4, 6, 6, 7, 8, 9]$$$, there are several three possible ways to plan a journey: $$$c = [1, 2, 4]$$$; $$$c = [3, 5, 6, 8]$$$; $$$c = [7]$$$ (a journey consisting of one city is also valid). There are some additional ways to plan a journey that are not listed above. Tanya wants her journey to be as beautiful as possible. The beauty value of the whole journey is the sum of beauty values over all visited cities. Can you help her to choose the optimal plan, that is, to maximize the beauty value of the journey? Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of cities in Berland. The second line contains $$$n$$$ integers $$$b_1$$$, $$$b_2$$$, ..., $$$b_n$$$ ($$$1 le b_i le 4 cdot 10^5$$$), where $$$b_i$$$ is the beauty value of the $$$i$$$-th city. Note The optimal journey plan in the first example is $$$c = [2, 4, 5]$$$. The optimal journey plan in the second example is $$$c = [1]$$$. The optimal journey plan in the third example is $$$c = [3, 6]$$$.
1,400
true
true
false
true
true
false
false
false
true
false
4,115
1980E
You have been given a matrix $$$a$$$ of size $$$n$$$ by $$$m$$$, containing a permutation of integers from $$$1$$$ to $$$n cdot m$$$. A permutation of $$$n$$$ integers is an array containing all numbers from $$$1$$$ to $$$n$$$ exactly once. For example, the arrays $$$[1]$$$, $$$[2, 1, 3]$$$, $$$[5, 4, 3, 2, 1]$$$ are permutations, while the arrays $$$[1, 1]$$$, $$$[100]$$$, $$$[1, 2, 4, 5]$$$ are not. A matrix contains a permutation if, when all its elements are written out, the resulting array is a permutation. Matrices $$$[[1, 2], [3, 4]]$$$, $$$[[1]]$$$, $$$[[1, 5, 3], [2, 6, 4]]$$$ contain permutations, while matrices $$$[[2]]$$$, $$$[[1, 1], [2, 2]]$$$, $$$[[1, 2], [100, 200]]$$$ do not. You can perform one of the following two actions in one operation: choose columns $$$c$$$ and $$$d$$$ ($$$1 le c, d le m$$$, $$$c e d$$$) and swap these columns; choose rows $$$c$$$ and $$$d$$$ ($$$1 le c, d le n$$$, $$$c e d$$$) and swap these rows. You can perform any number of operations. You are given the original matrix $$$a$$$ and the matrix $$$b$$$. Your task is to determine whether it is possible to transform matrix $$$a$$$ into matrix $$$b$$$ using the given operations. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The descriptions of the test cases follow. The first line of each test case description contains $$$2$$$ integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le n cdot m le 2 cdot 10^5$$$) — the sizes of the matrix. The next $$$n$$$ lines contain $$$m$$$ integers $$$a_{ij}$$$ each ($$$1 le a_{ij} le n cdot m$$$). It is guaranteed that matrix $$$a$$$ is a permutation. The next $$$n$$$ lines contain $$$m$$$ integers $$$b_{ij}$$$ each ($$$1 le b_{ij} le n cdot m$$$). It is guaranteed that matrix $$$b$$$ is a permutation. It is guaranteed that the sum of the values $$$n cdot m$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if the second matrix can be obtained from the first, and "NO" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Note In the second example, the original matrix looks like this: $$$ begin{pmatrix} 1 & 2 3 & 4 end{pmatrix} $$$ By swapping rows $$$1$$$ and $$$2$$$, it becomes: $$$ begin{pmatrix} 3 & 4 1 & 2 end{pmatrix} $$$ By swapping columns $$$1$$$ and $$$2$$$, it becomes equal to matrix $$$b$$$: $$$ begin{pmatrix} 4 & 3 2 & 1 end{pmatrix} $$$
1,600
true
true
true
false
true
true
false
false
true
false
406
1203F2
The only difference between easy and hard versions is that you should complete all the projects in easy version but this is not necessary in hard version. Polycarp is a very famous freelancer. His current rating is $$$r$$$ units. Some very rich customers asked him to complete some projects for their companies. To complete the $$$i$$$-th project, Polycarp needs to have at least $$$a_i$$$ units of rating; after he completes this project, his rating will change by $$$b_i$$$ (his rating will increase or decrease by $$$b_i$$$) ($$$b_i$$$ can be positive or negative). Polycarp's rating should not fall below zero because then people won't trust such a low rated freelancer. Polycarp can choose the order in which he completes projects. Furthermore, he can even skip some projects altogether. To gain more experience (and money, of course) Polycarp wants to choose the subset of projects having maximum possible size and the order in which he will complete them, so he has enough rating before starting each project, and has non-negative rating after completing each project. Your task is to calculate the maximum possible size of such subset of projects. Input The first line of the input contains two integers $$$n$$$ and $$$r$$$ ($$$1 le n le 100, 1 le r le 30000$$$) — the number of projects and the initial rating of Polycarp, respectively. The next $$$n$$$ lines contain projects, one per line. The $$$i$$$-th project is represented as a pair of integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i le 30000$$$, $$$-300 le b_i le 300$$$) — the rating required to complete the $$$i$$$-th project and the rating change after the project completion. Output Print one integer — the size of the maximum possible subset (possibly, empty) of projects Polycarp can choose. Examples Input 5 20 45 -6 34 -15 10 34 1 27 40 -45 Input 3 2 300 -300 1 299 1 123
2,300
false
true
false
true
false
false
false
false
false
false
4,676
31A
Problem - 31A - 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 *1200 No tag edit access → Contest materials ") — amount of worm's forms. The second line contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u20091000) — lengths of worms of each form. Output Output 3 distinct integers _i_ _j_ _k_ (1u2009≤u2009_i_,u2009_j_,u2009_k_u2009≤u2009_n_) — such indexes of worm's forms that _a__i_u2009=u2009_a__j_u2009+u2009_a__k_. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that _a__j_u2009=u2009_a__k_. Examples Input 5 1 2 3 5 7 Output 3 2 1 Input 5 1 8 1 5 1 Output -1
1,200
false
false
true
false
false
false
false
false
false
false
9,843
519A
A and B are preparing themselves for programming contests. To train their logical thinking and solve problems better, A and B decided to play chess. During the game A wondered whose position is now stronger. For each chess piece we know its weight: the queen's weight is 9, the rook's weight is 5, the bishop's weight is 3, the knight's weight is 3, the pawn's weight is 1, the king's weight isn't considered in evaluating position. The player's weight equals to the sum of weights of all his pieces on the board. As A doesn't like counting, he asked you to help him determine which player has the larger position weight. Input The input contains eight lines, eight characters each — the board's description. The white pieces on the board are marked with uppercase letters, the black pieces are marked with lowercase letters. The white pieces are denoted as follows: the queen is represented is 'Q', the rook — as 'R', the bishop — as'B', the knight — as 'N', the pawn — as 'P', the king — as 'K'. The black pieces are denoted as 'q', 'r', 'b', 'n', 'p', 'k', respectively. An empty square of the board is marked as '.' (a dot). It is not guaranteed that the given chess position can be achieved in a real game. Specifically, there can be an arbitrary (possibly zero) number pieces of each type, the king may be under attack and so on. Output Print "White" (without quotes) if the weight of the position of the white pieces is more than the weight of the position of the black pieces, print "Black" if the weight of the black pieces is more than the weight of the white pieces and print "Draw" if the weights of the white and black pieces are equal. Examples Input ...QK... ........ ........ ........ ........ ........ ........ ...rk... Input rnbqkbnr pppppppp ........ ........ ........ ........ PPPPPPPP RNBQKBNR Input rppppppr ...k.... ........ ........ ........ ........ K...Q... ........ Note In the first test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals 5. In the second test sample the weights of the positions of the black and the white pieces are equal to 39. In the third test sample the weight of the position of the white pieces equals to 9, the weight of the position of the black pieces equals to 16.
900
false
false
true
false
false
false
false
false
false
false
7,762
833C
Recently, a wild Krakozyabra appeared at Jelly Castle. It is, truth to be said, always eager to have something for dinner. Its favorite meal is natural numbers (typically served with honey sauce), or, to be more precise, the zeros in their corresponding decimal representations. As for other digits, Krakozyabra dislikes them; moreover, they often cause it indigestion! So, as a necessary precaution, Krakozyabra prefers to sort the digits of a number in non-descending order before proceeding to feast. Then, the leading zeros of the resulting number are eaten and the remaining part is discarded as an inedible tail. For example, if Krakozyabra is to have the number 57040 for dinner, its inedible tail would be the number 457. Slastyona is not really fond of the idea of Krakozyabra living in her castle. Hovewer, her natural hospitality prevents her from leaving her guest without food. Slastyona has a range of natural numbers from _L_ to _R_, which she is going to feed the guest with. Help her determine how many distinct inedible tails are going to be discarded by Krakozyabra by the end of the dinner. Note In the first sample case, the inedible tails are the numbers from 1 to 9. Note that 10 and 1 have the same inedible tailxa0– the number 1. In the second sample case, each number has a unique inedible tail, except for the pair 45,u200954. The answer to this sample case is going to be (57u2009-u200940u2009+u20091)u2009-u20091u2009=u200917.
2,700
true
true
false
false
false
false
true
false
false
false
6,432
1284A
Happy new year! The year 2020 is also known as Year Gyeongja (경자년, gyeongja-nyeon) in Korea. Where did the name come from? Let's briefly look at the Gapja system, which is traditionally used in Korea to name the years. There are two sequences of $$$n$$$ strings $$$s_1, s_2, s_3, ldots, s_{n}$$$ and $$$m$$$ strings $$$t_1, t_2, t_3, ldots, t_{m}$$$. These strings contain only lowercase letters. There might be duplicates among these strings. Let's call a concatenation of strings $$$x$$$ and $$$y$$$ as the string that is obtained by writing down strings $$$x$$$ and $$$y$$$ one right after another without changing the order. For example, the concatenation of the strings "code" and "forces" is the string "codeforces". The year 1 has a name which is the concatenation of the two strings $$$s_1$$$ and $$$t_1$$$. When the year increases by one, we concatenate the next two strings in order from each of the respective sequences. If the string that is currently being used is at the end of its sequence, we go back to the first string in that sequence. For example, if $$$n = 3, m = 4, s = $$${"a", "b", "c"}, $$$t =$$$ {"d", "e", "f", "g"}, the following table denotes the resulting year names. Note that the names of the years may repeat. You are given two sequences of strings of size $$$n$$$ and $$$m$$$ and also $$$q$$$ queries. For each query, you will be given the current year. Could you find the name corresponding to the given year, according to the Gapja system? Input The first line contains two integers $$$n, m$$$ ($$$1 le n, m le 20$$$). The next line contains $$$n$$$ strings $$$s_1, s_2, ldots, s_{n}$$$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $$$1$$$ and at most $$$10$$$. The next line contains $$$m$$$ strings $$$t_1, t_2, ldots, t_{m}$$$. Each string contains only lowercase letters, and they are separated by spaces. The length of each string is at least $$$1$$$ and at most $$$10$$$. Among the given $$$n + m$$$ strings may be duplicates (that is, they are not necessarily all different). The next line contains a single integer $$$q$$$ ($$$1 le q le 2,020$$$). In the next $$$q$$$ lines, an integer $$$y$$$ ($$$1 le y le 10^9$$$) is given, denoting the year we want to know the name for.
800
false
false
true
false
false
false
false
false
false
false
4,274
1979A
Alice and Bob came up with a rather strange game. They have an array of integers $$$a_1, a_2,ldots, a_n$$$. Alice chooses a certain integer $$$k$$$ and tells it to Bob, then the following happens: Bob chooses two integers $$$i$$$ and $$$j$$$ ($$$1 le i < j le n$$$), and then finds the maximum among the integers $$$a_i, a_{i + 1},ldots, a_j$$$; If the obtained maximum is strictly greater than $$$k$$$, Alice wins, otherwise Bob wins. Help Alice find the maximum $$$k$$$ at which she is guaranteed to win. Input Each test consists of multiple test cases. The first line 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 a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^4$$$)xa0— the number of elements in the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2,ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^4$$$. Note In the first test case, all possible subsegments that Bob can choose look as follows: $$$[2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7]$$$. The maximums on the subsegments are respectively equal to $$$4, 4, 7, 4, 7, 7$$$. It can be shown that $$$3$$$ is the largest integer such that any of the maximums will be strictly greater than it. In the third test case, the only segment that Bob can choose is $$$[1, 1]$$$. So the answer is $$$0$$$.
800
false
true
true
false
false
false
true
false
false
false
416
1101F
There are $$$n$$$ cities along the road, which can be represented as a straight line. The $$$i$$$-th city is situated at the distance of $$$a_i$$$ kilometers from the origin. All cities are situated in the same direction from the origin. There are $$$m$$$ trucks travelling from one city to another. Each truck can be described by $$$4$$$ integers: starting city $$$s_i$$$, finishing city $$$f_i$$$, fuel consumption $$$c_i$$$ and number of possible refuelings $$$r_i$$$. The $$$i$$$-th truck will spend $$$c_i$$$ litres of fuel per one kilometer. When a truck arrives in some city, it can be refueled (but refueling is impossible in the middle of nowhere). The $$$i$$$-th truck can be refueled at most $$$r_i$$$ times. Each refueling makes truck's gas-tank full. All trucks start with full gas-tank. All trucks will have gas-tanks of the same size $$$V$$$ litres. You should find minimum possible $$$V$$$ such that all trucks can reach their destinations without refueling more times than allowed. Input First line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 400$$$, $$$1 le m le 250000$$$) — the number of cities and trucks. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$, $$$a_i < a_{i+1}$$$) — positions of cities in the ascending order. Next $$$m$$$ lines contains $$$4$$$ integers each. The $$$i$$$-th line contains integers $$$s_i$$$, $$$f_i$$$, $$$c_i$$$, $$$r_i$$$ ($$$1 le s_i < f_i le n$$$, $$$1 le c_i le 10^9$$$, $$$0 le r_i le n$$$) — the description of the $$$i$$$-th truck. Output Print the only integer — minimum possible size of gas-tanks $$$V$$$ such that all trucks can reach their destinations. Example Input 7 6 2 5 7 10 14 15 17 1 3 10 0 1 7 12 7 4 5 13 3 4 7 10 1 4 7 10 1 1 5 11 2 Note Let's look at queries in details: 1. the $$$1$$$-st truck must arrive at position $$$7$$$ from $$$2$$$ without refuelling, so it needs gas-tank of volume at least $$$50$$$. 2. the $$$2$$$-nd truck must arrive at position $$$17$$$ from $$$2$$$ and can be refueled at any city (if it is on the path between starting point and ending point), so it needs gas-tank of volume at least $$$48$$$. 3. the $$$3$$$-rd truck must arrive at position $$$14$$$ from $$$10$$$, there is no city between, so it needs gas-tank of volume at least $$$52$$$. 4. the $$$4$$$-th truck must arrive at position $$$17$$$ from $$$10$$$ and can be refueled only one time: it's optimal to refuel at $$$5$$$-th city (position $$$14$$$) so it needs gas-tank of volume at least $$$40$$$. 5. the $$$5$$$-th truck has the same description, so it also needs gas-tank of volume at least $$$40$$$. 6. the $$$6$$$-th truck must arrive at position $$$14$$$ from $$$2$$$ and can be refueled two times: first time in city $$$2$$$ or $$$3$$$ and second time in city $$$4$$$ so it needs gas-tank of volume at least $$$55$$$.
2,400
false
false
false
true
false
false
false
true
false
false
5,195
1028E
While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers $$$a_1, a_2, ldots, a_n$$$. Since the talk was long and not promising, Kostya created a new cyclic array $$$b_1, b_2, ldots, b_{n}$$$ so that $$$b_i = (a_i mod a_{i + 1})$$$, where we take $$$a_{n+1} = a_1$$$. Here $$$mod$$$ is the . Input The first line contains a single integer $$$n$$$ ($$$2 le n le 140582$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_{n}$$$ ($$$0 le b_i le 187126$$$). Output If it is possible to restore some array $$$a$$$ of length $$$n$$$ so that $$$b_i = a_i mod a_{(i mod n) + 1}$$$ holds for all $$$i = 1, 2, ldots, n$$$, print «YES» in the first line and the integers $$$a_1, a_2, ldots, a_n$$$ in the second line. All $$$a_i$$$ should satisfy $$$1 le a_i le 10^{18}$$$. We can show that if an answer exists, then an answer with such constraint exists as well. It it impossible to restore any valid $$$a$$$, print «NO» in one line. You can print each letter in any case (upper or lower). Note In the first example: $$$1 mod 3 = 1$$$ $$$3 mod 5 = 3$$$ $$$5 mod 2 = 1$$$ $$$2 mod 1 = 0$$$
2,400
false
false
false
false
false
true
false
false
false
false
5,565
256E
Little Maxim loves interesting problems. He decided to share one such problem with you. Initially there is an array _a_, consisting of _n_ zeroes. The elements of the array are indexed, starting from 1. Then follow queries to change array _a_. Each query is characterized by two integers _v__i_,u2009_t__i_. In the answer to the query we should make the _v__i_-th array element equal _t__i_ (_a__v__i_u2009=u2009_t__i_;xa01u2009≤u2009_v__i_u2009≤u2009_n_). Maxim thinks that some pairs of integers (_x_,u2009_y_) are good and some are not. Maxim thinks that array _a_, consisting of _n_ integers, is lucky, if for all integer _i_, (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091) the pair of integers (_a__i_,u2009_a__i_u2009+u20091) — is good. Note that the order of numbers in the pairs is important, that is, specifically, (1,u20092)u2009≠u2009(2,u20091). After each query to change array _a_ Maxim wants to know, how many ways there are to replace all zeroes in array _a_ with integers from one to three so as to make the resulting array (without zeroes) lucky. Of course, distinct zeroes can be replaced by distinct integers. Maxim told you the sequence of queries and all pairs of integers he considers lucky. Help Maxim, solve this problem for him. Input The first line contains integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200977777) — the number of elements in the array and the number of commands. The next three lines contain matrix _w_, consisting only of zeroes and ones; the _j_-th number in the _i_-th of these lines — _w__i_,u2009_j_. If _w__i_,u2009_j_u2009=u20091 (1u2009≤u2009_i_,u2009_j_u2009≤u20093), then pair (_i_,u2009_j_) is good, otherwise it is not good. Matrix does not have to be symmetric relative to the main diagonal. Next _m_ lines contain pairs of integers _v__i_,u2009_t__i_ (1u2009≤u2009_v__i_u2009≤u2009_n_,u20090u2009≤u2009_t__i_u2009≤u20093) — the queries to change the array. Output Print _m_ integers — the _i_-th number should equal to the number of ways to replace all zeroes in array _a_ (changed after the _i_-th query) by integers from one to three so as to make the resulting array (without zeroes) lucky. Separate the numbers by whitespaces. As the answers can be rather large, print the remainder from dividing them by 777777777.
2,400
false
false
false
false
true
false
false
false
false
false
8,808
1420C2
This is the hard version of the problem. The difference between the versions is that the easy version has no swap operations. You can make hacks only if all versions of the problem are solved. Pikachu is a cute and friendly pokémon living in the wild pikachu herd. But it has become known recently that infamous team R wanted to steal all these pokémon! Pokémon trainer Andrew decided to help Pikachu to build a pokémon army to resist. First, Andrew counted all the pokémonxa0— there were exactly $$$n$$$ pikachu. The strength of the $$$i$$$-th pokémon is equal to $$$a_i$$$, and all these numbers are distinct. As an army, Andrew can choose any non-empty subsequence of pokemons. In other words, Andrew chooses some array $$$b$$$ from $$$k$$$ indices such that $$$1 le b_1 < b_2 < dots < b_k le n$$$, and his army will consist of pokémons with forces $$$a_{b_1}, a_{b_2}, dots, a_{b_k}$$$. The strength of the army is equal to the alternating sum of elements of the subsequence; that is, $$$a_{b_1} - a_{b_2} + a_{b_3} - a_{b_4} + dots$$$. Andrew is experimenting with pokémon order. He performs $$$q$$$ operations. In $$$i$$$-th operation Andrew swaps $$$l_i$$$-th and $$$r_i$$$-th pokémon. Andrew wants to know the maximal stregth of the army he can achieve with the initial pokémon placement. He also needs to know the maximal strength after each operation. Help Andrew and the pokémon, or team R will realize their tricky plan! Input Each test contains multiple test cases. The first line contains one positive integer $$$t$$$ ($$$1 le t le 10^3$$$) denoting the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 3 cdot 10^5, 0 le q le 3 cdot 10^5$$$) denoting the number of pokémon and number of operations respectively. The second line contains $$$n$$$ distinct positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$) denoting the strengths of the pokémon. $$$i$$$-th of the last $$$q$$$ lines contains two positive integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) denoting the indices of pokémon that were swapped in the $$$i$$$-th operation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$, and the sum of $$$q$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print $$$q+1$$$ integers: the maximal strength of army before the swaps and after each swap. Example Input 3 3 1 1 3 2 1 2 2 2 1 2 1 2 1 2 7 5 1 2 5 4 3 6 7 1 2 6 7 3 4 1 2 2 3 Output 3 4 2 2 2 9 10 10 10 9 11 Note Let's look at the third test case: Initially we can build an army in such way: [1 2 5 4 3 6 7], its strength will be $$$5-3+7=9$$$. After first operation we can build an army in such way: [2 1 5 4 3 6 7], its strength will be $$$2-1+5-3+7=10$$$. After second operation we can build an army in such way: [2 1 5 4 3 7 6], its strength will be $$$2-1+5-3+7=10$$$. After third operation we can build an army in such way: [2 1 4 5 3 7 6], its strength will be $$$2-1+5-3+7=10$$$. After forth operation we can build an army in such way: [1 2 4 5 3 7 6], its strength will be $$$5-3+7=9$$$. After all operations we can build an army in such way: [1 4 2 5 3 7 6], its strength will be $$$4-2+5-3+7=11$$$.
2,100
false
true
true
true
true
false
false
false
false
false
3,577
2036C
While rummaging through things in a distant drawer, Anya found a beautiful string $$$s$$$ consisting only of zeros and ones. Now she wants to make it even more beautiful by performing $$$q$$$ operations on it. Each operation is described by two integers $$$i$$$ ($$$1 le i le s$$$) and $$$v$$$ ($$$v in {0, 1}$$$) and means that the $$$i$$$-th character of the string is assigned the value $$$v$$$ (that is, the assignment $$$s_i = v$$$ is performed). But Anya loves the number $$$1100$$$, so after each query, she asks you to tell her whether the substring "1100" is present in her string (i.e. there exist such $$$1 le i le s - 3$$$ that $$$s_{i}s_{i + 1}s_{i + 2}s_{i + 3} = exttt{1100}$$$). Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The first line of the test case contains the string $$$s$$$ ($$$1 leq s leq 2 cdot 10^5$$$), consisting only of the characters "0" and "1". Here $$$s$$$ denotes the length of the string $$$s$$$. The next line contains an integer $$$q$$$ ($$$1 leq q leq 2 cdot 10^5$$$) — the number of queries. The following $$$q$$$ lines contain two integers $$$i$$$ ($$$1 leq i leq s$$$) and $$$v$$$ ($$$v in {0, 1}$$$), describing the query. It is guaranteed that the sum of $$$s$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. It is also guaranteed that the sum of $$$q$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each query, output "YES", if "1100" is present in Anya's string; otherwise, output "NO". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 4 100 4 1 1 2 0 2 0 3 1 1100000 3 6 1 7 1 4 1 111010 4 1 1 5 0 4 1 5 0 0100 4 3 1 1 1 2 0 2 1 Output NO NO NO NO YES YES NO NO YES YES YES NO NO NO NO
1,100
false
false
true
false
false
false
true
false
false
false
50
896D
Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema. However, one day, the No. 68 Cinema runs out of changes (they don't have 50-yuan notes currently), but Nephren still wants to start their business. (Assume that yuan is a kind of currency in Regulu Ere.) There are three types of customers: some of them bring exactly a 50-yuan note; some of them bring a 100-yuan note and Nephren needs to give a 50-yuan note back to him/her; some of them bring VIP cards so that they don't need to pay for the ticket. Now _n_ customers are waiting outside in queue. Nephren wants to know how many possible queues are there that they are able to run smoothly (i.e. every customer can receive his/her change), and that the number of 50-yuan notes they have after selling tickets to all these customers is between _l_ and _r_, inclusive. Two queues are considered different if there exists a customer whose type is different in two queues. As the number can be large, please output the answer modulo _p_. Input One line containing four integers _n_ (1u2009≤u2009_n_u2009≤u2009105), _p_ (1u2009≤u2009_p_u2009≤u20092·109), _l_ and _r_ (0u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_). Output One line indicating the answer modulo _p_. Note We use A, B and C to indicate customers with 50-yuan notes, customers with 100-yuan notes and customers with VIP cards respectively. For the first sample, the different possible queues that there are 2 50-yuan notes left are AAAB, AABA, ABAA, AACC, ACAC, ACCA, CAAC, CACA and CCAA, and the different possible queues that there are 3 50-yuan notes left are AAAC, AACA, ACAA and CAAA. So there are 13 different queues satisfying the first sample. Similarly, there are 35 different queues satisfying the second sample.
2,900
true
false
false
false
false
false
false
false
false
false
6,162
1967A
You have some cards. An integer between $$$1$$$ and $$$n$$$ is written on each card: specifically, for each $$$i$$$ from $$$1$$$ to $$$n$$$, you have $$$a_i$$$ cards which have the number $$$i$$$ written on them. There is also a shop which contains unlimited cards of each type. You have $$$k$$$ coins, so you can buy $$$k$$$ new cards in total, and the cards you buy can contain any integer between $$$1$$$ and $$$n$$$. After buying the new cards, you rearrange all your cards in a line. The score of a rearrangement is the number of (contiguous) subarrays of length $$$n$$$ which are a permutation of $$$[1, 2, ldots, n]$$$. What's the maximum score you can get? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t (1le tle 100)$$$. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$1le n le 2 cdot 10^5$$$, $$$0le k le 10^{12}$$$)xa0— the number of distinct types of cards and the number of coins. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{12}$$$)xa0— the number of cards of type $$$i$$$ you have at the beginning. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Note In the first test case, the final (and only) array we can get is $$$[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]$$$ (including $$$11$$$ single $$$1$$$s), which contains $$$11$$$ subarrays consisting of a permutation of $$$[1]$$$. In the second test case, we can buy $$$0$$$ cards of type $$$1$$$ and $$$4$$$ cards of type $$$2$$$, and then we rearrange the cards as following: $$$[1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]$$$. There are $$$8$$$ subarrays equal to $$$[1, 2]$$$ and $$$7$$$ subarrays equal to $$$[2, 1]$$$, which make a total of $$$15$$$ subarrays which are a permutation of $$$[1, 2]$$$. It can also be proved that this is the maximum score we can get. In the third test case, one of the possible optimal rearrangements is $$$[3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3]$$$.
1,400
true
true
true
false
false
false
false
true
true
false
508
1283D
There are $$$n$$$ Christmas trees on an infinite number line. The $$$i$$$-th tree grows at the position $$$x_i$$$. All $$$x_i$$$ are guaranteed to be distinct. Each integer point can be either occupied by the Christmas tree, by the human or not occupied at all. Non-integer points cannot be occupied by anything. There are $$$m$$$ people who want to celebrate Christmas. Let $$$y_1, y_2, dots, y_m$$$ be the positions of people (note that all values $$$x_1, x_2, dots, x_n, y_1, y_2, dots, y_m$$$ should be distinct and all $$$y_j$$$ should be integer). You want to find such an arrangement of people that the value $$$sumlimits_{j=1}^{m}minlimits_{i=1}^{n}x_i - y_j$$$ is the minimum possible (in other words, the sum of distances to the nearest Christmas tree for all people should be minimized). In other words, let $$$d_j$$$ be the distance from the $$$j$$$-th human to the nearest Christmas tree ($$$d_j = minlimits_{i=1}^{n} y_j - x_i$$$). Then you need to choose such positions $$$y_1, y_2, dots, y_m$$$ that $$$sumlimits_{j=1}^{m} d_j$$$ is the minimum possible. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the number of Christmas trees and the number of people. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$-10^9 le x_i le 10^9$$$), where $$$x_i$$$ is the position of the $$$i$$$-th Christmas tree. It is guaranteed that all $$$x_i$$$ are distinct. Output In the first line print one integer $$$res$$$ — the minimum possible value of $$$sumlimits_{j=1}^{m}minlimits_{i=1}^{n}x_i - y_j$$$ (in other words, the sum of distances to the nearest Christmas tree for all people). In the second line print $$$m$$$ integers $$$y_1, y_2, dots, y_m$$$ ($$$-2 cdot 10^9 le y_j le 2 cdot 10^9$$$), where $$$y_j$$$ is the position of the $$$j$$$-th human. All $$$y_j$$$ should be distinct and all values $$$x_1, x_2, dots, x_n, y_1, y_2, dots, y_m$$$ should be distinct. If there are multiple answers, print any of them.
1,800
false
true
false
false
false
false
false
false
false
true
4,277
1363D
2,100
true
false
true
false
false
false
false
true
false
false
3,865
1346B
Berland State University (BSU) is conducting a programming boot camp. The boot camp will last for $$$n$$$ days, and the BSU lecturers are planning to give some number of lectures during these days. Some days of the boot camp are already planned as excursion days, and no lectures should be held during these days. To make sure the participants don't get too tired of learning to program, the number of lectures for each day should not exceed $$$k_1$$$, and the number of lectures for each pair of consecutive days should not exceed $$$k_2$$$. Can you calculate the maximum number of lectures that can be conducted during the boot camp? Formally, find the maximum integer $$$m$$$ such that it is possible to choose $$$n$$$ non-negative integers $$$c_1$$$, $$$c_2$$$, ..., $$$c_n$$$ (where $$$c_i$$$ is the number of lectures held during day $$$i$$$) so that: $$$c_1 + c_2 + dots + c_n = m$$$; for each excursion day $$$d$$$, $$$c_d = 0$$$; for each day $$$i$$$, $$$c_i le k_1$$$; for each pair of consecutive days $$$(i, i + 1)$$$, $$$c_i + c_{i + 1} le k_2$$$. Note that there might be some non-excursion days without lectures (i.u2009e., it is possible that $$$c_i = 0$$$ even if $$$i$$$ is not an excursion day). Input The first line contains one integer $$$t$$$ ($$$1 le t le 50$$$) — the number of testcases. Then the testcases follow, each consists of two lines. The first line contains three integers $$$n$$$, $$$k_1$$$, $$$k_2$$$ ($$$1 le n le 5000$$$; $$$1 le k_1 le k_2 le 200,000$$$). The second line contains one string $$$s$$$ consisting of exactly $$$n$$$ characters, each character is either 0 or 1. If $$$s_i = 0$$$, then day $$$i$$$ is an excursion day (so there should be no lectures during that day); if $$$s_i = 1$$$, then day $$$i$$$ is not an excursion day. Output For each test case, print one integer — the maximum possible value of $$$m$$$ (the number of lectures that can be conducted). Example Input 4 4 5 7 1011 4 4 10 0101 5 3 4 11011 6 4 6 011101
1,400
false
true
false
false
false
false
false
false
false
false
3,978
88B
Vasya learns to type. He has an unusual keyboard at his disposal: it is rectangular and it has _n_ rows of keys containing _m_ keys in each row. Besides, the keys are of two types. Some of the keys have lowercase Latin letters on them and some of the keys work like the "Shift" key on standard keyboards, that is, they make lowercase letters uppercase. Vasya can press one or two keys with one hand. However, he can only press two keys if the Euclidean distance between the centers of the keys does not exceed _x_. The keys are considered as squares with a side equal to 1. There are no empty spaces between neighbouring keys. Vasya is a very lazy boy, that's why he tries to type with one hand as he eats chips with his other one. However, it is possible that some symbol can't be typed with one hand only, because the distance between it and the closest "Shift" key is strictly larger than _x_. In this case he will have to use his other hand. Having typed the symbol, Vasya returns other hand back to the chips. You are given Vasya's keyboard and the text. Count the minimum number of times Vasya will have to use the other hand. Input The first line contains three integers _n_, _m_, _x_ (1u2009≤u2009_n_,u2009_m_u2009≤u200930,u20091u2009≤u2009_x_u2009≤u200950). Next _n_ lines contain descriptions of all the keyboard keys. Each line contains the descriptions of exactly _m_ keys, without spaces. The letter keys are marked with the corresponding lowercase letters. The "Shift" keys are marked with the "S" symbol. Then follow the length of the text _q_ (1u2009≤u2009_q_u2009≤u20095·105). The last line contains the text _T_, which consists of _q_ symbols, which are uppercase and lowercase Latin letters. Output If Vasya can type the text, then print the minimum number of times he will have to use his other hand. Otherwise, print "-1" (without the quotes). Examples Input 3 9 4 qwertyuio asdfghjkl SzxcvbnmS 35 TheQuIcKbRoWnFOXjummsovertHeLazYDOG Note In the first sample the symbol "A" is impossible to print as there's no "Shift" key on the keyboard. In the second sample the symbol "e" is impossible to print as there's no such key on the keyboard. In the fourth sample the symbols "T", "G" are impossible to print with one hand. The other letters that are on the keyboard can be printed. Those symbols come up in the text twice, thus, the answer is 2.
1,500
false
false
true
false
false
false
false
false
false
false
9,530
1009B
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). You can swap any two adjacent (consecutive) characters '0' and '1' (i.e. replace "01" with "10" or vice versa) or any two adjacent (consecutive) characters '1' and '2' (i.e. replace "12" with "21" or vice versa). For example, for string "010210" we can perform the following moves: "010210" $$$ ightarrow$$$ "100210"; "010210" $$$ ightarrow$$$ "001210"; "010210" $$$ ightarrow$$$ "010120"; "010210" $$$ ightarrow$$$ "010201". Note than you cannot swap "02" $$$ ightarrow$$$ "20" and vice versa. You cannot perform any other operations with the given string excluding described above. You task is to obtain the minimum possible (lexicographically) string by using these swaps arbitrary number of times (possibly, zero). String $$$a$$$ is lexicographically less than string $$$b$$$ (if strings $$$a$$$ and $$$b$$$ have the same length) if there exists some position $$$i$$$ ($$$1 le i le a$$$, where $$$s$$$ is the length of the string $$$s$$$) such that for every $$$j < i$$$ holds $$$a_j = b_j$$$, and $$$a_i < b_i$$$. Input The first line of the input contains the string $$$s$$$ consisting only of characters '0', '1' and '2', its length is between $$$1$$$ and $$$10^5$$$ (inclusive). Output Print a single string — the minimum possible (lexicographically) string you can obtain by using the swaps described above arbitrary number of times (possibly, zero).
1,400
false
true
true
false
false
false
false
false
false
false
5,641
653E
A tree is a connected undirected graph consisting of _n_ vertices and _n_u2009u2009-u2009u20091 edges. Vertices are numbered 1 through _n_. Limak is a little polar bear. He once had a tree with _n_ vertices but he lost it. He still remembers something about the lost tree though. You are given _m_ pairs of vertices (_a_1,u2009_b_1),u2009(_a_2,u2009_b_2),u2009...,u2009(_a__m_,u2009_b__m_). Limak remembers that for each _i_ there was no edge between _a__i_ and _b__i_. He also remembers that vertex 1 was incident to exactly _k_ edges (its degree was equal to _k_). Is it possible that Limak remembers everything correctly? Check whether there exists a tree satisfying the given conditions. Input The first line of the input contains three integers _n_, _m_ and _k_ ()xa0— the number of vertices in Limak's tree, the number of forbidden pairs of vertices, and the degree of vertex 1, respectively. The _i_-th of next _m_ lines contains two distinct integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_)xa0— the _i_-th pair that is forbidden. It's guaranteed that each pair of vertices will appear at most once in the input. Output Print "possible" (without quotes) if there exists at least one tree satisfying the given conditions. Otherwise, print "impossible" (without quotes). Examples Input 6 5 3 1 2 1 3 1 4 1 5 1 6 Note In the first sample, there are _n_u2009=u20095 vertices. The degree of vertex 1 should be _k_u2009=u20092. All conditions are satisfied for a tree with edges 1u2009-u20095, 5u2009-u20092, 1u2009-u20093 and 3u2009-u20094. In the second sample, Limak remembers that none of the following edges existed: 1u2009-u20092, 1u2009-u20093, 1u2009-u20094, 1u2009-u20095 and 1u2009-u20096. Hence, vertex 1 couldn't be connected to any other vertex and it implies that there is no suitable tree.
2,400
false
false
false
false
false
false
false
false
false
true
7,200
348A
Problem - 348A - 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 math sortings *1600 No tag edit access → Contest materials . The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the _i_-th number in the list is the number of rounds the _i_-th person wants to play. Output In a single line print a single integer — the minimum number of game rounds the friends need to let the _i_-th person play at least _a__i_ rounds. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 3 3 2 2 Output 4 Input 4 2 2 2 2 Output 3 Note You don't need to know the rules of "Mafia" to solve this problem. If you're curious, it's a game Russia got from the Soviet times:
1,600
true
false
false
false
false
false
false
true
true
false
8,442
984A
Problem - 984A - Codeforces =============== xa0 xa0— the number of numbers on the board. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$). Output Print one number that will be left on the board. Examples Input 3 2 1 3 Output 2 Input 3 2 2 2 Output 2 Note In the first sample, the first player erases $$$3$$$ and the second erases $$$1$$$. $$$2$$$ is left on the board. In the second sample, $$$2$$$ is left on the board regardless of the actions of the players.
800
false
false
false
false
false
false
false
false
true
false
5,781
1920C
Allen has an array $$$a_1, a_2,ldots,a_n$$$. For every positive integer $$$k$$$ that is a divisor of $$$n$$$, Allen does the following: He partitions the array into $$$frac{n}{k}$$$ disjoint subarrays of length $$$k$$$. In other words, he partitions the array into the following subarrays: $$$$$$[a_1,a_2,ldots,a_k],[a_{k+1}, a_{k+2},ldots,a_{2k}],ldots,[a_{n-k+1},a_{n-k+2},ldots,a_{n}]$$$$$$ Allen earns one point if there exists some positive integer $$$m$$$ ($$$m geq 2$$$) such that if he replaces every element in the array with its remainder when divided by $$$m$$$, then all subarrays will be identical. Help Allen find the number of points he will earn. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of 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 2cdot10^5$$$) — the 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 n$$$) — the elements of the array $$$a$$$. 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 number of points Allen will earn. Example Input 8 4 1 2 1 4 3 1 2 3 5 1 1 1 1 1 6 1 3 1 1 3 1 6 6 2 6 2 2 2 6 2 6 3 6 6 6 10 1 7 5 1 4 3 1 3 1 4 1 1 Note In the first test case, $$$k=2$$$ earns a point since Allen can pick $$$m = 2$$$ and both subarrays will be equal to $$$[1, 0]$$$. $$$k=4$$$ also earns a point, since no matter what $$$m$$$ Allen chooses, there will be only one subarray and thus all subarrays are equal. In the second test case, Allen earns $$$1$$$ point for $$$k=3$$$, where his choice for $$$m$$$ does not matter.
1,600
true
false
false
false
false
false
true
false
false
false
770
520A
Problem - 520A - 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 strings *800 No tag edit access → Contest materials ") xa0— the number of characters in the string. The second line contains the string. The string consists only of uppercase and lowercase Latin letters. Output Output "YES", if the string is a pangram and "NO" otherwise. Examples Input 12 toosmallword Output NO Input 35 TheQuickBrownFoxJumpsOverTheLazyDog Output YES
800
false
false
true
false
false
false
false
false
false
false
7,757
1614B
The company "Divan's Sofas" is planning to build $$$n + 1$$$ different buildings on a coordinate line so that: the coordinate of each building is an integer number; no two buildings stand at the same point. Let $$$x_i$$$ be the coordinate of the $$$i$$$-th building. To get from the building $$$i$$$ to the building $$$j$$$, Divan spends $$$x_i - x_j$$$ minutes, where $$$y$$$ is the absolute value of $$$y$$$. All buildings that Divan is going to build can be numbered from $$$0$$$ to $$$n$$$. The businessman will live in the building $$$0$$$, the new headquarters of "Divan's Sofas". In the first ten years after construction Divan will visit the $$$i$$$-th building $$$a_i$$$ times, each time spending $$$2 cdot x_0-x_i$$$ minutes for walking. Divan asks you to choose the coordinates for all $$$n + 1$$$ buildings so that over the next ten years the businessman will spend as little time for walking as possible. Input Each test contains several test cases. The first line contains one integer number $$$t$$$ ($$$1 le t le 10^3$$$) — the number of test cases. The first line of each case contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of buildings that "Divan's Sofas" is going to build, apart from the headquarters. The second line contains the sequence $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^6$$$), where $$$a_i$$$ is the number of visits to the $$$i$$$-th building. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, on the first line print the number $$$T$$$ — the minimum time Divan will spend walking. On the second line print the sequence $$$x_0, x_1, ldots, x_n$$$ of $$$n + 1$$$ integers, where $$$x_i$$$ ($$$-10^6 le x_i le 10^6$$$) is the selected coordinate of the $$$i$$$-th building. It can be shown that an optimal answer exists with coordinates not exceeding $$$10^6$$$. If there are multiple answers, print any of them. Example Input 4 3 1 2 3 5 3 8 10 6 1 5 1 1 1 1 1 1 0 Output 14 2 4 1 3 78 1 -1 0 2 3 4 18 3 6 1 5 2 4 0 1 2 Note Let's look at the first example. Divan will visit the first building $$$a_1 = 1$$$ times, the second $$$a_2 = 2$$$ times and the third $$$a_3 = 3$$$ times. Then one of the optimal solution will be as follows: 1. the headquarters is located in $$$x_0 = 2$$$; 2. $$$x_1 = 4$$$: Divan will spend $$$2 cdot x_0-x_1 cdot a_1 = 2 cdot 2-4 cdot 1 = 4$$$ minutes walking to the first building; 3. $$$x_2 = 1$$$: Divan will spend $$$2 cdot x_0-x_2 cdot a_2 = 2 cdot 2-1 cdot 2 = 4$$$ minutes walking to the second building; 4. $$$x_3 = 3$$$: Divan will spend $$$2 cdot x_0-x_3 cdot a_3 = 2 cdot 2-3 cdot 3 = 6$$$ minutes walking to the third building. In total, Divan will spend $$$4 + 4 + 6 = 14$$$ minutes. It can be shown that it is impossible to arrange buildings so that the businessman spends less time. Among others, $$$x = [1, 3, 2, 0]$$$, $$$x = [-5, -3, -6, -4]$$$ are also correct answers for the first example.
1,000
false
false
false
false
false
true
false
false
true
false
2,569
909E
You are given a program you want to execute as a set of tasks organized in a dependency graph. The dependency graph is a directed acyclic graph: each task can depend on results of one or several other tasks, and there are no directed circular dependencies between tasks. A task can only be executed if all tasks it depends on have already completed. Some of the tasks in the graph can only be executed on a coprocessor, and the rest can only be executed on the main processor. In one coprocessor call you can send it a set of tasks which can only be executed on it. For each task of the set, all tasks on which it depends must be either already completed or be included in the set. The main processor starts the program execution and gets the results of tasks executed on the coprocessor automatically. Find the minimal number of coprocessor calls which are necessary to execute the given program. Input The first line contains two space-separated integers _N_ (1u2009≤u2009_N_u2009≤u2009105) — the total number of tasks given, and _M_ (0u2009≤u2009_M_u2009≤u2009105) — the total number of dependencies between tasks. The next line contains _N_ space-separated integers . If _E__i_u2009=u20090, task _i_ can only be executed on the main processor, otherwise it can only be executed on the coprocessor. The next _M_ lines describe the dependencies between tasks. Each line contains two space-separated integers _T_1 and _T_2 and means that task _T_1 depends on task _T_2 (_T_1u2009≠u2009_T_2). Tasks are indexed from 0 to _N_u2009-u20091. All _M_ pairs (_T_1,u2009_T_2) are distinct. It is guaranteed that there are no circular dependencies between tasks. Output Output one line containing an integer — the minimal number of coprocessor calls necessary to execute the program. Note In the first test, tasks 1 and 3 can only be executed on the coprocessor. The dependency graph is linear, so the tasks must be executed in order 3 -> 2 -> 1 -> 0. You have to call coprocessor twice: first you call it for task 3, then you execute task 2 on the main processor, then you call it for for task 1, and finally you execute task 0 on the main processor. In the second test, tasks 0, 1 and 2 can only be executed on the coprocessor. Tasks 1 and 2 have no dependencies, and task 0 depends on tasks 1 and 2, so all three tasks 0, 1 and 2 can be sent in one coprocessor call. After that task 3 is executed on the main processor.
1,900
false
true
false
true
false
false
false
false
false
true
6,108
1739A
There is a chess board of size $$$n imes m$$$. The rows are numbered from $$$1$$$ to $$$n$$$, the columns are numbered from $$$1$$$ to $$$m$$$. Let's call a cell isolated if a knight placed in that cell can't move to any other cell on the board. Recall that a chess knight moves two cells in one direction and one cell in a perpendicular direction: Find any isolated cell on the board. If there are no such cells, print any cell on the board. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 64$$$)xa0— the number of testcases. The only line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 8$$$)xa0— the number of rows and columns of the board. Output For each testcase, print two integersxa0— the row and the column of any isolated cell on the board. If there are no such cells, print any cell on the board. Note In the first testcase, all cells are isolated. A knight can't move from any cell of the board to any other one. Thus, any cell on board is a correct answer. In the second testcase, there are no isolated cells. On a normal chess board, a knight has at least two moves from any cell. Thus, again, any cell is a correct answer. In the third testcase, only the middle cell of the board is isolated. The knight can move freely around the border of the board, but can't escape the middle.
800
false
false
true
false
false
false
false
false
false
false
1,853
12E
Problem - 12E - 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 *2100 No tag edit access → Contest materials ") . Gods will never allow to start the championship without it. Matrix should contain integers from 0 to _n_u2009-u20091, main diagonal should contain only zeroes and matrix should be symmetric. Moreover, all numbers in each row should be different. Magicians are very tired of the thinking process, so they ask you to write a program to find such matrix. Input The first line contains one integer _n_ (2u2009≤u2009_n_u2009≤u20091000), _n_ is even. Output Output _n_ lines with _n_ numbers each — the required matrix. Separate numbers with spaces. If there are several solutions, output any. Examples Input 2 Output 0 1 1 0 Input 4 Output 0 1 3 2 1 0 2 3 3 2 0 1 2 3 1 0
2,100
false
false
false
false
false
true
false
false
false
false
9,931
229E
Once upon a time an old man and his wife lived by the great blue sea. One day the old man went fishing and caught a real live gold fish. The fish said: "Oh ye, old fisherman! Pray set me free to the ocean and I will grant you with _n_ gifts, any gifts you wish!". Then the fish gave the old man a list of gifts and their prices. Some gifts on the list can have the same names but distinct prices. However, there can't be two gifts with the same names and the same prices. Also, there can be gifts with distinct names and the same prices. The old man can ask for _n_ names of items from the list. If the fish's list has _p_ occurrences of the given name, then the old man can't ask for this name of item more than _p_ times. The old man knows that if he asks for _s_ gifts of the same name, the fish will randomly (i.e. uniformly amongst all possible choices) choose _s_ gifts of distinct prices with such name from the list. The old man wants to please his greedy wife, so he will choose the _n_ names in such a way that he can get _n_ gifts with the maximum price. Besides, he isn't the brightest of fishermen, so if there are several such ways, he chooses one of them uniformly. The old man wondered, what is the probability that he can get _n_ most expensive gifts. As the old man isn't good at probability theory, he asks you to help him. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000) — the number of the old man's wishes and the number of distinct names in the goldfish's list, correspondingly. Then _m_ lines follow: the _i_-th line first contains integer _k__i_ (_k__i_u2009>u20090)xa0— the number of distinct prices of gifts with the _i_-th name, then _k__i_ distinct space-separated integers _c__ij_ (1u2009≤u2009_c__ij_u2009≤u2009109), the gifts' prices. It is guaranteed that the sum of all _k__i_ doesn't exceed 1000. It is guaranteed that _n_ is not greater than the total number of the gifts. Output On a single line print one real number — the probability of getting _n_ most valuable gifts. The answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20099. Examples Input 3 2 1 40 4 10 20 30 40
2,600
true
false
false
true
false
false
false
false
false
false
8,919
1267I
This is an interactive problem. You are the head coach of a chess club. The club has $$$2n$$$ players, each player has some strength which can be represented by a number, and all those numbers are distinct. The strengths of the players are not known to you. You need to select $$$n$$$ players who would represent your club in the upcoming championship. Naturally, you want to select $$$n$$$ players with the highest strengths. You can organize matches between the players to do that. In every match, you pick two players, they play some games, and you learn which one of the two has higher strength. You can wait for the outcome of a match before deciding who will participate in the next one. However, you do not want to know exactly how those $$$n$$$ players compare between themselves, as that would make the championship itself less intriguing. More formally, you must reach a state where there is exactly one way to choose $$$n$$$ players with the highest strengths that is consistent with the outcomes of the matches you organized, but there must be at least two possible orderings of those $$$n$$$ players by strength that are consistent with the outcomes of the matches you organized. Interaction Your program has to process multiple test cases in one run. First, it should read the integer $$$t$$$ ($$$t ge 1$$$)xa0— the number of test cases. Then, it should process the test cases one by one. In each test case, your program should start by reading the integer $$$n$$$ ($$$3 le n le 100$$$)xa0— the number of players to select out of $$$2n$$$ players. The sum of squares of the values of $$$n$$$ over all test cases does not exceed $$$10,000$$$. Then your program can organize matches zero or more times. To organize a match, your program should print a match description formatted as ? $$$i$$$ $$$j$$$xa0— a question mark followed by two distinct numbers of players participating in the match. The players are numbered from 1 to $$$2n$$$, inclusive. Remember to flush the output after printing the match description. Then your program should read the match outcomexa0— it will be either the greater-than character (>), if the first player in the match description has higher strength, or the less-than character (<), if the second player in the match description has higher strength. Your program can organize at most $$$4n^2$$$ matches. After it is done organizing matches, it should print the exclamation mark (!) and continue to the next test case, or exit gracefully if this was the last test case. Remember to flush the output after printing the exclamation mark. There must be exactly one way to choose $$$n$$$ players with the highest strength that is consistent with the outcomes of the matches you organized, but there must be at least two possible orderings of those $$$n$$$ players by their strength that are consistent with the outcomes of the matches you organized. The judging program picks some distinct numbers as the strengths of all players before your program starts organizing matches and uses them to answer the requests. Example Input 2 3 > < > < > > 3 < < < > > Output ? 1 3 ? 4 2 ? 4 5 ? 6 5 ? 3 4 ? 5 6 ! ? 3 4 ? 4 2 ? 5 3 ? 6 4 ? 3 1 ! Note In the example, the players in the first test case are sorted by strength in decreasing order. From the matches in the example output, we can deduce that players 1, 2, and 3 have the highest strength, but we do not know how the player 1 compares to the player 2.
2,600
false
false
true
false
false
true
true
false
true
false
4,355
55B
Problem - 55B - Codeforces =============== xa0 and replaced them with their sum or their product. In the end he got one number. Unfortunately, due to the awful memory he forgot that number, but he remembers four original numbers, sequence of the operations and his surprise because of the very small result. Help Vladimir remember the forgotten number: find the smallest number that can be obtained from the original numbers by the given sequence of operations. Input First line contains four integers separated by space: 0u2009≤u2009_a_,u2009_b_,u2009_c_,u2009_d_u2009≤u20091000 — the original numbers. Second line contains three signs ('+' or '*' each) separated by space — the sequence of the operations in the order of performing. ('+' stands for addition, '*' — multiplication) Output Output one integer number — the minimal result which can be obtained. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). Examples Input 1 1 1 1 + + Output 3 Input 2 2 2 2 + Output 8 Input 1 2 3 4 + + Output 9
1,600
false
false
false
false
false
false
true
false
false
false
9,699
1987D
Alice and Bob are playing a game. Initially, there are $$$n$$$ cakes, with the $$$i$$$-th cake having a tastiness value of $$$a_i$$$. Alice and Bob take turns eating them, with Alice starting first: In her turn, Alice chooses and eats any remaining cake whose tastiness is strictly greater than the maximum tastiness of any of the cakes she's eaten before that. Note that on the first turn, she can choose any cake. In his turn, Bob chooses any remaining cake and eats it. The game ends when the current player can't eat a suitable cake. Let $$$x$$$ be the number of cakes that Alice ate. Then, Alice wants to maximize $$$x$$$, while Bob wants to minimize $$$x$$$. Find out how many cakes Alice will eat if both players play optimally. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5000$$$)xa0— the number of cakes. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the tastiness values of the cakes. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each test case, output a single integerxa0— the number of cakes Alice will eat if both players play optimally. Example Input 9 4 1 4 2 3 3 1 1 1 5 1 4 2 3 4 4 3 4 1 4 1 1 8 4 3 2 5 6 8 3 4 7 6 1 1 3 5 3 1 11 6 11 6 8 7 5 3 11 2 3 5 17 2 6 5 3 9 1 6 2 5 6 3 2 3 9 6 1 6 Note In the first test case, one possible sequence of turns is: 1. Alice eats a cake with a tastiness value of $$$1$$$. The remaining cakes are $$$[4, 2, 3]$$$. 2. Bob eats a cake with a tastiness value of $$$2$$$. The remaining cakes are $$$[4, 3]$$$. 3. Alice eats a cake with a tastiness of $$$3$$$. The remaining cakes are $$$[4]$$$. 4. Bob eats a cake with a tastiness value of $$$4$$$. The remaining cakes are $$$[]$$$. 5. Since there are no more cakes left, the game ends. In the second test case, one possible sequence of turns is: 1. Alice eats a cake with a tastiness value of $$$1$$$. The remaining cakes are $$$[1, 1]$$$. 2. Bob eats a cake with a tastiness value of $$$1$$$. The remaining cakes are $$$[1]$$$. 3. Since Alice has already eaten a cake with a tastiness value of $$$1$$$, she cannot make a turn, so the game ends.
1,800
false
false
false
true
false
false
false
false
false
false
354
2004C
Alice and Bob have $$$n$$$ items they'd like to split between them, so they decided to play a game. All items have a cost, and the $$$i$$$-th item costs $$$a_i$$$. Players move in turns starting from Alice. In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left. Let's say that $$$A$$$ is the total cost of items taken by Alice and $$$B$$$ is the total cost of Bob's items. The resulting score of the game then will be equal to $$$A - B$$$. Alice wants to maximize the score, while Bob wants to minimize it. Both Alice and Bob will play optimally. But the game will take place tomorrow, so today Bob can modify the costs a little. He can increase the costs $$$a_i$$$ of several (possibly none or all) items by an integer value (possibly, by the same value or by different values for each item). However, the total increase must be less than or equal to $$$k$$$. Otherwise, Alice may suspect something. Note that Bob can't decrease costs, only increase. What is the minimum possible score Bob can achieve? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5000$$$)xa0— the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$0 le k le 10^9$$$)xa0— the number of items and the maximum total increase Bob can make. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the initial costs of the items. It's guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the minimum possible score $$$A - B$$$ after Bob increases the costs of several (possibly none or all) items. Example Input 4 2 5 1 10 3 0 10 15 12 4 6 3 1 2 4 2 4 6 9 Note In the first test case, Bob can increase $$$a_1$$$ by $$$5$$$, making costs equal to $$$[6, 10]$$$. Tomorrow, Alice will take $$$10$$$ and Bob will take $$$6$$$. The total score will be equal to $$$10 - 6 = 4$$$, and it's the minimum possible. In the second test case, Bob can't change costs. So the score will be equal to $$$(15 + 10) - 12 = 13$$$, since Alice will take $$$15$$$, Bob will take $$$12$$$, and Alicexa0— $$$10$$$. In the third test case, Bob, for example, can increase $$$a_1$$$ by $$$1$$$, $$$a_2$$$ by $$$3$$$, and $$$a_3$$$ by $$$2$$$. The total change is equal to $$$1 + 3 + 2 le 6$$$ and costs will be equal to $$$[4, 4, 4, 4]$$$. Obviously, the score will be equal to $$$(4 + 4) - (4 + 4) = 0$$$. In the fourth test case, Bob can increase $$$a_1$$$ by $$$3$$$, making costs equal to $$$[9, 9]$$$. The score will be equal to $$$9 - 9 = 0$$$.
1,100
false
true
false
false
false
false
false
false
true
false
229
1359E
Problem - 1359E - 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 *2000 No tag edit access → Contest materials . Let's call an array of positive integers $$$
2,000
true
false
false
false
false
false
false
false
false
false
3,887
1807E
This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read and $$$k$$$ unique piles $$$p_1, p_2, dots, p_k$$$ ($$$1 leq p_i leq n$$$), and the director will return the total weight $$$m_{p_1} + m_{p_2} + dots + m_{p_k}$$$, where $$$m_i$$$ denotes the weight of pile $$$i$$$. Gon is tasked with finding the pile that contains the special stone. However, the director is busy. Help Gon find this pile in at most $$$mathbf{30}$$$ queries. Input The input data contains several test cases. The first line contains one integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$1 leq a_i leq 10^4$$$)xa0— the number of stones in each pile. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. After reading the input for each test case, proceed with the interaction as follows. Interaction You can perform the operation at most $$$mathbf{30}$$$ times to guess the pile. To make a guess, print a line with the following format: $$$ exttt{?} k p_1 p_2 p_3 ... p_{k-1} p_k$$$ ($$$1 leq k leq n$$$; $$$1 leq p_i leq n$$$; all $$$p_i$$$ are distinct)xa0— the indices of the piles. After each operation, you should read a line containing a single integer $$$x$$$xa0— the sum of weights of the chosen piles. (Formally, $$$x = m_{p_1} + m_{p_2} + dots + m_{p_k}$$$.) When you know the index of the pile with the special stone, print one line in the following format: $$$ exttt{!} m$$$ ($$$1 leq m leq n$$$). After that, move on to the next test case, or terminate the program if there are no more test cases remaining. If your program performs more than $$$30$$$ operations for one test case or makes an invalid query, you may receive a Wrong Answer verdict. After you print a query or the answer, please remember to output the end of the line and flush the output. Otherwise, you may get Idleness limit exceeded or some other verdict. To do this, use the following: 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. It is additionally recommended to read the xa0— the number of test cases. The first line of each test case should contain two integers $$$n, m$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0– the number of piles and the pile with the special stone. The second line of each test case should contain $$$n$$$ integers $$$a_i$$$ ($$$1 leq a_i leq 10^4$$$)xa0— the number of stones in each pile. Note that the interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant. Example Input 2 5 1 2 3 4 5 11 6 3 7 1 2 3 5 3 4 2 12 6 Output ? 4 1 2 3 4 ? 2 2 3 ? 1 2 ! 2 ? 4 2 3 5 6 ? 2 1 4 ! 7 Note In the first test case, the stone with weight two is located in pile $$$2$$$, as shown in the picture. We perform the following interaction: $$$ exttt{? 4 1 2 3 4}$$$xa0— ask the total weight of piles $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$. The total weight we receive back is $$$1+3+3+4=11$$$. $$$ exttt{? 2 2 3}$$$xa0— ask the total weight of piles $$$2$$$ and $$$3$$$. The total weight we receive back is $$$3+3=6$$$. $$$ exttt{? 1 2}$$$xa0— ask the total weight of pile $$$2$$$. The total weight we receive back is $$$3$$$. $$$ exttt{! 2}$$$xa0— we have figured out that pile $$$2$$$ contains the special stone, so we output it and move on to the next test case. In the second test case, the stone with weight two is located on index $$$7$$$. We perform the following interaction: $$$ exttt{? 4 2 3 5 6}$$$xa0— ask the total weight of piles $$$2$$$, $$$3$$$, $$$5$$$, and $$$6$$$. The total weight we receive back is $$$2+3+3+4=12$$$. $$$ exttt{? 2 1 4}$$$xa0— ask the total weight of piles $$$1$$$ and $$$4$$$. The total weight we receive back is $$$1+5=6$$$. $$$ exttt{! 7}$$$xa0— we have somehow figured out that pile $$$7$$$ contains the special stone, so we output it and end the interaction.
1,300
false
false
true
false
false
false
false
true
false
false
1,431
91A
Problem - 91A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy strings *1500 No tag edit access → Contest materials . Output If it is impossible to get the word _s_2 in the above-described manner, print "-1" (without the quotes). Otherwise, print the least number of newspaper headings _s_1, which Fangy will need to receive the word _s_2. Examples Input abc xyz Output -1 Input abcd dabc Output 2
1,500
false
true
false
false
false
false
false
false
false
false
9,522
907B
Two bears are playing tic-tac-toe via mail. It's boring for them to play usual tic-tac-toe game, so they are a playing modified version of this game. Here are its rules. The game is played on the following field. Players are making moves by turns. At first move a player can put his chip in any cell of any small field. For following moves, there are some restrictions: if during last move the opposite player put his chip to cell with coordinates (_x__l_,u2009_y__l_) in some small field, the next move should be done in one of the cells of the small field with coordinates (_x__l_,u2009_y__l_). For example, if in the first move a player puts his chip to lower left cell of central field, then the second player on his next move should put his chip into some cell of lower left field (pay attention to the first test case). If there are no free cells in the required field, the player can put his chip to any empty cell on any field. You are given current state of the game and coordinates of cell in which the last move was done. You should find all cells in which the current player can put his chip. A hare works as a postman in the forest, he likes to foul bears. Sometimes he changes the game field a bit, so the current state of the game could be unreachable. However, after his changes the cell where the last move was done is not empty. You don't need to find if the state is unreachable or not, just output possible next moves according to the rules. Input First 11 lines contains descriptions of table with 9 rows and 9 columns which are divided into 9 small fields by spaces and empty lines. Each small field is described by 9 characters without spaces and empty lines. character "x" (ASCII-code 120) means that the cell is occupied with chip of the first player, character "o" (ASCII-code 111) denotes a field occupied with chip of the second player, character "." (ASCII-code 46) describes empty cell. The line after the table contains two integers _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u20099). They describe coordinates of the cell in table where the last move was done. Rows in the table are numbered from up to down and columns are numbered from left to right. It's guaranteed that cell where the last move was done is filled with "x" or "o". Also, it's guaranteed that there is at least one empty cell. It's not guaranteed that current state of game is reachable. Output Output the field in same format with characters "!" (ASCII-code 33) on positions where the current player can put his chip. All other cells should not be modified. Examples Input ... ... ... ... ... ... ... ... ...... ... ... ... ... ... ... x.. ... ... ... ... ... ... ... ... ... ... 6 4 Output ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... x.. ... !!! ... ... !!! ... ... !!! ... ... Input xoo x.. x.. ooo ... ... ooo ... ...x.. x.. x.. ... ... ... ... ... ... x.. x.. x.. ... ... ... ... ... ... 7 4 Output xoo x!! x!! ooo !!! !!! ooo !!! !!! x!! x!! x!! !!! !!! !!! !!! !!! !!! x!! x!! x!! !!! !!! !!! !!! !!! !!! Input o.. ... ... ... ... ... ... ... ...... xxx ... ... xox ... ... ooo ... ... ... ... ... ... ... ... ... ... 5 5 Output o!! !!! !!! !!! !!! !!! !!! !!! !!! !!! xxx !!! !!! xox !!! !!! ooo !!! !!! !!! !!! !!! !!! !!! !!! !!! !!! Note In the first test case the first player made a move to lower left cell of central field, so the second player can put a chip only to cells of lower left field. In the second test case the last move was done to upper left cell of lower central field, however all cells in upper left field are occupied, so the second player can put his chip to any empty cell. In the third test case the last move was done to central cell of central field, so current player can put his chip to any cell of central field, which is already occupied, so he can move anywhere. Pay attention that this state of the game is unreachable.
1,400
false
false
true
false
false
false
false
false
false
false
6,121
1305G
Kuroni isn't good at economics. So he decided to found a new financial pyramid called Antihype. It has the following rules: 1. You can join the pyramid for free and get $$$0$$$ coins. 2. If you are already a member of Antihype, you can invite your friend who is currently not a member of Antihype, and get a number of coins equal to your age (for each friend you invite). $$$n$$$ people have heard about Antihype recently, the $$$i$$$-th person's age is $$$a_i$$$. Some of them are friends, but friendship is a weird thing now: the $$$i$$$-th person is a friend of the $$$j$$$-th person if and only if $$$a_i ext{ AND } a_j = 0$$$, where $$$ ext{AND}$$$ denotes the xa0— the number of people. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0le a_i le 2cdot 10^5$$$) xa0— the ages of the people. Output Output exactly one integer xa0— the maximum possible combined gainings of all $$$n$$$ people. Note Only the first and second persons are friends. The second can join Antihype and invite the first one, getting $$$2$$$ for it.
3,500
false
false
false
true
false
false
true
false
false
true
4,158
1363F
# Rotating Substrings Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes You are given two strings s and t, each of length n and consisting of lowercase Latin alphabets. You want to make s equal to t.You can perform the following operation on s any number of times to achieve it x16 • Choose any substring of s and rotate it clockwise once, that is, if the selected substring is s[l, l +1 ...r ],then it becomes s[r, l, l + 1 ...r − 1] . All the remaining characters of s stay in their position. For example, on rotating the substring [2 , 4] , string “ abcde ” becomes “ adbce ”. A string a is a substring of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Find the minimum number of operations required to convert s to t, or determine that it’s impossible. # Input The first line of the input contains a single integer t (1 ≤ t ≤ 2000) x16 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 ≤ n ≤ 2000) x16 the length of the strings. The second and the third lines contain strings s and t respectively. The sum of n over all the test cases does not exceed 2000 . # Output For each test case, output the minimum number of operations to convert s to t. If it is not possible to convert s to t, output −1 instead. # Example standard input standard output 61aa2ab ba 3abc cab 3abc cba 4abab baba 4abcc aabc 01121-1 Page 1 of 2 Note For the 1-st test case, since s and t are equal, you don’t need to apply any operation. For the 2-nd test case, you only need to apply one operation on the entire string ab to convert it to ba .For the 3-rd test case, you only need to apply one operation on the entire string abc to convert it to cab .For the 4-th test case, you need to apply the operation twice: first on the entire string abc to convert it to cab and then on the substring of length 2 beginning at the second character to convert it to cba .For the 5-th test case, you only need to apply one operation on the entire string abab to convert it to baba .For the 6-th test case, it is not possible to convert string s to t. Page 2 of 2
2,600
false
false
false
true
false
false
false
false
false
false
3,863
1988D
You, the monster killer, want to kill a group of monsters. The monsters are on a tree with $$$n$$$ vertices. On vertex with number $$$i$$$ ($$$1le ile n$$$), there is a monster with $$$a_i$$$ attack points. You want to battle with monsters for $$$10^{100}$$$ rounds. In each round, the following happens in order: 1. All living monsters attack you. Your health decreases by the sum of attack points of all living monsters. 2. You select some (possibly all or none) monsters and kill them. After being killed, the monster will not be able to do any attacks in the future. There is a restriction: in one round, you cannot kill two monsters that are directly connected by an edge. If you choose what monsters to attack optimally, what is the smallest health decrement you can have after all rounds? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1le nle 3cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,ldots,a_n$$$ ($$$1le a_ile 10^{12}$$$). The following $$$n-1$$$ lines each contain two integers $$$x,y$$$ ($$$1le x,yle n$$$), denoting an edge on the tree connecting vertex $$$x$$$ and $$$y$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3cdot 10^5$$$. Output For each test case, print one integer: the minimum possible health decrement. Example Input 3 1 1000000000000 5 47 15 32 29 23 1 2 1 3 2 4 2 5 7 8 10 2 3 5 7 4 1 2 1 4 3 2 5 3 6 2 7 5 Output 1000000000000 193 57 Note In the first test case, an optimal sequence of operations would be: In the first round: first, receive the attack from the monster on vertex $$$1$$$, so your health decreases by $$$10^{12}$$$. Then kill the monster on vertex $$$1$$$. In the second round to the $$$10^{100}$$$-th round: all monsters have been killed, so nothing happens. The total health decrement is $$$10^{12}$$$. In the second test case, an optimal sequence of operations would be: In the first round: first, receive the attack from the monster on vertex $$$1,2,3,4,5$$$, so your health decreases by $$$47+15+32+29+23=146$$$. Then kill the monsters on vertex $$$1,4,5$$$. In the second round: first, receive the attack from the monster on vertex $$$2,3$$$, so your health decreases by $$$15+32=47$$$. Then kill the monsters on vertex $$$2,3$$$. In the third round to the $$$10^{100}$$$-th round: all monsters have been killed, so nothing happens. The total health decrement is $$$193$$$. In the third test case, an optimal sequence of operations would be: In the first round: first, receive the attack from the monster on vertex $$$1,2,3,4,5,6,7$$$, so your health decreases by $$$8+10+2+3+5+7+4=39$$$. Then kill the monsters on vertex $$$1,3,6,7$$$. In the second round: first, receive the attack from the monster on vertex $$$2,4,5$$$, so your health decreases by $$$10+3+5=18$$$. Then kill the monsters on vertex $$$2,4,5$$$. In the third round to the $$$10^{100}$$$-th round: all monsters have been killed, so nothing happens. The total health decrement is $$$57$$$.
2,000
false
false
false
true
false
false
true
false
false
false
344
316E1
By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher: You are given a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Your task is to perform on it _m_ consecutive operations of the following type: 1. For given numbers _x__i_ and _v__i_ assign value _v__i_ to element _a__x__i_. 2. For given numbers _l__i_ and _r__i_ you've got to calculate sum , where _f_0u2009=u2009_f_1u2009=u20091 and at _i_u2009≥u20092: _f__i_u2009=u2009_f__i_u2009-u20091u2009+u2009_f__i_u2009-u20092. 3. For a group of three numbers _l__i_ _r__i_ _d__i_ you should increase value _a__x_ by _d__i_ for all _x_ (_l__i_u2009≤u2009_x_u2009≤u2009_r__i_). Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009105). Then follow _m_ lines, each describes an operation. Each line starts with an integer _t__i_ (1u2009≤u2009_t__i_u2009≤u20093) — the operation type: if _t__i_u2009=u20091, then next follow two integers _x__i_ _v__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_,u20090u2009≤u2009_v__i_u2009≤u2009105); if _t__i_u2009=u20092, then next follow two integers _l__i_ _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_); if _t__i_u2009=u20093, then next follow three integers _l__i_ _r__i_ _d__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_,u20090u2009≤u2009_d__i_u2009≤u2009105). The input limits for scoring 30 points are (subproblem E1): It is guaranteed that _n_ does not exceed 100, _m_ does not exceed 10000 and there will be no queries of the 3-rd type. The input limits for scoring 70 points are (subproblems E1+E2): It is guaranteed that there will be queries of the 1-st and 2-nd type only. The input limits for scoring 100 points are (subproblems E1+E2+E3): No extra limitations. Output For each query print the calculated sum modulo 1000000000 (109). Examples Input 5 5 1 3 1 2 4 2 1 4 2 1 5 2 2 4 1 3 10 2 1 5 Input 5 4 1 3 1 2 4 3 1 4 1 2 2 4 1 2 10 2 1 5
1,500
false
false
false
false
true
false
true
false
false
false
8,574
1493E
You are given two integers $$$l$$$ and $$$r$$$ in binary representation. Let $$$g(x, y)$$$ be equal to the oplus dots oplus (y-1) oplus y$$$). Let's define $$$f(l, r)$$$ as the maximum of all values of $$$g(x, y)$$$ satisfying $$$l le x le y le r$$$. Output $$$f(l, r)$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$) — the length of the binary representation of $$$r$$$. The second line contains the binary representation of $$$l$$$ — a string of length $$$n$$$ consisting of digits $$$0$$$ and $$$1$$$ ($$$0 le l < 2^n$$$). The third line contains the binary representation of $$$r$$$ — a string of length $$$n$$$ consisting of digits $$$0$$$ and $$$1$$$ ($$$0 le r < 2^n$$$). It is guaranteed that $$$l le r$$$. The binary representation of $$$r$$$ does not contain any extra leading zeros (if $$$r=0$$$, the binary representation of it consists of a single zero). The binary representation of $$$l$$$ is preceded with leading zeros so that its length is equal to $$$n$$$. Output In a single line output the value of $$$f(l, r)$$$ for the given pair of $$$l$$$ and $$$r$$$ in binary representation without extra leading zeros. Note In sample test case $$$l=19$$$, $$$r=122$$$. $$$f(x,y)$$$ is maximal and is equal to $$$127$$$, with $$$x=27$$$, $$$y=100$$$, for example.
2,600
true
true
false
false
false
true
false
false
false
false
3,204
567D
Alice and Bob love playing one-dimensional battle ships. They play on the field in the form of a line consisting of _n_ square cells (that is, on a 1u2009×u2009_n_ table). At the beginning of the game Alice puts _k_ ships on the field without telling their positions to Bob. Each ship looks as a 1u2009×u2009_a_ rectangle (that is, it occupies a sequence of _a_ consecutive squares of the field). The ships cannot intersect and even touch each other. After that Bob makes a sequence of "shots". He names cells of the field and Alice either says that the cell is empty ("miss"), or that the cell belongs to some ship ("hit"). But here's the problem! Alice like to cheat. May be that is why she responds to each Bob's move with a "miss". Help Bob catch Alice cheating — find Bob's first move, such that after it you can be sure that Alice cheated. Input The first line of the input contains three integers: _n_, _k_ and _a_ (1u2009≤u2009_n_,u2009_k_,u2009_a_u2009≤u20092·105) — the size of the field, the number of the ships and the size of each ship. It is guaranteed that the _n_, _k_ and _a_ are such that you can put _k_ ships of size _a_ on the field, so that no two ships intersect or touch each other. The second line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009_n_) — the number of Bob's moves. The third line contains _m_ distinct integers _x_1,u2009_x_2,u2009...,u2009_x__m_, where _x__i_ is the number of the cell where Bob made the _i_-th shot. The cells are numbered from left to right from 1 to _n_. Output Print a single integer — the number of such Bob's first move, after which you can be sure that Alice lied. Bob's moves are numbered from 1 to _m_ in the order the were made. If the sought move doesn't exist, then print "-1".
1,700
false
true
false
false
true
false
false
true
true
false
7,581
1263A
You have three piles of candies: red, green and blue candies: the first pile contains only red candies and there are $$$r$$$ candies in it, the second pile contains only green candies and there are $$$g$$$ candies in it, the third pile contains only blue candies and there are $$$b$$$ candies in it. Each day Tanya eats exactly two candies of different colors. She is free to choose the colors of eaten candies: the only restriction that she can't eat two candies of the same color in a day. Find the maximal number of days Tanya can eat candies? Each day she needs to eat exactly two candies. Input The first line contains integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a separate line of the input. It contains three integers $$$r$$$, $$$g$$$ and $$$b$$$ ($$$1 le r, g, b le 10^8$$$) — the number of red, green and blue candies, respectively. Output Print $$$t$$$ integers: the $$$i$$$-th printed integer is the answer on the $$$i$$$-th test case in the input. Example Input 6 1 1 1 1 2 1 4 1 1 7 4 10 8 1 4 8 2 8 Note In the first example, Tanya can eat candies for one day only. She can eat any pair of candies this day because all of them have different colors. In the second example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and green and blue candies on the second day. In the third example, Tanya can eat candies for two days. For example, she can eat red and green candies on the first day, and red and blue candies on the second day. Note, that two red candies will remain uneaten.
1,100
true
false
false
false
false
false
false
false
false
false
4,387
1473B
Let's define a multiplication operation between a string $$$a$$$ and a positive integer $$$x$$$: $$$a cdot x$$$ is the string that is a result of writing $$$x$$$ copies of $$$a$$$ one after another. For example, "abc" $$$cdot~2~=$$$ "abcabc", "a" $$$cdot~5~=$$$ "aaaaa". A string $$$a$$$ is divisible by another string $$$b$$$ if there exists an integer $$$x$$$ such that $$$b cdot x = a$$$. For example, "abababab" is divisible by "ab", but is not divisible by "ababab" or "aa". LCM of two strings $$$s$$$ and $$$t$$$ (defined as $$$LCM(s, t)$$$) is the shortest non-empty string that is divisible by both $$$s$$$ and $$$t$$$. You are given two strings $$$s$$$ and $$$t$$$. Find $$$LCM(s, t)$$$ or report that it does not exist. It can be shown that if $$$LCM(s, t)$$$ exists, it is unique. Input The first line contains one integer $$$q$$$ ($$$1 le q le 2000$$$) — the number of test cases. Each test case consists of two lines, containing strings $$$s$$$ and $$$t$$$ ($$$1 le s, t le 20$$$). Each character in each of these strings is either 'a' or 'b'. Output For each test case, print $$$LCM(s, t)$$$ if it exists; otherwise, print -1. It can be shown that if $$$LCM(s, t)$$$ exists, it is unique. Example Input 3 baba ba aa aaa aba ab Note In the first test case, "baba" = "baba" $$$cdot~1~=$$$ "ba" $$$cdot~2$$$. In the second test case, "aaaaaa" = "aa" $$$cdot~3~=$$$ "aaa" $$$cdot~2$$$.
1,000
true
false
false
false
false
false
true
false
false
false
3,316
37E
Having unraveled the Berland Dictionary, the scientists managed to read the notes of the chroniclers of that time. For example, they learned how the chief of the ancient Berland tribe was chosen. As soon as enough pretenders was picked, the following test took place among them: the chief of the tribe took a slab divided by horizontal and vertical stripes into identical squares (the slab consisted of _N_ lines and _M_ columns) and painted every square black or white. Then every pretender was given a slab of the same size but painted entirely white. Within a day a pretender could paint any side-linked set of the squares of the slab some color. The set is called linked if for any two squares belonging to the set there is a path belonging the set on which any two neighboring squares share a side. The aim of each pretender is to paint his slab in the exactly the same way as the chief’s slab is painted. The one who paints a slab like that first becomes the new chief. Scientists found the slab painted by the ancient Berland tribe chief. Help them to determine the minimal amount of days needed to find a new chief if he had to paint his slab in the given way. Input The first line contains two integers _N_ and _M_ (1u2009≤u2009_N_,u2009_M_u2009≤u200950) — the number of lines and columns on the slab. The next _N_ lines contain _M_ symbols each — the final coloration of the slab. _W_ stands for the square that should be painted white and _B_ — for the square that should be painted black. Output In the single line output the minimal number of repaintings of side-linked areas needed to get the required coloration of the slab.
2,600
false
true
false
false
false
false
false
false
false
true
9,809
1628E
Mihai lives in a town where meteor storms are a common problem. It's annoying, because Mihai has to buy groceries sometimes, and getting hit by meteors isn't fun. Therefore, we ask you to find the most dangerous way to buy groceries so that we can trick him to go there. The town has $$$n$$$ buildings numbered from $$$1$$$ to $$$n$$$. Some buildings have roads between them, and there is exactly $$$1$$$ simple path from any building to any other building. Each road has a certain meteor danger level. The buildings all have grocery stores, but Mihai only cares about the open ones, of course. Initially, all the grocery stores are closed. You are given $$$q$$$ queries of three types: 1. Given the integers $$$l$$$ and $$$r$$$, the buildings numbered from $$$l$$$ to $$$r$$$ open their grocery stores (nothing happens to buildings in the range that already have an open grocery store). 2. Given the integers $$$l$$$ and $$$r$$$, the buildings numbered from $$$l$$$ to $$$r$$$ close their grocery stores (nothing happens to buildings in the range that didn't have an open grocery store). 3. Given the integer $$$x$$$, find the maximum meteor danger level on the simple path from $$$x$$$ to any open grocery store, or $$$-1$$$ if there is no edge on any simple path to an open store. Input The first line contains the two integers $$$n$$$ and $$$q$$$ ($$$2 le n, q le 3cdot 10^5$$$). Then follows $$$n - 1$$$ lines, the $$$i$$$-th of which containing the integers $$$u_i$$$, $$$v_i$$$, and $$$w_i$$$ ($$$1 le u_i, v_i le n, enspace 1 le w_i le 10^9$$$) meaning there is two way road between building $$$u_i$$$ and $$$v_i$$$ with meteor danger level $$$w_i$$$. It is guaranteed that the given edges form a tree. Then follows $$$q$$$ lines, the $$$j$$$-th of which begin with the integer $$$t_j$$$ ($$$1 le t_j le 3$$$), meaning the $$$j$$$-th query is of the $$$t_j$$$-th type. If $$$t_j$$$ is $$$1$$$ or $$$2$$$ the rest of the line contains the integers $$$l_j$$$ and $$$r_j$$$ ($$$1 le l_j le r_j le n$$$). If $$$t_j$$$ is $$$3$$$ the rest of the line contains the integer $$$x_j$$$ ($$$1 le x_j le n$$$). Output For each query of the $$$3$$$rd type ($$$t_j = 3$$$), output the maximum meteor danger level that is on some edge on the simple path from $$$x_j$$$ to some open store, or $$$-1$$$ if there is no such edge. Example Input 6 9 1 3 1 2 3 2 4 5 3 4 6 4 3 4 5 3 1 1 1 1 3 1 2 1 1 1 5 6 3 4 2 6 6 3 4 3 1 Note This is an illustration of the town given in the sample input. In the first query, there are no open stores, so obviously there are no edges on the simple path from $$$1$$$ to any open store, so the answer is $$$-1$$$. After the second and third queries, the set of open stores is $$${1}$$$. The simple path from $$$1$$$ to $$$1$$$ has no edges, so the answer for the $$$3$$$rd query is $$$-1$$$. After the fourth query, there are no open stores. After the fifth and sixth queries, the set of open stores is $$${5, 6}$$$. In the sixth query, there are two paths from $$$x_j = 4$$$ to some open grocery store: $$$4$$$ to $$$5$$$ and $$$4$$$ to $$$6$$$. The biggest meteor danger is found on the edge from $$$4$$$ to $$$6$$$, so the answer for the $$$6$$$th query is $$$4$$$. This path is marked with red in the illustration. After the rest of the queries, the set of open stores is $$${5}$$$. In the eighth query, the only path from $$$x_j = 4$$$ to an open store is from $$$4$$$ to $$$5$$$, and the maximum weight on that path is $$$3$$$. This path is marked with green in the illustration. In the ninth query, the only path from $$$x_j = 1$$$ to an open store is from $$$1$$$ to $$$5$$$, and the maximum weight on that path is $$$5$$$. This path is marked with blue in the illustration.
3,100
false
false
false
false
true
false
false
true
false
false
2,470
637D
A sportsman starts from point _x__start_u2009=u20090 and runs to point with coordinate _x__finish_u2009=u2009_m_ (on a straight line). Also, the sportsman can jump — to jump, he should first take a run of length of not less than _s_ meters (in this case for these _s_ meters his path should have no obstacles), and after that he can jump over a length of not more than _d_ meters. Running and jumping is permitted only in the direction from left to right. He can start andfinish a jump only at the points with integer coordinates in which there are no obstacles. To overcome some obstacle, it is necessary to land at a point which is strictly to the right of this obstacle. On the way of an athlete are _n_ obstacles at coordinates _x_1,u2009_x_2,u2009...,u2009_x__n_. He cannot go over the obstacles, he can only jump over them. Your task is to determine whether the athlete will be able to get to the finish point. Input The first line of the input containsd four integers _n_, _m_, _s_ and _d_ (1u2009≤u2009_n_u2009≤u2009200u2009000, 2u2009≤u2009_m_u2009≤u2009109, 1u2009≤u2009_s_,u2009_d_u2009≤u2009109)xa0— the number of obstacles on the runner's way, the coordinate of the finishing point, the length of running before the jump and the maximum length of the jump, correspondingly. The second line contains a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_m_u2009-u20091)xa0— the coordinates of the obstacles. It is guaranteed that the starting and finishing point have no obstacles, also no point can have more than one obstacle, The coordinates of the obstacles are given in an arbitrary order. Output If the runner cannot reach the finishing point, print in the first line of the output "IMPOSSIBLE" (without the quotes). If the athlete can get from start to finish, print any way to do this in the following format: print a line of form "RUN X>" (where "X" should be a positive integer), if the athlete should run for "X" more meters; print a line of form "JUMP Y" (where "Y" should be a positive integer), if the sportsman starts a jump and should remain in air for "Y" more meters. All commands "RUN" and "JUMP" should strictly alternate, starting with "RUN", besides, they should be printed chronologically. It is not allowed to jump over the finishing point but it is allowed to land there after a jump. The athlete should stop as soon as he reaches finish. Examples Output RUN 2 JUMP 3 RUN 1 JUMP 2 RUN 2
1,600
false
true
false
true
true
false
false
false
false
false
7,262
550D
An undirected graph is called _k_-regular, if the degrees of all its vertices are equal _k_. An edge of a connected graph is called a bridge, if after removing it the graph is being split into two connected components. Build a connected undirected _k_-regular graph containing at least one bridge, or else state that such graph doesn't exist. Input The single line of the input contains integer _k_ (1u2009≤u2009_k_u2009≤u2009100) — the required degree of the vertices of the regular graph. Output Print "NO" (without quotes), if such graph doesn't exist. Otherwise, print "YES" in the first line and the description of any suitable graph in the next lines. The description of the made graph must start with numbers _n_ and _m_ — the number of vertices and edges respectively. Each of the next _m_ lines must contain two integers, _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_, _a_u2009≠u2009_b_), that mean that there is an edge connecting the vertices _a_ and _b_. A graph shouldn't contain multiple edges and edges that lead from a vertex to itself. A graph must be connected, the degrees of all vertices of the graph must be equal _k_. At least one edge of the graph must be a bridge. You can print the edges of the graph in any order. You can print the ends of each edge in any order. The constructed graph must contain at most 106 vertices and 106 edges (it is guaranteed that if at least one graph that meets the requirements exists, then there also exists the graph with at most 106 vertices and at most 106 edges). Note In the sample from the statement there is a suitable graph consisting of two vertices, connected by a single edge.
1,900
false
false
true
false
false
true
false
false
false
true
7,634
1236A
Alice is playing with some stones. Now there are three numbered heaps of stones. The first of them contains $$$a$$$ stones, the second of them contains $$$b$$$ stones and the third of them contains $$$c$$$ stones. Each time she can do one of two operations: 1. take one stone from the first heap and two stones from the second heap (this operation can be done only if the first heap contains at least one stone and the second heap contains at least two stones); 2. take one stone from the second heap and two stones from the third heap (this operation can be done only if the second heap contains at least one stone and the third heap contains at least two stones). She wants to get the maximum number of stones, but she doesn't know what to do. Initially, she has $$$0$$$ stones. Can you help her? Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 100$$$) xa0— the number of test cases. Next $$$t$$$ lines describe test cases in the following format: Line contains three non-negative integers $$$a$$$, $$$b$$$ and $$$c$$$, separated by spaces ($$$0 leq a,b,c leq 100$$$)xa0— the number of stones in the first, the second and the third heap, respectively. In hacks it is allowed to use only one test case in the input, so $$$t = 1$$$ should be satisfied. Output Print $$$t$$$ lines, the answers to the test cases in the same order as in the input. The answer to the test case is the integer xa0— the maximum possible number of stones that Alice can take after making some operations. Note For the first test case in the first test, Alice can take two stones from the second heap and four stones from the third heap, making the second operation two times. Then she can take one stone from the first heap and two stones from the second heap, making the first operation one time. The summary number of stones, that Alice will take is $$$9$$$. It is impossible to make some operations to take more than $$$9$$$ stones, so the answer is $$$9$$$.
800
true
true
false
false
false
false
true
false
false
false
4,518
1380B
Recently, you found a bot to play "Rock paper scissors" with. Unfortunately, the bot uses quite a simple algorithm to play: he has a string $$$s = s_1 s_2 dots s_{n}$$$ of length $$$n$$$ where each letter is either R, S or P. While initializing, the bot is choosing a starting index $$$pos$$$ ($$$1 le pos le n$$$), and then it can play any number of rounds. In the first round, he chooses "Rock", "Scissors" or "Paper" based on the value of $$$s_{pos}$$$: if $$$s_{pos}$$$ is equal to R the bot chooses "Rock"; if $$$s_{pos}$$$ is equal to S the bot chooses "Scissors"; if $$$s_{pos}$$$ is equal to P the bot chooses "Paper"; In the second round, the bot's choice is based on the value of $$$s_{pos + 1}$$$. In the third roundxa0— on $$$s_{pos + 2}$$$ and so on. After $$$s_n$$$ the bot returns to $$$s_1$$$ and continues his game. You plan to play $$$n$$$ rounds and you've already figured out the string $$$s$$$ but still don't know what is the starting index $$$pos$$$. But since the bot's tactic is so boring, you've decided to find $$$n$$$ choices to each round to maximize the average number of wins. In other words, let's suggest your choices are $$$c_1 c_2 dots c_n$$$ and if the bot starts from index $$$pos$$$ then you'll win in $$$win(pos)$$$ rounds. Find $$$c_1 c_2 dots c_n$$$ such that $$$frac{win(1) + win(2) + dots + win(n)}{n}$$$ is maximum possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Next $$$t$$$ lines contain test casesxa0— one per line. The first and only line of each test case contains string $$$s = s_1 s_2 dots s_{n}$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$s_i in { ext{R}, ext{S}, ext{P}}$$$)xa0— the string of the bot. It's guaranteed that the total length of all strings in one test doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print $$$n$$$ choices $$$c_1 c_2 dots c_n$$$ to maximize the average number of wins. Print them in the same manner as the string $$$s$$$. If there are multiple optimal answers, print any of them. Note In the first test case, the bot (wherever it starts) will always choose "Rock", so we can always choose "Paper". So, in any case, we will win all $$$n = 4$$$ rounds, so the average is also equal to $$$4$$$. In the second test case: if bot will start from $$$pos = 1$$$, then $$$(s_1, c_1)$$$ is draw, $$$(s_2, c_2)$$$ is draw and $$$(s_3, c_3)$$$ is draw, so $$$win(1) = 0$$$; if bot will start from $$$pos = 2$$$, then $$$(s_2, c_1)$$$ is win, $$$(s_3, c_2)$$$ is win and $$$(s_1, c_3)$$$ is win, so $$$win(2) = 3$$$; if bot will start from $$$pos = 3$$$, then $$$(s_3, c_1)$$$ is lose, $$$(s_1, c_2)$$$ is lose and $$$(s_2, c_3)$$$ is lose, so $$$win(3) = 0$$$; The average is equal to $$$frac{0 + 3 + 0}{3} = 1$$$ and it can be proven that it's the maximum possible average. A picture from Wikipedia explaining "Rock paper scissors" game:
1,400
false
true
false
false
false
false
false
false
false
false
3,770
1788E
You are given an array $$$a_1, a_2, ldots, a_n$$$ of $$$n$$$ integers. Consider $$$S$$$ as a set of segments satisfying the following conditions. Each element of $$$S$$$ should be in form $$$[x, y]$$$, where $$$x$$$ and $$$y$$$ are integers between $$$1$$$ and $$$n$$$, inclusive, and $$$x leq y$$$. No two segments in $$$S$$$ intersect with each other. Two segments $$$[a, b]$$$ and $$$[c, d]$$$ intersect if and only if there exists an integer $$$x$$$ such that $$$a leq x leq b$$$ and $$$c leq x leq d$$$. For each $$$[x, y]$$$ in $$$S$$$, $$$a_x+a_{x+1}+ ldots +a_y geq 0$$$. The length of the segment $$$[x, y]$$$ is defined as $$$y-x+1$$$. $$$f(S)$$$ is defined as the sum of the lengths of every element in $$$S$$$. In a formal way, $$$f(S) = sum_{[x, y] in S} (y - x + 1)$$$. Note that if $$$S$$$ is empty, $$$f(S)$$$ is $$$0$$$. What is the maximum $$$f(S)$$$ among all possible $$$S$$$? Input The first line contains one integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$). The next line is followed by $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$). Output Print a single integer, the maximum $$$f(S)$$$ among every possible $$$S$$$. Examples Input 10 5 -2 -4 -6 2 3 -6 5 3 -2 Note In the first example, $$$S={[1, 2], [4, 5]}$$$ can be a possible $$$S$$$ because $$$a_1+a_2=0$$$ and $$$a_4+a_5=1$$$. $$$S={[1, 4]}$$$ can also be a possible solution. Since there does not exist any $$$S$$$ that satisfies $$$f(S) > 4$$$, the answer is $$$4$$$. In the second example, $$$S={[1, 9]}$$$ is the only set that satisfies $$$f(S)=9$$$. Since every possible $$$S$$$ satisfies $$$f(S) leq 9$$$, the answer is $$$9$$$. In the third example, $$$S$$$ can only be an empty set, so the answer is $$$0$$$.
2,200
false
false
false
true
true
false
false
false
false
false
1,550
2013D
Zhan, tired after the contest, gave the only task that he did not solve during the contest to his friend, Sungat. However, he could not solve it either, so we ask you to try to solve this problem. You are given an array $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$. We can perform any number (possibly, zero) of operations on the array. In one operation, we choose a position $$$i$$$ ($$$1 leq i leq n - 1$$$) and perform the following action: $$$a_i := a_i - 1$$$, and $$$a_{i+1} := a_{i+1} + 1$$$. Find the minimum possible value of $$$max(a_1, a_2, ldots, a_n) - min(a_1, a_2, ldots, a_n)$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^{12}$$$). 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 possible value of $$$max(a_1, a_2, ldots, a_n) - min(a_1, a_2, ldots, a_n)$$$. Example Input 5 1 1 3 1 2 3 4 4 1 2 3 4 4 2 3 1 5 5 14 4 10 2 Note In the third testcase, you can perform the operation twice with $$$i = 1$$$. After that, the array is $$$a = [2, 3, 2, 3]$$$, and $$$max(2, 3, 2, 3) - min(2, 3, 2, 3) = 3 - 2 = 1$$$.
1,900
false
true
false
false
false
false
false
true
false
false
175
1234B1
The only difference between easy and hard versions are constraints on $$$n$$$ and $$$k$$$. You are messaging in one of the popular social networks via your smartphone. Your smartphone can show at most $$$k$$$ most recent conversations with your friends. Initially, the screen is empty (i.e. the number of displayed conversations equals $$$0$$$). Each conversation is between you and some of your friends. There is at most one conversation with any of your friends. So each conversation is uniquely defined by your friend. You (suddenly!) have the ability to see the future. You know that during the day you will receive $$$n$$$ messages, the $$$i$$$-th message will be received from the friend with ID $$$id_i$$$ ($$$1 le id_i le 10^9$$$). If you receive a message from $$$id_i$$$ in the conversation which is currently displayed on the smartphone then nothing happens: the conversations of the screen do not change and do not change their order, you read the message and continue waiting for new messages. Otherwise (i.e. if there is no conversation with $$$id_i$$$ on the screen): Firstly, if the number of conversations displayed on the screen is $$$k$$$, the last conversation (which has the position $$$k$$$) is removed from the screen. Now the number of conversations on the screen is guaranteed to be less than $$$k$$$ and the conversation with the friend $$$id_i$$$ is not displayed on the screen. The conversation with the friend $$$id_i$$$ appears on the first (the topmost) position on the screen and all the other displayed conversations are shifted one position down. Your task is to find the list of conversations (in the order they are displayed on the screen) after processing all $$$n$$$ messages. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 200)$$$ — the number of messages and the number of conversations your smartphone can show. The second line of the input contains $$$n$$$ integers $$$id_1, id_2, dots, id_n$$$ ($$$1 le id_i le 10^9$$$), where $$$id_i$$$ is the ID of the friend which sends you the $$$i$$$-th message. Output In the first line of the output print one integer $$$m$$$ ($$$1 le m le min(n, k)$$$) — the number of conversations shown after receiving all $$$n$$$ messages. In the second line print $$$m$$$ integers $$$ids_1, ids_2, dots, ids_m$$$, where $$$ids_i$$$ should be equal to the ID of the friend corresponding to the conversation displayed on the position $$$i$$$ after receiving all $$$n$$$ messages. Examples Input 10 4 2 3 3 1 1 2 1 2 3 3 Note In the first example the list of conversations will change in the following way (in order from the first to last message): $$$[]$$$; $$$[1]$$$; $$$[2, 1]$$$; $$$[3, 2]$$$; $$$[3, 2]$$$; $$$[1, 3]$$$; $$$[1, 3]$$$; $$$[2, 1]$$$. In the second example the list of conversations will change in the following way: $$$[]$$$; $$$[2]$$$; $$$[3, 2]$$$; $$$[3, 2]$$$; $$$[1, 3, 2]$$$; and then the list will not change till the end.
1,000
false
false
true
false
false
false
false
false
false
false
4,524
48F
The New Year celebrations in Berland last _n_ days. Only this year the winter is snowless, that’s why the winter celebrations’ organizers should buy artificial snow. There are _m_ snow selling companies in Berland. Every day the _i_-th company produces _w__i_ cubic meters of snow. Next day the snow thaws and the company has to produce _w__i_ cubic meters of snow again. During the celebration new year discounts are on, that’s why the snow cost decreases every day. It is known that on the first day the total cost of all the snow produced by the _i_-th company is equal to _c__i_ bourles. Every day this total cost decreases by _a__i_ bourles, i.e. on the second day it is equal to _c__i_u2009-u2009_a__i_,and on the third day — to _c__i_u2009-u20092_a__i_, and so on. It is known that for one company the cost of the snow produced by it does not get negative or equal to zero. You have to organize the snow purchase so as to buy every day exactly _W_ snow cubic meters. At that it is not necessary to buy from any company all the snow produced by it. If you buy _n__i_ cubic meters of snow (0u2009≤u2009_n__i_u2009≤u2009_w__i_, the number _n__i_ is not necessarily integer!) from the _i_-th company at one of the days when the cost of its snow is equal to _s__i_, then its price will total to bourles. During one day one can buy the snow from several companies. In different days one can buy the snow from different companies. It is required to make the purchases so as to spend as little money as possible. It is guaranteed that the snow produced by the companies will be enough. Input The first line contains integers _n_, _m_ and _W_ (1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u2009500000, 1u2009≤u2009_W_u2009≤u2009109) which represent the number of days, the number of companies and the amount of snow that needs to be purchased on every one of the _n_ days. The second line contains _m_ integers _w__i_. The third line contains _m_ integers _c__i_. The fourth line contains _m_ integers _a__i_. All the numbers are strictly positive and do not exceed 109. For all the _i_ the inequation _c__i_u2009-u2009(_n_u2009-u20091)_a__i_u2009>u20090 holds true. Output Print a single number — the answer to the given problem. Print the answer in the format with the decimal point (even if the answer is integer, it must contain the decimal point), without "e" and without leading zeroes. The answer should differ with the right one by no more than 10u2009-u20099. Examples Input 2 3 10 4 4 4 5 5 8 1 2 5 Input 100 2 1000000000 999999998 999999999 1000000000 1000000000 1 1 Output 99999995149.999995249999991
2,800
false
true
false
false
false
false
false
false
true
false
9,732
1374E1
, # Reading Books (easy version) Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Easy and hard versions are actually different problems, so read statements of both problems completely and carefully .Summer vacation has started so Alice and Bob want to play and joy, but... Their mom doesn’t think so. She says that they have to read some amount of books before all entertainments. Alice and Bob will read each book together to end this exercise faster. There are n books in the family library. The i-th book is described by three integers: ti x16 the amount of time Alice and Bob need to spend to read it, ai (equals 1 if Alice likes the i-th book and 0 if not), and bi (equals 1 if Bob likes the i-th book and 0 if not). So they need to choose some books from the given n books in such a way that: • Alice likes at least k books from the chosen set and Bob likes at least k books from the chosen set; • the total reading time of these books is minimized (they are children and want to play and joy as soon a possible). The set they choose is the same for both Alice an Bob (it’s shared between them) and they read all books together , so the total reading time is the sum of ti over all books that are in the chosen set. Your task is to help them and find any suitable set of books or determine that it is impossible to find such a set. # Input The first line of the input contains two integers n and k (1 ≤ k ≤ n ≤ 2 · 10 5). The next n lines contain descriptions of books, one description per line: the i-th line contains three integers ti, ai and bi (1 ≤ ti ≤ 10 4, 0 ≤ ai, b i ≤ 1), where: • ti x16 the amount of time required for reading the i-th book; • ai equals 1 if Alice likes the i-th book and 0 otherwise; • bi equals 1 if Bob likes the i-th book and 0 otherwise. # Output If there is no solution, print only one integer -1 . Otherwise print one integer T x16 the minimum total reading time of the suitable set of books. Page 1 of 2 , # Examples standard input standard output 8 4 7 1 1 2 1 1 4 0 1 8 1 1 1 0 1 1 1 1 1 0 1 3 0 0 18 5 2 6 0 0 9 0 0 1 0 1 2 1 1 5 1 0 85 3 3 0 0 2 1 0 3 1 0 5 0 1 3 0 1 -1 Page 2 of 2
1,600
false
true
false
false
true
false
false
false
true
false
3,790
1225D
Problem - 1225D - Codeforces =============== xa0 . The second line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 leq a_i leq 10^5$$$). Output Print a single integerxa0— the number of suitable pairs. Example Input 6 3 1 3 9 8 24 1 Output 5 Note In the sample case, the suitable pairs are: $$$a_1 cdot a_4 = 8 = 2^3$$$; $$$a_1 cdot a_6 = 1 = 1^3$$$; $$$a_2 cdot a_3 = 27 = 3^3$$$; $$$a_3 cdot a_5 = 216 = 6^3$$$; $$$a_4 cdot a_6 = 8 = 2^3$$$.
1,800
true
false
false
false
false
false
false
false
false
false
4,548
1023B
Tanechka is shopping in the toy shop. There are exactly $$$n$$$ toys in the shop for sale, the cost of the $$$i$$$-th toy is $$$i$$$ burles. She wants to choose two toys in such a way that their total cost is $$$k$$$ burles. How many ways to do that does she have? Each toy appears in the shop exactly once. Pairs $$$(a, b)$$$ and $$$(b, a)$$$ are considered equal. Pairs $$$(a, b)$$$, where $$$a=b$$$, are not allowed. Input The first line of the input contains two integers $$$n$$$, $$$k$$$ ($$$1 le n, k le 10^{14}$$$) — the number of toys and the expected total cost of the pair of toys. Output Print the number of ways to choose the pair of toys satisfying the condition above. Print 0, if Tanechka can choose no pair of toys in such a way that their total cost is $$$k$$$ burles. Examples Input 1000000000000 1000000000001 Note In the first example Tanechka can choose the pair of toys ($$$1, 4$$$) or the pair of toys ($$$2, 3$$$). In the second example Tanechka can choose only the pair of toys ($$$7, 8$$$). In the third example choosing any pair of toys will lead to the total cost less than $$$20$$$. So the answer is 0. In the fourth example she can choose the following pairs: $$$(1, 1000000000000)$$$, $$$(2, 999999999999)$$$, $$$(3, 999999999998)$$$, ..., $$$(500000000000, 500000000001)$$$. The number of such pairs is exactly $$$500000000000$$$.
1,000
true
false
false
false
false
false
false
false
false
false
5,589
237E
You desperately need to build some string _t_. For that you've got _n_ more strings _s_1,u2009_s_2,u2009...,u2009_s__n_. To build string _t_, you are allowed to perform exactly _t_ (_t_ is the length of string _t_) operations on these strings. Each operation looks like that: 1. choose any non-empty string from strings _s_1,u2009_s_2,u2009...,u2009_s__n_; 2. choose an arbitrary character from the chosen string and write it on a piece of paper; 3. remove the chosen character from the chosen string. Note that after you perform the described operation, the total number of characters in strings _s_1,u2009_s_2,u2009...,u2009_s__n_ decreases by 1. We are assumed to build string _t_, if the characters, written on the piece of paper, in the order of performed operations form string _t_. There are other limitations, though. For each string _s__i_ you know number _a__i_ — the maximum number of characters you are allowed to delete from string _s__i_. You also know that each operation that results in deleting a character from string _s__i_, costs _i_ rubles. That is, an operation on string _s_1 is the cheapest (it costs 1 ruble), and the operation on string _s__n_ is the most expensive one (it costs _n_ rubles). Your task is to count the minimum amount of money (in rubles) you will need to build string _t_ by the given rules. Consider the cost of building string _t_ to be the sum of prices of the operations you use. Input The first line of the input contains string _t_ — the string that you need to build. The second line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of strings to which you are allowed to apply the described operation. Each of the next _n_ lines contains a string and an integer. The _i_-th line contains space-separated string _s__i_ and integer _a__i_ (0u2009≤u2009_a__i_u2009≤u2009100). Number _a__i_ represents the maximum number of characters that can be deleted from string _s__i_. All strings in the input only consist of lowercase English letters. All strings are non-empty. The lengths of all strings do not exceed 100 characters. Note Notes to the samples: In the first sample from the first string you should take characters "b" and "z" with price 1 ruble, from the second string characters "a", "e" и "b" with price 2 rubles. The price of the string _t_ in this case is 2·1u2009+u20093·2u2009=u20098. In the second sample from the first string you should take two characters "a" with price 1 ruble, from the second string character "c" with price 2 rubles, from the third string two characters "a" with price 3 rubles, from the fourth string two characters "b" with price 4 rubles. The price of the string _t_ in this case is 2·1u2009+u20091·2u2009+u20092·3u2009+u20092·4u2009=u200918. In the third sample the solution doesn't exist because there is no character "y" in given strings.
2,000
false
false
false
false
false
false
false
false
false
true
8,885