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
7B
There is little time left before the release of the first national operating system BerlOS. Some of its components are not finished yet — the memory manager is among them. According to the developers' plan, in the first release the memory manager will be very simple and rectilinear. It will support three operations: alloc n — to allocate _n_ bytes of the memory and return the allocated block's identifier _x_; erase x — to erase the block with the identifier _x_; defragment — to defragment the free memory, bringing all the blocks as close to the beginning of the memory as possible and preserving their respective order; The memory model in this case is very simple. It is a sequence of _m_ bytes, numbered for convenience from the first to the _m_-th. The first operation alloc n takes as the only parameter the size of the memory block that is to be allocated. While processing this operation, a free block of _n_ successive bytes is being allocated in the memory. If the amount of such blocks is more than one, the block closest to the beginning of the memory (i.e. to the first byte) is prefered. All these bytes are marked as not free, and the memory manager returns a 32-bit integer numerical token that is the identifier of this block. If it is impossible to allocate a free block of this size, the function returns NULL. The second operation erase x takes as its parameter the identifier of some block. This operation frees the system memory, marking the bytes of this block as free for further use. In the case when this identifier does not point to the previously allocated block, which has not been erased yet, the function returns ILLEGAL_ERASE_ARGUMENT. The last operation defragment does not have any arguments and simply brings the occupied memory sections closer to the beginning of the memory without changing their respective order. In the current implementation you are to use successive integers, starting with 1, as identifiers. Each successful alloc operation procession should return following number. Unsuccessful alloc operations do not affect numeration. You are to write the implementation of the memory manager. You should output the returned value for each alloc command. You should also output ILLEGAL_ERASE_ARGUMENT for all the failed erase commands. Input The first line of the input data contains two positive integers _t_ and _m_ (1u2009≤u2009_t_u2009≤u2009100;1u2009≤u2009_m_u2009≤u2009100), where _t_ — the amount of operations given to the memory manager for processing, and _m_ — the available memory size in bytes. Then there follow _t_ lines where the operations themselves are given. The first operation is alloc n (1u2009≤u2009_n_u2009≤u2009100), where _n_ is an integer. The second one is erase x, where _x_ is an arbitrary 32-bit integer numerical token. The third operation is defragment. Output Output the sequence of lines. Each line should contain either the result of alloc operation procession , or ILLEGAL_ERASE_ARGUMENT as a result of failed erase operation procession. Output lines should go in the same order in which the operations are processed. Successful procession of alloc operation should return integers, starting with 1, as the identifiers of the allocated blocks.
1,600
false
false
true
false
false
false
false
false
false
false
9,959
1977E
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 948 (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 graphs interactive *2600 No tag edit access → Contest materials Announcement Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E. Tensor time limit per test3 seconds memory limit per test256 megabytes This is an interactive problem. You are given an integer $$$n$$$. The jury has hidden from you a directed graph with $$$n$$$ vertices (numbered from $$$1$$$ to $$$n$$$) and some number of edges. You additionally know that: The graph only contains edges of the form $$$i leftarrow j$$$, where $$$1 le i < j le n$$$. For any three vertices $$$1 le i < j < k le n$$$, at least one of the following holds$$$^dagger$$$: Vertex $$$i$$$ is reachable from vertex $$$j$$$, or Vertex $$$i$$$ is reachable from vertex $$$k$$$, or Vertex $$$j$$$ is reachable from vertex $$$k$$$. You want to color each vertex in either black or white such that for any two vertices $$$i$$$ and $$$j$$$ ($$$1 le i < j le n$$$) of the same color, vertex $$$i$$$ is reachable from vertex $$$j$$$. To do that, you can ask queries of the following type: ? i j — is vertex $$$i$$$ reachable from vertex $$$j$$$ ($$$1 le i < j le n$$$)? Find any valid vertex coloring of the hidden graph in at most $$$2 cdot n$$$ queries. It can be proven that such a coloring always exists. Note that the grader is not adaptive: the graph is fixed before any queries are made. $$$^dagger$$$ Vertex $$$a$$$ is reachable from vertex $$$b$$$ if there exists a path from vertex $$$b$$$ to vertex $$$a$$$ in the graph. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$3 le n le 100$$$)xa0— the number of vertices in the hidden graph. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Interaction The interaction for each test case begins by reading the integer $$$n$$$. To make a query, output "? i j" without quotes ($$$1 le i < j le n$$$). If vertex $$$i$$$ is reachable from vertex $$$j$$$, you will get YES as an answer. Otherwise, you will get NO as an answer. If you receive the integer $$$-1$$$ instead of an answer or a valid value of $$$n$$$, it means your program has made an invalid query, has exceeded the limit of queries, or has given an incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. When you are ready to give the final answer, output "! $$$c_1 c_2 ldots c_n$$$" without quotesxa0— the colors of the vertices, where $$$c_i = 0$$$ if the vertex is black, and $$$c_i = 1$$$ if the vertex is white. After solving all test cases, your program should be terminated immediately. After printing a query, do not forget to output an end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacks To hack, use the following format: The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n le 100$$$, $$$0 le m le frac{ncdot(n - 1)}{2}$$$)xa0— the number of vertices and edges in the graph. Each of the following $$$m$$$ lines should contain two integers $$$a$$$ and $$$b$$$ ($$$1 le b < a le n$$$), indicating that there is the edge $$$a ightarrow b$$$ in the graph. The graph should satisfy the conditions above. The sum of $$$n$$$ over all test cases should not exceed $$$1000$$$. Example input 2 4 YES YES YES NO NO NO 5 output ? 1 2 ? 2 3 ? 1 3 ? 1 4 ? 2 4 ? 3 4 ! 0 0 0 1 ! 1 1 0 1 0 Note The hidden graph in the first test case: The hidden graph in the second test case: The interaction happens as follows: Solution Jury Explanation 2 There are $$$2$$$ test cases. 4 In the first test case, the graph has $$$4$$$ vertices. ? 1 2 YES The solution asks if vertex $$$1$$$ is reachable from vertex $$$2$$$, and the jury answers YES. ? 2 3 YES The solution asks if vertex $$$2$$$ is reachable from vertex $$$3$$$, and the jury answers YES. ? 1 3 YES The solution asks if vertex $$$1$$$ is reachable from vertex $$$3$$$, and the jury answers YES. ? 1 4 NO The solution asks if vertex $$$1$$$ is reachable from vertex $$$4$$$, and the jury answers NO. ? 2 4 NO The solution asks if vertex $$$2$$$ is reachable from vertex $$$4$$$, and the jury answers NO. ? 3 4 NO The solution asks if vertex $$$3$$$ is reachable from vertex $$$4$$$, and the jury answers NO. ! 0 0 0 1 The solution has somehow determined a valid coloring and outputs it. Since the output is correct, the jury continues to the next test case. 5 In the second test case, the graph has $$$5$$$ vertices. ! 1 1 0 1 0 The solution has somehow determined a valid coloring, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction. Codeforces (c)
2,600
false
false
false
false
false
true
false
false
false
true
423
81C
After the educational reform Polycarp studies only two subjects at school, Safety Studies and PE (Physical Education). During the long months of the fourth term, he received _n_ marks in them. When teachers wrote a mark in the journal, they didn't write in what subject the mark was for, they just wrote the mark. Now it's time to show the journal to his strict parents. Polycarp knows that recently at the Parent Meeting the parents were told that he received _a_ Safety Studies marks and _b_ PE marks (_a_u2009+u2009_b_u2009=u2009_n_). Now Polycarp wants to write a subject's name in front of each mark so that: there are exactly _a_ Safety Studies marks, there are exactly _b_ PE marks, the total average score in both subjects is maximum. An average subject grade is the sum of all marks in it, divided by the number of them. Of course, the division is performed in real numbers without rounding up or down. Polycarp aims to maximize the _x_1u2009+u2009_x_2, where _x_1 is the average score in the first subject (Safety Studies), and _x_2 is the average score in the second one (Physical Education). Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009105), _n_ is the number of marks in Polycarp's Journal. The second line contains two positive integers _a_,u2009_b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_u2009-u20091,u2009_a_u2009+u2009_b_u2009=u2009_n_). The third line contains a sequence of integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u20095), they are Polycarp's marks. Output Print the sequence of integers _f_1,u2009_f_2,u2009...,u2009_f__n_, where _f__i_ (1u2009≤u2009_f__i_u2009≤u20092) is the number of a subject to which the _i_-th mark should be attributed. If there are several possible solutions, then print such that the sequence _f_1,u2009_f_2,u2009...,u2009_f__n_ is the smallest lexicographically. The sequence _p_1,u2009_p_2,u2009...,u2009_p__n_ is lexicographically less than _q_1,u2009_q_2,u2009...,u2009_q__n_ if there exists such _j_ (1u2009≤u2009_j_u2009≤u2009_n_) that _p__i_u2009=u2009_q__i_ for all 1u2009≤u2009_i_u2009<u2009_j_, аnd _p__j_u2009<u2009_q__j_. Note In the first sample the average score in the first subject is equal to 4, and in the second one — to 4.5. The total average score is 8.5.
1,700
true
true
false
false
false
false
false
false
true
false
9,561
1399E1
Easy and hard versions are actually different problems, so we advise you to read both statements carefully. You are given a weighted rooted tree, vertex $$$1$$$ is the root of this tree. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. A parent of a vertex $$$v$$$ is the last different from $$$v$$$ vertex on the path from the root to the vertex $$$v$$$. Children of vertex $$$v$$$ are all vertices for which $$$v$$$ is the parent. A vertex is a leaf if it has no children. The weighted tree is such a tree that each edge of this tree has some weight. The weight of the path is the sum of edges weights on this path. The weight of the path from the vertex to itself is $$$0$$$. You can make a sequence of zero or more moves. On each move, you select an edge and divide its weight by $$$2$$$ rounding down. More formally, during one move, you choose some edge $$$i$$$ and divide its weight by $$$2$$$ rounding down ($$$w_i := leftlfloorfrac{w_i}{2} ight floor$$$). Your task is to find the minimum number of moves required to make the sum of weights of paths from the root to each leaf at most $$$S$$$. In other words, if $$$w(i, j)$$$ is the weight of the path from the vertex $$$i$$$ to the vertex $$$j$$$, then you have to make $$$sumlimits_{v in leaves} w(root, v) le S$$$, where $$$leaves$$$ is the list of all leaves. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains two integers $$$n$$$ and $$$S$$$ ($$$2 le n le 10^5; 1 le S le 10^{16}$$$) — the number of vertices in the tree and the maximum possible sum of weights you have to obtain. The next $$$n-1$$$ lines describe edges of the tree. The edge $$$i$$$ is described as three integers $$$v_i$$$, $$$u_i$$$ and $$$w_i$$$ ($$$1 le v_i, u_i le n; 1 le w_i le 10^6$$$), where $$$v_i$$$ and $$$u_i$$$ are vertices the edge $$$i$$$ connects and $$$w_i$$$ is the weight of this edge. It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$ ($$$sum n le 10^5$$$).
2,000
false
true
false
false
true
false
false
false
false
false
3,675
766C
Mahmoud wrote a message _s_ of length _n_. He wants to send it as a birthday present to his friend Moaz who likes strings. He wrote it on a magical paper but he was surprised because some characters disappeared while writing the string. That's because this magical paper doesn't allow character number _i_ in the English alphabet to be written on it in a string of length more than _a__i_. For example, if _a_1u2009=u20092 he can't write character 'a' on this paper in a string of length 3 or more. String "aa" is allowed while string "aaa" is not. Mahmoud decided to split the message into some non-empty substrings so that he can write every substring on an independent magical paper and fulfill the condition. The sum of their lengths should be _n_ and they shouldn't overlap. For example, if _a_1u2009=u20092 and he wants to send string "aaa", he can split it into "a" and "aa" and use 2 magical papers, or into "a", "a" and "a" and use 3 magical papers. He can't split it into "aa" and "aa" because the sum of their lengths is greater than _n_. He can split the message into single string if it fulfills the conditions. A substring of string _s_ is a string that consists of some consecutive characters from string _s_, strings "ab", "abc" and "b" are substrings of string "abc", while strings "acb" and "ac" are not. Any string is a substring of itself. While Mahmoud was thinking of how to split the message, Ehab told him that there are many ways to split it. After that Mahmoud asked you three questions: How many ways are there to split the string into substrings such that every substring fulfills the condition of the magical paper, the sum of their lengths is _n_ and they don't overlap? Compute the answer modulo 109u2009+u20097. What is the maximum length of a substring that can appear in some valid splitting? What is the minimum number of substrings the message can be spit in? Two ways are considered different, if the sets of split positions differ. For example, splitting "aaa" and "aaa" are considered different splittings of message "aaa". Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009103) denoting the length of the message. The second line contains the message _s_ of length _n_ that consists of lowercase English letters. The third line contains 26 integers _a_1,u2009_a_2,u2009...,u2009_a_26 (1u2009≤u2009_a__x_u2009≤u2009103)xa0— the maximum lengths of substring each letter can appear in. Output Print three lines. In the first line print the number of ways to split the message into substrings and fulfill the conditions mentioned in the problem modulo 109u2009u2009+u2009u20097. In the second line print the length of the longest substring over all the ways. In the third line print the minimum number of substrings over all the ways. Note In the first example the three ways to split the message are: aab aab aab The longest substrings are "aa" and "ab" of length 2. The minimum number of substrings is 2 in "aab" or "aab". Notice that "aab" is not a possible splitting because the letter 'a' appears in a substring of length 3, while _a_1u2009=u20092.
1,700
false
true
false
true
false
false
true
false
false
false
6,721
1238A
You are given two integers $$$x$$$ and $$$y$$$ (it is guaranteed that $$$x > y$$$). You may choose any prime integer $$$p$$$ and subtract it any number of times from $$$x$$$. Is it possible to make $$$x$$$ equal to $$$y$$$? Recall that a prime number is a positive integer that has exactly two positive divisors: $$$1$$$ and this integer itself. The sequence of prime numbers starts with $$$2$$$, $$$3$$$, $$$5$$$, $$$7$$$, $$$11$$$. Your program should solve $$$t$$$ independent test cases. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then $$$t$$$ lines follow, each describing a test case. Each line contains two integers $$$x$$$ and $$$y$$$ ($$$1 le y < x le 10^{18}$$$). Output For each test case, print YES if it is possible to choose a prime number $$$p$$$ and subtract it any number of times from $$$x$$$ so that $$$x$$$ becomes equal to $$$y$$$. Otherwise, print NO. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answer). Example Input 4 100 98 42 32 1000000000000000000 1 41 40 Note In the first test of the example you may choose $$$p = 2$$$ and subtract it once. In the second test of the example you may choose $$$p = 5$$$ and subtract it twice. Note that you cannot choose $$$p = 7$$$, subtract it, then choose $$$p = 3$$$ and subtract it again. In the third test of the example you may choose $$$p = 3$$$ and subtract it $$$333333333333333333$$$ times.
900
true
false
false
false
false
false
false
false
false
false
4,503
1373E
Problem - 1373E - 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 constructive algorithms dp greedy *2200 No tag edit access → Contest materials $$$ be the sum of digits of a decimal number $$$x$$$. Find the smallest non-negative integer $$$x$$$ such that $$$f(x) + f(x + 1) + dots + f(x + k) = n$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 150$$$) — the number of test cases. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 150$$$, $$$0 le k le 9$$$). Output For each test case, print one integer without leading zeroes. If there is no such $$$x$$$ that $$$f(x) + f(x + 1) + dots + f(x + k) = n$$$, print $$$-1$$$; otherwise, print the minimum $$$x$$$ meeting that constraint. Example Input 7 1 0 1 1 42 7 13 7 99 1 99 0 99 2 Output 1 0 4 -1 599998 99999999999 7997
2,200
false
true
false
true
false
true
true
false
false
false
3,797
1438C
Artem is building a new robot. He has a matrix $$$a$$$ consisting of $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{i,j}$$$ written in it. If two adjacent cells contain the same value, the robot will break. A matrix is called good if no two adjacent cells contain the same value, where two cells are called adjacent if they share a side. Artem wants to increment the values in some cells by one to make $$$a$$$ good. More formally, find a good matrix $$$b$$$ that satisfies the following conditionxa0— For all valid ($$$i,j$$$), either $$$b_{i,j} = a_{i,j}$$$ or $$$b_{i,j} = a_{i,j}+1$$$. For the constraints of this problem, it can be shown that such a matrix $$$b$$$ always exists. If there are several such tables, you can output any of them. Please note that you do not have to minimize the number of increments. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 le n le 100$$$, $$$1 le m le 100$$$) xa0— the number of rows and columns, respectively. The following $$$n$$$ lines each contain $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$a_{i,j}$$$ ($$$1 leq a_{i,j} leq 10^9$$$). Output For each case, output $$$n$$$ lines each containing $$$m$$$ integers. The $$$j$$$-th integer in the $$$i$$$-th line is $$$b_{i,j}$$$. Example Input 3 3 2 1 2 4 5 7 8 2 2 1 1 3 3 2 2 1 3 2 2 Output 1 2 5 6 7 8 2 1 4 3 2 4 3 2 Note In all the cases, you can verify that no two adjacent cells have the same value and that $$$b$$$ is the same as $$$a$$$ with some values incremented by one.
2,000
false
false
false
false
false
true
false
false
false
false
3,476
1073G
Let $$$ ext{LCP}(s, t)$$$ be the length of the longest common prefix of strings $$$s$$$ and $$$t$$$. Also let $$$s[x dots y]$$$ be the substring of $$$s$$$ from index $$$x$$$ to index $$$y$$$ (inclusive). For example, if $$$s = $$$ "abcde", then $$$s[1 dots 3] =$$$ "abc", $$$s[2 dots 5] =$$$ "bcde". You are given a string $$$s$$$ of length $$$n$$$ and $$$q$$$ queries. Each query is a pair of integer sets $$$a_1, a_2, dots, a_k$$$ and $$$b_1, b_2, dots, b_l$$$. Calculate $$$sumlimits_{i = 1}^{i = k} sumlimits_{j = 1}^{j = l}{ ext{LCP}(s[a_i dots n], s[b_j dots n])}$$$ for each query. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$) — the length of string $$$s$$$ and the number of queries, respectively. The second line contains a string $$$s$$$ consisting of lowercase Latin letters ($$$s = n$$$). Next $$$3q$$$ lines contains descriptions of queries — three lines per query. The first line of each query contains two integers $$$k_i$$$ and $$$l_i$$$ ($$$1 le k_i, l_i le n$$$) — sizes of sets $$$a$$$ and $$$b$$$ respectively. The second line of each query contains $$$k_i$$$ integers $$$a_1, a_2, dots a_{k_i}$$$ ($$$1 le a_1 < a_2 < dots < a_{k_i} le n$$$) — set $$$a$$$. The third line of each query contains $$$l_i$$$ integers $$$b_1, b_2, dots b_{l_i}$$$ ($$$1 le b_1 < b_2 < dots < b_{l_i} le n$$$) — set $$$b$$$. It is guaranteed that $$$sumlimits_{i = 1}^{i = q}{k_i} le 2 cdot 10^5$$$ and $$$sumlimits_{i = 1}^{i = q}{l_i} le 2 cdot 10^5$$$. Output Print $$$q$$$ integers — answers for the queries in the same order queries are given in the input. Example Input 7 4 abacaba 2 2 1 2 1 2 3 1 1 2 3 7 1 7 1 1 2 3 4 5 6 7 2 2 1 5 1 5 Note Description of queries: 1. In the first query $$$s[1 dots 7] = ext{abacaba}$$$ and $$$s[2 dots 7] = ext{bacaba}$$$ are considered. The answer for the query is $$$ ext{LCP}( ext{abacaba}, ext{abacaba}) + ext{LCP}( ext{abacaba}, ext{bacaba}) + ext{LCP}( ext{bacaba}, ext{abacaba}) + ext{LCP}( ext{bacaba}, ext{bacaba}) = 7 + 0 + 0 + 6 = 13$$$. 2. In the second query $$$s[1 dots 7] = ext{abacaba}$$$, $$$s[2 dots 7] = ext{bacaba}$$$, $$$s[3 dots 7] = ext{acaba}$$$ and $$$s[7 dots 7] = ext{a}$$$ are considered. The answer for the query is $$$ ext{LCP}( ext{abacaba}, ext{a}) + ext{LCP}( ext{bacaba}, ext{a}) + ext{LCP}( ext{acaba}, ext{a}) = 1 + 0 + 1 = 2$$$. 3. In the third query $$$s[1 dots 7] = ext{abacaba}$$$ are compared with all suffixes. The answer is the sum of non-zero values: $$$ ext{LCP}( ext{abacaba}, ext{abacaba}) + ext{LCP}( ext{abacaba}, ext{acaba}) + ext{LCP}( ext{abacaba}, ext{aba}) + ext{LCP}( ext{abacaba}, ext{a}) = 7 + 1 + 3 + 1 = 12$$$. 4. In the fourth query $$$s[1 dots 7] = ext{abacaba}$$$ and $$$s[5 dots 7] = ext{aba}$$$ are considered. The answer for the query is $$$ ext{LCP}( ext{abacaba}, ext{abacaba}) + ext{LCP}( ext{abacaba}, ext{aba}) + ext{LCP}( ext{aba}, ext{abacaba}) + ext{LCP}( ext{aba}, ext{aba}) = 7 + 3 + 3 + 3 = 16$$$.
2,600
false
false
false
false
true
false
false
false
false
false
5,347
1863A
Petya is an administrator of a channel in one of the messengers. A total of $$$n$$$ people are subscribed to his channel, and Petya is not considered a subscriber. Petya has published a new post on the channel. At the moment of the publication, there were $$$a$$$ subscribers online. We assume that every subscriber always reads all posts in the channel if they are online. After this, Petya starts monitoring the number of subscribers online. He consecutively receives $$$q$$$ notifications of the form "a subscriber went offline" or "a subscriber went online". Petya does not know which exact subscriber goes online or offline. It is guaranteed that such a sequence of notifications could have indeed been received. Petya wonders if all of his subscribers have read the new post. Help him by determining one of the following: it is impossible that all $$$n$$$ subscribers have read the post; it is possible that all $$$n$$$ subscribers have read the post; it is guaranteed that all $$$n$$$ subscribers have read the post. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$a$$$, and $$$q$$$ ($$$1 le n le 100$$$, $$$0 le a le n$$$, $$$1 le q le 100$$$)xa0— the number of subscribers of the channel, the initial number of subscribers online, and the number of notifications. The second line of each test case contains a string of length $$$q$$$, consisting of characters '+' and '-'. The $$$i$$$-th of these characters is '+', if the $$$i$$$-th notification tells that a subscriber goes online, and it is '-' otherwise. Output For each test case, output a single line: "YES" if all $$$n$$$ subscribers are guaranteed to have read the post, "NO" if it is impossible for all $$$n$$$ subscribers to have read the post, and "MAYBE" otherwise. Example Input 4 5 5 3 --+ 5 2 3 ++- 5 4 2 -+ 5 0 7 ++++-++ Note In the first test case, there are $$$5$$$ out of $$$5$$$ subscribers online in the very beginning, so they will all read the post no matter what. The answer is "YES". In the second test case, the number of subscribers online becomes $$$4$$$ after the first two notifications, next someone goes offline, and thus the fifth subscriber has no chance of reading the post. It is impossible for all the subscribers to read the post, so the answer is "NO". In the third test case, on the one hand, the same person may have gone offline and online (in this case only $$$4$$$ out of $$$5$$$ subscribers have read the post), on the other hand, the last notification may have told that the fifth subscriber has gone online (in this case all subscribers have read the post). We cannot deduce which of the two holds, so the answer is "MAYBE". In the fourth test case, there have to be five subscribers online after all the notifications. All of them will read the post, so the answer is "YES".
800
false
true
true
false
false
false
false
false
false
false
1,106
682B
Someone gave Alyona an array containing _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_. In one operation, Alyona can choose any element of the array and decrease it, i.e. replace with any positive integer that is smaller than the current one. Alyona can repeat this operation as many times as she wants. In particular, she may not apply any operation to the array at all. Formally, after applying some operations Alyona will get an array of _n_ positive integers _b_1,u2009_b_2,u2009...,u2009_b__n_ such that 1u2009≤u2009_b__i_u2009≤u2009_a__i_ for every 1u2009≤u2009_i_u2009≤u2009_n_. Your task is to determine the maximum possible value of mex of this array. Mex of an array in this problem is the minimum positive integer that doesn't appear in this array. For example, mex of the array containing 1, 3 and 4 is equal to 2, while mex of the array containing 2, 3 and 2 is equal to 1. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of elements in the Alyona's array. The second line of the input contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the elements of the array. Output Print one positive integerxa0— the maximum possible value of mex of the array after Alyona applies some (possibly none) operations. Note In the first sample case if one will decrease the second element value to 2 and the fifth element value to 4 then the mex value of resulting array 1 2 3 3 4 will be equal to 5. To reach the answer to the second sample case one must not decrease any of the array elements.
1,200
false
false
false
false
false
false
false
false
true
false
7,097
255D
Problem - 255D - 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 implementation math *1800 No tag edit access → Contest materials (_x_ is the row number, _y_ is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on. For a cell with coordinates (_x_,u2009_y_) the side-adjacent cells are cells with coordinates (_x_u2009-u20091,u2009_y_), (_x_u2009+u20091,u2009_y_), (_x_,u2009_y_u2009-u20091), (_x_,u2009_y_u2009+u20091). In how many seconds will Mr. Bender get happy? Input The first line contains four space-separated integers _n_,u2009_x_,u2009_y_,u2009_c_ (1u2009≤u2009_n_,u2009_c_u2009≤u2009109;xa01u2009≤u2009_x_,u2009_y_u2009≤u2009_n_;xa0_c_u2009≤u2009_n_2). Output In a single line print a single integer — the answer to the problem. Examples Input 6 4 3 1 Output 0 Input 9 3 8 10 Output 2 Note Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. .
1,800
true
false
true
false
false
false
false
true
false
false
8,811
39E
Problem - 39E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp games *2000 No tag edit access → Contest materials ") : Tutorial") : Problem K Tutorial") — the initial number of the boxes, the number of the items and the number which constrains the number of ways, respectively. Guaranteed that the initial number of ways is strictly less than _n_. Output Output "Stas" if Masha wins. Output "Masha" if Stas wins. In case of a draw, output "Missing". Examples Input 2 2 10 Output Masha Input 5 5 16808 Output Masha Input 3 1 4 Output Stas Input 1 4 10 Output Missing Note In the second example the initial number of ways is equal to 3125. If Stas increases the number of boxes, he will lose, as Masha may increase the number of boxes once more during her turn. After that any Stas's move will lead to defeat. But if Stas increases the number of items, then any Masha's move will be losing.
2,000
false
false
false
true
false
false
false
false
false
false
9,796
1898C
Elena has a grid formed by $$$n$$$ horizontal lines and $$$m$$$ vertical lines. The horizontal lines are numbered by integers from $$$1$$$ to $$$n$$$ from top to bottom. The vertical lines are numbered by integers from $$$1$$$ to $$$m$$$ from left to right. For each $$$x$$$ and $$$y$$$ ($$$1 leq x leq n$$$, $$$1 leq y leq m$$$), the notation $$$(x, y)$$$ denotes the point at the intersection of the $$$x$$$-th horizontal line and $$$y$$$-th vertical line. Two points $$$(x_1,y_1)$$$ and $$$(x_2,y_2)$$$ are adjacent if and only if $$$x_1-x_2 + y_1-y_2 = 1$$$. The grid formed by $$$n=4$$$ horizontal lines and $$$m=5$$$ vertical lines. Elena calls a sequence of points $$$p_1, p_2, ldots, p_g$$$ of length $$$g$$$ a walk if and only if all the following conditions hold: The first point $$$p_1$$$ in this sequence is $$$(1, 1)$$$. The last point $$$p_g$$$ in this sequence is $$$(n, m)$$$. For each $$$1 le i < g$$$, the points $$$p_i$$$ and $$$p_{i+1}$$$ are adjacent. Note that the walk may contain the same point more than once. In particular, it may contain point $$$(1, 1)$$$ or $$$(n, m)$$$ multiple times. There are $$$n(m-1)+(n-1)m$$$ segments connecting the adjacent points in Elena's grid. Elena wants to color each of these segments in blue or red color so that there exists a walk $$$p_1, p_2, ldots, p_{k+1}$$$ of length $$$k+1$$$ such that out of $$$k$$$ segments connecting two consecutive points in this walk, no two consecutive segments have the same color (in other words, for each $$$1 le i < k$$$, the color of the segment between points $$$p_i$$$ and $$$p_{i+1}$$$ differs from the color of the segment between points $$$p_{i+1}$$$ and $$$p_{i+2}$$$). Please find any such coloring or report that there is no such coloring. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 32$$$). The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 leq n,m leq 16$$$, $$$1 leq k leq 10^9$$$)xa0— the dimensions of the grid and the number of segments in the walk Elena is looking for. Output For each test case, output "NO" if it is not possible to color each of the $$$n(m-1)+(n-1)m$$$ segments in blue or red color, so that there exists a walk of length $$$k+1$$$ satisfying the condition from the statement. Otherwise, output in the first line "YES", and then provide the required coloring. In each of the first $$$n$$$ lines of coloring description, output $$$m-1$$$ space-separated characters. The $$$j$$$-th character in the $$$i$$$-th of these $$$n$$$ lines should denote the color of the segment between points $$$(i,j)$$$ and $$$(i,j+1)$$$. Here, use 'B' to denote the blue color and 'R' to denote the red color. In each of the next $$$n-1$$$ lines of coloring description, output $$$m$$$ space-separated characters. The $$$j$$$-th character in the $$$i$$$-th of these $$$n-1$$$ lines should denote the color of the segment between points $$$(i,j)$$$ and $$$(i+1,j)$$$. Similarly, use 'B' to denote the blue color and 'R' to denote the red color. You can output each letter in the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses, and both 'R' and 'r' are valid notation of red. Example Input 5 4 5 11 3 3 2 3 4 1000000000 3 3 12588 4 4 8 Output YES R R B B R R R R B B B R R R B B R B B R B R B B B B B B R R R NO NO YES R B B B B R B B R R B B YES B B R R B R B R R R R B B R R B B B B B B R R R Note In the first test case, one of the correct answers is shown in the picture below. The color-alternating walk of length $$$12$$$ is highlighted. In the second and the third test cases, it can be shown that there is no coloring satisfying the condition from the statement.
1,700
false
false
false
false
false
true
false
false
false
false
926
1299A
Anu has created her own function $$$f$$$: $$$f(x, y) = (x y) - y$$$ where $$$$$$ denotes the = (116) - 6 = 15 - 6 = 9$$$. It can be proved that for any nonnegative numbers $$$x$$$ and $$$y$$$ value of $$$f(x, y)$$$ is also nonnegative. She would like to research more about this function and has created multiple problems for herself. But she isn't able to solve all of them and needs your help. Here is one of these problems. A value of an array $$$[a_1, a_2, dots, a_n]$$$ is defined as $$$f(f(dots f(f(a_1, a_2), a_3), dots a_{n-1}), a_n)$$$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$). Elements of the array are not guaranteed to be different. Output Output $$$n$$$ integers, the reordering of the array with maximum value. If there are multiple answers, print any. Note In the first testcase, value of the array $$$[11, 6, 4, 0]$$$ is $$$f(f(f(11, 6), 4), 0) = f(f(9, 4), 0) = f(9, 0) = 9$$$. $$$[11, 4, 0, 6]$$$ is also a valid answer.
1,500
true
true
false
false
false
false
true
false
false
false
4,201
2030G1
This is the easy version of the problem. In this version, $$$n leq 5000$$$. You can only make hacks if both versions of the problem are solved. Orangutans are powerful beings—so powerful that they only need $$$1$$$ unit of time to destroy every vulnerable planet in the universe! There are $$$n$$$ planets in the universe. Each planet has an interval of vulnerability $$$[l, r]$$$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $$$1$$$ unit. Specifically, suppose the expansion is performed on planet $$$p$$$ with interval of vulnerability $$$[l_p, r_p]$$$. Then, the resulting interval of vulnerability may be either $$$[l_p - 1, r_p]$$$ or $$$[l_p, r_p + 1]$$$. Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed. Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $$$998,244,353$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 5000$$$)xa0— the number of planets in the universe. The following $$$n$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i leq r_i leq n$$$)xa0— the initial interval of vulnerability of the $$$i$$$-th planet. It is guaranteed that the sum of $$$n$$$ does not exceed $$$5000$$$ over all test cases. Output For each test case, output an integerxa0— the sum of scores to destroy all non-empty subsets of the planets in the universe, modulo $$$998,244,353$$$. Note In the first testcase, there are seven non-empty subsets of planets we must consider: For each of the subsets $$${[1,1]}, {[2,3]}, {[3,3]}$$$, the score is $$$0$$$. For the subset $$${[2,3], [3,3]}$$$, the score is $$$0$$$, because the point $$$3$$$ is already contained in both planets' interval of vulnerability. For the subset $$${[1,1], [2,3]}$$$, the score is $$$1$$$. By using one operation on changing the interval of vulnerability of the second planet to be $$$[1,3]$$$, the two planets now both have the point $$$1$$$ in their interval. For the subset $$${[1,1], [3,3]}$$$, the score is $$$2$$$. By using two operations on changing the interval of vulnerability of the first planet to be $$$[1,3]$$$, the two planets now both have the point $$$3$$$ in their interval. For the subset $$${[1,1], [2,3], [3,3]}$$$, the score is $$$2$$$. By using one operation on changing the interval of vulnerability of the first planet to be $$$[1,2]$$$ and one operation on changing the interval of vulnerability of the third planet to $$$[2,3]$$$, all three planets will have the point $$$2$$$ in their interval. The sum of scores of all non-empty subsets of the first testcase is $$$0 cdot 4 + 1 cdot 1 + 2cdot2 = 5$$$.
2,900
true
true
false
false
false
false
false
false
false
false
82
1648B
You are given an array $$$a$$$ of $$$n$$$ positive integers numbered from $$$1$$$ to $$$n$$$. Let's call an array integral if for any two, not necessarily different, numbers $$$x$$$ and $$$y$$$ from this array, $$$x ge y$$$, the number $$$left lfloor frac{x}{y} ight floor$$$ ($$$x$$$ divided by $$$y$$$ with rounding down) is also in this array. You are guaranteed that all numbers in $$$a$$$ do not exceed $$$c$$$. Your task is to check whether this array is integral. Input The input 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. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 le n le 10^6$$$, $$$1 le c le 10^6$$$)xa0— the size of $$$a$$$ and the limit for the numbers in the array. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le c$$$)xa0— the array $$$a$$$. Let $$$N$$$ be the sum of $$$n$$$ over all test cases and $$$C$$$ be the sum of $$$c$$$ over all test cases. It is guaranteed that $$$N le 10^6$$$ and $$$C le 10^6$$$. Output For each test case print Yes if the array is integral and No otherwise. Examples Input 4 3 5 1 2 5 4 10 1 3 3 7 1 2 2 1 1 1 Note In the first test case it is easy to see that the array is integral: $$$left lfloor frac{1}{1} ight floor = 1$$$, $$$a_1 = 1$$$, this number occurs in the arry $$$left lfloor frac{2}{2} ight floor = 1$$$ $$$left lfloor frac{5}{5} ight floor = 1$$$ $$$left lfloor frac{2}{1} ight floor = 2$$$, $$$a_2 = 2$$$, this number occurs in the array $$$left lfloor frac{5}{1} ight floor = 5$$$, $$$a_3 = 5$$$, this number occurs in the array $$$left lfloor frac{5}{2} ight floor = 2$$$, $$$a_2 = 2$$$, this number occurs in the array Thus, the condition is met and the array is integral. In the second test case it is enough to see that $$$left lfloor frac{7}{3} ight floor = left lfloor 2frac{1}{3} ight floor = 2$$$, this number is not in $$$a$$$, that's why it is not integral. In the third test case $$$left lfloor frac{2}{2} ight floor = 1$$$, but there is only $$$2$$$ in the array, that's why it is not integral.
1,800
true
false
false
false
true
true
true
false
false
false
2,382
449C
Problem - 449C - 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 number theory *2500 No tag edit access → Contest materials , the number of the apples. Output The first line must contain a single integer _m_, representing the maximum number of groups he can get. Each of the next _m_ lines must contain two integers — the numbers of apples in the current group. If there are several optimal answers you can print any of them. Examples Input 6 Output 2 6 3 2 4 Input 9 Output 3 9 3 2 4 6 8 Input 2 Output 0
2,500
false
false
false
false
false
true
false
false
false
false
8,042
12C
The spring is coming and it means that a lot of fruits appear on the counters. One sunny day little boy Valera decided to go shopping. He made a list of _m_ fruits he wanted to buy. If Valera want to buy more than one fruit of some kind, he includes it into the list several times. When he came to the fruit stall of Ashot, he saw that the seller hadn't distributed price tags to the goods, but put all price tags on the counter. Later Ashot will attach every price tag to some kind of fruits, and Valera will be able to count the total price of all fruits from his list. But Valera wants to know now what can be the smallest total price (in case of the most «lucky» for him distribution of price tags) and the largest total price (in case of the most «unlucky» for him distribution of price tags). Input The first line of the input contains two integer number _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the number of price tags (which is equal to the number of different kinds of fruits that Ashot sells) and the number of items in Valera's list. The second line contains _n_ space-separated positive integer numbers. Each of them doesn't exceed 100 and stands for the price of one fruit of some kind. The following _m_ lines contain names of the fruits from the list. Each name is a non-empty string of small Latin letters which length doesn't exceed 32. It is guaranteed that the number of distinct fruits from the list is less of equal to _n_. Also it is known that the seller has in stock all fruits that Valera wants to buy. Output Print two numbers _a_ and _b_ (_a_u2009≤u2009_b_) — the minimum and the maximum possible sum which Valera may need to buy all fruits from his list. Examples Input 5 3 4 2 1 10 5 apple orange mango Input 6 5 3 5 1 6 8 1 peach grapefruit banana orange orange
1,100
false
true
true
false
false
false
false
false
true
false
9,933
84A
The hero of our story, Valera, and his best friend Arcady are still in school, and therefore they spend all the free time playing turn-based strategy "GAGA: Go And Go Again". The gameplay is as follows. There are two armies on the playing field each of which consists of _n_ men (_n_ is always even). The current player specifies for each of her soldiers an enemy's soldier he will shoot (a target) and then all the player's soldiers shot simultaneously. This is a game world, and so each soldier shoots perfectly, that is he absolutely always hits the specified target. If an enemy soldier is hit, he will surely die. It may happen that several soldiers had been indicated the same target. Killed soldiers do not participate in the game anymore. The game "GAGA" consists of three steps: first Valera makes a move, then Arcady, then Valera again and the game ends. You are asked to calculate the maximum total number of soldiers that may be killed during the game. Input The input data consist of a single integer _n_ (2u2009≤u2009_n_u2009≤u2009108, _n_ is even). Please note that before the game starts there are 2_n_ soldiers on the fields. Output Print a single number — a maximum total number of soldiers that could be killed in the course of the game in three turns. Note The first sample test: 1) Valera's soldiers 1 and 2 shoot at Arcady's soldier 1. 2) Arcady's soldier 2 shoots at Valera's soldier 1. 3) Valera's soldier 1 shoots at Arcady's soldier 2. There are 3 soldiers killed in total: Valera's soldier 1 and Arcady's soldiers 1 and 2.
900
true
false
false
false
false
false
false
false
false
false
9,548
350C
You've got a robot, its task is destroying bombs on a square plane. Specifically, the square plane contains _n_ bombs, the _i_-th bomb is at point with coordinates (_x__i_,u2009_y__i_). We know that no two bombs are at the same point and that no bomb is at point with coordinates (0,u20090). Initially, the robot is at point with coordinates (0,u20090). Also, let's mark the robot's current position as (_x_,u2009_y_). In order to destroy all the bombs, the robot can perform three types of operations: 1. Operation has format "1 k dir". To perform the operation robot have to move in direction _dir_ _k_ (_k_u2009≥u20091) times. There are only 4 directions the robot can move in: "R", "L", "U", "D". During one move the robot can move from the current point to one of following points: (_x_u2009+u20091,u2009_y_), (_x_u2009-u20091,u2009_y_), (_x_,u2009_y_u2009+u20091), (_x_,u2009_y_u2009-u20091) (corresponding to directions). It is forbidden to move from point (_x_,u2009_y_), if at least one point on the path (besides the destination point) contains a bomb. 2. Operation has format "2". To perform the operation robot have to pick a bomb at point (_x_,u2009_y_) and put it in a special container. Thus, the robot can carry the bomb from any point to any other point. The operation cannot be performed if point (_x_,u2009_y_) has no bomb. It is forbidden to pick a bomb if the robot already has a bomb in its container. 3. Operation has format "3". To perform the operation robot have to take a bomb out of the container and destroy it. You are allowed to perform this operation only if the robot is at point (0,u20090). It is forbidden to perform the operation if the container has no bomb. Help the robot and find the shortest possible sequence of operations he can perform to destroy all bombs on the coordinate plane. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of bombs on the coordinate plane. Next _n_ lines contain two integers each. The _i_-th line contains numbers (_x__i_,u2009_y__i_) (u2009-u2009109u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009109) — the coordinates of the _i_-th bomb. It is guaranteed that no two bombs are located at the same point and no bomb is at point (0,u20090). Output In a single line print a single integer _k_ — the minimum number of operations needed to destroy all bombs. On the next lines print the descriptions of these _k_ operations. If there are multiple sequences, you can print any of them. It is guaranteed that there is the solution where _k_u2009≤u2009106. Examples Output 12 1 1 R 1 1 U 2 1 1 L 1 1 D 3 1 1 L 1 1 D 2 1 1 R 1 1 U 3 Output 12 1 1 R 2 1 1 L 3 1 5 R 2 1 5 L 3 1 5 U 2 1 5 D 3
1,600
false
true
true
false
false
false
false
false
true
false
8,433
596C
Wilbur is playing with a set of _n_ points on the coordinate plane. All points have non-negative integer coordinates. Moreover, if some point (_x_, _y_) belongs to the set, then all points (_x_', _y_'), such that 0u2009≤u2009_x_'u2009≤u2009_x_ and 0u2009≤u2009_y_'u2009≤u2009_y_ also belong to this set. Now Wilbur wants to number the points in the set he has, that is assign them distinct integer numbers from 1 to _n_. In order to make the numbering aesthetically pleasing, Wilbur imposes the condition that if some point (_x_, _y_) gets number _i_, then all (_x_',_y_') from the set, such that _x_'u2009≥u2009_x_ and _y_'u2009≥u2009_y_ must be assigned a number not less than _i_. For example, for a set of four points (0, 0), (0, 1), (1, 0) and (1, 1), there are two aesthetically pleasing numberings. One is 1, 2, 3, 4 and another one is 1, 3, 2, 4. Wilbur's friend comes along and challenges Wilbur. For any point he defines it's special value as _s_(_x_,u2009_y_)u2009=u2009_y_u2009-u2009_x_. Now he gives Wilbur some _w_1, _w_2,..., _w__n_, and asks him to find an aesthetically pleasing numbering of the points in the set, such that the point that gets number _i_ has it's special value equal to _w__i_, that is _s_(_x__i_,u2009_y__i_)u2009=u2009_y__i_u2009-u2009_x__i_u2009=u2009_w__i_. Now Wilbur asks you to help him with this challenge. Input The first line of the input consists of a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of points in the set Wilbur is playing with. Next follow _n_ lines with points descriptions. Each line contains two integers _x_ and _y_ (0u2009≤u2009_x_,u2009_y_u2009≤u2009100u2009000), that give one point in Wilbur's set. It's guaranteed that all points are distinct. Also, it is guaranteed that if some point (_x_, _y_) is present in the input, then all points (_x_', _y_'), such that 0u2009≤u2009_x_'u2009≤u2009_x_ and 0u2009≤u2009_y_'u2009≤u2009_y_, are also present in the input. The last line of the input contains _n_ integers. The _i_-th of them is _w__i_ (u2009-u2009100u2009000u2009≤u2009_w__i_u2009≤u2009100u2009000)xa0— the required special value of the point that gets number _i_ in any aesthetically pleasing numbering. Output If there exists an aesthetically pleasant numbering of points in the set, such that _s_(_x__i_,u2009_y__i_)u2009=u2009_y__i_u2009-u2009_x__i_u2009=u2009_w__i_, then print "YES" on the first line of the output. Otherwise, print "NO". If a solution exists, proceed output with _n_ lines. On the _i_-th of these lines print the point of the set that gets number _i_. If there are multiple solutions, print any of them. Examples Input 5 2 0 0 0 1 0 1 1 0 1 0 -1 -2 1 0 Note In the first sample, point (2, 0) gets number 3, point (0, 0) gets number one, point (1, 0) gets number 2, point (1, 1) gets number 5 and point (0, 1) gets number 4. One can easily check that this numbering is aesthetically pleasing and _y__i_u2009-u2009_x__i_u2009=u2009_w__i_. In the second sample, the special values of the points in the set are 0, u2009-u20091, and u2009-u20092 while the sequence that the friend gives to Wilbur is 0, 1, 2. Therefore, the answer does not exist.
1,700
false
true
false
false
false
false
false
false
true
false
7,463
1909B
You are given an array $$$a_1, a_2, dots, a_n$$$ of distinct positive integers. You have to do the following operation exactly once: choose a positive integer $$$k$$$; for each $$$i$$$ from $$$1$$$ to $$$n$$$, replace $$$a_i$$$ with $$$a_i ext{ mod } k^dagger$$$. Find a value of $$$k$$$ such that $$$1 leq k leq 10^{18}$$$ and the array $$$a_1, a_2, dots, a_n$$$ contains exactly $$$2$$$ distinct values at the end of the operation. It can be shown that, under the constraints of the problem, at least one such $$$k$$$ always exists. If there are multiple solutions, you can print any of them. $$$^dagger$$$ $$$a ext{ mod } b$$$ denotes the remainder after dividing $$$a$$$ by $$$b$$$. For example: $$$7 ext{ mod } 3=1$$$ since $$$7 = 3 cdot 2 + 1$$$ $$$15 ext{ mod } 4=3$$$ since $$$15 = 4 cdot 3 + 3$$$ $$$21 ext{ mod } 1=0$$$ since $$$21 = 21 cdot 1 + 0$$$ Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 100$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^{17}$$$)xa0— the initial state of the array. It is guaranteed that all the $$$a_i$$$ are distinct. Note that there are no constraints on the sum of $$$n$$$ over all test cases. Output For each test case, output a single integer: a value of $$$k$$$ ($$$1 leq k leq 10^{18}$$$) such that the array $$$a_1, a_2, dots, a_n$$$ contains exactly $$$2$$$ distinct values at the end of the operation. Example Input 5 4 8 15 22 30 5 60 90 98 120 308 6 328 769 541 986 215 734 5 1000 2000 7000 11000 16000 2 2 1 Output 7 30 3 5000 1000000000000000000 Note In the first test case, you can choose $$$k = 7$$$. The array becomes $$$[8 ext{ mod } 7, 15 ext{ mod } 7, 22 ext{ mod } 7, 30 ext{ mod } 7] = [1, 1, 1, 2]$$$, which contains exactly $$$2$$$ distinct values ($$${1, 2}$$$). In the second test case, you can choose $$$k = 30$$$. The array becomes $$$[0, 0, 8, 0, 8]$$$, which contains exactly $$$2$$$ distinct values ($$${0, 8}$$$). Note that choosing $$$k = 10$$$ would also be a valid solution. In the last test case, you can choose $$$k = 10^{18}$$$. The array becomes $$$[2, 1]$$$, which contains exactly $$$2$$$ distinct values ($$${1, 2}$$$). Note that choosing $$$k = 10^{18} + 1$$$ would not be valid, because $$$1 leq k leq 10^{18}$$$ must be true.
1,200
true
false
false
false
false
true
false
false
false
false
856
1637H
You are given a permutation $$$p$$$ of length $$$n$$$. You can choose any subsequence, remove it from the permutation, and insert it at the beginning of the permutation keeping the same order. For every $$$k$$$ from $$$0$$$ to $$$n$$$, find the minimal possible number of inversions in the permutation after you choose a subsequence of length exactly $$$k$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 50,000$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$)xa0— the length of the permutation. The second line of each test case contains the permutation $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$). It is guaranteed that the total sum of $$$n$$$ doesn't exceed $$$5 cdot 10^5$$$. Output For each test case output $$$n + 1$$$ integers. The $$$i$$$-th of them must be the answer for the subsequence length of $$$i - 1$$$. Example Input 3 1 1 4 4 2 1 3 5 5 1 3 2 4 Output 0 0 4 2 2 1 4 5 4 2 2 1 5 Note In the second test case: For the length $$$0$$$: $$$[4, 2, 1, 3] ightarrow [4, 2, 1, 3]$$$: $$$4$$$ inversions. For the length $$$1$$$: $$$[4, 2, mathbf{1}, 3] ightarrow [1, 4, 2, 3]$$$: $$$2$$$ inversions. For the length $$$2$$$: $$$[4, mathbf{2}, mathbf{1}, 3] ightarrow [2, 1, 4, 3]$$$, or $$$[4, 2, mathbf{1}, extbf{3}] ightarrow [1, 3, 4, 2]$$$: $$$2$$$ inversions. For the length $$$3$$$: $$$[4, mathbf{2}, mathbf{1}, mathbf{3}] ightarrow [2, 1, 3, 4]$$$: $$$1$$$ inversion. For the length $$$4$$$: $$$[mathbf{4}, mathbf{2}, mathbf{1}, mathbf{3}] ightarrow [4, 2, 1, 3]$$$: $$$4$$$ inversions.
3,500
true
true
false
false
true
false
false
false
true
false
2,427
1260C
You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor. You must paint a fence which consists of $$$10^{100}$$$ planks in two colors in the following way (suppose planks are numbered from left to right from $$$0$$$): if the index of the plank is divisible by $$$r$$$ (such planks have indices $$$0$$$, $$$r$$$, $$$2r$$$ and so on) then you must paint it red; if the index of the plank is divisible by $$$b$$$ (such planks have indices $$$0$$$, $$$b$$$, $$$2b$$$ and so on) then you must paint it blue; if the index is divisible both by $$$r$$$ and $$$b$$$ you can choose the color to paint the plank; otherwise, you don't need to paint the plank at all (and it is forbidden to spent paint on it). Furthermore, the Government added one additional restriction to make your punishment worse. Let's list all painted planks of the fence in ascending order: if there are $$$k$$$ consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don't paint the fence according to the four aforementioned conditions, you will also be executed. The question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs. Input The first line contains single integer $$$T$$$ ($$$1 le T le 1000$$$) — the number of test cases. The next $$$T$$$ lines contain descriptions of test cases — one per line. Each test case contains three integers $$$r$$$, $$$b$$$, $$$k$$$ ($$$1 le r, b le 10^9$$$, $$$2 le k le 10^9$$$) — the corresponding coefficients. Output Print $$$T$$$ words — one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise. Example Input 4 1 1 2 2 10 4 5 2 3 3 2 2 Output OBEY REBEL OBEY OBEY
1,700
true
true
false
false
false
false
false
false
false
false
4,392
1305D
This is an interactive problem. After getting AC after 13 Time Limit Exceeded verdicts on a geometry problem, Kuroni went to an Italian restaurant to celebrate this holy achievement. Unfortunately, the excess sauce disoriented him, and he's now lost! The United States of America can be modeled as a tree (why though) with $$$n$$$ vertices. The tree is rooted at vertex $$$r$$$, wherein lies Kuroni's hotel. Kuroni has a phone app designed to help him in such emergency cases. To use the app, he has to input two vertices $$$u$$$ and $$$v$$$, and it'll return a vertex $$$w$$$, which is the lowest common ancestor of those two vertices. However, since the phone's battery has been almost drained out from live-streaming Kuroni's celebration party, he could only use the app at most $$$lfloor frac{n}{2} floor$$$ times. After that, the phone would die and there will be nothing left to help our dear friend! :( As the night is cold and dark, Kuroni needs to get back, so that he can reunite with his comfy bed and pillow(s). Can you help him figure out his hotel's location? Interaction The interaction starts with reading a single integer $$$n$$$ ($$$2 le n le 1000$$$), the number of vertices of the tree. Then you will read $$$n-1$$$ lines, the $$$i$$$-th of them has two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i e y_i$$$), denoting there is an edge connecting vertices $$$x_i$$$ and $$$y_i$$$. It is guaranteed that the edges will form a tree. Then you can make queries of type "? u v" ($$$1 le u, v le n$$$) to find the lowest common ancestor of vertex $$$u$$$ and $$$v$$$. After the query, read the result $$$w$$$ as an integer. In case your query is invalid or you asked more than $$$lfloor frac{n}{2} floor$$$ queries, the program will print $$$-1$$$ and will finish interaction. You will receive a Wrong answer verdict. Make sure to exit immediately to avoid getting other verdicts. When you find out the vertex $$$r$$$, print "! $$$r$$$" and quit after that. This query does not count towards the $$$lfloor frac{n}{2} floor$$$ limit. Note that the tree is fixed beforehand and will not change during the queries, i.e. the interactor is not adaptive. After printing any query do not forget to print end of line and flush the output. Otherwise, you might get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To hack, use the following format: The first line should contain two integers $$$n$$$ and $$$r$$$ ($$$2 le n le 1000$$$, $$$1 le r le n$$$), denoting the number of vertices and the vertex with Kuroni's hotel. The $$$i$$$-th of the next $$$n-1$$$ lines should contain two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$)xa0— denoting there is an edge connecting vertex $$$x_i$$$ and $$$y_i$$$. The edges presented should form a tree.
1,900
false
false
false
false
false
true
false
false
false
false
4,161
696D
Barney was hanging out with Nora for a while and now he thinks he may have feelings for her. Barney wants to send her a cheesy text message and wants to make her as happy as possible. Initially, happiness level of Nora is 0. Nora loves some pickup lines like "I'm falling for you" and stuff. Totally, she knows _n_ pickup lines, each consisting only of lowercase English letters, also some of them may be equal (in writing, but different in pronouncing or meaning though). Every time Nora sees _i_-th pickup line as a consecutive subsequence of Barney's text message her happiness level increases by _a__i_. These substrings may overlap, for example, Nora will see the pickup line aa twice and the pickup line ab once in text message aaab. Due to texting app limits, Barney's text may have up to _l_ characters. Barney asked you to help him make Nora as much happy as possible, it's gonna be legen... Input The first line of input contains two integers _n_ and _l_ (1u2009≤u2009_n_u2009≤u2009200,u20091u2009≤u2009_l_u2009≤u20091014)xa0— the number of pickup lines and the maximum length of Barney's text. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100), meaning that Nora's happiness level increases by _a__i_ after every time seeing _i_-th pickup line. The next _n_ lines contain the pickup lines. _i_-th of them contains a single string _s__i_ consisting of only English lowercase letter. Summary length of all pickup lines does not exceed 200. All strings are not empty. Output Print the only integerxa0— the maximum possible value of Nora's happiness level after reading Barney's text. Examples Input 3 6 3 2 1 heart earth art Input 3 6 3 2 8 heart earth art Note An optimal answer for the first sample case is hearth containing each pickup line exactly once. An optimal answer for the second sample case is artart.
2,500
false
false
false
true
true
false
false
false
false
false
7,038
12D
Problem - 12D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures sortings *2400 No tag edit access → Contest materials ") . The second line contains _N_ integer numbers _B__i_, separated by single spaces. The third and the fourth lines contain sequences _I__i_ and _R__i_ in the same format. It is guaranteed that 0u2009≤u2009_B__i_,u2009_I__i_,u2009_R__i_u2009≤u2009109. Output Output the answer to the problem. Examples Input 3 1 4 2 4 3 2 2 5 3 Output 1
2,400
false
false
false
false
true
false
false
false
true
false
9,932
114A
Problem - 114A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *1000 No tag edit access → Contest materials used to stand for a thousand and "tma tmyschaya" (which literally means "the tma of tmas") used to stand for a million. Petya wanted to modernize the words we use for numbers and invented a word petricium that represents number _k_. Moreover, petricium la petricium stands for number _k_2, petricium la petricium la petricium stands for _k_3 and so on. All numbers of this form are called petriciumus cifera, and the number's importance is the number of articles la in its title. Petya's invention brought on a challenge that needed to be solved quickly: does some number _l_ belong to the set petriciumus cifera? As Petya is a very busy schoolboy he needs to automate the process, he asked you to solve it. Input The first input line contains integer number _k_, the second line contains integer number _l_ (2u2009≤u2009_k_,u2009_l_u2009≤u2009231u2009-u20091). Output You should print in the first line of the output "YES", if the number belongs to the set petriciumus cifera and otherwise print "NO". If the number belongs to the set, then print on the seconds line the only number — the importance of number _l_. Examples Input 5 25 Output YES 1 Input 3 8 Output NO
1,000
true
false
false
false
false
false
false
false
false
false
9,430
1365D
Vivek has encountered a problem. He has a maze that can be represented as an $$$n imes m$$$ grid. Each of the grid cells may represent the following: Emptyxa0— '.' Wallxa0— '#' Good person xa0— 'G' Bad personxa0— 'B' The only escape from the maze is at cell $$$(n, m)$$$. A person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains 'G' or 'B' cannot be blocked and can be travelled through. Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions. It is guaranteed that the cell $$$(n,m)$$$ is empty. Vivek can also block this cell. Input The first line contains one integer $$$t$$$ $$$(1 le t le 100)$$$xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ $$$(1 le n, m le 50)$$$xa0— the number of rows and columns in the maze. Each of the next $$$n$$$ lines contain $$$m$$$ characters. They describe the layout of the maze. If a character on a line equals '.', the corresponding cell is empty. If it equals '#', the cell has a wall. 'G' corresponds to a good person and 'B' corresponds to a bad person. Output For each test case, print "Yes" if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print "No" You may print every letter in any case (upper or lower). Example Input 6 1 1 . 1 2 G. 2 2 #B G. 2 3 G.# B#. 3 3 #B. #.. GG. 2 2 #B B. Output Yes Yes No No Yes Yes Note For the first and second test cases, all conditions are already satisfied. For the third test case, there is only one empty cell $$$(2,2)$$$, and if it is replaced with a wall then the good person at $$$(1,2)$$$ will not be able to escape. For the fourth test case, the good person at $$$(1,1)$$$ cannot escape. For the fifth test case, Vivek can block the cells $$$(2,3)$$$ and $$$(2,2)$$$. For the last test case, Vivek can block the destination cell $$$(2, 2)$$$.
1,700
false
true
true
false
false
true
false
false
false
true
3,854
294E
The great Shaass is the new king of the Drakht empire. The empire has _n_ cities which are connected by _n_u2009-u20091 bidirectional roads. Each road has an specific length and connects a pair of cities. There's a unique simple path connecting each pair of cities. His majesty the great Shaass has decided to tear down one of the roads and build another road with the same length between some pair of cities. He should build such road that it's still possible to travel from each city to any other city. He might build the same road again. You as his advisor should help him to find a way to make the described action. You should find the way that minimize the total sum of pairwise distances between cities after the action. So calculate the minimum sum. Input The first line of the input contains an integer _n_ denoting the number of cities in the empire, (2u2009≤u2009_n_u2009≤u20095000). The next _n_u2009-u20091 lines each contains three integers _a__i_, _b__i_ and _w__i_ showing that two cities _a__i_ and _b__i_ are connected using a road of length _w__i_, (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_,u20091u2009≤u2009_w__i_u2009≤u2009106). Output On the only line of the output print the minimum pairwise sum of distances between the cities. Please do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator. Examples Input 6 1 2 1 2 3 1 3 4 1 4 5 1 5 6 1 Input 6 1 3 1 2 3 1 3 4 100 4 5 2 4 6 1
2,300
false
false
false
true
false
false
false
false
false
false
8,652
747D
The winter in Berland lasts _n_ days. For each day we know the forecast for the average air temperature that day. Vasya has a new set of winter tires which allows him to drive safely no more than _k_ days at any average air temperature. After _k_ days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these _k_ days form a continuous segment of days. Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative. Vasya can change summer tires to winter tires and vice versa at the beginning of any day. Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires. Input The first line contains two positive integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20092·105, 0u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than _k_ days in total. The second line contains a sequence of _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (u2009-u200920u2009≤u2009_t__i_u2009≤u200920)xa0— the average air temperature in the _i_-th winter day. Output Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1. Examples Input 10 6 2 -5 1 3 0 0 -4 -3 1 0 Note In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two. In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.
1,800
false
true
false
true
false
false
false
false
true
false
6,807
1975C
Mocha likes arrays, so before her departure, Chamo gave her an array $$$a$$$ consisting of $$$n$$$ positive integers as a gift. Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times: 1. Choose indices $$$l$$$ and $$$r$$$ ($$$1 leq l < r leq n$$$) 2. Let $$$x$$$ be the median$$$^dagger$$$ of the subarray $$$[a_l, a_{l+1},ldots, a_r]$$$ 3. Set all values $$$a_l, a_{l+1},ldots, a_r$$$ to $$$x$$$ Suppose $$$a=[1,2,3,4,5]$$$ initially: If Mocha chooses $$$(l,r)=(3,4)$$$ in the first operation, then $$$x=3$$$, the array will be changed into $$$a=[1,2,3,3,5]$$$. If Mocha chooses $$$(l,r)=(1,3)$$$ in the first operation, then $$$x=2$$$, the array will be changed into $$$a=[2,2,2,4,5]$$$. Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number. $$$^dagger$$$ The median in an array $$$b$$$ of length $$$m$$$ is an element that occupies position number $$$lfloor frac{m+1}{2} floor$$$ after we sort the elements in non-decreasing order. For example, the median of $$$[3,1,4,1,5]$$$ is $$$3$$$ and the median of $$$[5,25,20,24]$$$ is $$$20$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1leq tleq 500$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2leq nleq 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq a_i leq 10^9$$$)xa0— the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output the maximum value of the number. Note In the first test case, $$$a=[1,2]$$$. Mocha can only choose the interval $$$(l,r)=(1,2)$$$. The array will be changed to $$$a=[1,1]$$$. Therefore, the answer is $$$1$$$. In the second test case, Mocha can perform the following operations: Choose the interval $$$(l,r)=(4,5)$$$, then $$$a=[1,2,3,4,4]$$$. Choose the interval $$$(l,r)=(3,5)$$$, then $$$a=[1,2,4,4,4]$$$. Choose the interval $$$(l,r)=(1,5)$$$, then $$$a=[4,4,4,4,4]$$$. The array contains only the same number, which is $$$4$$$. It can be proven that the maximum value of the final number cannot be greater than $$$4$$$.
1,200
false
true
false
false
false
false
true
true
false
false
440
1980C
Sofia had an array of $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$. One day she got bored with it, so she decided to sequentially apply $$$m$$$ modification operations to it. Each modification operation is described by a pair of numbers $$$langle c_j, d_j angle$$$ and means that the element of the array with index $$$c_j$$$ should be assigned the value $$$d_j$$$, i.e., perform the assignment $$$a_{c_j} = d_j$$$. After applying all modification operations sequentially, Sofia discarded the resulting array. Recently, you found an array of $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$. You are interested in whether this array is Sofia's array. You know the values of the original array, as well as the values $$$d_1, d_2, ldots, d_m$$$. The values $$$c_1, c_2, ldots, c_m$$$ turned out to be lost. Is there a sequence $$$c_1, c_2, ldots, c_m$$$ such that the sequential application of modification operations $$$langle c_1, d_1, angle, langle c_2, d_2, angle, ldots, langle c_m, d_m angle$$$ to the array $$$a_1, a_2, ldots, a_n$$$ transforms it into the array $$$b_1, b_2, ldots, b_n$$$? Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the size of 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 original array. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the elements of the found array. The fourth line contains an integer $$$m$$$ ($$$1 le m le 2 cdot 10^5$$$)xa0— the number of modification operations. The fifth line contains $$$m$$$ integers $$$d_1, d_2, ldots, d_m$$$ ($$$1 le d_j le 10^9$$$)xa0— the preserved value for each modification operation. It is guaranteed that the sum of the values of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$, similarly the sum of the values of $$$m$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if there exists a suitable sequence $$$c_1, c_2, ldots, c_m$$$, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
1,300
false
true
false
false
false
true
false
false
false
false
408
433E
Tachibana Kanade likes Mapo Tofu very much. One day, the canteen cooked all kinds of tofu to sell, but not all tofu is Mapo Tofu, only those spicy enough can be called Mapo Tofu. Each piece of tofu in the canteen is given a _m_-based number, all numbers are in the range [_l_,u2009_r_] (_l_ and _r_ being _m_-based numbers), and for every _m_-based integer in the range [_l_,u2009_r_], there exists a piece of tofu with that number. To judge what tofu is Mapo Tofu, Tachibana Kanade chose _n_ _m_-based number strings, and assigned a value to each string. If a string appears in the number of a tofu, the value of the string will be added to the value of that tofu. If a string appears multiple times, then the value is also added that many times. Initially the value of each tofu is zero. Tachibana Kanade considers tofu with values no more than _k_ to be Mapo Tofu. So now Tachibana Kanade wants to know, how many pieces of tofu are Mapo Tofu? Input The first line contains three integers _n_, _m_ and _k_xa0(1u2009≤u2009_n_u2009≤u2009200;xa02u2009≤u2009_m_u2009≤u200920;xa01u2009≤u2009_k_u2009≤u2009500). Where _n_ denotes the number of strings, _m_ denotes the base used, and _k_ denotes the limit of the value for Mapo Tofu. The second line represents the number _l_. The first integer in the line is _len_ (1u2009≤u2009_len_u2009≤u2009200), describing the length (number of digits in base _m_) of _l_. Then follow _len_ integers _a_1,u2009_a_2,u2009...,u2009_a__len_xa0(0u2009≤u2009_a__i_u2009<u2009_m_;xa0_a_1u2009>u20090) separated by spaces, representing the digits of _l_, with _a_1 being the highest digit and _a__len_ being the lowest digit. The third line represents the number _r_ in the same format as _l_. It is guaranteed that 1u2009≤u2009_l_u2009≤u2009_r_. Then follow _n_ lines, each line describing a number string. The _i_-th line contains the _i_-th number string and _v__i_ — the value of the _i_-th string (1u2009≤u2009_v__i_u2009≤u2009200). All number strings are described in almost the same format as _l_, the only difference is number strings may contain necessary leading zeros (see the first example). The sum of the lengths of all number strings does not exceed 200. Output Output the number of pieces of Mapo Tofu modulo 1000000007 (109u2009+u20097). The answer should be a decimal integer. Examples Input 2 10 1 1 1 3 1 0 0 1 1 1 1 0 1 Input 2 10 12 2 5 9 6 6 3 5 4 9 7 2 0 6 1 3 6 7 2 1 Input 4 2 6 6 1 0 1 1 1 0 6 1 1 0 1 0 0 1 1 2 3 0 1 0 5 4 0 1 1 0 4 3 1 0 1 2 Note In the first sample, 10, 11 and 100 are the only three decimal numbers in [1,u2009100] with a value greater than 1. Here the value of 1 is 1 but not 2, since numbers cannot contain leading zeros and thus cannot be written as "01". In the second sample, no numbers in the given interval have a value greater than 12. In the third sample, 110000 and 110001 are the only two binary numbers in the given interval with a value no greater than 6.
2,500
false
false
false
true
false
false
false
false
false
false
8,105
2039E
Problem - 2039E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp implementation math *2200 No tag edit access → Contest materials ") ") such that $$$i < j$$$ and $$$a_i > a_j$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first and only line of each test case contains an integer $$$n$$$ ($$$2 le n le 10^6$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output an integerxa0— the number of possible arrays modulo $$$998,244,353$$$. Example Input 4 4 2 7 69 Output 5 1 682 325188814 Note In the first test case, the following $$$5$$$ arrays can be obtained (the inserted inversion count is shown in bold): $$$
2,200
true
false
true
true
false
false
false
false
false
false
19
432A
Problem - 432A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation sortings *800 No tag edit access → Contest materials ") Editorial ") has _n_ students. For each student you know the number of times he/she has participated in the ACM ICPC world programming championship. According to the ACM ICPC rules, each person can participate in the world championship at most 5 times. The head of the SSU OPTC is recently gathering teams to participate in the world championship. Each team must consist of exactly three people, at that, any person cannot be a member of two or more teams. What maximum number of teams can the head make if he wants each team to participate in the world championship with the same members at least _k_ times? Input The first line contains two integers, _n_ and _k_ (1u2009≤u2009_n_u2009≤u20092000;xa01u2009≤u2009_k_u2009≤u20095). The next line contains _n_ integers: _y_1,u2009_y_2,u2009...,u2009_y__n_ (0u2009≤u2009_y__i_u2009≤u20095), where _y__i_ shows the number of times the _i_-th person participated in the ACM ICPC world championship. Output Print a single number — the answer to the problem. Examples Input 5 2 0 4 5 1 0 Output 1 Input 6 4 0 1 2 3 4 5 Output 0 Input 6 5 0 0 0 0 0 0 Output 2 Note In the first sample only one team could be made: the first, the fourth and the fifth participants. In the second sample no teams could be created. In the third sample two teams could be created. Any partition into two teams fits.
800
false
true
true
false
false
false
false
false
true
false
8,114
1234D
You are given a string $$$s$$$ consisting of lowercase Latin letters and $$$q$$$ queries for this string. Recall that the substring $$$s[l; r]$$$ of the string $$$s$$$ is the string $$$s_l s_{l + 1} dots s_r$$$. For example, the substrings of "codeforces" are "code", "force", "f", "for", but not "coder" and "top". There are two types of queries: $$$1~ pos~ c$$$ ($$$1 le pos le s$$$, $$$c$$$ is lowercase Latin letter): replace $$$s_{pos}$$$ with $$$c$$$ (set $$$s_{pos} := c$$$); $$$2~ l~ r$$$ ($$$1 le l le r le s$$$): calculate the number of distinct characters in the substring $$$s[l; r]$$$. Input The first line of the input contains one string $$$s$$$ consisting of no more than $$$10^5$$$ lowercase Latin letters. The second line of the input contains one integer $$$q$$$ ($$$1 le q le 10^5$$$) — the number of queries. The next $$$q$$$ lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type. Output For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query. Examples Input abacaba 5 2 1 4 1 4 b 1 5 b 2 4 6 2 1 7 Input dfcbbcfeeedbaea 15 1 6 e 1 4 b 2 6 14 1 7 b 1 12 c 2 6 8 2 1 6 1 7 c 1 2 f 1 10 a 2 7 9 1 10 a 1 14 b 1 1 f 2 1 11
1,600
false
false
false
false
true
false
false
false
false
false
4,521
1558E
In a certain video game, the player controls a hero characterized by a single integer value: power. On the current level, the hero got into a system of $$$n$$$ caves numbered from $$$1$$$ to $$$n$$$, and $$$m$$$ tunnels between them. Each tunnel connects two distinct caves. Any two caves are connected with at most one tunnel. Any cave can be reached from any other cave by moving via tunnels. The hero starts the level in cave $$$1$$$, and every other cave contains a monster. The hero can move between caves via tunnels. If the hero leaves a cave and enters a tunnel, he must finish his movement and arrive at the opposite end of the tunnel. The hero can use each tunnel to move in both directions. However, the hero can not use the same tunnel twice in a row. Formally, if the hero has just moved from cave $$$i$$$ to cave $$$j$$$ via a tunnel, he can not head back to cave $$$i$$$ immediately after, but he can head to any other cave connected to cave $$$j$$$ with a tunnel. It is known that at least two tunnels come out of every cave, thus, the hero will never find himself in a dead end even considering the above requirement. To pass the level, the hero must beat the monsters in all the caves. When the hero enters a cave for the first time, he will have to fight the monster in it. The hero can beat the monster in cave $$$i$$$ if and only if the hero's power is strictly greater than $$$a_i$$$. In case of beating the monster, the hero's power increases by $$$b_i$$$. If the hero can't beat the monster he's fighting, the game ends and the player loses. After the hero beats the monster in cave $$$i$$$, all subsequent visits to cave $$$i$$$ won't have any consequences: the cave won't have any monsters, and the hero's power won't change either. Find the smallest possible power the hero must start the level with to be able to beat all the monsters and pass the level. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n le 1000$$$; $$$n le m le min(frac{n(n-1)}{2}, 2000)$$$)xa0— the number of caves and tunnels. The second line contains $$$n-1$$$ integers $$$a_2, a_3, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— values the hero's power are compared to while fighting monsters in caves $$$2, 3, ldots, n$$$. The third line contains $$$n-1$$$ integers $$$b_2, b_3, ldots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— increases applied to the hero's power for beating monsters in caves $$$2, 3, ldots, n$$$. Each of the next $$$m$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i e v_i$$$)xa0— the numbers of caves connected with a tunnel. No two caves are connected with more than one tunnel. Any cave can be reached from any other cave by moving via tunnels. At least two tunnels come out of every cave. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$, and the sum of $$$m$$$ over all test cases does not exceed $$$2000$$$. Output For each test case print a single integerxa0— the smallest possible power the hero must start the level with to be able to beat all the monsters and pass the level. Example Input 3 4 4 11 22 13 8 7 5 1 2 2 3 3 4 4 1 4 4 11 22 13 5 7 8 1 2 2 3 3 4 4 1 5 7 10 40 20 30 7 2 10 5 1 2 1 5 2 3 2 4 2 5 3 4 4 5 Note In the first test case, the hero can pass the level with initial power $$$15$$$ as follows: move from cave $$$1$$$ to cave $$$2$$$: since $$$15 > 11$$$, the hero beats the monster, and his power increases to $$$15 + 8 = 23$$$; move from cave $$$2$$$ to cave $$$3$$$: since $$$23 > 22$$$, the hero beats the monster, and his power increases to $$$23 + 7 = 30$$$; move from cave $$$3$$$ to cave $$$4$$$: since $$$30 > 13$$$, the hero beats the monster, and his power increases to $$$30 + 5 = 35$$$. In the second test case, the situation is similar except that the power increases for beating monsters in caves $$$2$$$ and $$$4$$$ are exchanged. The hero can follow a different route, $$$1 ightarrow 4 ightarrow 3 ightarrow 2$$$, and pass the level with initial power $$$15$$$. In the third test case, the hero can pass the level with initial power $$$19$$$ as follows: move from cave $$$1$$$ to cave $$$2$$$: since $$$19 > 10$$$, the hero beats the monster, and his power increases to $$$19 + 7 = 26$$$; move from cave $$$2$$$ to cave $$$4$$$: since $$$26 > 20$$$, the hero beats the monster, and his power increases to $$$26 + 10 = 36$$$; move from cave $$$4$$$ to cave $$$5$$$: since $$$36 > 30$$$, the hero beats the monster, and his power increases to $$$36 + 5 = 41$$$; move from cave $$$5$$$ to cave $$$2$$$: there is no monster in this cave anymore, nothing happens; move from cave $$$2$$$ to cave $$$3$$$: since $$$41 > 40$$$, the hero beats the monster, and his power increases to $$$41 + 2 = 43$$$.
3,000
false
true
false
false
false
false
false
true
false
true
2,838
1610G
Right before the UEFA Euro 2020, AmShZ and Safar placed bets on who'd be the champion, AmShZ betting on Italy, and Safar betting on France. Of course, AmShZ won. Hence, Safar gave him a bracket sequence $$$S$$$. Note that a bracket sequence is a string made of '(' and ')' characters. AmShZ can perform the following operation any number of times: First, he cuts his string $$$S$$$ into three (possibly empty) contiguous substrings $$$A, B$$$ and $$$C$$$. Then, he glues them back by using a '(' and a ')' characters, resulting in a new string $$$S$$$ = $$$A$$$ + "(" + $$$B$$$ + ")" + $$$C$$$.For example, if $$$S$$$ = "))((" and AmShZ cuts it into $$$A$$$ = "", $$$B$$$ = "))", and $$$C$$$ = "((", He will obtain $$$S$$$ = "()))((" as a new string. After performing some (possibly none) operations, AmShZ gives his string to Keshi and asks him to find the initial string. Of course, Keshi might be able to come up with more than one possible initial string. Keshi is interested in finding the lexicographically smallest possible initial string. Your task is to help Keshi in achieving his goal. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input The only line of input contains a single string $$$S$$$xa0— the string after the operations $$$(1le Sle 3 cdot 10^5)$$$. It is guaranteed that the first character of $$$S$$$ is ')'. Output Print the lexicographically smallest possible initial string before operations. Note In the first sample, you can transform ")((())))" into ")(()(())))" by splitting it into ")(", empty string, and "(())))". It can be shown that this is the lexicographically smallest possible initial string
3,300
false
true
false
false
true
false
false
false
false
false
2,594
518D
Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Let's assume that _n_ people stand in the queue for the escalator. At each second one of the two following possibilities takes place: either the first person in the queue enters the escalator with probability _p_, or the first person in the queue doesn't move with probability (1u2009-u2009_p_), paralyzed by his fear of escalators and making the whole queue wait behind him. Formally speaking, the _i_-th person in the queue cannot enter the escalator until people with indices from 1 to _i_u2009-u20091 inclusive enter it. In one second only one person can enter the escalator. The escalator is infinite, so if a person enters it, he never leaves it, that is he will be standing on the escalator at any following second. Ilya needs to count the expected value of the number of people standing on the escalator after _t_ seconds. Your task is to help him solve this complicated task. Input The first line of the input contains three numbers _n_,u2009_p_,u2009_t_ (1u2009≤u2009_n_,u2009_t_u2009≤u20092000, 0u2009≤u2009_p_u2009≤u20091). Numbers _n_ and _t_ are integers, number _p_ is real, given with exactly two digits after the decimal point. Output Print a single real number — the expected number of people who will be standing on the escalator after _t_ seconds. The absolute or relative error mustn't exceed 10u2009-u20096.
1,700
true
false
false
true
false
false
false
false
false
false
7,765
624B
Problem - 624B - 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 *1100 No tag edit access → Contest materials xa0— the number of letters in the alphabet. The next line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— _i_-th of these integers gives the limitation on the number of occurrences of the _i_-th character in the string. Output Print a single integer — the maximum length of the string that meets all the requirements. Examples Input 3 2 5 5 Output 11 Input 3 1 1 2 Output 3 Note For convenience let's consider an alphabet consisting of three letters: "a", "b", "c". In the first sample, some of the optimal strings are: "cccaabbccbb", "aabcbcbcbcb". In the second sample some of the optimal strings are: "acc", "cbc".
1,100
false
true
false
false
false
false
false
false
true
false
7,334
1392D
Omkar is playing his favorite pixelated video game, Bed Wars! In Bed Wars, there are $$$n$$$ players arranged in a circle, so that for all $$$j$$$ such that $$$2 leq j leq n$$$, player $$$j - 1$$$ is to the left of the player $$$j$$$, and player $$$j$$$ is to the right of player $$$j - 1$$$. Additionally, player $$$n$$$ is to the left of player $$$1$$$, and player $$$1$$$ is to the right of player $$$n$$$. Currently, each player is attacking either the player to their left or the player to their right. This means that each player is currently being attacked by either $$$0$$$, $$$1$$$, or $$$2$$$ other players. A key element of Bed Wars strategy is that if a player is being attacked by exactly $$$1$$$ other player, then they should logically attack that player in response. If instead a player is being attacked by $$$0$$$ or $$$2$$$ other players, then Bed Wars strategy says that the player can logically attack either of the adjacent players. Unfortunately, it might be that some players in this game are not following Bed Wars strategy correctly. Omkar is aware of whom each player is currently attacking, and he can talk to any amount of the $$$n$$$ players in the game to make them instead attack another player xa0— i. e. if they are currently attacking the player to their left, Omkar can convince them to instead attack the player to their right; if they are currently attacking the player to their right, Omkar can convince them to instead attack the player to their left. Omkar would like all players to be acting logically. Calculate the minimum amount of players that Omkar needs to talk to so that after all players he talked to (if any) have changed which player they are attacking, all players are acting logically according to Bed Wars strategy. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The descriptions of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$3 leq n leq 2 cdot 10^5$$$) xa0— the amount of players (and therefore beds) in this game of Bed Wars. The second line of each test case contains a string $$$s$$$ of length $$$n$$$. The $$$j$$$-th character of $$$s$$$ is equal to L if the $$$j$$$-th player is attacking the player to their left, and R if the $$$j$$$-th player is attacking the player to their right. 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 one integer: the minimum number of players Omkar needs to talk to to make it so that all players are acting logically according to Bed Wars strategy. It can be proven that it is always possible for Omkar to achieve this under the given constraints. Example Input 5 4 RLRL 6 LRRRRL 8 RLLRRRLL 12 LLLLRRLRRRLL 5 RRRRR Note In the first test case, players $$$1$$$ and $$$2$$$ are attacking each other, and players $$$3$$$ and $$$4$$$ are attacking each other. Each player is being attacked by exactly $$$1$$$ other player, and each player is attacking the player that is attacking them, so all players are already being logical according to Bed Wars strategy and Omkar does not need to talk to any of them, making the answer $$$0$$$. In the second test case, not every player acts logically: for example, player $$$3$$$ is attacked only by player $$$2$$$, but doesn't attack him in response. Omkar can talk to player $$$3$$$ to convert the attack arrangement to LRLRRL, in which you can see that all players are being logical according to Bed Wars strategy, making the answer $$$1$$$.
1,700
false
true
false
true
false
false
false
false
false
false
3,713
799G
Arkady reached the _n_-th level in Township game, so Masha decided to bake a pie for him! Of course, the pie has a shape of convex _n_-gon, i.e. a polygon with _n_ vertices. Arkady decided to cut the pie in two equal in area parts by cutting it by a straight line, so that he can eat one of them and give the other to Masha. There is a difficulty because Arkady has already put a knife at some point of the pie, so he now has to cut the pie by a straight line passing trough this point. Help Arkady: find a line that passes through the point Arkady has put a knife into and cuts the pie into two parts of equal area, or determine that it's impossible. Your program has to quickly answer many queries with the same pie, but different points in which Arkady puts a knife. Input The first line contains two integers _n_ and _q_ (3u2009≤u2009_n_u2009≤u2009104, 1u2009≤u2009_q_u2009≤u2009105)xa0— the number of vertices in the pie and the number of queries. _n_ line follow describing the polygon vertices in clockwise order. The _i_-th of these line contains two integers _x__i_ and _y__i_ (u2009-u2009106u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009106)xa0— the coordinates of the _i_-th vertex. It is guaranteed that the polygon is strictly convex, in particular, no three vertices line on the same line. An empty line follows. _q_ lines follow describing the query points. The _i_-th of these lines contain two integers _x__i_ and _y__i_ (u2009-u2009106u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009106)xa0— the coordinates of the point in which Arkady puts the knife in the _i_-th query. In is guaranteed that in each query the given point is strictly inside the polygon, in particular, is not on its edges. Output For each query print single integerxa0— the polar angle of the line that is the answer for the corresponding query, in radians. The angle should be in the segment [0;π], the angles are measured from the direction of _OX_ axis in counter-clockwise order. For example, the polar angle of the _OY_ axis is . If there is no answer in that query, print -1. If there are several answers, print any of them. Your answer is considered correct if the difference between the areas of the parts divided by the total area of the polygon doesn't exceed 10u2009-u20094 by absolute value. In other words, if _a_ and _b_ are the areas of the parts after the cut, then your answer is correct if and only of . Examples Output 2.67794504460098710000 Input 5 3 6 5 6 3 5 0 0 0 0 55 4 3 3 5 2 Output 0.60228734612690049000 1.27933953226473580000 2.85805511179015910000
3,500
false
false
false
false
true
false
false
true
false
false
6,570
1788D
We play a game with $$$n$$$ dots on a number line. The initial coordinate of the $$$i$$$-th dot is $$$x_i$$$. These coordinates are distinct. Every dot starts moving simultaneously with the same constant speed. Each dot moves in the direction of the closest dot (different from itself) until it meets another dot. In the case of a tie, it goes to the left. Two dots meet if they are in the same coordinate, after that, they stop moving. After enough time, every dot stops moving. The result of a game is the number of distinct coordinates where the dots stop. Because this game is too easy, calculate the sum of results when we play the game for every subset of the given $$$n$$$ dots that has at least two dots. As the result can be very large, print the sum modulo $$$10^9+7$$$. Input The first line contains one integer $$$n$$$ ($$$2 leq n leq 3000$$$). The next line contains $$$n$$$ integers $$$x_1, x_2, ldots, x_n$$$ ($$$1 leq x_1 < x_2 < ldots < x_n leq 10^9$$$), where $$$x_i$$$ represents the initial coordinate of $$$i$$$-th dot. Note In the first example, for a subset of size $$$2$$$, two dots move toward each other, so there is $$$1$$$ coordinate where the dots stop. For a subset of size $$$3$$$, the first dot and third dot move toward the second dot, so there is $$$1$$$ coordinate where the dots stop no matter the direction where the second dot moves. For $$$[1, 2, 4, 6]$$$, the first and second dots move toward each other. For the third dot, initially, the second dot and the fourth dot are the closest dots. Since it is a tie, the third dot moves left. The fourth dot also moves left. So there is $$$1$$$ coordinate where the dots stop, which is $$$1.5$$$. Because there are $$$6$$$ subsets of size $$$2$$$, $$$4$$$ subsets of size $$$3$$$ and one subset of size $$$4$$$, the answer is $$$6 cdot 1 + 4 cdot 1 + 1 = 11$$$. In the second example, for a subset of size $$$5$$$ (when there are dots at $$$1$$$, $$$3$$$, $$$5$$$, $$$11$$$, $$$15$$$), dots at $$$1$$$ and $$$11$$$ will move right and dots at $$$3$$$, $$$5$$$, $$$15$$$ will move left. Dots at $$$1$$$, $$$3$$$, $$$5$$$ will eventually meet at $$$2$$$, and dots at $$$11$$$ and $$$15$$$ will meet at $$$13$$$, so there are $$$2$$$ coordinates where the dots stop.
2,000
true
false
false
false
false
false
true
true
false
false
1,551
105D
The maps in the game are divided into square cells called Geo Panels. Some of these panels are painted. We shall assume that the Geo Panels without color are painted the transparent color. Besides, the map has so-called Geo Symbols. They look like pyramids of different colors (including Geo Symbols of the transparent color). Each Geo Symbol is located on one Geo Panel, and each Geo Panel may contain no more than one Geo Symbol. Geo Symbols can be eliminated. To understand better what happens when a Geo Symbol is eliminated, let us introduce some queue to which we will put the recently eliminated Geo Symbols. Let's put at the head of the queue a Geo Symbol that was eliminated just now. Next, we will repeat the following operation: Extract the Geo Symbol from the queue. Look at the color of the panel containing the given Geo Symbol. If it differs from transparent and differs from the color of the Geo Symbol, then all Geo Panels of this color are repainted in the color of the given Geo Symbol (transparent Geo Symbols repaint the Geo Panels transparent). Repainting is executed in an infinite spiral strictly in the following order starting from the panel, which contained the Geo Symbol: In other words, we select all the panels that need to be repainted and find their numbers in the infinite spiral whose center is placed in the position of the given Geo Symbol. After that, we repaint them in the order of the number's increasing. If a panel contains another Geo Symbol and this panel is being repainted, then the Geo Symbol is removed from the field and placed at the tail of the queue. After repainting the Geo Symbol is completely eliminated and the next Geo Symbol is taken from the head of the queue (if there is any) and the process repeats. The process ends if the queue is empty. See the sample analysis for better understanding. You know the colors of all the Geo Panels and the location of all the Geo Symbols. Determine the number of repaintings, which will occur if you destroy one of the Geo Symbols. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009300) — the height and the width of the map (in cells). Then follow _n_ line; each of them contains _m_ numbers — the Geo Panels' colors. Then follow _n_ more lines; each of them contains _m_ numbers — the Geo Symbols' description. -1 means that the given position contains no Geo Symbol. Otherwise, the number represents the color of the Geo Symbol in the given position. All colors are integers from 0 to 109. 0 represents the transparent color. The last line contains two integers _x_ and _y_ (1u2009≤u2009_x_u2009≤u2009_n_, 1u2009≤u2009_y_u2009≤u2009_m_) — the row and the column where the Geo Symbol is placed that needs to be eliminated. The rows are numbered from top to bottom, the columns are numbered from left to right. Coordinates are 1-based. It is guaranteed that the position with coordinates (_x_,u2009_y_) contains a Geo Symbol. Output Print the single number — the total number of repaintings after the Geo Symbol is eliminated. Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cout stream (you may also use the %I64d specificator). Note All actions of the sample you can see on the following picture: If your browser does not support APNG and you see just static image, you can see GIF version of this image by the following link:
2,700
false
false
true
false
false
false
true
false
false
false
9,463
479C
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly _n_ exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order. According to the schedule, a student can take the exam for the _i_-th subject on the day number _a__i_. However, Valera has made an arrangement with each teacher and the teacher of the _i_-th subject allowed him to take an exam before the schedule time on day _b__i_ (_b__i_u2009<u2009_a__i_). Thus, Valera can take an exam for the _i_-th subject either on day _a__i_, or on day _b__i_. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number _a__i_. Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date. Input The first line contains a single positive integer _n_ (1u2009≤u2009_n_u2009≤u20095000) — the number of exams Valera will take. Each of the next _n_ lines contains two positive space-separated integers _a__i_ and _b__i_ (1u2009≤u2009_b__i_u2009<u2009_a__i_u2009≤u2009109) — the date of the exam in the schedule and the early date of passing the _i_-th exam, correspondingly. Output Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date. Note In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5. In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
1,400
false
true
false
false
false
false
false
false
true
false
7,914
625E
Ostap Bender recently visited frog farm and was inspired to create his own frog game. Number of frogs are places on a cyclic gameboard, divided into _m_ cells. Cells are numbered from 1 to _m_, but the board is cyclic, so cell number 1 goes right after the cell number _m_ in the direction of movement. _i_-th frog during its turn can jump for _a__i_ cells. Frogs move in turns, game starts with a move by frog 1. On its turn _i_-th frog moves _a__i_ cells forward, knocking out all the frogs on its way. If there is a frog in the last cell of the path of the _i_-th frog, that frog is also knocked out. After this the value _a__i_ is decreased by the number of frogs that were knocked out during this turn. If _a__i_ is zero or goes negative, then _i_-th frog doesn't make moves anymore. After frog number 1 finishes its turn, frog number 2 starts to move, then frog number 3 and so on. After the frog number _n_ makes its move, frog 1 starts to move again, then frog 2 and so on this process goes forever. If some frog was already knocked out from the board, we consider that it skips all its moves. Help Ostap to identify, what frogs will stay on the board at the end of a game? Input First line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009100000,u20091u2009≤u2009_m_u2009≤u2009109,u2009_n_u2009≤u2009_m_)xa0— number of frogs and gameboard size, respectively. Following _n_ lines contains frogs descriptionsxa0— two integers _p__i_ and _a__i_ (1u2009≤u2009_p__i_,u2009_a__i_u2009≤u2009_m_)xa0— the number of cell occupied by _i_-th frog initially and initial jump length. All _p__i_ are guaranteed to be distinct. Note In the first sample first frog jumps 1 cell and finishes in cell number 3. Second frog jumps for 3 cells and finishes on cell number 3, knocking out frog number 1. Current jump length for frog number 2 is now 2. Third frog jumps to cell 2, then second frog jumps to cell 5. Third frog in turn finishes in cell 5 and removes frog 2 from the gameboard. Now, it's the only remaining frog in the game. In the second sample first frog jumps 2 cells and knocks out frogs in cells 2 and 3. Its value _a__i_ is now 0. Then fourth frog jumps and knocks out fifth frog and its _a__i_ is now 0 too. These two frogs will remains on the gameboard forever.
2,800
false
true
false
false
true
false
false
false
false
false
7,329
70A
Fangy collects cookies. Once he decided to take a box and put cookies into it in some way. If we take a square _k_u2009×u2009_k_ in size, divided into blocks 1u2009×u20091 in size and paint there the main diagonal together with cells, which lie above it, then the painted area will be equal to the area occupied by one cookie _k_ in size. Fangy also has a box with a square base 2_n_u2009×u20092_n_, divided into blocks 1u2009×u20091 in size. In a box the cookies should not overlap, and they should not be turned over or rotated. See cookies of sizes 2 and 4 respectively on the figure: To stack the cookies the little walrus uses the following algorithm. He takes out of the repository the largest cookie which can fit in some place in the box and puts it there. Everything could be perfect but alas, in the repository the little walrus has infinitely many cookies of size 2 and larger, and there are no cookies of size 1, therefore, empty cells will remain in the box. Fangy wants to know how many empty cells will be left in the end. Input The first line contains a single integer _n_ (0u2009≤u2009_n_u2009≤u20091000). Output Print the single number, equal to the number of empty cells in the box. The answer should be printed modulo 106u2009+u20093. Note If the box possesses the base of 23u2009×u200923 (as in the example), then the cookies will be put there in the following manner:
1,300
true
false
false
false
false
false
false
false
false
false
9,621
1324C
There is a frog staying to the left of the string $$$s = s_1 s_2 ldots s_n$$$ consisting of $$$n$$$ characters (to be more precise, the frog initially stays at the cell $$$0$$$). Each character of $$$s$$$ is either 'L' or 'R'. It means that if the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'L', the frog can jump only to the left. If the frog is staying at the $$$i$$$-th cell and the $$$i$$$-th character is 'R', the frog can jump only to the right. The frog can jump only to the right from the cell $$$0$$$. Note that the frog can jump into the same cell twice and can perform as many jumps as it needs. The frog wants to reach the $$$n+1$$$-th cell. The frog chooses some positive integer value $$$d$$$ before the first jump (and cannot change it later) and jumps by no more than $$$d$$$ cells at once. I.e. if the $$$i$$$-th character is 'L' then the frog can jump to any cell in a range $$$[max(0, i - d); i - 1]$$$, and if the $$$i$$$-th character is 'R' then the frog can jump to any cell in a range $$$[i + 1; min(n + 1; i + d)]$$$. The frog doesn't want to jump far, so your task is to find the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it can jump by no more than $$$d$$$ cells at once. It is guaranteed that it is always possible to reach $$$n+1$$$ from $$$0$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. The $$$i$$$-th test case is described as a string $$$s$$$ consisting of at least $$$1$$$ and at most $$$2 cdot 10^5$$$ characters 'L' and 'R'. It is guaranteed that the sum of lengths of strings over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum s le 2 cdot 10^5$$$). Output For each test case, print the answer — the minimum possible value of $$$d$$$ such that the frog can reach the cell $$$n+1$$$ from the cell $$$0$$$ if it jumps by no more than $$$d$$$ at once. Example Input 6 LRLRRLL L LLR RRRR LLLLLL R Note The picture describing the first test case of the example and one of the possible answers: In the second test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$. In the third test case of the example, the frog can choose $$$d=3$$$, jump to the cell $$$3$$$ from the cell $$$0$$$ and then to the cell $$$4$$$ from the cell $$$3$$$. In the fourth test case of the example, the frog can choose $$$d=1$$$ and jump $$$5$$$ times to the right. In the fifth test case of the example, the frog can only jump directly from $$$0$$$ to $$$n+1$$$. In the sixth test case of the example, the frog can choose $$$d=1$$$ and jump $$$2$$$ times to the right.
1,100
false
true
true
false
true
false
false
true
false
false
4,097
147A
Problem - 147A - Codeforces =============== xa0 . A word is defined as a sequence of consecutive Latin letters. Your task is to add spaces to the text by the following rules: if there is no punctuation mark between two words, then they should be separated by exactly one space there should be no spaces before each punctuation mark there should be exactly one space after each punctuation mark It is guaranteed that there is at least one word between any two punctuation marks. The text begins and ends with a Latin letter. Input The input data contains of a single non-empty line — the text whose length is no more than 10000 characters. Output Print the text, edited according to the rules. In this problem you should follow the output format very strictly. For example, extra space at the end of the output line is considered as wrong answer. Note that a newline character at the end of the line doesn't matter. Examples Input galileo galilei was an italian physicist ,mathematician,astronomer Output galileo galilei was an italian physicist, mathematician, astronomer Input galileo was born in pisa Output galileo was born in pisa
1,300
false
false
true
false
false
false
false
false
false
false
9,288
889E
Problem - 889E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search dp math *3000 No tag edit access → Contest materials ") ") over all nonnegative integers _x_. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200000)xa0— the length of the sequence. The second lines contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091013)xa0— the elements of the sequence. Output Output a single integerxa0— the maximum value of _f_(_x_,u20091) over all nonnegative integers _x_. Examples Input 2 10 5 Output 13 Input 5 5 4 3 2 1 Output 6 Input 4 5 10 5 10 Output 16 Note In the first example you can choose, for example, _x_u2009=u200919. In the second example you can choose, for example, _x_u2009=u20093 or _x_u2009=u20092.
3,000
true
false
false
true
false
false
false
true
false
false
6,189
349B
Problem - 349B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures dp greedy implementation *1700 No tag edit access → Contest materials . The second line contains nine positive integers _a_1,u2009_a_2,u2009...,u2009_a_9 (1u2009≤u2009_a__i_u2009≤u2009105). Output Print the maximum number Igor can write on the fence. If he has too little paint for any digit (so, he cannot write anything), print -1. Examples Input 5 5 4 3 2 1 2 3 4 5 Output 55555 Input 2 9 11 1 12 5 8 9 10 6 Output 33 Input 0 1 1 1 1 1 1 1 1 1 Output -1
1,700
false
true
true
true
true
false
false
false
false
false
8,436
852A
John gave Jack a very hard problem. He wrote a very big positive integer _A_0 on a piece of paper. The number is less than 10200000 . In each step, Jack is allowed to put 'u2009+u2009' signs in between some of the digits (maybe none) of the current number and calculate the sum of the expression. He can perform the same procedure on that sum and so on. The resulting sums can be labeled respectively by _A_1, _A_2 etc. His task is to get to a single digit number. The problem is that there is not much blank space on the paper. There are only three lines of space, so he can't perform more than three steps. Since he wants to fill up the paper completely, he will perform exactly three steps. Jack must not add leading zeros to intermediate results, but he can put 'u2009+u2009' signs in front of digit 0. For example, if the current number is 1000100, 10u2009+u2009001u2009+u200900 is a valid step, resulting in number 11. Input First line contains a positive integer _N_ (1u2009≤u2009_N_u2009≤u2009200000), representing the number of digits of _A_0. Second line contains a string of length _N_ representing positive integer number _A_0. Each character is digit. There will be no leading zeros. Output Output exactly three lines, the steps Jack needs to perform to solve the problem. You can output any sequence of steps which results in a single digit number (and is logically consistent). Every step consists of digits and 'u2009+u2009' signs. Steps should not contain several 'u2009+u2009' signs in a row, whitespaces, or 'u2009+u2009' signs as the first or last character. They also need to be arithmetically consistent. Solution might not be unique. Output any of them in that case. Note In the first sample, Jack can't put 'u2009+u2009' signs anywhere, so he just writes 1 in each line and solves the problem. Here, solution is unique. In the second sample, Jack first puts 'u2009+u2009' between every two consecutive digits, thus getting the result 5u2009+u20098u2009+u20090u2009+u20096u2009=u200919. He does the same on the second step, getting 1u2009+u20099u2009=u200910. Once more, he gets 1u2009+u20090u2009=u20091, so after three steps, the result is 1 and his solution is correct.
2,500
true
false
true
false
false
false
true
false
false
false
6,343
48G
In a far away galaxy there are _n_ inhabited planets numbered with numbers from 1 to _n_. One day the presidents of all the _n_ planets independently from each other came up with an idea of creating the Galaxy Union. Now they need to share this wonderful idea with their galaxymates, that’s why each president is busy working out a project of negotiating with the other presidents. For negotiations between some pairs of the planets there are bidirectional communication channels, each of which is characterized with "dial duration" _t__i_ which, as a rule, takes several hours and exceeds the call duration greatly. Overall the galaxy has _n_ communication channels and they unite all the planets into a uniform network. That means that it is possible to phone to any planet _v_ from any planet _u_, perhaps, using some transitional planets _v_1, _v_2, ..., _v__m_ via the existing channels between _u_ and _v_1, _v_1 and _v_2, ..., _v__m_u2009-u20091 and _v__m_, _v__m_ and _v_. At that the dial duration from _u_ to _v_ will be equal to the sum of dial durations of the used channels. So, every president has to talk one by one to the presidents of all the rest _n_u2009-u20091 planets. At that the negotiations take place strictly consecutively, and until the negotiations with a planet stop, the dial to another one does not begin. As the matter is urgent, from the different ways to call the needed planet every time the quickest one is chosen. Little time is needed to assure another president on the importance of the Galaxy Union, that’s why the duration of the negotiations with each planet can be considered equal to the dial duration time for those planets. As the presidents know nothing about each other’s plans, they do not take into consideration the possibility that, for example, the sought president may call himself or already know about the founding of the Galaxy Union from other sources. The governments of all the _n_ planets asked you to work out the negotiation plans. First you are to find out for every president how much time his supposed negotiations will take. Input The first line contains an integer _n_ (3u2009≤u2009_n_u2009≤u2009200000) which represents the number of planets in the Galaxy and the number of communication channels equal to it. The next _n_ lines contain three integers each _a__i_, _b__i_ and _t__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_,u20091u2009≤u2009_t__i_u2009≤u2009103) that represent the numbers of planet joined by a communication channel and its "dial duration". There can be no more than one communication channel between a pair of planets.
2,700
false
false
false
true
false
false
false
false
false
false
9,731
1119C
Ramesses came to university to algorithms practice, and his professor, who is a fairly known programmer, gave him the following task. You are given two matrices $$$A$$$ and $$$B$$$ of size $$$n imes m$$$, each of which consists of $$$0$$$ and $$$1$$$ only. You can apply the following operation to the matrix $$$A$$$ arbitrary number of times: take any submatrix of the matrix $$$A$$$ that has at least two rows and two columns, and invert the values in its corners (i.e. all corners of the submatrix that contain $$$0$$$, will be replaced by $$$1$$$, and all corners of the submatrix that contain $$$1$$$, will be replaced by $$$0$$$). You have to answer whether you can obtain the matrix $$$B$$$ from the matrix $$$A$$$. An example of the operation. The chosen submatrix is shown in blue and yellow, its corners are shown in yellow. Ramesses don't want to perform these operations by himself, so he asks you to answer this question. A submatrix of matrix $$$M$$$ is a matrix which consist of all elements which come from one of the rows with indices $$$x_1, x_1+1, ldots, x_2$$$ of matrix $$$M$$$ and one of the columns with indices $$$y_1, y_1+1, ldots, y_2$$$ of matrix $$$M$$$, where $$$x_1, x_2, y_1, y_2$$$ are the edge rows and columns of the submatrix. In other words, a submatrix is a set of elements of source matrix which form a solid rectangle (i.e. without holes) with sides parallel to the sides of the original matrix. The corners of the submatrix are cells $$$(x_1, y_1)$$$, $$$(x_1, y_2)$$$, $$$(x_2, y_1)$$$, $$$(x_2, y_2)$$$, where the cell $$$(i,j)$$$ denotes the cell on the intersection of the $$$i$$$-th row and the $$$j$$$-th column. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 500$$$)xa0— the number of rows and the number of columns in matrices $$$A$$$ and $$$B$$$. Each of the next $$$n$$$ lines contain $$$m$$$ integers: the $$$j$$$-th integer in the $$$i$$$-th line is the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$A$$$ ($$$0 leq A_{ij} leq 1$$$). Each of the next $$$n$$$ lines contain $$$m$$$ integers: the $$$j$$$-th integer in the $$$i$$$-th line is the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$B$$$ ($$$0 leq B_{ij} leq 1$$$). Output Print "Yes" (without quotes) if it is possible to transform the matrix $$$A$$$ to the matrix $$$B$$$ using the operations described above, and "No" (without quotes), if it is not possible. You can print each letter in any case (upper or lower).
1,500
true
true
true
false
false
true
false
false
false
false
5,092
342B
Xenia the vigorous detective faced _n_ (_n_u2009≥u20092) foreign spies lined up in a row. We'll consider the spies numbered from 1 to _n_ from left to right. Spy _s_ has an important note. He has to pass the note to spy _f_. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is _x_, he can pass the note to another spy, either _x_u2009-u20091 or _x_u2009+u20091 (if _x_u2009=u20091 or _x_u2009=u2009_n_, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During _m_ steps Xenia watches some spies attentively. Specifically, during step _t__i_ (steps are numbered from 1) Xenia watches spies numbers _l__i_,u2009_l__i_u2009+u20091,u2009_l__i_u2009+u20092,u2009...,u2009_r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got _s_ and _f_. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy _s_ to spy _f_ as quickly as possible (in the minimum number of steps). Input The first line contains four integers _n_, _m_, _s_ and _f_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105;xa01u2009≤u2009_s_,u2009_f_u2009≤u2009_n_;xa0_s_u2009≠u2009_f_;xa0_n_u2009≥u20092). Each of the following _m_ lines contains three integers _t__i_,u2009_l__i_,u2009_r__i_ (1u2009≤u2009_t__i_u2009≤u2009109,u20091u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). It is guaranteed that _t_1u2009<u2009_t_2u2009<u2009_t_3u2009<u2009...u2009<u2009_t__m_. Output Print _k_ characters in a line: the _i_-th character in the line must represent the spies' actions on step _i_. If on step _i_ the spy with the note must pass the note to the spy with a lesser number, the _i_-th character should equal "L". If on step _i_ the spy with the note must pass it to the spy with a larger number, the _i_-th character must equal "R". If the spy must keep the note at the _i_-th step, the _i_-th character must equal "X". As a result of applying the printed sequence of actions spy _s_ must pass the note to spy _f_. The number of printed characters _k_ must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists.
1,500
false
true
true
false
false
false
true
false
false
false
8,467
839D
Winter is here at the North and the White Walkers are close. John Snow has an army consisting of _n_ soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers. He has created a method to know how strong his army is. Let the _i_-th soldier’s strength be _a__i_. For some _k_ he calls _i_1,u2009_i_2,u2009...,u2009_i__k_ a clan if _i_1u2009<u2009_i_2u2009<u2009_i_3u2009<u2009...u2009<u2009_i__k_ and _gcd_(_a__i_1,u2009_a__i_2,u2009...,u2009_a__i__k_)u2009>u20091 . He calls the strength of that clan _k_·_gcd_(_a__i_1,u2009_a__i_2,u2009...,u2009_a__i__k_). Then he defines the strength of his army by the sum of strengths of all possible clans. Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109u2009+u20097). Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009200000)xa0— the size of the army. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091000000)xa0— denoting the strengths of his soldiers. Output Print one integerxa0— the strength of John Snow's army modulo 1000000007 (109u2009+u20097). Note In the first sample the clans are {1},u2009{2},u2009{1,u20092} so the answer will be 1·3u2009+u20091·3u2009+u20092·3u2009=u200912
2,200
true
false
false
true
false
false
false
false
false
false
6,405
1623C
There are $$$n$$$ heaps of stone. The $$$i$$$-th heap has $$$h_i$$$ stones. You want to change the number of stones in the heap by performing the following process once: You go through the heaps from the $$$3$$$-rd heap to the $$$n$$$-th heap, in this order. Let $$$i$$$ be the number of the current heap. You can choose a number $$$d$$$ ($$$0 le 3 cdot d le h_i$$$), move $$$d$$$ stones from the $$$i$$$-th heap to the $$$(i - 1)$$$-th heap, and $$$2 cdot d$$$ stones from the $$$i$$$-th heap to the $$$(i - 2)$$$-th heap. So after that $$$h_i$$$ is decreased by $$$3 cdot d$$$, $$$h_{i - 1}$$$ is increased by $$$d$$$, and $$$h_{i - 2}$$$ is increased by $$$2 cdot d$$$. You can choose different or same $$$d$$$ for different operations. Some heaps may become empty, but they still count as heaps. What is the maximum number of stones in the smallest heap after the process? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2cdot 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$). The second lines of each test case contains $$$n$$$ integers $$$h_1, h_2, h_3, ldots, h_n$$$ ($$$1 le h_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print the maximum number of stones that the smallest heap can contain. Example Input 4 4 1 2 10 100 4 100 100 100 1 5 5 1 1 1 8 6 1 2 3 4 5 6 Note In the first test case, the initial heap sizes are $$$[1, 2, 10, 100]$$$. We can move the stones as follows. move $$$3$$$ stones and $$$6$$$ from the $$$3$$$-rd heap to the $$$2$$$-nd and $$$1$$$ heap respectively. The heap sizes will be $$$[7, 5, 1, 100]$$$; move $$$6$$$ stones and $$$12$$$ stones from the last heap to the $$$3$$$-rd and $$$2$$$-nd heap respectively. The heap sizes will be $$$[7, 17, 7, 82]$$$. In the second test case, the last heap is $$$1$$$, and we can not increase its size. In the third test case, it is better not to move any stones. In the last test case, the final achievable configuration of the heaps can be $$$[3, 5, 3, 4, 3, 3]$$$.
1,600
false
true
false
false
false
false
false
true
false
false
2,503
802G
Problem - 802G - 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 time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output As it's the first of April, Heidi is suspecting that the news she reads today are fake, and she does not want to look silly in front of all the contestants. She knows that a newspiece is fake if it contains heidi as a subsequence. Help Heidi assess whether the given piece is true, but please be discreet about it... Input The first and only line of input contains a single nonempty string _s_ of length at most 1000 composed of lowercase letters (a-z). Output Output YES if the string _s_ contains heidi as a subsequence and NO otherwise. Examples Input abcheaibcdi Output YES Input hiedi Output NO Note A string _s_ contains another string _p_ as a subsequence if it is possible to delete some characters from _s_ and obtain _p_.
800
false
false
true
false
false
false
false
false
false
false
6,561
1103B
This is an interactive problem. Vasya and Petya are going to play the following game: Petya has some positive integer number $$$a$$$. After that Vasya should guess this number using the following questions. He can say a pair of non-negative integer numbers $$$(x, y)$$$. Petya will answer him: "x", if $$$(x bmod a) geq (y bmod a)$$$. "y", if $$$(x bmod a) < (y bmod a)$$$. We define $$$(x bmod a)$$$ as a remainder of division $$$x$$$ by $$$a$$$. Vasya should guess the number $$$a$$$ using no more, than 60 questions. It's guaranteed that Petya has a number, that satisfies the inequality $$$1 leq a leq 10^9$$$. Help Vasya playing this game and write a program, that will guess the number $$$a$$$. Interaction Your program should play several games. Before the start of any game your program should read the string: "start" (without quotes)xa0— the start of the new game. "mistake" (without quotes)xa0— in the previous game, you found the wrong answer. Your program should terminate after reading this string and it will get verdict "Wrong answer". "end" (without quotes)xa0— all games finished. Your program should terminate after reading this string. After reading the string "start" (without quotes) the new game starts. At the beginning, your program should ask several questions about pairs of non-negative integer numbers $$$(x, y)$$$. You can only ask the numbers, that satisfy the inequalities $$$0 leq x, y leq 2 cdot 10^9$$$. To ask a question print "? x y" (without quotes). As the answer, you should read one symbol: "x" (without quotes), if $$$(x bmod a) geq (y bmod a)$$$. "y" (without quotes), if $$$(x bmod a) < (y bmod a)$$$. "e" (without quotes)xa0— you asked more than $$$60$$$ questions. Your program should terminate after reading this string and it will get verdict "Wrong answer". After your program asked several questions your program should print the answer in form "! a" (without quotes). You should print the number $$$a$$$ satisfying the inequalities $$$1 leq a leq 10^9$$$. It's guaranteed that Petya's number $$$a$$$ satisfied this condition. After that, the current game will finish. We recall that your program can't ask more than $$$60$$$ questions during one game. If your program doesn't terminate after reading "mistake" (without quotes), "end" (without quotes) or "e" (without quotes), it can get any verdict, because it will continue reading from closed input. Also, if your program prints answer or question in the incorrect format it can get any verdict, too. Be careful. Don't forget to flush the output after printing questions and answers. To flush the output, you can use: fflush(stdout) in C++. System.out.flush() in Java. stdout.flush() in Python. flush(output) in Pascal. See the documentation for other languages. It's guaranteed that you should play at least $$$1$$$ and no more than $$$100$$$ games. Hacks: In hacks, you can use only one game. To hack a solution with Petya's number $$$a$$$ ($$$1 leq a leq 10^9$$$) in the first line you should write a single number $$$1$$$ and in the second line you should write a single number $$$a$$$. Note In the first test, you should play $$$3$$$ games with Petya's numbers $$$1$$$, $$$2$$$ and $$$3$$$. In the first game, Petya will answer "x" (without quotes) to any question, because $$$(x bmod 1) = 0$$$ for any integer $$$x$$$. In the second game, if you will ask pair $$$(0, 0)$$$, the answer will be "x" (without quotes), because $$$(0 bmod 2) geq (0 bmod 2)$$$. But if you will ask pair $$$(2, 5)$$$, the answer will be "y" (without quotes), because $$$(2 bmod 2) < (5 bmod 2)$$$, because $$$(2 bmod 2) = 0$$$ and $$$(5 bmod 2) = 1$$$.
2,000
false
false
false
false
false
true
false
true
false
false
5,186
1611A
Polycarp has an integer $$$n$$$ that doesn't contain the digit 0. He can do the following operation with his number several (possibly zero) times: Reverse the prefix of length $$$l$$$ (in other words, $$$l$$$ leftmost digits) of $$$n$$$. So, the leftmost digit is swapped with the $$$l$$$-th digit from the left, the second digit from the left swapped with ($$$l-1$$$)-th left, etc. For example, if $$$n=123456789$$$ and $$$l=5$$$, then the new value of $$$n$$$ will be $$$543216789$$$. Note that for different operations, the values of $$$l$$$ can be different. The number $$$l$$$ can be equal to the length of the number $$$n$$$xa0— in this case, the whole number $$$n$$$ is reversed. Polycarp loves even numbers. Therefore, he wants to make his number even. At the same time, Polycarp is very impatient. He wants to do as few operations as possible. Help Polycarp. Determine the minimum number of operations he needs to perform with the number $$$n$$$ to make it even or determine that this is impossible. You need to answer $$$t$$$ independent test cases. Input The first line contains the number $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each of the following $$$t$$$ lines contains one integer $$$n$$$ ($$$1 le n < 10^9$$$). It is guaranteed that the given number doesn't contain the digit 0. Output Print $$$t$$$ lines. On each line print one integerxa0— the answer to the corresponding test case. If it is impossible to make an even number, print -1. Note In the first test case, $$$n=3876$$$, which is already an even number. Polycarp doesn't need to do anything, so the answer is $$$0$$$. In the second test case, $$$n=387$$$. Polycarp needs to do $$$2$$$ operations: 1. Select $$$l=2$$$ and reverse the prefix $$$underline{38}7$$$. The number $$$n$$$ becomes $$$837$$$. This number is odd. 2. Select $$$l=3$$$ and reverse the prefix $$$underline{837}$$$. The number $$$n$$$ becomes $$$738$$$. This number is even. It can be shown that $$$2$$$ is the minimum possible number of operations that Polycarp needs to do with his number to make it even. In the third test case, $$$n=4489$$$. Polycarp can reverse the whole number (choose a prefix of length $$$l=4$$$). It will become $$$9844$$$ and this is an even number. In the fourth test case, $$$n=3$$$. No matter how hard Polycarp tried, he would not be able to make an even number.
800
true
false
false
false
false
true
false
false
false
false
2,591
623A
One day student Vasya was sitting on a lecture and mentioned a string _s_1_s_2... _s__n_, consisting of letters "a", "b" and "c" that was written on his desk. As the lecture was boring, Vasya decided to complete the picture by composing a graph _G_ with the following properties: _G_ has exactly _n_ vertices, numbered from 1 to _n_. For all pairs of vertices _i_ and _j_, where _i_u2009≠u2009_j_, there is an edge connecting them if and only if characters _s__i_ and _s__j_ are either equal or neighbouring in the alphabet. That is, letters in pairs "a"-"b" and "b"-"c" are neighbouring, while letters "a"-"c" are not. Vasya painted the resulting graph near the string and then erased the string. Next day Vasya's friend Petya came to a lecture and found some graph at his desk. He had heard of Vasya's adventure and now he wants to find out whether it could be the original graph _G_, painted by Vasya. In order to verify this, Petya needs to know whether there exists a string _s_, such that if Vasya used this _s_ he would produce the given graph _G_. Input The first line of the input contains two integers _n_ and _m_ xa0— the number of vertices and edges in the graph found by Petya, respectively. Each of the next _m_ lines contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_,u2009_u__i_u2009≠u2009_v__i_)xa0— the edges of the graph _G_. It is guaranteed, that there are no multiple edges, that is any pair of vertexes appear in this list no more than once. Output In the first line print "Yes" (without the quotes), if the string _s_ Petya is interested in really exists and "No" (without the quotes) otherwise. If the string _s_ exists, then print it on the second line of the output. The length of _s_ must be exactly _n_, it must consist of only letters "a", "b" and "c" only, and the graph built using this string must coincide with _G_. If there are multiple possible answers, you may print any of them. Note In the first sample you are given a graph made of two vertices with an edge between them. So, these vertices can correspond to both the same and adjacent letters. Any of the following strings "aa", "ab", "ba", "bb", "bc", "cb", "cc" meets the graph's conditions. In the second sample the first vertex is connected to all three other vertices, but these three vertices are not connected with each other. That means that they must correspond to distinct letters that are not adjacent, but that is impossible as there are only two such letters: a and c.
1,800
false
false
false
false
false
true
false
false
false
true
7,340
1153B
Luckily, Serval got onto the right bus, and he came to the kindergarten on time. After coming to kindergarten, he found the toy bricks very funny. He has a special interest to create difficult problems for others to solve. This time, with many $$$1 imes 1 imes 1$$$ toy bricks, he builds up a 3-dimensional object. We can describe this object with a $$$n imes m$$$ matrix, such that in each cell $$$(i,j)$$$, there are $$$h_{i,j}$$$ bricks standing on the top of each other. However, Serval doesn't give you any $$$h_{i,j}$$$, and just give you the front view, left view, and the top view of this object, and he is now asking you to restore the object. Note that in the front view, there are $$$m$$$ columns, and in the $$$i$$$-th of them, the height is the maximum of $$$h_{1,i},h_{2,i},dots,h_{n,i}$$$. It is similar for the left view, where there are $$$n$$$ columns. And in the top view, there is an $$$n imes m$$$ matrix $$$t_{i,j}$$$, where $$$t_{i,j}$$$ is $$$0$$$ or $$$1$$$. If $$$t_{i,j}$$$ equals $$$1$$$, that means $$$h_{i,j}>0$$$, otherwise, $$$h_{i,j}=0$$$. However, Serval is very lonely because others are bored about his unsolvable problems before, and refused to solve this one, although this time he promises there will be at least one object satisfying all the views. As his best friend, can you have a try? Input The first line contains three positive space-separated integers $$$n, m, h$$$ ($$$1leq n, m, h leq 100$$$)xa0— the length, width and height. The second line contains $$$m$$$ non-negative space-separated integers $$$a_1,a_2,dots,a_m$$$, where $$$a_i$$$ is the height in the $$$i$$$-th column from left to right of the front view ($$$0leq a_i leq h$$$). The third line contains $$$n$$$ non-negative space-separated integers $$$b_1,b_2,dots,b_n$$$ ($$$0leq b_j leq h$$$), where $$$b_j$$$ is the height in the $$$j$$$-th column from left to right of the left view. Each of the following $$$n$$$ lines contains $$$m$$$ numbers, each is $$$0$$$ or $$$1$$$, representing the top view, where $$$j$$$-th number of $$$i$$$-th row is $$$1$$$ if $$$h_{i, j}>0$$$, and $$$0$$$ otherwise. It is guaranteed that there is at least one structure satisfying the input.
1,200
false
true
false
false
false
true
false
false
false
false
4,948
337C
Manao is taking part in a quiz. The quiz consists of _n_ consecutive questions. A correct answer gives one point to the player. The game also has a counter of consecutive correct answers. When the player answers a question correctly, the number on this counter increases by 1. If the player answers a question incorrectly, the counter is reset, that is, the number on it reduces to 0. If after an answer the counter reaches the number _k_, then it is reset, and the player's score is doubled. Note that in this case, first 1 point is added to the player's score, and then the total score is doubled. At the beginning of the game, both the player's score and the counter of consecutive correct answers are set to zero. Manao remembers that he has answered exactly _m_ questions correctly. But he does not remember the order in which the questions came. He's trying to figure out what his minimum score may be. Help him and compute the remainder of the corresponding number after division by 1000000009 (109u2009+u20099). Input The single line contains three space-separated integers _n_, _m_ and _k_ (2u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009109;xa00u2009≤u2009_m_u2009≤u2009_n_). Output Print a single integer — the remainder from division of Manao's minimum possible score in the quiz by 1000000009 (109u2009+u20099). Note Sample 1. Manao answered 3 questions out of 5, and his score would double for each two consecutive correct answers. If Manao had answered the first, third and fifth questions, he would have scored as much as 3 points. Sample 2. Now Manao answered 4 questions. The minimum possible score is obtained when the only wrong answer is to the question 4. Also note that you are asked to minimize the score and not the remainder of the score modulo 1000000009. For example, if Manao could obtain either 2000000000 or 2000000020 points, the answer is 2000000000xa0_mod_xa01000000009, even though 2000000020xa0_mod_xa01000000009 is a smaller number.
1,600
true
true
false
false
false
false
false
true
false
false
8,485
1970A3
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Helvetic Coding Contest 2024 online mirror (teams allowed, unrated) 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 trees *2400 No tag edit access → Contest materials Announcement (en) Statements PDF (en) Tutorial PDF (en) Tutorial #2 (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST A3. Balanced Unshuffle (Hard) time limit per test2 seconds memory limit per test256 megabytes The only difference with the medium version is the maximum length of the input. A parentheses sequence is a string consisting of characters "(" and ")", for example "(()((". A balanced parentheses sequence is a parentheses sequence which can become a valid mathematical expression after inserting numbers and operations into it, for example "(()(()))". The balance of a parentheses sequence is defined as the number of opening parentheses "(" minus the number of closing parentheses ")". For example, the balance of the sequence "(()((" is 3. A balanced parentheses sequence can also be defined as a parentheses sequence with balance 0 such that each of its prefixes has a non-negative balance. We define the balanced shuffle operation that takes a parentheses sequence and returns a parentheses sequence as follows: first, for every character of the input sequence, we compute the balance of the prefix of the sequence before that character and write those down in a table together with the positions of the characters in the input sequence, for example: Prefix balance 0 1 2 1 2 3 2 1 Position 1 2 3 4 5 6 7 8 Character ( ( ) ( ( ) ) ) Then, we sort the columns of this table in increasing order of prefix balance, breaking ties in decreasing order of position. In the above example, we get: Prefix balance 0 1 1 1 2 2 2 3 Position 1 8 4 2 7 5 3 6 Character ( ) ( ( ) ( ) ) The last row of this table forms another parentheses sequence, in this case "()(()())". This sequence is called the result of applying the balanced shuffle operation to the input sequence, or in short just the balanced shuffle of the input sequence. Surprisingly, it turns out that the balanced shuffle of any balanced parentheses sequence is always another balanced parentheses sequence (we will omit the proof for brevity). Even more surprisingly, the balanced shuffles of two different balanced parentheses sequences are always different, therefore the balanced shuffle operation is a bijection on the set of balanced parentheses sequences of any given length (we will omit this proof, too). You are given a balanced parentheses sequence. Find its preimage: the balanced parentheses sequence the balanced shuffle of which is equal to the given sequence. Input The only line of input contains a string $$$s$$$ consisting only of characters "(" and ")". This string is guaranteed to be a non-empty balanced parentheses sequence with its length not exceeding $$$500,000$$$. Output Print the balanced parentheses sequence $$$t$$$ such that the balanced shuffle of $$$t$$$ is equal to $$$s$$$. It is guaranteed that the answer always exists and is unique. Example input ()(()()) output (()(())) Codeforces (c)
2,400
false
false
false
false
false
true
false
false
false
false
484
2014E
In the humble act of meeting, joy doth unfold like a flower in bloom. Absence makes the heart grow fonder. Marian sold her last ware at the Market at the same time Robin finished training at the Major Oak. They couldn't wait to meet, so they both start without delay. The travel network is represented as $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$ and $$$m$$$ edges. The $$$i$$$-th edge connects vertices $$$u_i$$$ and $$$v_i$$$, and takes $$$w_i$$$ seconds to travel (all $$$w_i$$$ are even). Marian starts at vertex $$$1$$$ (Market) and Robin starts at vertex $$$n$$$ (Major Oak). In addition, $$$h$$$ of the $$$n$$$ vertices each has a single horse available. Both Marian and Robin are capable riders, and could mount horses in no time (i.e. in $$$0$$$ seconds). Travel times are halved when riding. Once mounted, a horse lasts the remainder of the travel. Meeting must take place on a vertex (i.e. not on an edge). Either could choose to wait on any vertex. Output the earliest time Robin and Marian can meet. If vertices $$$1$$$ and $$$n$$$ are disconnected, output $$$-1$$$ as the meeting is cancelled. Input The first line of the input contains a single integer $$$t$$$ ($$$1leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case consists of three integers $$$n$$$, $$$m$$$, $$$h$$$ ($$$2 le n le 2 cdot 10^5, ;1 le m le 2 cdot 10^5, ; 1 le h le n$$$)xa0— the number of vertices, the number of edges and the number of vertices that have a single horse. The second line of each test case contains $$$h$$$ distinct integers $$$a_1, a_2, ldots, a_h$$$ ($$$1 le a_i le n$$$)xa0— the vertices that have a single horse available. Then follow $$$m$$$ lines of each test case, each with three integers $$$u_i$$$, $$$v_i$$$, $$$w_i$$$ ($$$1le u_i,v_i le n, ; 2le w_i le 10^6$$$)xa0— meaning that there is an edge between vertices $$$u_i$$$ and $$$v_i$$$ with travel cost $$$w_i$$$ seconds without a horse. There are no self loops or multiple edges. By good fortune, all $$$w_i$$$ are even integers. It is guaranteed that the sums of both $$$n$$$ and $$$m$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer, the earliest time Robin and Marian can meet. If it is impossible for them to meet, output $$$-1$$$. Example Input 6 2 1 1 1 1 2 10 3 1 2 2 3 1 2 10 3 3 1 2 1 2 4 1 3 10 2 3 6 4 3 2 2 3 1 2 10 2 3 18 3 4 16 3 2 1 2 1 2 4 1 3 16 7 7 1 3 1 5 2 2 6 12 1 2 12 6 4 8 7 3 4 6 3 4 7 6 4 Note In the first test case, Marian rides from vertex $$$1$$$ to vertex $$$2$$$, Robin waits. In the second test case, vertices $$$1$$$ and $$$3$$$ are not connected. In the third test case, both Marian and Robin travel to vertex $$$2$$$ to meet. In the fourth test case, Marian travels to vertex $$$2$$$ without a horse, mounts the horse at vertex $$$2$$$ and rides to vertex $$$3$$$ to meet Robin. In the fifth test case, Marian travels to vertex $$$2$$$ without a horse, mounts the horse at vertex $$$2$$$ and rides back to vertex $$$1$$$ and then to vertex $$$3$$$. Robin waits.
1,800
false
false
false
false
false
false
false
false
false
true
167
161A
The Two-dimensional kingdom is going through hard times... This morning the Three-Dimensional kingdom declared war on the Two-dimensional one. This (possibly armed) conflict will determine the ultimate owner of the straight line. The Two-dimensional kingdom has a regular army of _n_ people. Each soldier registered himself and indicated the desired size of the bulletproof vest: the _i_-th soldier indicated size _a__i_. The soldiers are known to be unpretentious, so the command staff assumes that the soldiers are comfortable in any vests with sizes from _a__i_u2009-u2009_x_ to _a__i_u2009+u2009_y_, inclusive (numbers _x_,u2009_y_u2009≥u20090 are specified). The Two-dimensional kingdom has _m_ vests at its disposal, the _j_-th vest's size equals _b__j_. Help mobilize the Two-dimensional kingdom's army: equip with vests as many soldiers as possible. Each vest can be used only once. The _i_-th soldier can put on the _j_-th vest, if _a__i_u2009-u2009_x_u2009≤u2009_b__j_u2009≤u2009_a__i_u2009+u2009_y_. Input The first input line contains four integers _n_, _m_, _x_ and _y_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105, 0u2009≤u2009_x_,u2009_y_u2009≤u2009109) — the number of soldiers, the number of vests and two numbers that specify the soldiers' unpretentiousness, correspondingly. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) in non-decreasing order, separated by single spaces — the desired sizes of vests. The third line contains _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (1u2009≤u2009_b__j_u2009≤u2009109) in non-decreasing order, separated by single spaces — the sizes of the available vests. Output In the first line print a single integer _k_ — the maximum number of soldiers equipped with bulletproof vests. In the next _k_ lines print _k_ pairs, one pair per line, as "_u__i_ _v__i_" (without the quotes). Pair (_u__i_, _v__i_) means that soldier number _u__i_ must wear vest number _v__i_. Soldiers and vests are numbered starting from one in the order in which they are specified in the input. All numbers of soldiers in the pairs should be pairwise different, all numbers of vests in the pairs also should be pairwise different. You can print the pairs in any order. If there are multiple optimal answers, you are allowed to print any of them. Examples Input 5 3 0 0 1 2 3 3 4 1 3 5 Note In the first sample you need the vests' sizes to match perfectly: the first soldier gets the first vest (size 1), the third soldier gets the second vest (size 3). This sample allows another answer, which gives the second vest to the fourth soldier instead of the third one. In the second sample the vest size can differ from the desired size by at most 2 sizes, so all soldiers can be equipped.
1,300
false
true
false
false
false
false
true
true
false
false
9,230
118B
Problem - 118B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms implementation *1000 No tag edit access → Contest materials ") . Output Print a picture for the given _n_. You should strictly observe the number of spaces before the first digit on each line. Every two adjacent digits in the same line should be separated by exactly one space. There should be no spaces after the last digit at the end of each line. Examples Input 2 Output 0 0 1 0 0 1 2 1 0 0 1 0 0 Input 3 Output 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0
1,000
false
false
true
false
false
true
false
false
false
false
9,415
540D
Problem - 540D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp probabilities *1900 No tag edit access → Contest materials , and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper, and paper kills a rock. Your task is to determine for each species what is the probability that this species will be the only one to inhabit this island after a long enough period of time. Input The single line contains three integers _r_, _s_ and _p_ (1u2009≤u2009_r_,u2009_s_,u2009_p_u2009≤u2009100)xa0— the original number of individuals in the species of rock, scissors and paper, respectively. Output Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10u2009-u20099. Examples Input 2 2 2 Output 0.333333333333 0.333333333333 0.333333333333 Input 2 1 2 Output 0.150000000000 0.300000000000 0.550000000000 Input 1 1 3 Output 0.057142857143 0.657142857143 0.285714285714
1,900
false
false
false
true
false
false
false
false
false
false
7,677
44I
Little Masha loves arranging her toys into piles on the floor. And she also hates it when somebody touches her toys. One day Masha arranged all her _n_ toys into several piles and then her elder brother Sasha came and gathered all the piles into one. Having seen it, Masha got very upset and started crying. Sasha still can't calm Masha down and mom is going to come home soon and punish Sasha for having made Masha crying. That's why he decides to restore the piles' arrangement. However, he doesn't remember at all the way the toys used to lie. Of course, Masha remembers it, but she can't talk yet and can only help Sasha by shouting happily when he arranges the toys in the way they used to lie. That means that Sasha will have to arrange the toys in every possible way until Masha recognizes the needed arrangement. The relative position of the piles and toys in every pile is irrelevant, that's why the two ways of arranging the toys are considered different if can be found two such toys that when arranged in the first way lie in one and the same pile and do not if arranged in the second way. Sasha is looking for the fastest way of trying all the ways because mom will come soon. With every action Sasha can take a toy from any pile and move it to any other pile (as a result a new pile may appear or the old one may disappear). Sasha wants to find the sequence of actions as a result of which all the pile arrangement variants will be tried exactly one time each. Help Sasha. As we remember, initially all the toys are located in one pile. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u200910) — the number of toys. Output In the first line print the number of different variants of arrangement of toys into piles. Then print all the ways of arranging toys into piles in the order in which Sasha should try them (i.e. every next way must result from the previous one through the operation described in the statement). Every way should be printed in the following format. In every pile the toys should be arranged in ascending order of the numbers. Then the piles should be sorted in ascending order of the numbers of the first toys there. Output every way on a single line. Cf. the example to specify the output data format. If the solution is not unique, output any of them. Examples Output 5 {1,2,3} {1,2},{3} {1},{2,3} {1},{2},{3} {1,3},{2}
2,300
false
false
false
false
false
false
true
false
false
false
9,761
2001A
You are given a cyclic array $$$a_1, a_2, ldots, a_n$$$. You can perform the following operation on $$$a$$$ at most $$$n - 1$$$ times: Let $$$m$$$ be the current size of $$$a$$$, you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, $$$a_m$$$ and $$$a_1$$$ are adjacent and $$$a_m$$$ is the previous one), and delete exactly one of them. In other words, choose an integer $$$i$$$ ($$$1 leq i leq m$$$) where $$$a_i leq a_{(i bmod m) + 1}$$$ holds, and delete exactly one of $$$a_i$$$ or $$$a_{(i bmod m) + 1}$$$ from $$$a$$$. Your goal is to find the minimum number of operations needed to make all elements in $$$a$$$ equal. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$) — the elements of array $$$a$$$. Output For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in $$$a$$$ equal. Example Input 7 1 1 3 1 2 3 3 1 2 2 5 5 4 3 2 1 6 1 1 2 2 3 3 8 8 7 6 3 8 7 6 3 6 1 1 4 5 1 4 Note In the first test case, there is only one element in $$$a$$$, so we can't do any operation. In the second test case, we can perform the following operations to make all elements in $$$a$$$ equal: choose $$$i = 2$$$, delete $$$a_3$$$, then $$$a$$$ would become $$$[1, 2]$$$. choose $$$i = 1$$$, delete $$$a_1$$$, then $$$a$$$ would become $$$[2]$$$. It can be proven that we can't make all elements in $$$a$$$ equal using fewer than $$$2$$$ operations, so the answer is $$$2$$$.
800
false
true
true
false
false
false
false
false
false
false
255
1694A
Define the score of some binary string $$$T$$$ as the absolute difference between the number of zeroes and ones in it. (for example, $$$T=$$$ 010001 contains $$$4$$$ zeroes and $$$2$$$ ones, so the score of $$$T$$$ is $$$4-2 = 2$$$). Define the creepiness of some binary string $$$S$$$ as the maximum score among all of its prefixes (for example, the creepiness of $$$S=$$$ 01001 is equal to $$$2$$$ because the score of the prefix $$$S[1 ldots 4]$$$ is $$$2$$$ and the rest of the prefixes have a score of $$$2$$$ or less). Given two integers $$$a$$$ and $$$b$$$, construct a binary string consisting of $$$a$$$ zeroes and $$$b$$$ ones with the minimum possible creepiness. Input The first line contains a single integer $$$t$$$ $$$(1le tle 1000)$$$ xa0— the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$ 1 le a, b le 100$$$) xa0— the numbers of zeroes and ones correspondingly. Output For each test case, print a binary string consisting of $$$a$$$ zeroes and $$$b$$$ ones with the minimum possible creepiness. If there are multiple answers, print any of them. Example Input 5 1 1 1 2 5 2 4 5 3 7 Output 10 011 0011000 101010101 0001111111 Note In the first test case, the score of $$$S[1 ldots 1]$$$ is $$$1$$$, and the score of $$$S[1 ldots 2]$$$ is $$$0$$$. In the second test case, the minimum possible creepiness is $$$1$$$ and one of the other answers is 101. In the third test case, the minimum possible creepiness is $$$3$$$ and one of the other answers is 0001100.
800
false
true
true
false
false
false
false
false
false
false
2,116
727E
Several years ago Tolya had _n_ computer games and at some point of time he decided to burn them to CD. After that he wrote down the names of the games one after another in a circle on the CD in clockwise order. The names were distinct, the length of each name was equal to _k_. The names didn't overlap. Thus, there is a cyclic string of length _n_·_k_ written on the CD. Several years have passed and now Tolya can't remember which games he burned to his CD. He knows that there were _g_ popular games that days. All of the games he burned were among these _g_ games, and no game was burned more than once. You have to restore any valid list of games Tolya could burn to the CD several years ago. Input The first line of the input contains two positive integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_k_u2009≤u2009105)xa0— the amount of games Tolya burned to the CD, and the length of each of the names. The second line of the input contains one string consisting of lowercase English lettersxa0— the string Tolya wrote on the CD, split in arbitrary place. The length of the string is _n_·_k_. It is guaranteed that the length is not greater than 106. The third line of the input contains one positive integer _g_ (_n_u2009≤u2009_g_u2009≤u2009105)xa0— the amount of popular games that could be written on the CD. It is guaranteed that the total length of names of all popular games is not greater than 2·106. Each of the next _g_ lines contains a single stringxa0— the name of some popular game. Each name consists of lowercase English letters and has length _k_. It is guaranteed that the names are distinct. Output If there is no answer, print "NO" (without quotes). Otherwise, print two lines. In the first line print "YES" (without quotes). In the second line, print _n_ integersxa0— the games which names were written on the CD. You should print games in the order they could have been written on the CD, it means, in clockwise order. You can print games starting from any position. Remember, that no game was burned to the CD more than once. If there are several possible answers, print any of them. Examples Input 4 2 aabbccdd 4 dd ab bc cd
2,300
false
false
false
false
true
false
false
false
false
false
6,896
1477F
Nezzar buys his favorite snackxa0— $$$n$$$ chocolate bars with lengths $$$l_1,l_2,ldots,l_n$$$. However, chocolate bars might be too long to store them properly! In order to solve this problem, Nezzar designs an interesting process to divide them into small pieces. Firstly, Nezzar puts all his chocolate bars into a black box. Then, he will perform the following operation repeatedly until the maximum length over all chocolate bars does not exceed $$$k$$$. Nezzar picks a chocolate bar from the box with probability proportional to its length $$$x$$$. After step $$$1$$$, Nezzar uniformly picks a real number $$$r in (0,x)$$$ and divides the chosen chocolate bar into two chocolate bars with lengths $$$r$$$ and $$$x-r$$$. Lastly, he puts those two new chocolate bars into the black box. Nezzar now wonders, what is the expected number of operations he will perform to divide his chocolate bars into small pieces. It can be shown that the answer can be represented as $$$frac{P}{Q}$$$, where $$$P$$$ and $$$Q$$$ are coprime integers and $$$Q ot equiv 0$$$ ($$$bmod 998,244,353$$$). Print the value of $$$Pcdot Q^{-1} mod 998,244,353$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 50, 1 le k le 2000$$$). The second line contains $$$n$$$ integers $$$l_1, l_2, ldots, l_n$$$ ($$$1 le l_i$$$, $$$sum_{i=1}^{n} l_i le 2000$$$). Output Print a single integer — the expected number of operations Nezzar will perform to divide his chocolate bars into small pieces modulo $$$998,244,353$$$. Examples Input 10 33 10 20 30 40 50 60 70 80 90 100
3,500
true
false
false
false
false
false
false
false
false
false
3,285
762A
Problem - 762A - Codeforces =============== xa0 ]( "Announcement") . Output If _n_ has less than _k_ divisors, output -1. Otherwise, output the _k_-th smallest divisor of _n_. Examples Input 4 2 Output 2 Input 5 3 Output -1 Input 12 5 Output 6 Note In the first example, number 4 has three divisors: 1, 2 and 4. The second one is 2. In the second example, number 5 has only two divisors: 1 and 5. The third divisor doesn't exist, so the answer is -1.
1,400
true
false
false
false
false
false
false
false
false
false
6,743
1887B
Berland is a country with ancient history, where roads were built and destroyed for centuries. It is known that there always were $$$n$$$ cities in Berland. You also have records of $$$t$$$ key moments in the history of the country, numbered from $$$1$$$ to $$$t$$$. Each record contains a list of bidirectional roads between some pairs of cities, which could be used for travel in Berland at a specific moment in time. You have discovered a time machine that transports you between key moments. Unfortunately, you cannot choose what point in time to end up at, but you know the order consisting of $$$k$$$ moments in time $$$a_{i}$$$, in which the machine will transport you. Since there is little time between the travels, when you find yourself in the next key moment in time (including after the last time travel), you can travel on at most one existing road at that moment, coming out from the city you were in before time travel. Currently, you are in city $$$1$$$, and the time machine has already transported you to moment $$$a_{1}$$$. You want to reach city $$$n$$$ as quickly as possible. Determine the minimum number of time travels, including the first one, that you need to make in order to reach city $$$n$$$. Input The first line contains two integers $$$n$$$ and $$$t$$$ ($$$2 le n le 2 cdot 10^5, 1 le t le 2 cdot 10^5$$$)xa0— the number of cities in Berland and the number of records about key moments in history. Then follows the description of each of the $$$t$$$ records. The first line of each record description contains a single integer $$$m_{i}$$$ ($$$0 le m_{i} le min left(frac{n(n-1)}{2}, 2 cdot 10^5 ight)$$$)xa0— the number of roads in the $$$i$$$-th record. Each of the next $$$m_i$$$ lines contains two integers $$$v_{j}$$$ and $$$u_{j}$$$ ($$$1 le v_{j}, u_{j} le n$$$, $$$v_{j} eq u_{j}$$$)xa0— the numbers of cities connected by the $$$j$$$-th road in the $$$i$$$-th key moment in history. The next line contains a single integer $$$k$$$ ($$$1 le k le 2 cdot 10^5$$$)xa0— the number of time moments between which movements will occur. The next line contains $$$k$$$ integers $$$a_1, a_2, ldots, a_k$$$ ($$$1 le a_{i} le t$$$)xa0— the time moments at which you will be after each movement. It is guaranteed that the sum of $$$m_{i}$$$ does not exceed $$$2 cdot 10^5$$$. It is guaranteed that each unordered pair $$$(u, v)$$$ occurs in the road description for one record no more than once. Output Output a single integer — the minimum number of time travels required to reach city $$$n$$$ from city $$$1$$$, or $$$-1$$$ if it is impossible. Note that movement to time moment $$$a_{1}$$$ is also considered a movement. Examples Input 5 2 4 1 2 2 3 3 4 4 5 2 2 3 3 5 6 2 1 2 1 2 1 Input 5 2 3 1 2 3 1 4 3 2 2 1 4 5 5 1 2 1 1 1 Note In the first example, you are in city $$$1$$$ and move to moment $$$a_{1} = 2$$$. Since there are no suitable roads to pass, you do nothing and move to moment $$$a_{2} = 1$$$, after which you travel along the road $$$(1, 2)$$$. Moving to moment $$$a_{3} = 2$$$, you travel along the road $$$(2, 3)$$$. Moving to moment $$$a_{4} = 1$$$, you stay in city $$$3$$$ and make the next time travel. At time moment $$$a_{5} = 2$$$, you travel along the road $$$(3, 5)$$$ and end up in the final city after $$$5$$$ time travels. In the second example, it can be shown that it is impossible to reach city $$$5$$$ with the given time travels.
1,900
false
false
false
false
false
false
false
true
false
true
971
920E
You are given an undirected graph consisting of _n_ vertices and edges. Instead of giving you the edges that exist in the graph, we give you _m_ unordered pairs (_x_,u2009_y_) such that there is no edge between _x_ and _y_, and if some pair of vertices is not listed in the input, then there is an edge between these vertices. You have to find the number of connected components in the graph and the size of each component. A connected component is a set of vertices _X_ such that for every two vertices from this set there exists at least one path in the graph connecting these vertices, but adding any other vertex to _X_ violates this rule. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009200000, ). Then _m_ lines follow, each containing a pair of integers _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_, _x_u2009≠u2009_y_) denoting that there is no edge between _x_ and _y_. Each pair is listed at most once; (_x_,u2009_y_) and (_y_,u2009_x_) are considered the same (so they are never listed in the same test). If some pair of vertices is not listed in the input, then there exists an edge between those vertices. Output Firstly print _k_ — the number of connected components in this graph. Then print _k_ integers — the sizes of components. You should output these integers in non-descending order.
2,100
false
false
false
false
true
false
false
false
false
true
6,046
371E
Berland is going through tough times — the dirt price has dropped and that is a blow to the country's economy. Everybody knows that Berland is the top world dirt exporter! The President of Berland was forced to leave only _k_ of the currently existing _n_ subway stations. The subway stations are located on a straight line one after another, the trains consecutively visit the stations as they move. You can assume that the stations are on the _Ox_ axis, the _i_-th station is at point with coordinate _x__i_. In such case the distance between stations _i_ and _j_ is calculated by a simple formula _x__i_u2009-u2009_x__j_. Currently, the Ministry of Transport is choosing which stations to close and which ones to leave. Obviously, the residents of the capital won't be too enthusiastic about the innovation, so it was decided to show the best side to the people. The Ministry of Transport wants to choose such _k_ stations that minimize the average commute time in the subway! Assuming that the train speed is constant (it is a fixed value), the average commute time in the subway is calculated as the sum of pairwise distances between stations, divided by the number of pairs (that is ) and divided by the speed of the train. Help the Minister of Transport to solve this difficult problem. Write a program that, given the location of the stations selects such _k_ stations that the average commute time in the subway is minimized. Input The first line of the input contains integer _n_ (3u2009≤u2009_n_u2009≤u20093·105) — the number of the stations before the innovation. The second line contains the coordinates of the stations _x_1,u2009_x_2,u2009...,u2009_x__n_ (u2009-u2009108u2009≤u2009_x__i_u2009≤u2009108). The third line contains integer _k_ (2u2009≤u2009_k_u2009≤u2009_n_u2009-u20091) — the number of stations after the innovation. The station coordinates are distinct and not necessarily sorted. Output Print a sequence of _k_ distinct integers _t_1,u2009_t_2,u2009...,u2009_t__k_ (1u2009≤u2009_t__j_u2009≤u2009_n_) — the numbers of the stations that should be left after the innovation in arbitrary order. Assume that the stations are numbered 1 through _n_ in the order they are given in the input. The number of stations you print must have the minimum possible average commute time among all possible ways to choose _k_ stations. If there are multiple such ways, you are allowed to print any of them.
2,000
true
true
false
false
false
false
false
false
false
false
8,344
327E
Iahub wants to meet his girlfriend Iahubina. They both live in _Ox_ axis (the horizontal axis). Iahub lives at point 0 and Iahubina at point _d_. Iahub has _n_ positive integers _a_1, _a_2, ..., _a__n_. The sum of those numbers is _d_. Suppose _p_1, _p_2, ..., _p__n_ is a permutation of {1,u20092,u2009...,u2009_n_}. Then, let _b_1u2009=u2009_a__p_1, _b_2u2009=u2009_a__p_2 and so on. The array b is called a "route". There are _n_! different routes, one for each permutation _p_. Iahub's travel schedule is: he walks _b_1 steps on _Ox_ axis, then he makes a break in point _b_1. Then, he walks _b_2 more steps on _Ox_ axis and makes a break in point _b_1u2009+u2009_b_2. Similarly, at _j_-th (1u2009≤u2009_j_u2009≤u2009_n_) time he walks _b__j_ more steps on _Ox_ axis and makes a break in point _b_1u2009+u2009_b_2u2009+u2009...u2009+u2009_b__j_. Iahub is very superstitious and has _k_ integers which give him bad luck. He calls a route "good" if he never makes a break in a point corresponding to one of those _k_ numbers. For his own curiosity, answer how many good routes he can make, modulo 1000000007 (109u2009+u20097). Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u200924). The following line contains _n_ integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). The third line contains integer _k_ (0u2009≤u2009_k_u2009≤u20092). The fourth line contains _k_ positive integers, representing the numbers that give Iahub bad luck. Each of these numbers does not exceed 109. Note In the first case consider six possible orderings: [2, 3, 5]. Iahub will stop at position 2, 5 and 10. Among them, 5 is bad luck for him. [2, 5, 3]. Iahub will stop at position 2, 7 and 10. Among them, 7 is bad luck for him. [3, 2, 5]. He will stop at the unlucky 5. [3, 5, 2]. This is a valid ordering. [5, 2, 3]. He got unlucky twice (5 and 7). [5, 3, 2]. Iahub would reject, as it sends him to position 5. In the second case, note that it is possible that two different ways have the identical set of stopping. In fact, all six possible ways have the same stops: [2, 4, 6], so there's no bad luck for Iahub.
2,300
false
false
false
true
false
true
false
false
false
false
8,532
1096D
Vasya is preparing a contest, and now he has written a statement for an easy problem. The statement is a string of length $$$n$$$ consisting of lowercase Latin latters. Vasya thinks that the statement can be considered hard if it contains a subsequence hard; otherwise the statement is easy. For example, hard, hzazrzd, haaaaard can be considered hard statements, while har, hart and drah are easy statements. Vasya doesn't want the statement to be hard. He may remove some characters from the statement in order to make it easy. But, of course, some parts of the statement can be crucial to understanding. Initially the ambiguity of the statement is $$$0$$$, and removing $$$i$$$-th character increases the ambiguity by $$$a_i$$$ (the index of each character is considered as it was in the original statement, so, for example, if you delete character r from hard, and then character d, the index of d is still $$$4$$$ even though you delete it from the string had). Vasya wants to calculate the minimum ambiguity of the statement, if he removes some characters (possibly zero) so that the statement is easy. Help him to do it! Recall that subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Input The first line contains one integer $$$n$$$ ($$$1 le n le 10^5$$$) — the length of the statement. The second line contains one string $$$s$$$ of length $$$n$$$, consisting of lowercase Latin letters — the statement written by Vasya. The third line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 998244353$$$). Note In the first example, first two characters are removed so the result is ardh. In the second example, $$$5$$$-th character is removed so the result is hhzawde. In the third example there's no need to remove anything.
1,800
false
false
false
true
false
false
false
false
false
false
5,228
1634F
One of my most productive days was throwing away 1,000 lines of code. — Ken Thompson Fibonacci addition is an operation on an array $$$X$$$ of integers, parametrized by indices $$$l$$$ and $$$r$$$. Fibonacci addition increases $$$X_l$$$ by $$$F_1$$$, increases $$$X_{l + 1}$$$ by $$$F_2$$$, and so on up to $$$X_r$$$ which is increased by $$$F_{r - l + 1}$$$. $$$F_i$$$ denotes the $$$i$$$-th Fibonacci number ($$$F_1 = 1$$$, $$$F_2 = 1$$$, $$$F_{i} = F_{i - 1} + F_{i - 2}$$$ for $$$i > 2$$$), and all operations are performed modulo $$$MOD$$$. You are given two arrays $$$A$$$ and $$$B$$$ of the same length. We will ask you to perform several Fibonacci additions on these arrays with different parameters, and after each operation you have to report whether arrays $$$A$$$ and $$$B$$$ are equal modulo $$$MOD$$$. Input The first line contains 3 numbers $$$n$$$, $$$q$$$ and $$$MOD$$$ ($$$1 le n, q le 3cdot 10^5, 1 le MOD le 10^9+7$$$)xa0— the length of the arrays, the number of operations, and the number modulo which all operations are performed. The second line contains $$$n$$$ numbersxa0— array $$$A$$$ ($$$0 le A_i < MOD$$$). The third line also contains $$$n$$$ numbersxa0— array $$$B$$$ ($$$0 le B_i < MOD$$$). The next $$$q$$$ lines contain character $$$c$$$ and two numbers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$)xa0— operation parameters. If $$$c$$$ is "A", Fibonacci addition is to be performed on array $$$A$$$, and if it is is "B", the operation is to be performed on $$$B$$$. Output After each operation, print "YES" (without quotes) if the arrays are equal and "NO" otherwise. Letter case does not matter. Examples Input 3 5 3 2 2 1 0 0 0 A 1 3 A 1 3 B 1 1 B 2 2 A 3 3 Input 5 3 10 2 5 0 3 5 3 5 8 2 5 B 2 3 B 3 4 A 1 2 Note Explanation of the test from the condition: Initially $$$A=[2,2,1]$$$, $$$B=[0,0,0]$$$. After operation "A 1 3": $$$A=[0,0,0]$$$, $$$B=[0,0,0]$$$ (addition is modulo 3). After operation "A 1 3": $$$A=[1,1,2]$$$, $$$B=[0,0,0]$$$. After operation "B 1 1": $$$A=[1,1,2]$$$, $$$B=[1,0,0]$$$. After operation "B 2 2": $$$A=[1,1,2]$$$, $$$B=[1,1,0]$$$. After operation "A 3 3": $$$A=[1,1,0]$$$, $$$B=[1,1,0]$$$.
2,700
true
false
true
false
true
false
true
false
false
false
2,441
686B
Little Robber Girl likes to scare animals in her zoo for fun. She decided to arrange the animals in a row in the order of non-decreasing height. However, the animals were so scared that they couldn't stay in the right places. The robber girl was angry at first, but then she decided to arrange the animals herself. She repeatedly names numbers _l_ and _r_ such that _r_u2009-u2009_l_u2009+u20091 is even. After that animals that occupy positions between _l_ and _r_ inclusively are rearranged as follows: the animal at position _l_ swaps places with the animal at position _l_u2009+u20091, the animal _l_u2009+u20092 swaps with the animal _l_u2009+u20093, ..., finally, the animal at position _r_u2009-u20091 swaps with the animal _r_. Help the robber girl to arrange the animals in the order of non-decreasing height. You should name at most 20u2009000 segments, since otherwise the robber girl will become bored and will start scaring the animals again. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— number of animals in the robber girl's zoo. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is the height of the animal occupying the _i_-th place. Output Print the sequence of operations that will rearrange the animals by non-decreasing height. The output should contain several lines, _i_-th of the lines should contain two space-separated integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009_n_)xa0— descriptions of segments the robber girl should name. The segments should be described in the order the operations are performed. The number of operations should not exceed 20u2009000. If the animals are arranged correctly from the start, you are allowed to output nothing. Examples Input 7 36 28 57 39 66 69 68 Note Note that you don't have to minimize the number of operations. Any solution that performs at most 20u2009000 operations is allowed.
1,100
false
false
true
false
false
true
false
false
true
false
7,077
1178E
Alice bought a Congo Prime Video subscription and was watching a documentary on the archaeological findings from Factor's Island on Loch Katrine in Scotland. The archaeologists found a book whose age and origin are unknown. Perhaps Alice can make some sense of it? The book contains a single string of characters "a", "b" and "c". It has been pointed out that no two consecutive characters are the same. It has also been conjectured that the string contains an unusually long subsequence that reads the same from both sides. Help Alice verify this by finding such subsequence that contains at least half of the characters of the original string, rounded down. Note that you don't have to maximise the length of it. A string $$$a$$$ is a subsequence of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters. Input The input consists of a single string $$$s$$$xa0($$$2 leq s leq 10^6$$$). The string $$$s$$$ consists only of characters "a", "b", "c". It is guaranteed that no two consecutive characters are equal. Output Output a palindrome $$$t$$$ that is a subsequence of $$$s$$$ and $$$t geq lfloor frac{s}{2} floor$$$. If there are multiple solutions, you may print any of them. You don't have to maximise the length of $$$t$$$. If there are no solutions, output a string "IMPOSSIBLE" (quotes for clarity). Examples Input cbacacacbcbababacbcb Note In the first example, other valid answers include "cacac", "caac", "aca" and "ccc".
1,900
false
true
false
false
false
true
true
false
false
false
4,826
988B
You are given $$$n$$$ strings. Each string consists of lowercase English letters. Rearrange (reorder) the given strings in such a way that for every string, all strings that are placed before it are its substrings. String $$$a$$$ is a substring of string $$$b$$$ if it is possible to choose several consecutive letters in $$$b$$$ in such a way that they form $$$a$$$. For example, string "for" is contained as a substring in strings "codeforces", "for" and "therefore", but is not contained as a substring in strings "four", "fofo" and "rof". Input The first line contains an integer $$$n$$$ ($$$1 le n le 100$$$) — the number of strings. The next $$$n$$$ lines contain the given strings. The number of letters in each string is from $$$1$$$ to $$$100$$$, inclusive. Each string consists of lowercase English letters. Some strings might be equal. Output If it is impossible to reorder $$$n$$$ given strings in required order, print "NO" (without quotes). Otherwise print "YES" (without quotes) and $$$n$$$ given strings in required order. Examples Output YES a ba aba aba abacaba Output YES qwerty qwerty qwerty Note In the second example you cannot reorder the strings because the string "abab" is not a substring of the string "abacaba".
1,100
false
false
false
false
false
false
false
false
true
false
5,762
1151B
Student Dima from Kremland has a matrix $$$a$$$ of size $$$n imes m$$$ filled with non-negative integers. He wants to select exactly one integer from each row of the matrix so that the bitwise exclusive OR of the selected integers is strictly greater than zero. Help him! Formally, he wants to choose an integers sequence $$$c_1, c_2, ldots, c_n$$$ ($$$1 leq c_j leq m$$$) so that the inequality $$$a_{1, c_1} oplus a_{2, c_2} oplus ldots oplus a_{n, c_n} > 0$$$ holds, where $$$a_{i, j}$$$ is the matrix element from the $$$i$$$-th row and the $$$j$$$-th column. Here $$$x oplus y$$$ denotes the xa0— the number of rows and the number of columns in the matrix $$$a$$$. Each of the next $$$n$$$ lines contains $$$m$$$ integers: the $$$j$$$-th integer in the $$$i$$$-th line is the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$a$$$, i.e. $$$a_{i, j}$$$ ($$$0 leq a_{i, j} leq 1023$$$). Output If there is no way to choose one integer from each row so that their bitwise exclusive OR is strictly greater than zero, print "NIE". Otherwise print "TAK" in the first line, in the next line print $$$n$$$ integers $$$c_1, c_2, ldots c_n$$$ ($$$1 leq c_j leq m$$$), so that the inequality $$$a_{1, c_1} oplus a_{2, c_2} oplus ldots oplus a_{n, c_n} > 0$$$ holds. If there is more than one possible answer, you may output any. Note In the first example, all the numbers in the matrix are $$$0$$$, so it is impossible to select one number in each row of the table so that their bitwise exclusive OR is strictly greater than zero. In the second example, the selected numbers are $$$7$$$ (the first number in the first line) and $$$10$$$ (the third number in the second line), $$$7 oplus 10 = 13$$$, $$$13$$$ is more than $$$0$$$, so the answer is found.
1,600
false
false
false
true
false
true
true
false
false
false
4,961
1348C
Phoenix has a string $$$s$$$ consisting of lowercase Latin letters. He wants to distribute all the letters of his string into $$$k$$$ non-empty strings $$$a_1, a_2, dots, a_k$$$ such that every letter of $$$s$$$ goes to exactly one of the strings $$$a_i$$$. The strings $$$a_i$$$ do not need to be substrings of $$$s$$$. Phoenix can distribute letters of $$$s$$$ and rearrange the letters within each string $$$a_i$$$ however he wants. For example, if $$$s = $$$ baba and $$$k=2$$$, Phoenix may distribute the letters of his string in many ways, such as: ba and ba a and abb ab and ab aa and bb But these ways are invalid: baa and ba b and ba baba and empty string ($$$a_i$$$ should be non-empty) Phoenix wants to distribute the letters of his string $$$s$$$ into $$$k$$$ strings $$$a_1, a_2, dots, a_k$$$ to minimize the lexicographically maximum string among them, i.xa0e. minimize $$$max(a_1, a_2, dots, a_k)$$$. Help him find the optimal distribution and print the minimal possible value of $$$max(a_1, a_2, dots, a_k)$$$. String $$$x$$$ is lexicographically less than string $$$y$$$ if either $$$x$$$ is a prefix of $$$y$$$ and $$$x e y$$$, or there exists an index $$$i$$$ ($$$1 le i le min(x, y))$$$ such that $$$x_i$$$ < $$$y_i$$$ and for every $$$j$$$ $$$(1 le j < i)$$$ $$$x_j = y_j$$$. Here $$$x$$$ denotes the length of the string $$$x$$$. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Each test case consists of two lines. The first line of each test case consists of two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^5$$$)xa0— the length of string $$$s$$$ and the number of non-empty strings, into which Phoenix wants to distribute letters of $$$s$$$, respectively. The second line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting only of lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases is $$$le 10^5$$$. Output Print $$$t$$$ answersxa0— one per test case. The $$$i$$$-th answer should be the minimal possible value of $$$max(a_1, a_2, dots, a_k)$$$ in the $$$i$$$-th test case. Example Input 6 4 2 baba 5 2 baacb 5 3 baacb 5 3 aaaaa 6 4 aaxxzz 7 1 phoenix Output ab abbc b aa x ehinopx Note In the first test case, one optimal solution is to distribute baba into ab and ab. In the second test case, one optimal solution is to distribute baacb into abbc and a. In the third test case, one optimal solution is to distribute baacb into ac, ab, and b. In the fourth test case, one optimal solution is to distribute aaaaa into aa, aa, and a. In the fifth test case, one optimal solution is to distribute aaxxzz into az, az, x, and x. In the sixth test case, one optimal solution is to distribute phoenix into ehinopx.
1,600
false
true
false
false
false
true
false
false
true
false
3,968
212D
Vasya the carpenter has an estate that is separated from the wood by a fence. The fence consists of _n_ planks put in a line. The fence is not closed in a circle. The planks are numbered from left to right from 1 to _n_, the _i_-th plank is of height _a__i_. All planks have the same width, the lower edge of each plank is located at the ground level. Recently a local newspaper "Malevich and Life" wrote that the most fashionable way to decorate a fence in the summer is to draw a fuchsia-colored rectangle on it, the lower side of the rectangle must be located at the lower edge of the fence. Vasya is delighted with this idea! He immediately bought some fuchsia-colored paint and began to decide what kind of the rectangle he should paint. Vasya is sure that the rectangle should cover _k_ consecutive planks. In other words, he will paint planks number _x_, _x_u2009+u20091, ..., _x_u2009+u2009_k_u2009-u20091 for some _x_ (1u2009≤u2009_x_u2009≤u2009_n_u2009-u2009_k_u2009+u20091). He wants to paint the rectangle of maximal area, so the rectangle height equals _min_ _a__i_ for _x_u2009≤u2009_i_u2009≤u2009_x_u2009+u2009_k_u2009-u20091, _x_ is the number of the first colored plank. Vasya has already made up his mind that the rectangle width can be equal to one of numbers of the sequence _k_1,u2009_k_2,u2009...,u2009_k__m_. For each _k__i_ he wants to know the expected height of the painted rectangle, provided that he selects _x_ for such fence uniformly among all _n_u2009-u2009_k__i_u2009+u20091 possible values. Help him to find the expected heights. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009106) — the number of planks in the fence. The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) where _a__i_ is the height of the _i_-th plank of the fence. The third line contains an integer _m_ (1u2009≤u2009_m_u2009≤u2009106) and the next line contains _m_ space-separated integers _k_1,u2009_k_2,u2009...,u2009_k__m_ (1u2009≤u2009_k__i_u2009≤u2009_n_) where _k__i_ is the width of the desired fuchsia-colored rectangle in planks. Output Print _m_ whitespace-separated real numbers, the _i_-th number equals the expected value of the rectangle height, if its width in planks equals _k__i_. The value will be considered correct if its absolute or relative error doesn't exceed 10u2009-u20099.
2,500
false
false
false
false
true
false
false
true
false
false
8,990
774K
Problem - 774K - Codeforces =============== xa0 . → Problem tags *special problem implementation strings *1600 No tag edit access → Contest materials — the number of letters in the word written by Stepan. The second line contains the string _s_ which has length that equals to _n_ and contains only lowercase English letters — the word written by Stepan. Output Print the single string — the word written by Stepan converted according to the rules described in the statement. Examples Input 13 pobeeeedaaaaa Output pobeda Input 22 iiiimpleeemeentatiioon Output implemeentatioon Input 18 aeiouyaaeeiioouuyy Output aeiouyaeeioouy Input 24 aaaoooiiiuuuyyyeeeggghhh Output aoiuyeggghhh
1,600
false
false
true
false
false
false
false
false
false
false
6,671
1737B
Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Dytechlab Cup 2022 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 implementation math *1300 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST B. Ela's Fitness and the Luxury Number time limit per test1 second memory limit per test256 megabytes While working at DTL, Ela is very aware of her physical and mental health. She started to practice various sports, such as Archery, Yoga, and Football. Since she started engaging in sports activities, Ela switches to trying a new sport on days she considers being "Luxury" days. She counts the days since she started these activities, in which the day she starts is numbered as day $$$1$$$. A "Luxury" day is the day in which the number of this day is a luxurious number. An integer $$$x$$$ is called a luxurious number if it is divisible by $$${lfloor sqrt{x} floor}$$$. Here $$$lfloor r floor$$$ denotes the "floor" of a real number $$$r$$$. In other words, it's the largest integer not greater than $$$r$$$. For example: $$$8$$$, $$$56$$$, $$$100$$$ are luxurious numbers, since $$$8$$$ is divisible by $$$lfloor sqrt{8} floor = lfloor 2.8284 floor = 2$$$, $$$56$$$ is divisible $$$lfloor sqrt{56} floor = lfloor 7.4833 floor = 7$$$, and $$$100$$$ is divisible by $$$lfloor sqrt{100} floor = lfloor 10 floor = 10$$$, respectively. On the other hand $$$5$$$, $$$40$$$ are not, since $$$5$$$ are not divisible by $$$lfloor sqrt{5} floor = lfloor 2.2361 floor = 2$$$, and $$$40$$$ are not divisible by $$$lfloor sqrt{40} floor = lfloor 6.3246 floor = 6$$$. Being a friend of Ela, you want to engage in these fitness activities with her to keep her and yourself accompanied (and have fun together, of course). Between day $$$l$$$ and day $$$r$$$, you want to know how many times she changes the activities. Input Each test contains multiple test cases. The first line has the number of test cases $$$t$$$ ($$$1 le t le 10 000$$$). The description of the test cases follows. The only line of each test case contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le 10^{18}$$$) — the intervals at which you want to know how many times Ela changes her sports. Output For each test case, output an integer that denotes the answer. Example input 5 8 19 8 20 119 121 1 100000000000000000 1234567891011 1000000000000000000 output 5 6 2 948683296 2996666667 Note In the first test case, $$$5$$$ luxury numbers in range $$$[8, 19]$$$ are: $$$8, 9, 12, 15, 16$$$. Codeforces (c)
1,300
true
false
true
false
false
false
false
true
false
false
1,867
1773L
Lisa loves playing with the sequences of integers. When she gets a new integer sequence $$$a_i$$$ of length $$$n$$$, she starts looking for all monotone subsequences. A monotone subsequence $$$[l, r]$$$ is defined by two indices $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$) such that $$$forall i = l, l+1, ldots, r-1: a_i le a_{i+1}$$$ or $$$forall i = l, l+1, ldots, r-1: a_i ge a_{i+1}$$$. Lisa considers a sequence $$$a_i$$$ to be boring if there is a monotone subsequence $$$[l, r]$$$ that is as long as her boredom threshold $$$k$$$, that is when $$$r - l + 1 = k$$$. Lucas has a sequence $$$b_i$$$ that he wants to present to Lisa, but the sequence might be boring for Lisa. So, he wants to change some elements of his sequence $$$b_i$$$, so that Lisa does not get bored playing with it. However, Lucas is lazy and wants to change as few elements of the sequence $$$b_i$$$ as possible. Your task is to help Lucas find the required changes. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$3 le k le n le 10^6$$$)xa0— the length of the sequence and Lisa's boredom threshold. The second line contains $$$n$$$ integers $$$b_i$$$ ($$$1 le b_i le 99,999$$$)xa0— the original sequence that Lucas has. Output On the first line output an integer $$$m$$$xa0— the minimal number of elements in $$$b_i$$$ that needs to be changed to make the sequence not boring for Lisa. On the second line output $$$n$$$ integers $$$a_i$$$ ($$$0 le a_i le 100,000$$$), so that the sequence of integers $$$a_i$$$ is not boring for Lisa and is different from the original sequence $$$b_i$$$ in exactly $$$m$$$ positions. Examples Input 10 4 1 1 1 2 2 1 1 2 2 1 Output 2 1 1 100000 2 2 100000 1 2 2 1 Input 10 10 1 1 1 1 1 1 1 1 1 1 Output 1 1 1 1 1 1 1 1 1 0 1
3,500
false
false
false
true
false
false
false
false
false
false
1,645
1490A
Polycarp calls an array dense if the greater of any two adjacent elements is not more than twice bigger than the smaller. More formally, for any $$$i$$$ ($$$1 le i le n-1$$$), this condition must be satisfied: $$$$$$frac{max(a[i], a[i+1])}{min(a[i], a[i+1])} le 2$$$$$$ For example, the arrays $$$[1, 2, 3, 4, 3]$$$, $$$[1, 1, 1]$$$ and $$$[5, 10]$$$ are dense. And the arrays $$$[5, 11]$$$, $$$[1, 4, 2]$$$, $$$[6, 6, 1]$$$ are not dense. You are given an array $$$a$$$ of $$$n$$$ integers. What is the minimum number of numbers you need to add to an array to make it dense? You can insert numbers anywhere in the array. If the array is already dense, no numbers need to be added. For example, if $$$a=[4,2,10,1]$$$, then the answer is $$$5$$$, and the array itself after inserting elements into it may look like this: $$$a=[4,2,underline{ extbf{3}},underline{ extbf{5}},10,underline{ extbf{6}},underline{ extbf{4}},underline{ extbf{2}},1]$$$ (there are other ways to build such $$$a$$$). Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$). Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 le n le 50$$$)xa0— the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 50$$$). Note The first test case is explained in the statements. In the second test case, you can insert one element, $$$a=[1,underline{ extbf{2}},3]$$$. In the third test case, you can insert two elements, $$$a=[6,underline{ extbf{4}},underline{ extbf{2}},1]$$$. In the fourth test case, you can insert one element, $$$a=[1,underline{ extbf{2}},4,2]$$$. In the fifth test case, the array $$$a$$$ is already dense.
800
true
true
false
false
false
false
false
false
false
false
3,229
405D
Little Chris is very keen on his toy blocks. His teacher, however, wants Chris to solve more problems, so he decided to play a trick on Chris. There are exactly _s_ blocks in Chris's set, each block has a unique number from 1 to _s_. Chris's teacher picks a subset of blocks _X_ and keeps it to himself. He will give them back only if Chris can pick such a non-empty subset _Y_ from the remaining blocks, that the equality holds: "Are you kidding me?", asks Chris. For example, consider a case where _s_u2009=u20098 and Chris's teacher took the blocks with numbers 1, 4 and 5. One way for Chris to choose a set is to pick the blocks with numbers 3 and 6, see figure. Then the required sums would be equal: (1u2009-u20091)u2009+u2009(4u2009-u20091)u2009+u2009(5u2009-u20091)u2009=u2009(8u2009-u20093)u2009+u2009(8u2009-u20096)u2009=u20097. However, now Chris has exactly _s_u2009=u2009106 blocks. Given the set _X_ of blocks his teacher chooses, help Chris to find the required set _Y_! Input The first line of input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095·105), the number of blocks in the set _X_. The next line contains _n_ distinct space-separated integers _x_1, _x_2, ..., _x__n_ (1u2009≤u2009_x__i_u2009≤u2009106), the numbers of the blocks in _X_. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output In the first line of output print a single integer _m_ (1u2009≤u2009_m_u2009≤u2009106u2009-u2009_n_), the number of blocks in the set _Y_. In the next line output _m_ distinct space-separated integers _y_1, _y_2, ..., _y__m_ (1u2009≤u2009_y__i_u2009≤u2009106), such that the required equality holds. The sets _X_ and _Y_ should not intersect, i.e. _x__i_u2009≠u2009_y__j_ for all _i_, _j_ (1u2009≤u2009_i_u2009≤u2009_n_; 1u2009≤u2009_j_u2009≤u2009_m_). It is guaranteed that at least one solution always exists. If there are multiple solutions, output any of them.
1,700
true
true
true
false
false
false
false
false
false
false
8,202
741A
As you have noticed, there are lovely girls in Arpa’s land. People in Arpa's land are numbered from 1 to _n_. Everyone has exactly one crush, _i_-th person's crush is person with the number _crush__i_. Someday Arpa shouted Owf loudly from the top of the palace and a funny game started in Arpa's land. The rules are as follows. The game consists of rounds. Assume person _x_ wants to start a round, he calls _crush__x_ and says: "Oww...wwf" (the letter w is repeated _t_ times) and cuts off the phone immediately. If _t_u2009>u20091 then _crush__x_ calls _crush__crush__x_ and says: "Oww...wwf" (the letter w is repeated _t_u2009-u20091 times) and cuts off the phone immediately. The round continues until some person receives an "Owf" (_t_u2009=u20091). This person is called the Joon-Joon of the round. There can't be two rounds at the same time. Mehrdad has an evil plan to make the game more funny, he wants to find smallest _t_ (_t_u2009≥u20091) such that for each person _x_, if _x_ starts some round and _y_ becomes the Joon-Joon of the round, then by starting from _y_, _x_ would become the Joon-Joon of the round. Find such _t_ for Mehrdad if it's possible. Some strange fact in Arpa's land is that someone can be himself's crush (i.e. _crush__i_u2009=u2009_i_). Input The first line of input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of people in Arpa's land. The second line contains _n_ integers, _i_-th of them is _crush__i_ (1u2009≤u2009_crush__i_u2009≤u2009_n_)xa0— the number of _i_-th person's crush. Output If there is no _t_ satisfying the condition, print -1. Otherwise print such smallest _t_. Note In the first sample suppose _t_u2009=u20093. If the first person starts some round: The first person calls the second person and says "Owwwf", then the second person calls the third person and says "Owwf", then the third person calls the first person and says "Owf", so the first person becomes Joon-Joon of the round. So the condition is satisfied if _x_ is 1. The process is similar for the second and the third person. If the fourth person starts some round: The fourth person calls himself and says "Owwwf", then he calls himself again and says "Owwf", then he calls himself for another time and says "Owf", so the fourth person becomes Joon-Joon of the round. So the condition is satisfied when _x_ is 4. In the last example if the first person starts a round, then the second person becomes the Joon-Joon, and vice versa.
1,600
true
false
false
false
false
false
false
false
false
false
6,836
1688A
Even if it's a really easy question, she won't be able to answer it — Perfect Memento in Strict Sense Cirno's perfect bitmasks classroom has just started! Cirno gave her students a positive integer $$$x$$$. As an assignment, her students need to find the minimum positive integer $$$y$$$, which satisfies the following two conditions: $$$$$$x exttt{and} y > 0$$$$$$ $$$$$$x exttt{xor} y > 0$$$$$$ Where $$$ exttt{and}$$$ is the — the number of input test cases. For each test case, the only line of input contains one integer $$$x$$$ ($$$1 leq x leq 2^{30}$$$). Output For each test case, print a single integer — the minimum number of $$$y$$$. Example Input 7 1 2 5 9 16 114514 1000000 Note Test case 1: $$$1; exttt{and}; 3=1>0$$$, $$$1; exttt{xor}; 3=2>0$$$. Test case 2: $$$2; exttt{and}; 3=2>0$$$, $$$2; exttt{xor}; 3=1>0$$$.
800
false
false
false
false
false
false
true
false
false
false
2,151
1912F
The city of F. can be represented as a tree. A famous fugitive is hiding in it, and today a faithful police officer decided to catch him at all costs. The police officer is stronger than the fugitive, but the fugitive is much faster than the former. That is why the pursuit proceeds as follows. At the moment $$$t = 0$$$ the police officer appears at the vertex with number $$$s$$$, and the fugitive spawns at any other vertex of his choice. After that, they take turns, starting with the police officer. During the police officer's move, she selects any vertex adjacent to the one where she is currently located and moves there. The police officer spends one minute moving. Also, the police officer may decide to stand still instead, in which case she waits one minute at the vertex at which she started her move. If at the end of the turn the police officer ends up at the same vertex as the fugitive, she instantly catches him and the chase ends. The fugitive's move is as follows. Let him be at vertex $$$b$$$, and the police officer at vertex $$$p$$$. Then the fugitive chooses any vertex $$$b' e p$$$ such that the path between the vertices $$$b$$$ and $$$b'$$$ does not contain vertex $$$p$$$ and instantly moves there. In particular, he can always choose $$$b' = b$$$ to stay where he is. The fugitive's move takes no time. Note that the fugitive managed to attach a radio bug to the police officer's badge a week ago, so the fugitive knows the location of the police officer at every moment (in particular, he knows the number $$$s$$$). On the contrary, the police officer knows nothing about the fugitive's movements and will only be able to detect him at the very moment she catches him. The police officer aims to catch the fugitive as fast as possible, and the fugitive aims to be caught as late as possible. Since the chase can be thought of as a game with incomplete information, participants can use mixed (probabilistic) strategiesxa0— thus, the police officer acts to minimize the expected duration of the chase, and the fugitivexa0— to maximize it. Find the mathematical expectation of the duration of the chase with optimal actions of the police officer and the fugitive. It can be proven that it is always finite. In particular, with optimal strategies, the probability that the chase continues indefinitely is equal to zero. Input The first line contains an integer $$$n$$$xa0— the number of vertices in the tree ($$$2 le n le 100$$$). The next $$$n - 1$$$ lines describe the city of F.: each of them contains a pair of integers $$$u_i$$$, $$$v_i$$$xa0— the numbers of the ends of an edge ($$$1 le u_i, v_i le n$$$). These edges are guaranteed to form a tree. The last line contains an integer $$$s$$$xa0— the number of the vertex where the police officer initially appears ($$$1 le s le n$$$). Output Print one real numberxa0— the mathematical expectation of the duration of the chase with the optimal strategies of the police officer and the fugitive. Your answer will be accepted if its absolute or relative error does not exceed $$$10^{-6}$$$; formally, if $$$p$$$ is your answer, and $$$j$$$ is the jury's answer, this should hold: $$$frac{p - j}{max{1, j}} le 10^{-6}$$$.
3,100
true
false
false
false
false
false
false
false
false
false
832
887C
During the breaks between competitions, top-model Izabella tries to develop herself and not to be bored. For example, now she tries to solve Rubik's cube 2x2x2. It's too hard to learn to solve Rubik's cube instantly, so she learns to understand if it's possible to solve the cube in some state using 90-degrees rotation of one face of the cube in any direction. To check her answers she wants to use a program which will for some state of cube tell if it's possible to solve it using one rotation, described above. Cube is called solved if for each face of cube all squares on it has the same color. first line given a sequence of 24 integers _a__i_ (1u2009≤u2009_a__i_u2009≤u20096), where _a__i_ denotes color of _i_-th square. There are exactly 4 occurrences of all colors in this sequence. Output Print «YES» (without quotes) if it's possible to solve cube using one rotation and «NO» (without quotes) otherwise. Examples Input 2 5 4 6 1 3 6 2 5 5 1 2 3 5 3 1 1 2 4 6 6 4 3 4 Input 5 3 5 3 2 5 2 5 6 2 6 2 4 4 4 4 1 1 1 1 6 3 6 3 Note In first test case cube looks like this: In second test case cube looks like this: It's possible to solve cube by rotating face with squares with numbers 13, 14, 15, 16.
1,500
false
false
true
false
false
false
true
false
false
false
6,200
1778F
You are given a rooted tree consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Vertex $$$1$$$ is the root of the tree. Each vertex has an integer value. The value of $$$i$$$-th vertex is $$$a_i$$$. You can do the following operation at most $$$k$$$ times. Choose a vertex $$$v$$$ that has not been chosen before and an integer $$$x$$$ such that $$$x$$$ is a common divisor of the values of all vertices of the subtree of $$$v$$$. Multiply by $$$x$$$ the value of each vertex in the subtree of $$$v$$$. What is the maximum possible value of the root node $$$1$$$ after at most $$$k$$$ operations? Formally, you have to maximize the value of $$$a_1$$$. A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. The subtree of a node $$$u$$$ is the set of all nodes $$$y$$$ such that the simple path from $$$y$$$ to the root passes through $$$u$$$. Note that $$$u$$$ is in the subtree of $$$u$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 50,000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 10^5$$$, $$$0 leq k leq n$$$)xa0— the number of vertices in the tree and the number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 1000$$$), where $$$a_i$$$ denotes the value of vertex $$$i$$$. Each of the next $$$n - 1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$), denoting the edge of the tree between vertices $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree. 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 the maximum value of the root after performing at most $$$k$$$ operations. Example Input 2 5 2 24 12 24 6 12 1 2 1 3 2 4 2 5 5 3 24 12 24 6 12 1 2 1 3 2 4 2 5 Note Both examples have the same tree: For the first test case, you can do two operations as follows: Choose the subtree of vertex $$$4$$$ and $$$x = 2$$$. After this operation, the node values become $$${24, 12, 24, 12, 12}.$$$ Choose the subtree of vertex $$$1$$$ and $$$x = 12$$$. After this operation, the node values become $$${288, 144, 288, 144, 144}.$$$ The value of the root is $$$288$$$ and it is the maximum. For the second test case, you can do three operations as follows: Choose the subtree of vertex $$$4$$$ and $$$x = 2$$$. After this operation, the node values become $$${24, 12, 24, 12, 12}.$$$ Choose the subtree of vertex $$$2$$$ and $$$x = 4$$$. After this operation, the node values become $$${24, 48, 24, 48, 48}.$$$ Choose the subtree of vertex $$$1$$$ and $$$x = 24$$$. After this operation, the node values become $$${576, 1152, 576, 1152, 1152}.$$$ The value of the root is $$$576$$$ and it is the maximum.
2,600
true
false
false
true
false
false
false
false
false
true
1,603
1430F
Recently you've discovered a new shooter. They say it has realistic game mechanics. Your character has a gun with magazine size equal to $$$k$$$ and should exterminate $$$n$$$ waves of monsters. The $$$i$$$-th wave consists of $$$a_i$$$ monsters and happens from the $$$l_i$$$-th moment of time up to the $$$r_i$$$-th moments of time. All $$$a_i$$$ monsters spawn at moment $$$l_i$$$ and you have to exterminate all of them before the moment $$$r_i$$$ ends (you can kill monsters right at moment $$$r_i$$$). For every two consecutive waves, the second wave starts not earlier than the first wave ends (though the second wave can start at the same moment when the first wave ends) — formally, the condition $$$r_i le l_{i + 1}$$$ holds. Take a look at the notes for the examples to understand the process better. You are confident in yours and your character's skills so you can assume that aiming and shooting are instant and you need exactly one bullet to kill one monster. But reloading takes exactly $$$1$$$ unit of time. One of the realistic mechanics is a mechanic of reloading: when you reload you throw away the old magazine with all remaining bullets in it. That's why constant reloads may cost you excessive amounts of spent bullets. You've taken a liking to this mechanic so now you are wondering: what is the minimum possible number of bullets you need to spend (both used and thrown) to exterminate all waves. Note that you don't throw the remaining bullets away after eradicating all monsters, and you start with a full magazine. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2000$$$; $$$1 le k le 10^9$$$)xa0— the number of waves and magazine size. The next $$$n$$$ lines contain descriptions of waves. The $$$i$$$-th line contains three integers $$$l_i$$$, $$$r_i$$$ and $$$a_i$$$ ($$$1 le l_i le r_i le 10^9$$$; $$$1 le a_i le 10^9$$$)xa0— the period of time when the $$$i$$$-th wave happens and the number of monsters in it. It's guaranteed that waves don't overlap (but may touch) and are given in the order they occur, i. e. $$$r_i le l_{i + 1}$$$. Output If there is no way to clear all waves, print $$$-1$$$. Otherwise, print the minimum possible number of bullets you need to spend (both used and thrown) to clear all waves. Examples Input 5 42 42 42 42 42 43 42 43 44 42 44 45 42 45 45 1 Note In the first example: At the moment $$$2$$$, the first wave occurs and $$$6$$$ monsters spawn. You kill $$$3$$$ monsters and start reloading. At the moment $$$3$$$, the second wave occurs and $$$3$$$ more monsters spawn. You kill remaining $$$3$$$ monsters from the first wave and start reloading. At the moment $$$4$$$, you kill remaining $$$3$$$ monsters from the second wave. In total, you'll spend $$$9$$$ bullets. In the second example: At moment $$$3$$$, the first wave occurs and $$$11$$$ monsters spawn. You kill $$$5$$$ monsters and start reloading. At moment $$$4$$$, you kill $$$5$$$ more monsters and start reloading. At moment $$$5$$$, you kill the last monster and start reloading throwing away old magazine with $$$4$$$ bullets. At moment $$$10$$$, the second wave occurs and $$$15$$$ monsters spawn. You kill $$$5$$$ monsters and start reloading. At moment $$$11$$$, you kill $$$5$$$ more monsters and start reloading. At moment $$$12$$$, you kill last $$$5$$$ monsters. In total, you'll spend $$$30$$$ bullets.
2,600
false
true
false
true
false
false
false
false
false
false
3,511
1169A
The circle line of the Roflanpolis subway has $$$n$$$ stations. There are two parallel routes in the subway. The first one visits stations in order $$$1 o 2 o ldots o n o 1 o 2 o ldots$$$ (so the next stop after station $$$x$$$ is equal to $$$(x+1)$$$ if $$$x < n$$$ and $$$1$$$ otherwise). The second route visits stations in order $$$n o (n-1) o ldots o 1 o n o (n-1) o ldots$$$ (so the next stop after station $$$x$$$ is equal to $$$(x-1)$$$ if $$$x>1$$$ and $$$n$$$ otherwise). All trains depart their stations simultaneously, and it takes exactly $$$1$$$ minute to arrive at the next station. Two toads live in this city, their names are Daniel and Vlad. Daniel is currently in a train of the first route at station $$$a$$$ and will exit the subway when his train reaches station $$$x$$$. Coincidentally, Vlad is currently in a train of the second route at station $$$b$$$ and he will exit the subway when his train reaches station $$$y$$$. Surprisingly, all numbers $$$a,x,b,y$$$ are distinct. Toad Ilya asks you to check if Daniel and Vlad will ever be at the same station at the same time during their journey. In other words, check if there is a moment when their trains stop at the same station. Note that this includes the moments when Daniel or Vlad enter or leave the subway. Input The first line contains five space-separated integers $$$n$$$, $$$a$$$, $$$x$$$, $$$b$$$, $$$y$$$ ($$$4 leq n leq 100$$$, $$$1 leq a, x, b, y leq n$$$, all numbers among $$$a$$$, $$$x$$$, $$$b$$$, $$$y$$$ are distinct)xa0— the number of stations in Roflanpolis, Daniel's start station, Daniel's finish station, Vlad's start station and Vlad's finish station, respectively. Output Output "YES" if there is a time moment when Vlad and Daniel are at the same station, and "NO" otherwise. You can print each letter in any case (upper or lower). Note In the first example, Daniel and Vlad start at the stations $$$(1, 3)$$$. One minute later they are at stations $$$(2, 2)$$$. They are at the same station at this moment. Note that Vlad leaves the subway right after that. Consider the second example, let's look at the stations Vlad and Daniel are at. They are: initially $$$(2, 9)$$$, after $$$1$$$ minute $$$(3, 8)$$$, after $$$2$$$ minutes $$$(4, 7)$$$, after $$$3$$$ minutes $$$(5, 6)$$$, after $$$4$$$ minutes $$$(6, 5)$$$, after $$$5$$$ minutes $$$(7, 4)$$$, after $$$6$$$ minutes $$$(8, 3)$$$, after $$$7$$$ minutes $$$(9, 2)$$$, after $$$8$$$ minutes $$$(10, 1)$$$, after $$$9$$$ minutes $$$(1, 10)$$$. After that, they both leave the subway because they are at their finish stations, so there is no moment when they both are at the same station.
900
true
false
true
false
false
false
false
false
false
false
4,871