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
85D
In one well-known algorithm of finding the _k_-th order statistics we should divide all elements into groups of five consecutive elements and find the median of each five. A median is called the middle element of a sorted array (it's the third largest element for a group of five). To increase the algorithm's performance speed on a modern video card, you should be able to find a sum of medians in each five of the array. A sum of medians of a sorted _k_-element set _S_u2009=u2009{_a_1,u2009_a_2,u2009...,u2009_a__k_}, where _a_1u2009<u2009_a_2u2009<u2009_a_3u2009<u2009...u2009<u2009_a__k_, will be understood by as The operator stands for taking the remainder, that is stands for the remainder of dividing _x_ by _y_. To organize exercise testing quickly calculating the sum of medians for a changing set was needed. Input The first line contains number _n_ (1u2009≤u2009_n_u2009≤u2009105), the number of operations performed. Then each of _n_ lines contains the description of one of the three operations: add _x_xa0— add the element _x_ to the set; del _x_xa0— delete the element _x_ from the set; sumxa0— find the sum of medians of the set. For any add _x_ operation it is true that the element _x_ is not included in the set directly before the operation. For any del _x_ operation it is true that the element _x_ is included in the set directly before the operation. All the numbers in the input are positive integers, not exceeding 109. Output For each operation sum print on the single line the sum of medians of the current set. If the set is empty, print 0. 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 (also you may use the %I64d specificator). Examples Input 6 add 4 add 5 add 1 add 2 add 3 sum Input 14 add 1 add 7 add 2 add 5 sum add 6 add 8 add 9 add 3 add 4 add 10 sum del 1 sum
2,300
false
false
true
false
true
false
true
true
false
false
9,543
44J
There are many interesting tasks on domino tilings. For example, an interesting fact is known. Let us take a standard chessboard (8u2009×u20098) and cut exactly two squares out of it. It turns out that the resulting board can always be tiled using dominoes 1u2009×u20092, if the two cut out squares are of the same color, otherwise it is impossible. Petya grew bored with dominoes, that's why he took a chessboard (not necessarily 8u2009×u20098), cut some squares out of it and tries to tile it using triminoes. Triminoes are reactangles 1u2009×u20093 (or 3u2009×u20091, because triminoes can be rotated freely), also the two extreme squares of a trimino are necessarily white and the square in the middle is black. The triminoes are allowed to put on the chessboard so that their squares matched the colors of the uncut squares of the chessboard, and also the colors must match: the black squares must be matched with the black ones only and the white ones — with the white squares. The triminoes must not protrude above the chessboard or overlap each other. All the uncut squares of the board must be covered with triminoes. Help Petya find out if it is possible to tile his board using triminos in the described way and print one of the variants of tiling. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000) — the board size. Next _n_ lines contain _m_ symbols each and represent the board description. If some position contains ".", then the square in this position has been cut out. Symbol "w" stands for a white square, "b" stands for a black square. It is guaranteed that through adding the cut squares can result in a correct chessboard (i.e. with alternating black and white squares), thought, perhaps, of a non-standard size. Output If at least one correct tiling exists, in the first line print "YES" (without quotes), and then — the tiling description. The description must contain _n_ lines, _m_ symbols in each. The cut out squares, as well as in the input data, are marked by ".". To denote triminoes symbols "a", "b", "c", "d" can be used, and all the three squares of each trimino must be denoted by the same symbol. If two triminoes share a side, than they must be denoted by different symbols. Two triminoes not sharing a common side can be denoted by one and the same symbol (c.f. sample). If there are multiple correct ways of tiling, it is allowed to print any. If it is impossible to tile the board using triminoes or the correct tiling, for which four symbols "a", "b", "c", "d" would be enough, doesn't exist, print "NO" (without quotes) in the first line. Examples Input 6 10 .w.wbw.wbw wbwbw.w.w. bw.wbwbwbw w.wbw.wbwb ...wbw.w.w ..wbw.wbw. Output YES .a.aaa.ccc baccc.c.a. ba.dddcbab b.aaa.cbab ...bbb.b.b ..ccc.ddd.
2,000
false
true
false
false
false
true
false
false
false
false
9,760
659F
The farmer Polycarp has a warehouse with hay, which can be represented as an _n_u2009×u2009_m_ rectangular table, where _n_ is the number of rows, and _m_ is the number of columns in the table. Each cell of the table contains a haystack. The height in meters of the hay located in the _i_-th row and the _j_-th column is equal to an integer _a__i_,u2009_j_ and coincides with the number of cubic meters of hay in the haystack, because all cells have the size of the base 1u2009×u20091. Polycarp has decided to tidy up in the warehouse by removing an arbitrary integer amount of cubic meters of hay from the top of each stack. You can take different amounts of hay from different haystacks. Besides, it is allowed not to touch a stack at all, or, on the contrary, to remove it completely. If a stack is completely removed, the corresponding cell becomes empty and no longer contains the stack. Polycarp wants the following requirements to hold after the reorganization: the total amount of hay remaining in the warehouse must be equal to _k_, the heights of all stacks (i.e., cells containing a non-zero amount of hay) should be the same, the height of at least one stack must remain the same as it was, for the stability of the remaining structure all the stacks should form one connected region. The two stacks are considered adjacent if they share a side in the table. The area is called connected if from any of the stack in the area you can get to any other stack in this area, moving only to adjacent stacks. In this case two adjacent stacks necessarily belong to the same area. Help Polycarp complete this challenging task or inform that it is impossible. Input The first line of the input contains three integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000) and _k_ (1u2009≤u2009_k_u2009≤u20091018)xa0— the number of rows and columns of the rectangular table where heaps of hay are lain and the required total number cubic meters of hay after the reorganization. Then _n_ lines follow, each containing _m_ positive integers _a__i_,u2009_j_ (1u2009≤u2009_a__i_,u2009_j_u2009≤u2009109), where _a__i_,u2009_j_ is equal to the number of cubic meters of hay making the hay stack on the _i_-th row and _j_-th column of the table. Output In the first line print "YES" (without quotes), if Polycarpus can perform the reorganisation and "NO" (without quotes) otherwise. If the answer is "YES" (without quotes), then in next _n_ lines print _m_ numbersxa0— the heights of the remaining hay stacks. All the remaining non-zero values should be equal, represent a connected area and at least one of these values shouldn't be altered. If there are multiple answers, print any of them. Examples Input 4 4 50 5 9 1 1 5 1 1 5 5 1 5 5 5 5 7 1 Output YES 5 5 0 0 5 0 0 5 5 0 5 5 5 5 5 0 Input 2 4 12 1 1 3 1 1 6 2 4 Note In the first sample non-zero values make up a connected area, their values do not exceed the initial heights of hay stacks. All the non-zero values equal 7, and their number is 5, so the total volume of the remaining hay equals the required value _k_u2009=u20097·5u2009=u200935. At that the stack that is on the second line and third row remained unaltered.
2,000
false
true
false
false
false
false
false
false
true
true
7,184
509C
Problem - 509C - 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 greedy implementation *2000 No tag edit access → Contest materials + qualifying round for the WCC") (Second Winter Computer Camp Selection 2015)") . Next _n_ lines contain integer numbers _b_1, ..., _b__n_ xa0— the required sums of digits. All _b__i_ belong to the range 1u2009≤u2009_b__i_u2009≤u2009300. Output Print _n_ integer numbers, one per linexa0— the correct option for numbers _a__i_, in order of following in sequence. The sequence should be strictly increasing. The sum of digits of the _i_-th number should be equal to _b__i_. If there are multiple sequences with least possible number _a__n_, print any of them. Print the numbers without leading zeroes. Examples Input 3 1 2 3 Output 1 2 3 Input 3 3 2 1 Output 3 11 100
2,000
false
true
true
true
false
false
false
false
false
false
7,804
1118A
Polycarp wants to cook a soup. To do it, he needs to buy exactly $$$n$$$ liters of water. There are only two types of water bottles in the nearby shop — $$$1$$$-liter bottles and $$$2$$$-liter bottles. There are infinitely many bottles of these two types in the shop. The bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles correspondingly. Polycarp wants to spend as few money as possible. Your task is to find the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a$$$ burles and the bottle of the second type costs $$$b$$$ burles. You also have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 500$$$) — the number of queries. The next $$$q$$$ lines contain queries. The $$$i$$$-th query is given as three space-separated integers $$$n_i$$$, $$$a_i$$$ and $$$b_i$$$ ($$$1 le n_i le 10^{12}, 1 le a_i, b_i le 1000$$$) — how many liters Polycarp needs in the $$$i$$$-th query, the cost (in burles) of the bottle of the first type in the $$$i$$$-th query and the cost (in burles) of the bottle of the second type in the $$$i$$$-th query, respectively. Output Print $$$q$$$ integers. The $$$i$$$-th integer should be equal to the minimum amount of money (in burles) Polycarp needs to buy exactly $$$n_i$$$ liters of water in the nearby shop if the bottle of the first type costs $$$a_i$$$ burles and the bottle of the second type costs $$$b_i$$$ burles. Example Input 4 10 1 3 7 3 2 1 1000 1 1000000000000 42 88 Output 10 9 1000 42000000000000
800
true
false
false
false
false
false
false
false
false
false
5,102
1425I
Mr. Chanek has an orchard structured as a rooted ternary tree with $$$N$$$ vertices numbered from $$$1$$$ to $$$N$$$. The root of the tree is vertex $$$1$$$. $$$P_i$$$ denotes the parent of vertex $$$i$$$, for $$$(2 le i le N)$$$. Interestingly, the height of the tree is not greater than $$$10$$$. Height of a tree is defined to be the largest distance from the root to a vertex in the tree. There exist a bush on each vertex of the tree. Initially, all bushes have fruits. Fruits will not grow on bushes that currently already have fruits. The bush at vertex $$$i$$$ will grow fruits after $$$A_i$$$ days since its last harvest. Mr. Chanek will visit his orchard for $$$Q$$$ days. In day $$$i$$$, he will harvest all bushes that have fruits on the subtree of vertex $$$X_i$$$. For each day, determine the sum of distances from every harvested bush to $$$X_i$$$, and the number of harvested bush that day. Harvesting a bush means collecting all fruits on the bush. For example, if Mr. Chanek harvests all fruits on subtree of vertex $$$X$$$, and harvested bushes $$$[Y_1, Y_2, dots, Y_M]$$$, the sum of distances is $$$sum_{i = 1}^M ext{distance}(X, Y_i)$$$ $$$ ext{distance}(U, V)$$$ in a tree is defined to be the number of edges on the simple path from $$$U$$$ to $$$V$$$. Input The first line contains two integers $$$N$$$ and $$$Q$$$ $$$(1 le N, Q,le 5 cdot 10^4)$$$, which denotes the number of vertices and the number of days Mr. Chanek visits the orchard. The second line contains $$$N$$$ integers $$$A_i$$$ $$$(1 le A_i le 5 cdot 10^4)$$$, which denotes the fruits growth speed on the bush at vertex $$$i$$$, for $$$(1 le i le N)$$$. The third line contains $$$N-1$$$ integers $$$P_i$$$ $$$(1 le P_i le N, P_i e i)$$$, which denotes the parent of vertex $$$i$$$ in the tree, for $$$(2 le i le N)$$$. It is guaranteed that each vertex can be the parent of at most $$$3$$$ other vertices. It is also guaranteed that the height of the tree is not greater than $$$10$$$. The next $$$Q$$$ lines contain a single integer $$$X_i$$$ $$$(1 le X_i le N)$$$, which denotes the start of Mr. Chanek's visit on day $$$i$$$, for $$$(1 le i le Q)$$$. Output Output $$$Q$$$ lines, line $$$i$$$ gives the sum of distances from the harvested bushes to $$$X_i$$$, and the number of harvested bushes. Examples Input 5 3 2 1 1 3 2 1 2 2 1 1 1 1 Note For the first example: On day 1, Mr. Chanek starts at vertex $$$2$$$ and can harvest the bush at vertex 2. On day 2, Mr. Chanek starts at vertex $$$1$$$ and only harvest from bush $$$1$$$ (bush 2's fruit still has not grown yet). On day 3, Mr. Chanek starts at vertex $$$1$$$ and harvests the fruits on bush $$$1$$$ and $$$2$$$. The sum of distances from every harvested bush to $$$1$$$ is $$$1$$$. For the second example, Mr. Chanek always starts at vertex $$$1$$$. The bushes which Mr. Chanek harvests on day one, two, and three are $$$[1, 2, 3, 4, 5], [2, 3], [1, 2, 3, 5]$$$, respectively.
2,800
false
false
false
false
true
false
false
false
false
false
3,540
234A
One fine October day a mathematics teacher Vasily Petrov went to a class and saw there _n_ pupils who sat at the desks, two people at each desk. Vasily quickly realized that number _n_ is even. Like all true mathematicians, Vasily has all students numbered from 1 to _n_. But Vasily Petrov did not like the way the children were seated at the desks. According to him, the students whose numbers differ by 1, can not sit together, as they talk to each other all the time, distract others and misbehave. On the other hand, if a righthanded student sits at the left end of the desk and a lefthanded student sits at the right end of the desk, they hit elbows all the time and distract each other. In other cases, the students who sit at the same desk, do not interfere with each other. Vasily knows very well which students are lefthanders and which ones are righthanders, and he asks you to come up with any order that meets these two uncomplicated conditions (students do not talk to each other and do not bump their elbows). It is guaranteed that the input is such that at least one way to seat the students always exists. Input The first input line contains a single even integer _n_ (4u2009≤u2009_n_u2009≤u2009100) — the number of students in the class. The second line contains exactly _n_ capital English letters "L" and "R". If the _i_-th letter at the second line equals "L", then the student number _i_ is a lefthander, otherwise he is a righthander. Output Print integer pairs, one pair per line. In the _i_-th line print the numbers of students that will sit at the _i_-th desk. The first number in the pair stands for the student who is sitting to the left, and the second number stands for the student who is sitting to the right. Separate the numbers in the pairs by spaces. If there are multiple solutions, print any of them.
1,200
false
false
true
false
false
false
false
false
false
false
8,904
416B
A well-known art union called "Kalevich is Alive!" manufactures objects d'art (pictures). The union consists of _n_ painters who decided to organize their work as follows. Each painter uses only the color that was assigned to him. The colors are distinct for all painters. Let's assume that the first painter uses color 1, the second one uses color 2, and so on. Each picture will contain all these _n_ colors. Adding the _j_-th color to the _i_-th picture takes the _j_-th painter _t__ij_ units of time. Order is important everywhere, so the painters' work is ordered by the following rules: Each picture is first painted by the first painter, then by the second one, and so on. That is, after the _j_-th painter finishes working on the picture, it must go to the (_j_u2009+u20091)-th painter (if _j_u2009<u2009_n_); each painter works on the pictures in some order: first, he paints the first picture, then he paints the second picture and so on; each painter can simultaneously work on at most one picture. However, the painters don't need any time to have a rest; as soon as the _j_-th painter finishes his part of working on the picture, the picture immediately becomes available to the next painter. Given that the painters start working at time 0, find for each picture the time when it is ready for sale. Input The first line of the input contains integers _m_,u2009_n_ (1u2009≤u2009_m_u2009≤u200950000,u20091u2009≤u2009_n_u2009≤u20095), where _m_ is the number of pictures and _n_ is the number of painters. Then follow the descriptions of the pictures, one per line. Each line contains _n_ integers _t__i_1,u2009_t__i_2,u2009...,u2009_t__in_ (1u2009≤u2009_t__ij_u2009≤u20091000), where _t__ij_ is the time the _j_-th painter needs to work on the _i_-th picture. Output Print the sequence of _m_ integers _r_1,u2009_r_2,u2009...,u2009_r__m_, where _r__i_ is the moment when the _n_-th painter stopped working on the _i_-th picture.
1,300
false
false
true
true
false
false
true
false
false
false
8,161
570D
Roman planted a tree consisting of _n_ vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the _n_u2009-u20091 remaining vertices has a parent in the tree. Vertex is connected with its parent by an edge. The parent of vertex _i_ is vertex _p__i_, the parent index is always less than the index of the vertex (i.e., _p__i_u2009<u2009_i_). The depth of the vertex is the number of nodes on the path from the root to _v_ along the edges. In particular, the depth of the root is equal to 1. We say that vertex _u_ is in the subtree of vertex _v_, if we can get from _u_ to _v_, moving from the vertex to the parent. In particular, vertex _v_ is in its subtree. Roma gives you _m_ queries, the _i_-th of which consists of two numbers _v__i_, _h__i_. Let's consider the vertices in the subtree _v__i_ located at depth _h__i_. Determine whether you can use the letters written at these vertices to make a string that is a palindrome. The letters that are written in the vertexes, can be rearranged in any order to make a palindrome, but all letters should be used. Input The first line contains two integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009500u2009000) — the number of nodes in the tree and queries, respectively. The following line contains _n_u2009-u20091 integers _p_2,u2009_p_3,u2009...,u2009_p__n_ — the parents of vertices from the second to the _n_-th (1u2009≤u2009_p__i_u2009<u2009_i_). The next line contains _n_ lowercase English letters, the _i_-th of these letters is written on vertex _i_. Next _m_ lines describe the queries, the _i_-th line contains two numbers _v__i_, _h__i_ (1u2009≤u2009_v__i_,u2009_h__i_u2009≤u2009_n_) — the vertex and the depth that appear in the _i_-th query. Output Print _m_ lines. In the _i_-th line print "Yes" (without the quotes), if in the _i_-th query you can make a palindrome from the letters written on the vertices, otherwise print "No" (without the quotes). Examples Input 6 5 1 1 1 3 3 zacccd 1 1 3 3 4 1 6 1 1 2 Note String _s_ is a palindrome if reads the same from left to right and from right to left. In particular, an empty string is a palindrome. Clarification for the sample test. In the first query there exists only a vertex 1 satisfying all the conditions, we can form a palindrome "z". In the second query vertices 5 and 6 satisfy condititions, they contain letters "с" and "d" respectively. It is impossible to form a palindrome of them. In the third query there exist no vertices at depth 1 and in subtree of 4. We may form an empty palindrome. In the fourth query there exist no vertices in subtree of 6 at depth 1. We may form an empty palindrome. In the fifth query there vertices 2, 3 and 4 satisfying all conditions above, they contain letters "a", "c" and "c". We may form a palindrome "cac".
2,200
false
false
false
false
false
true
false
true
false
true
7,568
1677D
Tokitsukaze has a permutation $$$p$$$. She performed the following operation to $$$p$$$ exactly $$$k$$$ times: in one operation, for each $$$i$$$ from $$$1$$$ to $$$n - 1$$$ in order, if $$$p_i$$$ > $$$p_{i+1}$$$, swap $$$p_i$$$, $$$p_{i+1}$$$. After exactly $$$k$$$ times of operations, Tokitsukaze got a new sequence $$$a$$$, obviously the sequence $$$a$$$ is also a permutation. After that, Tokitsukaze wrote down the value sequence $$$v$$$ of $$$a$$$ on paper. Denote the value sequence $$$v$$$ of the permutation $$$a$$$ of length $$$n$$$ as $$$v_i=sum_{j=1}^{i-1}[a_i < a_j]$$$, where the value of $$$[a_i < a_j]$$$ define as if $$$a_i < a_j$$$, the value is $$$1$$$, otherwise is $$$0$$$ (in other words, $$$v_i$$$ is equal to the number of elements greater than $$$a_i$$$ that are to the left of position $$$i$$$). Then Tokitsukaze went out to work. There are three naughty cats in Tokitsukaze's house. When she came home, she found the paper with the value sequence $$$v$$$ to be bitten out by the cats, leaving several holes, so that the value of some positions could not be seen clearly. She forgot what the original permutation $$$p$$$ was. She wants to know how many different permutations $$$p$$$ there are, so that the value sequence $$$v$$$ of the new permutation $$$a$$$ after exactly $$$k$$$ operations is the same as the $$$v$$$ written on the paper (not taking into account the unclear positions). Since the answer may be too large, print it modulo $$$998,244,353$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^6$$$; $$$0 leq k leq n-1$$$)xa0— the length of the permutation and the exactly number of operations. The second line contains $$$n$$$ integers $$$v_1, v_2, dots, v_n$$$ ($$$-1 leq v_i leq i-1$$$)xa0— the value sequence $$$v$$$. $$$v_i = -1$$$ means the $$$i$$$-th position of $$$v$$$ can't be seen clearly. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, print a single integerxa0— the number of different permutations modulo $$$998,244,353$$$. Example Input 3 5 0 0 1 2 3 4 5 2 -1 1 2 0 0 5 2 0 1 1 0 0 Note In the first test case, only permutation $$$p=[5,4,3,2,1]$$$ satisfies the constraint condition. In the second test case, there are $$$6$$$ permutations satisfying the constraint condition, which are: $$$[3,4,5,2,1]$$$ $$$ ightarrow$$$ $$$[3,4,2,1,5]$$$ $$$ ightarrow$$$ $$$[3,2,1,4,5]$$$ $$$[3,5,4,2,1]$$$ $$$ ightarrow$$$ $$$[3,4,2,1,5]$$$ $$$ ightarrow$$$ $$$[3,2,1,4,5]$$$ $$$[4,3,5,2,1]$$$ $$$ ightarrow$$$ $$$[3,4,2,1,5]$$$ $$$ ightarrow$$$ $$$[3,2,1,4,5]$$$ $$$[4,5,3,2,1]$$$ $$$ ightarrow$$$ $$$[4,3,2,1,5]$$$ $$$ ightarrow$$$ $$$[3,2,1,4,5]$$$ $$$[5,3,4,2,1]$$$ $$$ ightarrow$$$ $$$[3,4,2,1,5]$$$ $$$ ightarrow$$$ $$$[3,2,1,4,5]$$$ $$$[5,4,3,2,1]$$$ $$$ ightarrow$$$ $$$[4,3,2,1,5]$$$ $$$ ightarrow$$$ $$$[3,2,1,4,5]$$$ So after exactly $$$2$$$ times of swap they will all become $$$a=[3,2,1,4,5]$$$, whose value sequence is $$$v=[0,1,2,0,0]$$$.
2,500
true
false
false
true
false
false
false
false
false
false
2,203
1695E
Polycarp and Monocarp are both solving the same puzzle with dominoes. They are given the same set of $$$n$$$ dominoes, the $$$i$$$-th of which contains two numbers $$$x_i$$$ and $$$y_i$$$. They are also both given the same $$$m$$$ by $$$k$$$ grid of values $$$a_{ij}$$$ such that $$$mcdot k = 2n$$$. The puzzle asks them to place the $$$n$$$ dominoes on the grid in such a way that none of them overlap, and the values on each domino match the $$$a_{ij}$$$ values that domino covers. Dominoes can be rotated arbitrarily before being placed on the grid, so the domino $$$(x_i, y_i)$$$ is equivalent to the domino $$$(y_i, x_i)$$$. They have both solved the puzzle, and compared their answers, but noticed that not only did their solutions not match, but none of the $$$n$$$ dominoes were in the same location in both solutions! Formally, if two squares were covered by the same domino in Polycarp's solution, they were covered by different dominoes in Monocarp's solution. The diagram below shows one potential $$$a$$$ grid, along with the two players' solutions. Polycarp and Monocarp remember the set of dominoes they started with, but they have lost the grid $$$a$$$. Help them reconstruct one possible grid $$$a$$$, along with both of their solutions, or determine that no such grid exists. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 3cdot 10^5$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le 2n$$$). Output If there is no solution, print a single integer $$$-1$$$. Otherwise, print $$$m$$$ and $$$k$$$, the height and width of the puzzle grid, on the first line of output. These should satisfy $$$mcdot k = 2n$$$. The $$$i$$$-th of the next $$$m$$$ lines should contain $$$k$$$ integers, the $$$j$$$-th of which is $$$a_{ij}$$$. The next $$$m$$$ lines describe Polycarp's solution. Print $$$m$$$ lines of $$$k$$$ characters each. For each square, if it is covered by the upper half of a domino in Polycarp's solution, it should contain a "U". Similarly, if it is covered by the bottom, left, or right half of a domino, it should contain "D", "L", or "R", respectively. The next $$$m$$$ lines should describe Monocarp's solution, in the same format as Polycarp's solution. If there are multiple answers, print any. Examples Output 2 2 2 1 1 1 LR LR UU DD Input 10 1 3 1 1 2 1 3 4 1 5 1 5 3 1 2 4 3 3 4 1 Output 4 5 1 2 5 1 5 3 4 1 3 1 1 2 4 4 1 1 3 3 3 1 LRULR LRDLR ULRLR DLRLR UULRU DDUUD LRDDU LRLRD Note Extra blank lines are added to the output for clarity, but are not required. The third sample case corresponds to the image from the statement.
2,700
false
false
false
false
false
true
false
false
false
true
2,109
540A
Problem - 540A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials xa0— the number of disks on the combination lock. The second line contains a string of _n_ digitsxa0— the original state of the disks. The third line contains a string of _n_ digitsxa0— Scrooge McDuck's combination that opens the lock. Output Print a single integerxa0— the minimum number of moves Scrooge McDuck needs to open the lock. Examples Input 5 82195 64723 Output 13 Note In the sample he needs 13 moves: 1 disk: 2 disk: 3 disk: 4 disk: 5 disk:
800
false
false
true
false
false
false
false
false
false
false
7,680
132B
Piet is one of the most known visual esoteric programming languages. The programs in Piet are constructed from colorful blocks of pixels and interpreted using pretty complicated rules. In this problem we will use a subset of Piet language with simplified rules. The program will be a rectangular image consisting of colored and black pixels. The color of each pixel will be given by an integer number between 0 and 9, inclusive, with 0 denoting black. A block of pixels is defined as a rectangle of pixels of the same color (not black). It is guaranteed that all connected groups of colored pixels of the same color will form rectangular blocks. Groups of black pixels can form arbitrary shapes. The program is interpreted using movement of instruction pointer (IP) which consists of three parts: current block pointer (BP); note that there is no concept of current pixel within the block; direction pointer (DP) which can point left, right, up or down; block chooser (CP) which can point to the left or to the right from the direction given by DP; in absolute values CP can differ from DP by 90 degrees counterclockwise or clockwise, respectively. Initially BP points to the block which contains the top-left corner of the program, DP points to the right, and CP points to the left (see the orange square on the image below). One step of program interpretation changes the state of IP in a following way. The interpreter finds the furthest edge of the current color block in the direction of the DP. From all pixels that form this edge, the interpreter selects the furthest one in the direction of CP. After this, BP attempts to move from this pixel into the next one in the direction of DP. If the next pixel belongs to a colored block, this block becomes the current one, and two other parts of IP stay the same. It the next pixel is black or outside of the program, BP stays the same but two other parts of IP change. If CP was pointing to the left, now it points to the right, and DP stays the same. If CP was pointing to the right, now it points to the left, and DP is rotated 90 degrees clockwise. This way BP will never point to a black block (it is guaranteed that top-left pixel of the program will not be black). You are given a Piet program. You have to figure out which block of the program will be current after _n_ steps. Input The first line of the input contains two integer numbers _m_ (1u2009≤u2009_m_u2009≤u200950) and _n_ (1u2009≤u2009_n_u2009≤u20095·107). Next _m_ lines contain the rows of the program. All the lines have the same length between 1 and 50 pixels, and consist of characters 0-9. The first character of the first line will not be equal to 0. Note In the first example IP changes in the following way. After step 1 block 2 becomes current one and stays it after two more steps. After step 4 BP moves to block 3, after step 7 — to block 4, and finally after step 10 BP returns to block 1. The sequence of states of IP is shown on the image: the arrows are traversed clockwise, the main arrow shows direction of DP, the side one — the direction of CP.
2,100
false
false
true
false
false
false
false
false
false
false
9,346
616D
Problem - 616D - Codeforces =============== xa0 — the number of elements in _a_ and the parameter _k_. The second line contains _n_ integers _a__i_ (0u2009≤u2009_a__i_u2009≤u2009106) — the elements of the array _a_. Output Print two integers _l_,u2009_r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_) — the index of the left and the index of the right ends of some k-good longest segment. If there are several longest segments you can print any of them. The elements in _a_ are numbered from 1 to _n_ from left to right. Examples Input 5 5 1 2 3 4 5 Output 1 5 Input 9 3 6 5 1 2 3 2 1 4 5 Output 3 7 Input 3 1 1 2 3 Output 1 1
1,600
false
false
false
false
true
false
false
true
false
false
7,372
1056D
There is one apple tree in Arkady's garden. It can be represented as a set of junctions connected with branches so that there is only one way to reach any junctions from any other one using branches. The junctions are enumerated from $$$1$$$ to $$$n$$$, the junction $$$1$$$ is called the root. A subtree of a junction $$$v$$$ is a set of junctions $$$u$$$ such that the path from $$$u$$$ to the root must pass through $$$v$$$. Note that $$$v$$$ itself is included in a subtree of $$$v$$$. A leaf is such a junction that its subtree contains exactly one junction. The New Year is coming, so Arkady wants to decorate the tree. He will put a light bulb of some color on each leaf junction and then count the number happy junctions. A happy junction is such a junction $$$t$$$ that all light bulbs in the subtree of $$$t$$$ have different colors. Arkady is interested in the following question: for each $$$k$$$ from $$$1$$$ to $$$n$$$, what is the minimum number of different colors needed to make the number of happy junctions be greater than or equal to $$$k$$$? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of junctions in the tree. The second line contains $$$n - 1$$$ integers $$$p_2$$$, $$$p_3$$$, ..., $$$p_n$$$ ($$$1 le p_i < i$$$), where $$$p_i$$$ means there is a branch between junctions $$$i$$$ and $$$p_i$$$. It is guaranteed that this set of branches forms a tree. Output Output $$$n$$$ integers. The $$$i$$$-th of them should be the minimum number of colors needed to make the number of happy junctions be at least $$$i$$$. Note In the first example for $$$k = 1$$$ and $$$k = 2$$$ we can use only one color: the junctions $$$2$$$ and $$$3$$$ will be happy. For $$$k = 3$$$ you have to put the bulbs of different colors to make all the junctions happy. In the second example for $$$k = 4$$$ you can, for example, put the bulbs of color $$$1$$$ in junctions $$$2$$$ and $$$4$$$, and a bulb of color $$$2$$$ into junction $$$5$$$. The happy junctions are the ones with indices $$$2$$$, $$$3$$$, $$$4$$$ and $$$5$$$ then.
1,600
false
true
false
true
false
true
false
false
true
true
5,429
1949K
You are given $$$n$$$ positive integers $$$x_1, x_2, ldots, x_n$$$ and three positive integers $$$n_a, n_b, n_c$$$ satisfying $$$n_a+n_b+n_c = n$$$. You want to split the $$$n$$$ positive integers into three groups, so that: The first group contains $$$n_a$$$ numbers, the second group contains $$$n_b$$$ numbers, the third group contains $$$n_c$$$ numbers. Let $$$s_a$$$ be the sum of the numbers in the first group, $$$s_b$$$ be the sum in the second group, and $$$s_c$$$ be the sum in the third group. Then $$$s_a, s_b, s_c$$$ are the sides of a triangle with positive area. Determine if this is possible. If this is possible, find one way to do so. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1le tle 100,000$$$)xa0— the number of test cases. The descriptions of the $$$t$$$ test cases follow. The first line of each test case contains the integers $$$n, n_a, n_b, n_c$$$ ($$$3 leq n leq 200,000, 1leq n_a,n_b,n_c leq n-2, n_a+n_b+n_c = n$$$)xa0— the number of integers to split into three groups, and the desired sizes of the three groups. The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, ldots, x_n$$$ ($$$1 leq x_i leq 10^{9}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200,000$$$. Output For each test case, print $$$ exttt{YES}$$$ if it is possible to split the numbers into three groups satisfying all the conditions. Otherwise, print $$$ exttt{NO}$$$. If such a split exists, then describe the three groups as follows. On the next line, print $$$n_a$$$ integers $$$a_1, a_2, ldots, a_{n_a}$$$xa0— the numbers in the first group. On the next line, print $$$n_b$$$ integers $$$b_1, b_2, ldots, b_{n_b}$$$xa0— the numbers in the second group. On the next line, print $$$n_c$$$ integers $$$c_1, c_2, ldots, c_{n_c}$$$xa0— the numbers in the third group. These $$$n_a+n_b+n_c=n$$$ integers should be a permutation of $$$x_1, x_2, ldots, x_n$$$, and they should satisfy the conditions from the statement. If there are multiple solutions, print any of them. Example Input 4 6 2 2 2 1 1 1 1 1 1 5 3 1 1 1 1 1 1 1 6 2 2 2 1 1 1 1 1 3 8 1 2 5 16 1 1 1 1 1 1 12 Output YES 1 1 1 1 1 1 NO NO YES 16 12 1 1 1 1 1 1 Note In the first test case, we can put two $$$1$$$s into each group: the sum in each group would be $$$2$$$, and there exists a triangle with positive area and sides $$$2$$$, $$$2$$$, $$$2$$$. In the second and third test cases, it can be shown that there is no such way to split numbers into groups. In the fourth test case, we can put number $$$16$$$ into the first group, with sum $$$16$$$, numbers $$$12$$$ and $$$1$$$ into the second group, with sum $$$13$$$, and the remaining five $$$1$$$s into the third group, with sum $$$5$$$, as there exists a triangle with positive area and sides $$$16, 13, 5$$$.
2,800
true
false
false
false
false
true
false
false
false
false
582
1790B
Taisia has $$$n$$$ six-sided dice. Each face of the die is marked with a number from $$$1$$$ to $$$6$$$, each number from $$$1$$$ to $$$6$$$ is used once. Taisia rolls all $$$n$$$ dice at the same time and gets a sequence of values $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 6$$$), where $$$a_i$$$ is the value on the upper face of the $$$i$$$-th dice. The sum of this sequence is equal to $$$s$$$. Suddenly, Taisia's pet cat steals exactly one dice with maximum value $$$a_i$$$ and calculates the sum of the values on the remaining $$$n-1$$$ dice, which is equal to $$$r$$$. You only know the number of dice $$$n$$$ and the values of $$$s$$$, $$$r$$$. Restore a possible sequence $$$a$$$ that fulfills the constraints. Input The first line contains the integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Each testcase is given on a separate line and contains three integers $$$n$$$, $$$s$$$, $$$r$$$ ($$$2 le n le 50$$$, $$$1 le r < s le 300$$$). It is guaranteed that a solution exists. Output For each testcase, print: $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ in any order. It is guaranteed that such sequence exists. If there are multiple solutions, print any. Example Input 7 2 2 1 2 4 2 4 9 5 5 17 11 3 15 10 4 4 3 5 20 15 Output 1 1 2 2 1 2 2 4 6 4 2 3 2 5 5 5 1 1 1 1 1 4 5 5 5
800
true
true
false
false
false
false
false
false
false
false
1,541
25D
Berland Government decided to improve relations with neighboring countries. First of all, it was decided to build new roads so that from each city of Berland and neighboring countries it became possible to reach all the others. There are _n_ cities in Berland and neighboring countries in total and exactly _n_u2009-u20091 two-way roads. Because of the recent financial crisis, the Berland Government is strongly pressed for money, so to build a new road it has to close some of the existing ones. Every day it is possible to close one existing road and immediately build a new one. Your task is to determine how many days would be needed to rebuild roads so that from each city it became possible to reach all the others, and to draw a plan of closure of old roads and building of new ones. Input The first line contains integer _n_ (2u2009≤u2009_n_u2009≤u20091000) — amount of cities in Berland and neighboring countries. Next _n_u2009-u20091 lines contain the description of roads. Each road is described by two space-separated integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_) — pair of cities, which the road connects. It can't be more than one road between a pair of cities. No road connects the city with itself. Output Output the answer, number _t_ — what is the least amount of days needed to rebuild roads so that from each city it became possible to reach all the others. Then output _t_ lines — the plan of closure of old roads and building of new ones. Each line should describe one day in the format i j u v — it means that road between cities i and j became closed and a new road between cities u and v is built. Cities are numbered from 1. If the answer is not unique, output any.
1,900
false
false
false
false
false
false
false
false
false
true
9,870
1574B
You are given four integer values $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$. Check if there exists a string that contains: $$$a$$$ letters 'A'; $$$b$$$ letters 'B'; $$$c$$$ letters 'C'; no other letters; exactly $$$m$$$ pairs of adjacent equal letters (exactly $$$m$$$ such positions $$$i$$$ that the $$$i$$$-th letter is equal to the $$$(i+1)$$$-th one). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. Each of the next $$$t$$$ lines contains the description of the testcasexa0— four integers $$$a$$$, $$$b$$$, $$$c$$$ and $$$m$$$ ($$$1 le a, b, c le 10^8$$$; $$$0 le m le 10^8$$$). Output For each testcase print "YES" if there exists a string that satisfies all the requirements. Print "NO" if there are no such strings. 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 3 2 2 1 0 1 1 1 1 1 2 3 2 Note In the first testcase strings "ABCAB" or "BCABA" satisfy the requirements. There exist other possible strings. In the second testcase there's no way to put adjacent equal letters if there's no letter that appears at least twice. In the third testcase string "CABBCC" satisfies the requirements. There exist other possible strings.
1,100
true
true
false
false
false
false
false
false
false
false
2,773
1713B
Consider an array $$$a$$$ of $$$n$$$ positive integers. You may perform the following operation: select two indices $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$), then decrease all elements $$$a_l, a_{l + 1}, dots, a_r$$$ by $$$1$$$. Let's call $$$f(a)$$$ the minimum number of operations needed to change array $$$a$$$ into an array of $$$n$$$ zeros. Determine if for all permutations$$$^dagger$$$ $$$b$$$ of $$$a$$$, $$$f(a) leq f(b)$$$ is true. $$$^dagger$$$ An array $$$b$$$ is a permutation of an array $$$a$$$ if $$$b$$$ consists of the elements of $$$a$$$ in arbitrary order. For example, $$$[4,2,3,4]$$$ is a permutation of $$$[3,2,4,4]$$$ while $$$[1,2,2]$$$ is not a permutation of $$$[1,2,3]$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the length of the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) — description 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, print "YES" (without quotes) if for all permutations $$$b$$$ of $$$a$$$, $$$f(a) leq f(b)$$$ is true, and "NO" (without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Input 3 4 2 3 5 4 3 1 2 3 4 3 1 3 2 Note In the first test case, we can change all elements to $$$0$$$ in $$$5$$$ operations. It can be shown that no permutation of $$$[2, 3, 5, 4]$$$ requires less than $$$5$$$ operations to change all elements to $$$0$$$. In the third test case, we need $$$5$$$ operations to change all elements to $$$0$$$, while $$$[2, 3, 3, 1]$$$ only needs $$$3$$$ operations.
1,000
false
false
false
false
false
true
false
false
true
false
2,005
314B
Problem - 314B - 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 dfs and similar strings *2000 No tag edit access → Contest materials , that
2,000
false
false
false
false
false
false
false
true
false
false
8,589
1543D2
This is the hard version of the problem. The only difference is that here $$$2leq kleq 100$$$. You can make hacks only if both the versions of the problem are solved. This is an interactive problem! Every decimal number has a base $$$k$$$ equivalent. The individual digits of a base $$$k$$$ number are called $$$k$$$-its. Let's define the $$$k$$$-itwise XOR of two $$$k$$$-its $$$a$$$ and $$$b$$$ as $$$(a + b)bmod k$$$. The $$$k$$$-itwise XOR of two base $$$k$$$ numbers is equal to the new number formed by taking the $$$k$$$-itwise XOR of their corresponding $$$k$$$-its. The $$$k$$$-itwise XOR of two decimal numbers $$$a$$$ and $$$b$$$ is denoted by $$$aoplus_{k} b$$$ and is equal to the decimal representation of the $$$k$$$-itwise XOR of the base $$$k$$$ representations of $$$a$$$ and $$$b$$$. All further numbers used in the statement below are in decimal unless specified. You have hacked the criminal database of Rockport Police Department (RPD), also known as the Rap Sheet. But in order to access it, you require a password. You don't know it, but you are quite sure that it lies between $$$0$$$ and $$$n-1$$$ inclusive. So, you have decided to guess it. Luckily, you can try at most $$$n$$$ times without being blocked by the system. But the system is adaptive. Each time you make an incorrect guess, it changes the password. Specifically, if the password before the guess was $$$x$$$, and you guess a different number $$$y$$$, then the system changes the password to a number $$$z$$$ such that $$$xoplus_{k} z=y$$$. Guess the password and break into the system. Input The first line of input contains a single integer $$$t$$$ ($$$1leq tleq 10,000$$$) denoting the number of test cases. $$$t$$$ test cases follow. The first line of each test case contains two integers $$$n$$$ ($$$1leq nleq 2cdot 10^5$$$) and $$$k$$$ $$$(2leq kleq 100)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Interaction For each test case, first read two integers $$$n$$$ and $$$k$$$. Then you may ask up to $$$n$$$ queries. For each query, print a single integer $$$y$$$ ($$$0leq yleq 2cdot 10^7$$$). Let the current password be $$$x$$$. After that, read an integer $$$r$$$. If $$$x=y$$$, you will read $$$r=1$$$ and the test case is solved. You must then continue solving the remaining test cases. Else, you will read $$$r=0$$$. At this moment the password is changed to a number $$$z$$$ such that $$$xoplus_{k} z=y$$$. After printing a query, do not forget to output the end of line and flush the output. Otherwise, you will get the Idleness limit exceeded verdict. 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. If you ask an invalid query or exceed $$$n$$$ queries, you will read $$$r=-1$$$ and you will receive the Wrong Answer verdict. Make sure to exit immediately to avoid unexpected verdicts. Note that the interactor is adaptive. That is, the original password is not fixed in the beginning and may depend on your queries. But it is guaranteed that at any moment there is at least one initial password such that all the answers to the queries are consistent. Hacks: To use hacks, use the following format of tests: The first line should contain a single integer $$$t$$$ ($$$1leq tleq 10,000$$$) — the number of test cases. The first and only line of each test case should contain two integers $$$n$$$ ($$$1leq nleq 2cdot 10^5$$$) and $$$k$$$ ($$$2leq kleq 100$$$) denoting the number of queries and the base respectively. The optimal original password is automatically decided by the adaptive interactor. You must ensure that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Example Input 2 5 2 0 0 1 5 3 0 0 1 Note Test Case 1: In this case, the hidden password is $$$2$$$. The first query is $$$3$$$. It is not equal to the current password. So, $$$0$$$ is returned, and the password is changed to $$$1$$$ since $$$2oplus_2 1=3$$$. The second query is $$$4$$$. It is not equal to the current password. So, $$$0$$$ is returned, and the password is changed to $$$5$$$ since $$$1oplus_2 5=4$$$. The third query is $$$5$$$. It is equal to the current password. So, $$$1$$$ is returned, and the job is done. Test Case 2: In this case, the hidden password is $$$3$$$. The first query is $$$1$$$. It is not equal to the current password. So, $$$0$$$ is returned, and the password is changed to $$$7$$$ since $$$3oplus_3 7=1$$$. $$$[3=(10)_3$$$, $$$7=(21)_3$$$, $$$1=(01)_3$$$ and $$$(10)_3oplus_3 (21)_3 = (01)_3]$$$. The second query is $$$4$$$. It is not equal to the current password. So, $$$0$$$ is returned, and the password is changed to $$$6$$$ since $$$7oplus_3 6=4$$$. $$$[7=(21)_3$$$, $$$6=(20)_3$$$, $$$4=(11)_3$$$ and $$$(21)_3oplus_3 (20)_3 = (11)_3]$$$. The third query is $$$6$$$. It is equal to the current password. So, $$$1$$$ is returned, and the job is done. Note that these initial passwords are taken just for the sake of explanation. In reality, the grader might behave differently because it is adaptive.
2,200
true
false
false
false
false
true
true
false
false
false
2,924
778C
Peterson loves to learn new languages, but his favorite hobby is making new ones. Language is a set of words, and word is a sequence of lowercase Latin letters. Peterson makes new language every morning. It is difficult task to store the whole language, so Peterson have invented new data structure for storing his languages which is called broom. Broom is rooted tree with edges marked with letters. Initially broom is represented by the only vertexxa0— the root of the broom. When Peterson wants to add new word to the language he stands at the root and processes the letters of new word one by one. Consider that Peterson stands at the vertex _u_. If there is an edge from _u_ marked with current letter, Peterson goes through this edge. Otherwise Peterson adds new edge from _u_ to the new vertex _v_, marks it with the current letter and goes through the new edge. Size of broom is the number of vertices in it. In the evening after working day Peterson can't understand the language he made this morning. It is too difficult for bored Peterson and he tries to make it simpler. Simplification of the language is the process of erasing some letters from some words of this language. Formally, Peterson takes some positive integer _p_ and erases _p_-th letter from all the words of this language having length at least _p_. Letters in words are indexed starting by 1. Peterson considers that simplification should change at least one word, i.e. there has to be at least one word of length at least _p_. Peterson tries to make his language as simple as possible, so he wants to choose _p_ such that the size of the broom for his simplified language is as small as possible. Peterson is pretty annoyed with this task so he asks you for help. Write a program to find the smallest possible size of the broom and integer _p_. Input The first line of input contains integer _n_ (2u2009≤u2009_n_u2009≤u20093·105)xa0— the size of the broom. Next _n_u2009-u20091 lines describe the broom: _i_-th of them contains integers _u__i_, _v__i_ and letter _x__i_xa0— describing the edge from _u__i_ to _v__i_ marked with letter _x__i_. Vertices are numbered from 1 to _n_. All _x__i_ are lowercase latin letters. Vertex 1 is the root of the broom. Edges describe correct broom which is made from Peterson's language. Output The first line of output should contain the minimum possible size of the broom after its simplification. The second line of output should contain integer _p_ to choose. If there are several suitable _p_ values, print the smallest one. Examples Input 5 1 2 c 2 3 a 3 4 t 2 5 t Input 16 1 2 o 2 3 f 1 4 p 4 5 i 5 6 e 6 7 c 7 8 e 4 9 r 9 10 e 10 11 t 11 12 t 12 13 y 10 14 f 14 15 i 15 16 x Note Broom from the second sample test can be built using language "piece", "of", "pie", "pretty", "prefix". Its simplification with _p_u2009=u20092 obtains the language of words "pece", "o", "pe", "petty", "pefix". This language gives us the broom with minimum possible size.
2,500
false
false
false
false
false
false
true
false
false
false
6,654
1715D
The Narrator has an integer array $$$a$$$ of length $$$n$$$, but he will only tell you the size $$$n$$$ and $$$q$$$ statements, each of them being three integers $$$i, j, x$$$, which means that $$$a_i mid a_j = x$$$, where $$$$$$ denotes the . In the next $$$q$$$ lines you are given with three integers $$$i$$$, $$$j$$$, and $$$x$$$ ($$$1 le i, j le n$$$, $$$0 le x < 2^{30}$$$)xa0— the statements. It is guaranteed that all $$$q$$$ statements hold for at least one array. Output On a single line print $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < 2^{30}$$$)xa0— array $$$a$$$. Examples Input 4 3 1 2 3 1 3 2 4 1 2 Note In the first sample, these are all the arrays satisfying the statements: $$$[0, 3, 2, 2]$$$, $$$[2, 1, 0, 0]$$$, $$$[2, 1, 0, 2]$$$, $$$[2, 1, 2, 0]$$$, $$$[2, 1, 2, 2]$$$, $$$[2, 3, 0, 0]$$$, $$$[2, 3, 0, 2]$$$, $$$[2, 3, 2, 0]$$$, $$$[2, 3, 2, 2]$$$.
1,900
false
true
false
false
false
false
false
false
false
true
1,990
38A
Problem - 38A - Codeforces =============== xa0 - Codeforces Beta Round 38 (ACM-ICPC Rules)]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials - Codeforces Beta Round #38 (ACM-ICPC Rules)") ") ") ") ") ") ") ") . The second line contains _n_u2009-u20091 integers _d__i_ (1u2009≤u2009_d__i_u2009≤u2009100). The third input line contains two integers _a_ and _b_ (1u2009≤u2009_a_u2009<u2009_b_u2009≤u2009_n_). The numbers on the lines are space-separated. Output Print the single number which is the number of years that Vasya needs to rise from rank _a_ to rank _b_. Examples Input 3 5 6 1 2 Output 5 Input 3 5 6 1 3 Output 11
800
false
false
true
false
false
false
false
false
false
false
9,808
873E
Alexey recently held a programming contest for students from Berland. _n_ students participated in a contest, _i_-th of them solved _a__i_ problems. Now he wants to award some contestants. Alexey can award the students with diplomas of three different degrees. Each student either will receive one diploma of some degree, or won't receive any diplomas at all. Let _cnt__x_ be the number of students that are awarded with diplomas of degree _x_ (1u2009≤u2009_x_u2009≤u20093). The following conditions must hold: For each _x_ (1u2009≤u2009_x_u2009≤u20093) _cnt__x_u2009>u20090; For any two degrees _x_ and _y_ _cnt__x_u2009≤u20092·_cnt__y_. Of course, there are a lot of ways to distribute the diplomas. Let _b__i_ be the degree of diploma _i_-th student will receive (or u2009-u20091 if _i_-th student won't receive any diplomas). Also for any _x_ such that 1u2009≤u2009_x_u2009≤u20093 let _c__x_ be the maximum number of problems solved by a student that receives a diploma of degree _x_, and _d__x_ be the minimum number of problems solved by a student that receives a diploma of degree _x_. Alexey wants to distribute the diplomas in such a way that: 1. If student _i_ solved more problems than student _j_, then he has to be awarded not worse than student _j_ (it's impossible that student _j_ receives a diploma and _i_ doesn't receive any, and also it's impossible that both of them receive a diploma, but _b__j_u2009<u2009_b__i_); 2. _d_1u2009-u2009_c_2 is maximum possible; 3. Among all ways that maximize the previous expression, _d_2u2009-u2009_c_3 is maximum possible; 4. Among all ways that correspond to the two previous conditions, _d_3u2009-u2009_c_u2009-u20091 is maximum possible, where _c_u2009-u20091 is the maximum number of problems solved by a student that doesn't receive any diploma (or 0 if each student is awarded with some diploma). Help Alexey to find a way to award the contestants! Input The first line contains one integer number _n_ (3u2009≤u2009_n_u2009≤u20093000). The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20095000). Output Output _n_ numbers. _i_-th number must be equal to the degree of diploma _i_-th contestant will receive (or u2009-u20091 if he doesn't receive any diploma). If there are multiple optimal solutions, print any of them. It is guaranteed that the answer always exists.
2,300
false
false
false
true
true
false
true
false
false
false
6,250
400A
There always is something to choose from! And now, instead of "Noughts and Crosses", Inna choose a very unusual upgrade of this game. The rules of the game are given below: There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: "X" or "O". Then the player chooses two positive integers _a_ and _b_ (_a_·_b_u2009=u200912), after that he makes a table of size _a_u2009×u2009_b_ from the cards he put on the table as follows: the first _b_ cards form the first row of the table, the second _b_ cards form the second row of the table and so on, the last _b_ cards form the last (number _a_) row of the table. The player wins if some column of the table contain characters "X" on all cards. Otherwise, the player loses. Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers _a_ and _b_ to choose. Help her win the game: print to her all the possible ways of numbers _a_,u2009_b_ that she can choose and win. Input The first line of the input contains integer _t_ (1u2009≤u2009_t_u2009≤u2009100). This value shows the number of sets of test data in the input. Next follows the description of each of the _t_ tests on a separate line. The description of each test is a string consisting of 12 characters, each character is either "X", or "O". The _i_-th character of the string shows the character that is written on the _i_-th card from the start. Output For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair _a_,u2009_b_. Next, print on this line the pairs in the format _a_x_b_. Print the pairs in the order of increasing first parameter (_a_). Separate the pairs in the line by whitespaces. Examples Input 4 OXXXOXOOXOOX OXOXOXOXOXOX XXXXXXXXXXXX OOOOOOOOOOOO Output 3 1x12 2x6 4x3 4 1x12 2x6 3x4 6x2 6 1x12 2x6 3x4 4x3 6x2 12x1 0
1,000
false
false
true
false
false
false
false
false
false
false
8,227
839A
Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies. At first, Arya and Bran have 0 Candies. There are _n_ days, at the _i_-th day, Arya finds _a__i_ candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candies. If she don't give him the candies at the same day, they are saved for her and she can give them to him later. Your task is to find the minimum number of days Arya needs to give Bran _k_ candies before the end of the _n_-th day. Formally, you need to output the minimum day index to the end of which _k_ candies will be given out (the days are indexed from 1 to _n_). Print -1 if she can't give him _k_ candies during _n_ given days. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u200910000). The second line contains _n_ integers _a_1,u2009_a_2,u2009_a_3,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100). Output If it is impossible for Arya to give Bran _k_ candies within _n_ days, print -1. Otherwise print a single integerxa0— the minimum number of days Arya needs to give Bran _k_ candies before the end of the _n_-th day. Note In the first sample, Arya can give Bran 3 candies in 2 days. In the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day. In the third sample, Arya can't give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1 day.
900
false
false
true
false
false
false
false
false
false
false
6,408
1677C
Tokitsukaze has two colorful tapes. There are $$$n$$$ distinct colors, numbered $$$1$$$ through $$$n$$$, and each color appears exactly once on each of the two tapes. Denote the color of the $$$i$$$-th position of the first tape as $$$ca_i$$$, and the color of the $$$i$$$-th position of the second tape as $$$cb_i$$$. Now Tokitsukaze wants to select each color an integer value from $$$1$$$ to $$$n$$$, distinct for all the colors. After that she will put down the color values in each colored position on the tapes. Denote the number of the $$$i$$$-th position of the first tape as $$$numa_i$$$, and the number of the $$$i$$$-th position of the second tape as $$$numb_i$$$. For example, for the above picture, assuming that the color red has value $$$x$$$ ($$$1 leq x leq n$$$), it appears at the $$$1$$$-st position of the first tape and the $$$3$$$-rd position of the second tape, so $$$numa_1=numb_3=x$$$. Note that each color $$$i$$$ from $$$1$$$ to $$$n$$$ should have a distinct value, and the same color which appears in both tapes has the same value. After labeling each color, the beauty of the two tapes is calculated as $$$$$$sum_{i=1}^{n}numa_i-numb_i.$$$$$$ Please help Tokitsukaze to find the highest possible beauty. Input The first contains a single positive integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$1leq n leq 10^5$$$)xa0— the number of colors. The second line contains $$$n$$$ integers $$$ca_1, ca_2, ldots, ca_n$$$ ($$$1 leq ca_i leq n$$$)xa0— the color of each position of the first tape. It is guaranteed that $$$ca$$$ is a permutation. The third line contains $$$n$$$ integers $$$cb_1, cb_2, ldots, cb_n$$$ ($$$1 leq cb_i leq n$$$)xa0— the color of each position of the second tape. It is guaranteed that $$$cb$$$ is a permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^{5}$$$. Output For each test case, print a single integerxa0— the highest possible beauty. Example Input 3 6 1 5 4 3 2 6 5 3 1 4 6 2 6 3 5 4 6 2 1 3 6 4 5 2 1 1 1 1 Note An optimal solution for the first test case is shown in the following figure: The beauty is $$$left4-3 ight+left3-5 ight+left2-4 ight+left5-2 ight+left1-6 ight+left6-1 ight=18$$$. An optimal solution for the second test case is shown in the following figure: The beauty is $$$left2-2 ight+left1-6 ight+left3-3 ight+left6-1 ight+left4-4 ight+left5-5 ight=10$$$.
1,900
false
true
false
false
false
true
false
false
false
true
2,204
154C
You have been offered a job in a company developing a large social network. Your first task is connected with searching profiles that most probably belong to the same user. The social network contains _n_ registered profiles, numbered from 1 to _n_. Some pairs there are friends (the "friendship" relationship is mutual, that is, if _i_ is friends with _j_, then _j_ is also friends with _i_). Let's say that profiles _i_ and _j_ (_i_u2009≠u2009_j_) are doubles, if for any profile _k_ (_k_u2009≠u2009_i_, _k_u2009≠u2009_j_) one of the two statements is true: either _k_ is friends with _i_ and _j_, or _k_ isn't friends with either of them. Also, _i_ and _j_ can be friends or not be friends. Your task is to count the number of different unordered pairs (_i_,u2009_j_), such that the profiles _i_ and _j_ are doubles. Note that the pairs are unordered, that is, pairs (_a_,u2009_b_) and (_b_,u2009_a_) are considered identical. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009106, 0u2009≤u2009_m_u2009≤u2009106), — the number of profiles and the number of pairs of friends, correspondingly. Next _m_ lines contains descriptions of pairs of friends in the format "_v_ _u_", where _v_ and _u_ (1u2009≤u2009_v_,u2009_u_u2009≤u2009_n_,u2009_v_u2009≠u2009_u_) are numbers of profiles that are friends with each other. It is guaranteed that each unordered pair of friends occurs no more than once and no profile is friends with itself. Output Print the single integer — the number of unordered pairs of profiles that are doubles. Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the %I64d specificator. Note In the first and second sample any two profiles are doubles. In the third sample the doubles are pairs of profiles (1,u20093) and (2,u20094).
2,300
false
false
false
false
false
false
false
false
true
true
9,257
887D
There are two main kinds of events in the life of top-model: fashion shows and photo shoots. Participating in any of these events affects the rating of appropriate top-model. After each photo shoot model's rating increases by _a_ and after each fashion show decreases by _b_ (designers do too many experiments nowadays). Moreover, sometimes top-models participates in talk shows. After participating in talk show model becomes more popular and increasing of her rating after photo shoots become _c_ and decreasing of her rating after fashion show becomes _d_. Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will never become negative. Help her to find a suitable moment for participating in the talk show. Let's assume that model's career begins in moment 0. At that moment Izabella's rating was equal to _start_. If talk show happens in moment _t_ if will affect all events in model's life in interval of time [_t_.._t_u2009+u2009_len_) (including _t_ and not including _t_u2009+u2009_len_), where _len_ is duration of influence. Izabella wants to participate in a talk show, but she wants to do it in such a way that her rating will not become become negative before talk show or during period of influence of talk show. Help her to find a suitable moment for participating in the talk show. Input In first line there are 7 positive integers _n_, _a_, _b_, _c_, _d_, _start_, _len_ (1u2009≤u2009_n_u2009≤u20093·105, 0u2009≤u2009_start_u2009≤u2009109, 1u2009≤u2009_a_,u2009_b_,u2009_c_,u2009_d_,u2009_len_u2009≤u2009109), where _n_ is a number of fashion shows and photo shoots, _a_, _b_, _c_ and _d_ are rating changes described above, _start_ is an initial rating of model and _len_ is a duration of influence of talk show. In next _n_ lines descriptions of events are given. Each of those lines contains two integers _t__i_ and _q__i_ (1u2009≤u2009_t__i_u2009≤u2009109, 0u2009≤u2009_q_u2009≤u20091)xa0— moment, in which event happens and type of this event. Type 0 corresponds to the fashion show and type 1xa0— to photo shoot. Events are given in order of increasing _t__i_, all _t__i_ are different. Output Print one non-negative integer _t_xa0— the moment of time in which talk show should happen to make Izabella's rating non-negative before talk show and during period of influence of talk show. If there are multiple answers print smallest of them. If there are no such moments, print u2009-u20091.
2,400
false
false
false
false
true
false
false
false
false
false
6,199
1958E
Suppose you have a permutation $$$p$$$ of $$$n$$$ integers — an array where each element is an integer from $$$1$$$ to $$$n$$$, and every integer from $$$1$$$ to $$$n$$$ appears exactly once. In one operation, you remove every element of this permutation which is less than at least one of its neighbors. For example, when you apply the operation to $$$[3, 1, 2, 5, 4]$$$, you get $$$[3, 5]$$$. If you apply an operation again, you get $$$[5]$$$. It's easy to see that after applying a finite number of operations, you get an array consisting of a single integer $$$n$$$. You are given two integers $$$n$$$ and $$$k$$$. Find a permutation of $$$n$$$ integers such that it becomes an array consisting of a single element $$$n$$$ after exactly $$$k$$$ operations (and not earlier). Input The first line contains one integer $$$t$$$ ($$$1 le t le 2000$$$) — the number of test cases. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 100$$$; $$$1 le k le n - 1$$$). Output For each test case, print the answer as follows: if a permutation of size $$$n$$$ which becomes an array consisting of a single element $$$n$$$ after exactly $$$k$$$ operations does not exist, print $$$-1$$$; otherwise, print $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ — the requested permutation. If there are multiple such permutations, print any of them. Example Output 3 1 2 5 4 -1 1 2 2 1 3
1,900
false
false
false
false
false
true
false
false
false
false
522
903F
You are given a matrix _f_ with 4 rows and _n_ columns. Each element of the matrix is either an asterisk (*) or a dot (.). You may perform the following operation arbitrary number of times: choose a square submatrix of _f_ with size _k_u2009×u2009_k_ (where 1u2009≤u2009_k_u2009≤u20094) and replace each element of the chosen submatrix with a dot. Choosing a submatrix of size _k_u2009×u2009_k_ costs _a__k_ coins. What is the minimum number of coins you have to pay to replace all asterisks with dots? Input The first line contains one integer _n_ (4u2009≤u2009_n_u2009≤u20091000) — the number of columns in _f_. The second line contains 4 integers _a_1, _a_2, _a_3, _a_4 (1u2009≤u2009_a__i_u2009≤u20091000) — the cost to replace the square submatrix of size 1u2009×u20091, 2u2009×u20092, 3u2009×u20093 or 4u2009×u20094, respectively. Then four lines follow, each containing _n_ characters and denoting a row of matrix _f_. Each character is either a dot or an asterisk. Output Print one integer — the minimum number of coins to replace all asterisks with dots. Examples Input 4 1 10 8 20 ***. ***. ***. ... Input 7 2 1 8 2 .***... .***.. .***... ....*.. Input 4 10 10 1 10 ***. *.. *.. .** Note In the first example you can spend 8 coins to replace the submatrix 3u2009×u20093 in the top-left corner, and 1 coin to replace the 1u2009×u20091 submatrix in the bottom-right corner. In the second example the best option is to replace the 4u2009×u20094 submatrix containing columns 2u2009–u20095, and the 2u2009×u20092 submatrix consisting of rows 2u2009–u20093 and columns 6u2009–u20097. In the third example you can select submatrix 3u2009×u20093 in the top-left corner and then submatrix 3u2009×u20093 consisting of rows 2u2009–u20094 and columns 2u2009–u20094.
2,200
false
false
false
true
false
false
false
false
false
false
6,129
1901C
You are given an integer array $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$). In one operation, you can choose an integer $$$x$$$ ($$$0 le x le 10^{18}$$$) and replace $$$a_i$$$ with $$$lfloor frac{a_i + x}{2} floor$$$ ($$$lfloor y floor$$$ denotes rounding $$$y$$$ down to the nearest integer) for all $$$i$$$ from $$$1$$$ to $$$n$$$. Pay attention to the fact that all elements of the array are affected on each operation. Print the smallest number of operations required to make all elements of the array equal. If the number of operations is less than or equal to $$$n$$$, then print the chosen $$$x$$$ for each operation. If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print the smallest number of operations required to make all elements of the array equal. If the number of operations is less than or equal to $$$n$$$, then print the chosen $$$x$$$ for each operation in the next line. If there are multiple answers, print any of them. Example Input 4 1 10 2 4 6 6 2 1 2 1 2 1 2 0 32 Note In the first testcase, all elements are already equal, so $$$0$$$ operations are required. It doesn't matter if you print an empty line afterwards or not. In the second testcase, you can't make less than $$$2$$$ operations. There are multiple answers, let's consider the answer sequence $$$[2, 5]$$$. After applying an operation with $$$x = 2$$$, the array becomes $$$[lfloor frac{4 + 2}{2} floor, lfloor frac{6 + 2}{2} floor] = [3, 4]$$$. After applying an operation with $$$x = 5$$$ after that, the array becomes $$$[lfloor frac{3 + 5}{2} floor, lfloor frac{4 + 5}{2} floor] = [4, 4]$$$. Both elements are the same, so we are done. In the last testcase, you can't make less than $$$6$$$ operations. Since $$$6$$$ is greater than $$$n$$$, you don't have to print them. One possible answer sequence is $$$[0, 0, 0, 0, 0, 0]$$$. We are just dividing the second element by $$$2$$$ every time and not changing the first element.
1,400
true
true
false
false
false
true
false
false
false
false
907
1396D
Shrimpy Duc is a fat and greedy boy who is always hungry. After a while of searching for food to satisfy his never-ending hunger, Shrimpy Duc finds M&M candies lying unguarded on a $$$L imes L$$$ grid. There are $$$n$$$ M&M candies on the grid, the $$$i$$$-th M&M is currently located at $$$(x_i + 0.5, y_i + 0.5),$$$ and has color $$$c_i$$$ out of a total of $$$k$$$ colors (the size of M&Ms are insignificant). Shrimpy Duc wants to steal a rectangle of M&Ms, specifically, he wants to select a rectangle with integer coordinates within the grid and steal all candies within the rectangle. Shrimpy Duc doesn't need to steal every single candy, however, he would like to steal at least one candy for each color. In other words, he wants to select a rectangle whose sides are parallel to the coordinate axes and whose left-bottom vertex $$$(X_1, Y_1)$$$ and right-top vertex $$$(X_2, Y_2)$$$ are points with integer coordinates satisfying $$$0 le X_1 < X_2 le L$$$ and $$$0 le Y_1 < Y_2 le L$$$, so that for every color $$$1 le c le k$$$ there is at least one M&M with color $$$c$$$ that lies within that rectangle. How many such rectangles are there? This number may be large, so you only need to find it modulo $$$10^9 + 7$$$. Input The first line contains three positive integers $$$n, k, L$$$ $$$(1 leq k leq n leq 2 cdot 10^3, 1 leq L leq 10^9 )$$$ — the number of M&Ms, the number of colors and the length of the grid respectively. The following $$$n$$$ points describe the M&Ms. Each line contains three integers $$$x_i, y_i, c_i$$$ $$$(0 leq x_i, y_i < L, 1 le c_i le k)$$$ — the coordinates and color of the $$$i$$$-th M&M respectively. Different M&Ms have different coordinates ($$$x_i e x_j$$$ or $$$y_i e y_j$$$ for every $$$i e j$$$), and for every $$$1 le c le k$$$ there is at least one M&M with color $$$c$$$.
3,300
false
false
false
false
true
false
false
false
true
false
3,690
1131D
Mr. Apple, a gourmet, works as editor-in-chief of a gastronomic periodical. He travels around the world, tasting new delights of famous chefs from the most fashionable restaurants. Mr. Apple has his own signature method of review xa0— in each restaurant Mr. Apple orders two sets of dishes on two different days. All the dishes are different, because Mr. Apple doesn't like to eat the same food. For each pair of dishes from different days he remembers exactly which was better, or that they were of the same quality. After this the gourmet evaluates each dish with a positive integer. Once, during a revision of a restaurant of Celtic medieval cuisine named «Poisson», that serves chestnut soup with fir, warm soda bread, spicy lemon pie and other folk food, Mr. Apple was very pleasantly surprised the gourmet with its variety of menu, and hence ordered too much. Now he's confused about evaluating dishes. The gourmet tasted a set of $$$n$$$ dishes on the first day and a set of $$$m$$$ dishes on the second day. He made a table $$$a$$$ of size $$$n imes m$$$, in which he described his impressions. If, according to the expert, dish $$$i$$$ from the first set was better than dish $$$j$$$ from the second set, then $$$a_{ij}$$$ is equal to ">", in the opposite case $$$a_{ij}$$$ is equal to "<". Dishes also may be equally good, in this case $$$a_{ij}$$$ is "=". Now Mr. Apple wants you to help him to evaluate every dish. Since Mr. Apple is very strict, he will evaluate the dishes so that the maximal number used is as small as possible. But Mr. Apple also is very fair, so he never evaluates the dishes so that it goes against his feelings. In other words, if $$$a_{ij}$$$ is "<", then the number assigned to dish $$$i$$$ from the first set should be less than the number of dish $$$j$$$ from the second set, if $$$a_{ij}$$$ is ">", then it should be greater, and finally if $$$a_{ij}$$$ is "=", then the numbers should be the same. Help Mr. Apple to evaluate each dish from both sets so that it is consistent with his feelings, or determine that this is impossible. Input The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 1000$$$)xa0— the number of dishes in both days. Each of the next $$$n$$$ lines contains a string of $$$m$$$ symbols. The $$$j$$$-th symbol on $$$i$$$-th line is $$$a_{ij}$$$. All strings consist only of "<", ">" and "=". Output The first line of output should contain "Yes", if it's possible to do a correct evaluation for all the dishes, or "No" otherwise. If case an answer exist, on the second line print $$$n$$$ integersxa0— evaluations of dishes from the first set, and on the third line print $$$m$$$ integersxa0— evaluations of dishes from the second set. Note In the first sample, all dishes of the first day are better than dishes of the second day. So, the highest score will be $$$2$$$, for all dishes of the first day. In the third sample, the table is contradictoryxa0— there is no possible evaluation of the dishes that satisfies it.
2,000
false
true
false
true
false
false
false
false
false
true
5,065
16E
Problem - 16E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks dp probabilities *1900 No tag edit access → Contest materials ") — the amount of fish in the lake. Then there follow _n_ lines with _n_ real numbers each — matrix _a_. _a__ij_ (0u2009≤u2009_a__ij_u2009≤u20091) — the probability that fish with index _i_ eats up fish with index _j_. It's guaranteed that the main diagonal contains zeros only, and for other elements the following is true: _a__ij_u2009=u20091u2009-u2009_a__ji_. All real numbers are given with not more than 6 characters after the decimal point. Output Output _n_ space-separated real numbers accurate to not less than 6 decimal places. Number with index _i_ should be equal to the probability that fish with index _i_ will survive to be the last in the lake. Examples Input 2 0 0.5 0.5 0 Output 0.500000 0.500000 Input 5 0 1 1 1 1 0 0 0.5 0.5 0.5 0 0.5 0 0.5 0.5 0 0.5 0.5 0 0.5 0 0.5 0.5 0.5 0 Output 1.000000 0.000000 0.000000 0.000000 0.000000
1,900
false
false
false
true
false
false
false
false
false
false
9,911
1666F
Little Fiona has a collection of $$$n$$$ blocks of various sizes $$$a_1, a_2, ldots, a_n$$$, where $$$n$$$ is even. Some of the blocks can be equal in size. She would like to put all these blocks one onto another to form a fancy stack. Let $$$b_1, b_2, ldots, b_n$$$ be the sizes of blocks in the stack from top to bottom. Since Fiona is using all her blocks, $$$b_1, b_2, ldots, b_n$$$ must be a permutation of $$$a_1, a_2, ldots, a_n$$$. Fiona thinks the stack is fancy if both of the following conditions are satisfied: The second block is strictly bigger than the first one, and then each block is alternately strictly smaller or strictly bigger than the previous one. Formally, $$$b_1 < b_2 > b_3 < b_4 > ldots > b_{n-1} < b_n$$$. The sizes of the blocks on even positions are strictly increasing. Formally, $$$b_2 < b_4 < b_6 < ldots < b_n$$$ (remember that $$$n$$$ is even). Two stacks are considered different if their corresponding sequences $$$b_1, b_2, ldots, b_n$$$ differ in at least one position. Fiona wants to know how many different fancy stacks she can build with all of her blocks. Since large numbers scare Fiona, find this number modulo $$$998,244,353$$$. Input Each input contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 2500$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$xa0— the number of blocks at Fiona's disposal ($$$2 le n le 5000$$$; $$$n$$$ is even). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0— the sizes of the blocks in non-decreasing order ($$$1 le a_1 le a_2 le dotsb le a_n le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each test case, print the number of ways to build a fancy stack, modulo $$$998,244,353$$$.
2,200
false
false
true
true
false
false
false
false
false
false
2,280
168B
Let's dive into one of the most interesting areas of magic — writing spells. Learning this exciting but challenging science is very troublesome, so now you will not learn the magic words, but only get to know the basic rules of writing spells. Each spell consists of several lines. The line, whose first non-space character is character "#" is an amplifying line and it is responsible for spell power. The remaining lines are common, and determine the effect of the spell. You came across the text of some spell. Spell was too long, so you cannot understand its meaning. So you want to make it as short as possible without changing the meaning. The only way to shorten a spell that you know is the removal of some spaces and line breaks. We know that when it comes to texts of spells, the spaces carry meaning only in the amplifying lines, so we should remove all spaces in other lines. Newlines also do not matter, unless any of the two separated lines is amplifying. Thus, if two consecutive lines are not amplifying, they need to be joined into one (i.e. we should concatenate the second line to the first one). Removing spaces in amplifying lines and concatenating the amplifying lines to anything is forbidden. Note that empty lines must be processed just like all the others: they must be joined to the adjacent non-amplifying lines, or preserved in the output, if they are surrounded with amplifying lines on both sides (i.e. the line above it, if there is one, is amplifying, and the line below it, if there is one, is amplifying too). For now those are the only instructions for removing unnecessary characters that you have to follow (oh yes, a newline is a character, too). The input contains the text of the spell, which should be reduced. Remove the extra characters and print the result to the output. Input The input contains multiple lines. All characters in the lines have codes from 32 to 127 (inclusive). Please note that the lines may begin with or end with one or more spaces. The size of the input does not exceed 1048576 (u2009=u2009220) bytes. Newlines are included in this size. In the Windows operating system used on the testing computer, a newline is a sequence of characters with codes #13#10. It is guaranteed that after each line of input there is a newline. In particular, the input ends with a newline. Note that the newline is the end of the line, and not the beginning of the next one. It is guaranteed that the input contains at least one character other than a newline. It is recommended to organize the input-output line by line, in this case the newlines will be processed correctly by the language means. Output Print the text of the spell where all extra characters are deleted. Please note that each output line should be followed by a newline. Please be careful: your answers will be validated by comparing them to the jury's answer byte-by-byte. So, all spaces and newlines matter. Note In the first sample the amplifying lines are lines 1 and 7. So, lines 2 to 6 are concatenated to each other, all spaces are deleted from them. In the second sample the amplifying lines are lines 1 and 3. So, no lines are concatenated to each other.
1,700
false
false
true
false
false
false
false
false
false
false
9,189
891E
Problem - 891E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics math matrices *3000 No tag edit access → Contest materials — the number of elements and parameter _k_ that is specified in the statement. The second line contains _n_ space separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109). Output Output a single integerxa0— the value . Examples Input 2 1 5 5 Output 5 Input 1 10 80 Output 10 Input 2 2 0 0 Output 500000003 Input 9 4 0 11 12 9 20 7 8 18 2 Output 169316356
3,000
true
false
false
false
false
false
false
false
false
false
6,184
181A
The Berland capital is shaken with three bold crimes committed by the Pihsters, a notorious criminal gang. The Berland capital's map is represented by an _n_u2009×u2009_m_ rectangular table. Each cell of the table on the map represents some districts of the capital. The capital's main detective Polycarpus took a map and marked there the districts where the first three robberies had been committed as asterisks. Deduction tells Polycarpus that the fourth robbery will be committed in such district, that all four robbed districts will form the vertices of some rectangle, parallel to the sides of the map. Polycarpus is good at deduction but he's hopeless at math. So he asked you to find the district where the fourth robbery will be committed. Input The first line contains two space-separated integers _n_ and _m_ (2u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the number of rows and columns in the table, correspondingly. Each of the next _n_ lines contains _m_ characters — the description of the capital's map. Each character can either be a "." (dot), or an "*" (asterisk). A character equals "*" if the corresponding district has been robbed. Otherwise, it equals ".". It is guaranteed that the map has exactly three characters "*" and we can always find the fourth district that meets the problem requirements. Output Print two integers — the number of the row and the number of the column of the city district that is the fourth one to be robbed. The rows are numbered starting from one from top to bottom and the columns are numbered starting from one from left to right.
800
false
false
true
false
false
false
true
false
false
false
9,116
1979C
You have been offered to play a game. In this game, there are $$$n$$$ possible outcomes, and for each of them, you must bet a certain integer amount of coins. In the event that the $$$i$$$-th outcome turns out to be winning, you will receive back the amount of coins equal to your bet on that outcome, multiplied by $$$k_i$$$. Note that exactly one of the $$$n$$$ outcomes will be winning. Your task is to determine how to distribute the coins in such a way that you will come out ahead in the event of any winning outcome. More formally, the total amount of coins you bet on all outcomes must be strictly less than the number of coins received back for each possible winning outcome. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the number of outcomes. The second line of each test case contains $$$n$$$ integers $$$k_1,k_2,ldots,k_n$$$ ($$$2 le k_i le 20$$$)xa0— the multiplier for the amount of coins if the $$$i$$$-th outcome turns out to be winning. 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 $$$-1$$$ if there is no way to distribute the coins as required. Otherwise, output $$$n$$$ integers $$$x_1, x_2,ldots, x_n$$$ ($$$1 le x_i le 10^{9}$$$)xa0— your bets on the outcomes. It can be shown that if a solution exists, there is always a solution that satisfies these constraints. If there are multiple suitable solutions, output any of them. Example Input 6 3 3 2 7 2 3 3 5 5 5 5 5 5 6 7 9 3 17 9 13 3 6 3 2 5 9 4 6 8 3 Output 27 41 12 1 1 -1 1989 1547 4641 819 1547 1071 -1 8 18 12 9 24 Note In the first test case, the coins can be distributed as follows: $$$27$$$ coins on the first outcome, $$$41$$$ coins on the second outcome, $$$12$$$ coins on the third outcome. Then the total amount of coins bet on all outcomes is $$$27 + 41 + 12 = 80$$$ coins. If the first outcome turns out to be winning, you will receive back $$$3 cdot 27 = 81$$$ coins, if the second outcome turns out to be winning, you will receive back $$$2 cdot 41 = 82$$$ coins, if the third outcome turns out to be winning, you will receive back $$$7 cdot 12 = 84$$$ coins. All these values are strictly greater than $$$80$$$. In the second test case, one way is to bet one coin on each of the outcomes.
1,200
false
false
false
false
false
true
false
true
false
false
414
1693F
You are given a binary string $$$S$$$ of length $$$n$$$ indexed from $$$1$$$ to $$$n$$$. You can perform the following operation any number of times (possibly zero): Choose two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$). Let $$$cnt_0$$$ be the number of times 0 occurs in $$$S[l ldots r]$$$ and $$$cnt_1$$$ be the number of times 1 occurs in $$$S[l ldots r]$$$. You can pay $$$cnt_0 - cnt_1 + 1$$$ coins and sort the $$$S[l ldots r]$$$. (by $$$S[l ldots r]$$$ we mean the substring of $$$S$$$ starting at position $$$l$$$ and ending at position $$$r$$$) For example if $$$S = $$$ 11001, we can perform the operation on $$$S[2 ldots 4]$$$, paying $$$2 - 1 + 1 = 2$$$ coins, and obtain $$$S = $$$ 10011 as a new string. Find the minimum total number of coins required to sort $$$S$$$ in increasing order. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the size of $$$S$$$. The second line of each test case contains a binary string $$$S$$$ of $$$n$$$ characters $$$S_1S_2 ldots S_n$$$. ($$$S_i = $$$ 0 or $$$S_i = $$$ 1 for each $$$1 le i le n$$$) It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output the minimum total number of coins required to sort $$$S$$$ in increasing order. Example Input 7 1 1 2 10 3 101 4 1000 5 11010 6 110000 20 01000010001010011000 Note In the first test case, $$$S$$$ is already sorted. In the second test case, it's enough to apply the operation with $$$l = 1, r = 2$$$. In the third test case, it's enough to apply the operation with $$$l = 1, r = 2$$$.
3,400
false
true
false
false
false
false
false
true
false
false
2,117
1019A
As you know, majority of students and teachers of Summer Informatics School live in Berland for the most part of the year. Since corruption there is quite widespread, the following story is not uncommon. Elections are coming. You know the number of voters and the number of partiesxa0— $$$n$$$ and $$$m$$$ respectively. For each voter you know the party he is going to vote for. However, he can easily change his vote given a certain amount of money. In particular, if you give $$$i$$$-th voter $$$c_i$$$ bytecoins you can ask him to vote for any other party you choose. The United Party of Berland has decided to perform a statistical studyxa0— you need to calculate the minimum number of bytecoins the Party needs to spend to ensure its victory. In order for a party to win the elections, it needs to receive strictly more votes than any other party. Input The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 3000$$$)xa0— the number of voters and the number of parties respectively. Each of the following $$$n$$$ lines contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$1 le p_i le m$$$, $$$1 le c_i le 10^9$$$)xa0— the index of this voter's preferred party and the number of bytecoins needed for him to reconsider his decision. The United Party of Berland has the index $$$1$$$. Output Print a single numberxa0— the minimum number of bytecoins needed for The United Party of Berland to win the elections. Examples Input 5 5 2 100 3 200 4 300 5 400 5 900 Input 5 5 2 100 3 200 4 300 5 800 5 900 Note In the first sample, The United Party wins the elections even without buying extra votes. In the second sample, The United Party can buy the votes of the first and the fourth voter. This way The Party gets two votes, while parties $$$3$$$, $$$4$$$ and $$$5$$$ get one vote and party number $$$2$$$ gets no votes. In the third sample, The United Party can buy the votes of the first three voters and win, getting three votes against two votes of the fifth party.
1,700
false
true
false
false
false
false
true
false
false
false
5,597
843E
You are given a directed graph, consisting of _n_ vertices and _m_ edges. The vertices _s_ and _t_ are marked as source and sink correspondingly. Additionally, there are no edges ending at _s_ and there are no edges beginning in _t_. The graph was constructed in a following way: initially each edge had capacity _c__i_u2009>u20090. A maximum flow with source at _s_ and sink at _t_ was constructed in this flow network. Let's denote _f__i_ as the value of flow passing through edge with index _i_. Next, all capacities _c__i_ and flow value _f__i_ were erased. Instead, indicators _g__i_ were written on edges — if flow value passing through edge _i_ was positive, i.e. 1 if _f__i_u2009>u20090 and 0 otherwise. Using the graph and values _g__i_, find out what is the minimum possible number of edges in the initial flow network that could be saturated (the passing flow is equal to capacity, i.e. _f__i_u2009=u2009_c__i_). Also construct the corresponding flow network with maximum flow in it. A flow in directed graph is described by flow values _f__i_ on each of the edges so that the following conditions are satisfied: for each vertex, except source and sink, total incoming flow and total outcoming flow are equal, for each edge 0u2009≤u2009_f__i_u2009≤u2009_c__i_ A flow is maximum if the difference between the sum of flow values on edges from the source, and the sum of flow values on edges to the source (there are no such in this problem), is maximum possible. Input The first line of input data contains four positive integers _n_,u2009_m_,u2009_s_,u2009_t_ (2u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u20091000, 1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_, _s_u2009≠u2009_t_) — the number of vertices, the number of edges, index of source vertex and index of sink vertex correspondingly. Each of next _m_ lines of input data contain non-negative integers _u__i_, _v__i_, _g__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, ) — the beginning of edge _i_, the end of edge _i_ and indicator, which equals to 1 if flow value passing through edge _i_ was positive and 0 if not. It's guaranteed that no edge connects vertex with itself. Also it's guaranteed that there are no more than one edge between each ordered pair of vertices and that there exists at least one network flow that satisfies all the constrains from input data. Output In the first line print single non-negative integer _k_ — minimum number of edges, which should be saturated in maximum flow. In each of next _m_ lines print two integers _f__i_,u2009_c__i_ (1u2009≤u2009_c__i_u2009≤u2009109, 0u2009≤u2009_f__i_u2009≤u2009_c__i_) — the flow value passing through edge _i_ and capacity of edge _i_. This data should form a correct maximum flow in flow network. Also there must be exactly _k_ edges with statement _f__i_u2009=u2009_c__i_ satisfied. Also statement _f__i_u2009>u20090 must be true if and only if _g__i_u2009=u20091. If there are several possible answers, print any of them. Example Input 5 6 1 5 1 2 1 2 3 1 3 5 1 1 4 1 4 3 0 4 5 1 Output 2 3 3 3 8 3 4 4 4 0 5 4 9 Note The illustration for second sample case. The saturated edges are marked dark, while edges with _g__i_u2009=u20090 are marked with dotted line. The integer on edge is the index of this edge in input list.
3,000
false
false
false
false
false
false
false
false
false
true
6,387
42D
Problem - 42D - Codeforces =============== xa0 assigned to it and all prices are distinct. But the most striking thing about this town is that each city sightseeing tour has the same total price! That is, if we choose any city sightseeing tour — a cycle which visits every attraction exactly once — the sum of the costs of the tour roads is independent of the tour. Volodya is curious if you can find such price system with all road prices not greater than 1000. Input Input contains just one natural number (3u2009≤u2009_N_u2009≤u200920) — the number of town attractions. Output Output should contain _N_ rows containing _N_ positive integer numbers each — the adjacency matrix of the prices graph (thus, _j_-th number in _i_-th row should be equal to the price of the road between the _j_-th and the _i_-th attraction). Diagonal numbers should be equal to zero. All numbers should not be greater than 1000. All prices should be positive and pairwise distinct. If there are several solutions, output any of them. Examples Input 3 Output 0 3 4 3 0 5 4 5 0
2,300
true
false
false
false
false
true
false
false
false
false
9,776
1819D
Schoolboy Misha got tired of doing sports programming, so he decided to quit everything and go to the magical forest to sell magic apples. His friend Danya came to the magical forest to visit Misha. What was his surprise when he found out that Misha found a lot of friends there, the same former sports programmers. And all of them, like Misha, have their own shop where they sell magic apples. To support his friends, who have changed their lives so drastically, he decided to buy up their entire assortment. The buying process works as follows: in total there are $$$n$$$ stalls, numbered with integers from $$$1$$$ to $$$n$$$, and $$$m$$$ kinds of magic apples, numbered with integers from $$$1$$$ to $$$m$$$. Each shop sells some number of kinds of apples. Danya visits all the shops in order of increasing number, starting with the first one. Upon entering the shop he buys one magic apple of each kind sold in that shop and puts them in his backpack. However, magical apples wouldn't be magical if they were all right. The point is that when two apples of the same type end up together in the backpack, all of the apples in it magically disappear. Importantly, the disappearance happens after Danya has put the apples in the backpack and left the shop. Upon returning home, Danya realized that somewhere in the forest he had managed to lose his backpack. Unfortunately, for some shops Danya had forgotten what assortment of apples there was. Remembering only for some shops, what kinds of magical apples were sold in them, he wants to know what is the maximum number of apples he could have in his backpack after all his purchases at best. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^5$$$)xa0—the number of test cases. The description of test cases follows. The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 2 cdot 10^5$$$)xa0—the number of stalls and kinds of apples. Each of the following $$$n$$$ lines describes the assortment of the next stall in the format described below. Each line starts with an integer $$$k_i$$$ ($$$0 le k_i le 2 cdot 10^5$$$). This is followed by $$$k_i$$$ of different integers $$$a_{ij}$$$ ($$$1 le a_{ij} le m$$$)xa0—the kinds of apples sold in the $$$i$$$-th stall. If $$$k_i = 0$$$, then Danya does not remember what assortment was in that shop, and the set of apple kinds can be anything (including empty). It is guaranteed that the sum of all $$$k_i$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ and the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ Output For each test case, output a single integerxa0— the maximum number of apples that could be in Dani's backpack after visiting all the shops at best. Example Input 4 3 4 2 1 2 2 4 1 2 1 2 4 4 2 1 2 2 3 4 0 1 1 2 5 0 0 5 3 0 3 1 2 3 2 3 1 0 1 3 Note In the first test case, Danya remembers all the shops, so the process will be deterministic. He will take two apples at the first shop and two more at the second, but after he puts them in his backpack, they will disappear. So at the end there will only be $$$2$$$ apples left, which he will take at the third shop. In the second test case, if the third shop is empty, then after visiting the fourth shop all the apples will disappear. In any other case the apples will disappear after the third shop, and in the fourth shop Dan can take one apple, so the answer is $$$1$$$. In the third test case, the first shop may sell all kinds of apples, and the second shop may sell nothing. Then all $$$5$$$ apples will be left at the end.
2,800
false
false
false
true
true
false
true
false
false
false
1,362
132E
Another feature of Shakespeare language is that the variables are named after characters of plays by Shakespeare, and all operations on them (value assignment, output etc.) look like a dialog with other characters. New values of variables are defined in a rather lengthy way, so a programmer should try to minimize their usage. You have to print the given sequence of _n_ integers. To do this, you have _m_ variables and two types of operations on them: variable=integer print(variable) Any of the _m_ variables can be used as variable. Variables are denoted by lowercase letters between "a" and "z", inclusive. Any integer number can be used as integer. Let's say that the penalty for using first type of operations equals to the number of set bits in the number integer. There is no penalty on using second type of operations. Find and output the program which minimizes the penalty for printing the given sequence of numbers. Input The first line of input contains integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009250, 1u2009≤u2009_m_u2009≤u200926). The second line contains the sequence to be printed. Each element of the sequence is an integer between 1 and 109, inclusive. The sequence has to be printed in the given order (from left to right). Output Output the number of lines in the optimal program and the optimal penalty. Next, output the program itself, one command per line. If there are several programs with minimal penalty, output any of them (you have only to minimize the penalty). Examples Output 11 4 b=1 print(b) a=2 print(a) print(a) b=4 print(b) print(a) b=1 print(b) print(a) Output 9 4 c=1 print(c) b=2 print(b) a=3 print(a) print(c) print(b) print(a)
2,700
false
false
false
false
false
false
false
false
false
true
9,343
475B
Imagine a city with _n_ horizontal streets crossing _m_ vertical streets, forming an (_n_u2009-u20091)u2009×u2009(_m_u2009-u20091) grid. In order to increase the traffic flow, mayor of the city has decided to make each street one way. This means in each horizontal street, the traffic moves only from west to east or only from east to west. Also, traffic moves only from north to south or only from south to north in each vertical street. It is possible to enter a horizontal street from a vertical street, or vice versa, at their intersection. The mayor has received some street direction patterns. Your task is to check whether it is possible to reach any junction from any other junction in the proposed street direction pattern. Input The first line of input contains two integers _n_ and _m_, (2u2009≤u2009_n_,u2009_m_u2009≤u200920), denoting the number of horizontal streets and the number of vertical streets. The second line contains a string of length _n_, made of characters '<' and '>', denoting direction of each horizontal street. If the _i_-th character is equal to '<', the street is directed from east to west otherwise, the street is directed from west to east. Streets are listed in order from north to south. The third line contains a string of length _m_, made of characters '^' and 'v', denoting direction of each vertical street. If the _i_-th character is equal to '^', the street is directed from south to north, otherwise the street is directed from north to south. Streets are listed in order from west to east. Output If the given pattern meets the mayor's criteria, print a single line containing "YES", otherwise print a single line containing "NO". Note The figure above shows street directions in the second sample test case.
1,400
false
false
true
false
false
false
true
false
false
true
7,933
1218B
Bob Bubblestrong just got a new job as security guard. Bob is now responsible for safety of a collection of warehouses, each containing the most valuable Bubble Cup assets - the high-quality bubbles. His task is to detect thieves inside the warehouses and call the police. Looking from the sky, each warehouse has a shape of a convex polygon. Walls of no two warehouses intersect, and of course, none of the warehouses is built inside of another warehouse. Little did the Bubble Cup bosses know how lazy Bob is and that he enjoys watching soap operas (he heard they are full of bubbles) from the coziness of his office. Instead of going from one warehouse to another to check if warehouses are secured, the plan Bob has is to monitor all the warehouses from the comfort of his office using the special X-ray goggles. The goggles have an infinite range, so a thief in any of the warehouses could easily be spotted. However, the goggles promptly broke and the X-rays are now strong only enough to let Bob see through a single wall. Now, Bob would really appreciate if you could help him find out what is the total area inside of the warehouses monitored by the broken goggles, so that he could know how much area of the warehouses he needs to monitor in person. Input The first line contains one integer $$$N$$$ ($$$1$$$ $$$leq$$$ $$$N$$$ $$$leq$$$ $$$10^4$$$) – the number of warehouses. The next $$$N$$$ lines describe the warehouses. The first number of the line is integer $$$c_i$$$ ($$$3$$$ $$$leq$$$ $$$c_i$$$ $$$leq$$$ $$$10^4$$$) – the number corners in the $$$i^{th}$$$ warehouse, followed by $$$c_i$$$ pairs of integers. The $$$j^{th}$$$ pair is $$$(x_j, y_j)$$$ – the coordinates of the $$$j^{th}$$$ corner ($$$x_j$$$, $$$y_j$$$ $$$leq$$$ $$$3 10^4$$$). The corners are listed in the clockwise order. The total number of corners in all the warehouses is at most $$$5 10^4$$$. Bob's office is positioned at the point with coordinates $$$(0, 0)$$$. The office is not contained within any of the warehouses. Output Print a single line containing a single decimal number accurate to at least four decimal places – the total area of the warehouses Bob can monitor using the broken X-ray goggles. Example Input 5 4 1 1 1 3 3 3 3 1 4 4 3 6 2 6 0 4 0 6 -5 3 -4 4 -3 4 -2 3 -3 2 -4 2 3 0 -1 1 -3 -1 -3 4 1 -4 1 -6 -1 -6 -1 -4 Note Areas monitored by the X-ray goggles are colored green and areas not monitored by the goggles are colored red. The warehouses $$$ABCD$$$, $$$IJK$$$ and $$$LMNOPQ$$$ are completely monitored using the googles. The warehouse $$$EFGH$$$ is partially monitored using the goggles: part $$$EFW$$$ is not monitored because to monitor each point inside it, the X-rays must go through two walls of warehouse $$$ABCD$$$. The warehouse $$$RUTS$$$ is not monitored from the Bob's office, because there are two walls of the warehouse $$$IJK$$$ between Bob's office and each point in $$$RUTS$$$. The total area monitored by the goggles is $$$P$$$ = $$$P_{ABCD}$$$ + $$$P_{FGHW}$$$ + $$$P_{IJK}$$$ + $$$P_{LMNOPQ}$$$ = $$$4$$$ + $$$3.333333333333$$$ + $$$2$$$ + $$$4$$$ = $$$13.333333333333$$$.
3,000
false
false
false
false
true
false
false
false
false
false
4,583
1725F
Pak Chanek is traveling to Manado. It turns out that OSN (Indonesian National Scientific Olympiad) 2019 is being held. The contestants of OSN 2019 are currently lining up in a field to be photographed. The field is shaped like a grid of size $$$N imes 10^{100}$$$ with $$$N$$$ rows and $$$10^{100}$$$ columns. The rows are numbered from $$$1$$$ to $$$N$$$ from north to south, the columns are numbered from $$$1$$$ to $$$10^{100}$$$ from west to east. The tile in row $$$r$$$ and column $$$c$$$ is denoted as $$$(r,c)$$$. There are $$$N$$$ provinces that participate in OSN 2019. Initially, each contestant who represents province $$$i$$$ stands in tile $$$(i, p)$$$ for each $$$p$$$ satisfying $$$L_i leq p leq R_i$$$. So, we can see that there are $$$R_i-L_i+1$$$ contestants who represent province $$$i$$$. Pak Chanek has a variable $$$Z$$$ that is initially equal to $$$0$$$. In one operation, Pak Chanek can choose a row $$$i$$$ and a positive integer $$$k$$$. Then, Pak Chanek will do one of the two following possibilities: Move all contestants in row $$$i$$$ exactly $$$k$$$ tiles to the west. In other words, a contestant who is in $$$(i, p)$$$ is moved to $$$(i, p-k)$$$. Move all contestants in row $$$i$$$ exactly $$$k$$$ tiles to the east. In other words, a contestant who is in $$$(i, p)$$$ is moved to $$$(i, p+k)$$$. After each operation, the value of $$$Z$$$ will change into $$$Z ext{ OR } k$$$, with $$$ ext{OR}$$$ being the — the number of rows in the grid and also the number of provinces that participate in OSN 2019. The $$$i$$$-th of the next $$$N$$$ lines contains two integers $$$L_i$$$ and $$$R_i$$$ ($$$1 leq L_i leq R_i leq 10^9$$$) describing the positions of the contestants who represent province $$$i$$$. The next line contains a single integer $$$Q$$$ ($$$1 leq Q leq 10^5$$$) — the number of questions. The $$$j$$$-th of the next $$$Q$$$ lines contains a single integer $$$W_j$$$ ($$$1 leq W_j leq 10^9$$$) — the required final value of $$$Z$$$ for the $$$j$$$-th question. Note For the $$$1$$$-st question, Pak Chanek can do the following operations to get $$$M=2$$$: Move all contestants in row $$$2$$$ to the east by $$$4$$$ tiles. $$$Z$$$ changes into $$$0 ext{ OR } 4 = 4$$$. Move all contestants in row $$$1$$$ to the east by $$$12$$$ tiles. $$$Z$$$ changes into $$$4 ext{ OR } 12 = 12$$$. Now, columns $$$14$$$ and $$$15$$$ each contains exactly $$$2$$$ contestants. For the $$$2$$$-nd question, Pak Chanek can do the following operations to get $$$M=3$$$: Move all contestants in row $$$3$$$ to the east by $$$4$$$ tiles. $$$Z$$$ changes into $$$0 ext{ OR } 4 = 4$$$. Move all contestants in row $$$3$$$ to the west by $$$1$$$ tiles. $$$Z$$$ changes into $$$4 ext{ OR } 1 = 5$$$. Move all contestants in row $$$1$$$ to the east by $$$5$$$ tiles. $$$Z$$$ changes into $$$5 ext{ OR } 5 = 5$$$. Move all contestants in row $$$1$$$ to the east by $$$5$$$ tiles. $$$Z$$$ changes into $$$5 ext{ OR } 5 = 5$$$. Now, column $$$11$$$ contains exactly $$$3$$$ contestants. The following is an illustration of the example operations for the $$$2$$$-nd question.
2,100
false
false
false
false
true
false
false
false
true
false
1,941
78C
Two beavers, Timur and Marsel, play the following game. There are _n_ logs, each of exactly _m_ meters in length. The beavers move in turns. For each move a beaver chooses a log and gnaws it into some number (more than one) of equal parts, the length of each one is expressed by an integer and is no less than _k_ meters. Each resulting part is also a log which can be gnawed in future by any beaver. The beaver that can't make a move loses. Thus, the other beaver wins. Timur makes the first move. The players play in the optimal way. Determine the winner. Input The first line contains three integers _n_, _m_, _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u2009109). Output Print "Timur", if Timur wins, or "Marsel", if Marsel wins. You should print everything without the quotes. Note In the first sample the beavers only have one log, of 15 meters in length. Timur moves first. The only move he can do is to split the log into 3 parts each 5 meters in length. Then Marsel moves but he can't split any of the resulting logs, as _k_u2009=u20094. Thus, the winner is Timur. In the second example the beavers have 4 logs 9 meters in length. Timur can't split any of them, so that the resulting parts possessed the length of not less than 5 meters, that's why he loses instantly.
2,000
false
false
false
true
false
false
false
false
false
false
9,573
1864D
There is a matrix of size $$$n imes n$$$ which consists of 0s and 1s. The rows are numbered from $$$1$$$ to $$$n$$$ from top to bottom, the columns are numbered from $$$1$$$ to $$$n$$$ from left to right. The cell at the intersection of the $$$x$$$-th row and the $$$y$$$-th column is denoted as $$$(x, y)$$$. AquaMoon wants to turn all elements of the matrix to 0s. In one step she can perform the following operation: Select an arbitrary cell, let it be $$$(i, j)$$$, then invert the element in $$$(i, j)$$$ and also invert all elements in cells $$$(x, y)$$$ for $$$x > i$$$ and $$$x - i ge lefty - j ight$$$. To invert a value means to change it to the opposite: 0 changes to 1, 1 changes to 0. Help AquaMoon determine the minimum number of steps she need to perform to turn all elements of the matrix to 0s. We can show that an answer always exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 3000$$$). The $$$i$$$-th of the following $$$n$$$ lines contains a binary string only of characters 0 and 1, of length $$$n$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$9,000,000$$$. Output For each test case, print the minimum number of steps. Example Input 3 5 00100 01110 11111 11111 11111 3 100 110 110 6 010101 111101 011110 000000 111010 001110 Note In the first test case, we can use the following scheme: 1. perform the operation on the cell $$$(1, 3)$$$. Clearly, the elements of the initial matrix are not all 0, so at least one operation is required. Thus, $$$1$$$ is the answer. In the second test case, we use the following scheme: 1. perform the operation on the cell $$$(3, 3)$$$; 2. perform the operation on the cell $$$(1, 1)$$$. It can be shown that there is no way to convert all elements to 0s in $$$0$$$ or $$$1$$$ steps, so the answer is exactly $$$2$$$.
1,700
true
true
false
true
true
true
true
false
false
false
1,094
18E
According to a new ISO standard, a flag of every country should have, strangely enough, a chequered field _n_u2009×u2009_m_, each square should be wholly painted one of 26 colours. The following restrictions are set: In each row at most two different colours can be used. No two adjacent squares can be painted the same colour. Pay attention, please, that in one column more than two different colours can be used. Berland's government took a decision to introduce changes into their country's flag in accordance with the new standard, at the same time they want these changes to be minimal. By the given description of Berland's flag you should find out the minimum amount of squares that need to be painted different colour to make the flag meet the new ISO standard. You are as well to build one of the possible variants of the new Berland's flag. Input The first input line contains 2 integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009500) — amount of rows and columns in Berland's flag respectively. Then there follows the flag's description: each of the following _n_ lines contains _m_ characters. Each character is a letter from a to z, and it stands for the colour of the corresponding square. Output In the first line output the minimum amount of squares that need to be repainted to make the flag meet the new ISO standard. The following _n_ lines should contain one of the possible variants of the new flag. Don't forget that the variant of the flag, proposed by you, should be derived from the old flag with the minimum amount of repainted squares. If the answer isn't unique, output any.
2,000
false
false
false
true
false
false
false
false
false
false
9,901
1809E
There are two water tanks, the first one fits $$$a$$$ liters of water, the second one fits $$$b$$$ liters of water. The first tank has $$$c$$$ ($$$0 le c le a$$$) liters of water initially, the second tank has $$$d$$$ ($$$0 le d le b$$$) liters of water initially. You want to perform $$$n$$$ operations on them. The $$$i$$$-th operation is specified by a single non-zero integer $$$v_i$$$. If $$$v_i > 0$$$, then you try to pour $$$v_i$$$ liters of water from the first tank into the second one. If $$$v_i < 0$$$, you try to pour $$$-v_i$$$ liters of water from the second tank to the first one. When you try to pour $$$x$$$ liters of water from the tank that has $$$y$$$ liters currently available to the tank that can fit $$$z$$$ more liters of water, the operation only moves $$$min(x, y, z)$$$ liters of water. For all pairs of the initial volumes of water $$$(c, d)$$$ such that $$$0 le c le a$$$ and $$$0 le d le b$$$, calculate the volume of water in the first tank after all operations are performed. Input The first line contains three integers $$$n, a$$$ and $$$b$$$ ($$$1 le n le 10^4$$$; $$$1 le a, b le 1000$$$)xa0— the number of operations and the capacities of the tanks, respectively. The second line contains $$$n$$$ integers $$$v_1, v_2, dots, v_n$$$ ($$$-1000 le v_i le 1000$$$; $$$v_i eq 0$$$)xa0— the volume of water you try to pour in each operation. Output For all pairs of the initial volumes of water $$$(c, d)$$$ such that $$$0 le c le a$$$ and $$$0 le d le b$$$, calculate the volume of water in the first tank after all operations are performed. Print $$$a + 1$$$ lines, each line should contain $$$b + 1$$$ integers. The $$$j$$$-th value in the $$$i$$$-th line should be equal to the answer for $$$c = i - 1$$$ and $$$d = j - 1$$$. Examples Output 0 0 0 0 0 0 0 0 0 1 0 0 1 1 2 0 1 1 2 3 1 1 2 3 4 Output 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 2 1 2 2 2 2 3 2 3 3 3 3 4 3 4 4 4 4 5 4 5 5 5 5 6 5 6 6 6 6 7 6 7 7 7 7 8 7 7 7 7 8 9 Note Consider $$$c = 3$$$ and $$$d = 2$$$ from the first example: The first operation tries to move $$$2$$$ liters of water from the second tank to the first one, the second tank has $$$2$$$ liters available, the first tank can fit $$$1$$$ more liter. Thus, $$$min(2, 2, 1) = 1$$$ liter is moved, the first tank now contains $$$4$$$ liters, the second tank now contains $$$1$$$ liter. The second operation tries to move $$$1$$$ liter of water from the first tank to the second one. $$$min(1, 4, 3) = 1$$$ liter is moved, the first tank now contains $$$3$$$ liters, the second tank now contains $$$2$$$ liter. The third operation tries to move $$$2$$$ liter of water from the first tank to the second one. $$$min(2, 3, 2) = 2$$$ liters are moved, the first tank now contains $$$1$$$ liter, the second tank now contains $$$4$$$ liters. There's $$$1$$$ liter of water in the first tank at the end. Thus, the third value in the fourth row is $$$1$$$.
2,400
true
false
true
true
false
false
false
true
false
false
1,416
768F
Tarly has two different type of items, food boxes and wine barrels. There are _f_ food boxes and _w_ wine barrels. Tarly stores them in various stacks and each stack can consist of either food boxes or wine barrels but not both. The stacks are placed in a line such that no two stacks of food boxes are together and no two stacks of wine barrels are together. The height of a stack is defined as the number of items in the stack. Two stacks are considered different if either their heights are different or one of them contains food and other contains wine. Jon Snow doesn't like an arrangement if any stack of wine barrels has height less than or equal to _h_. What is the probability that Jon Snow will like the arrangement if all arrangement are equiprobably? Two arrangement of stacks are considered different if exists such _i_, that _i_-th stack of one arrangement is different from the _i_-th stack of the other arrangement. Input The first line of input contains three integers _f_, _w_, _h_ (0u2009≤u2009_f_,u2009_w_,u2009_h_u2009≤u2009105) — number of food boxes, number of wine barrels and _h_ is as described above. It is guaranteed that he has at least one food box or at least one wine barrel. Output Output the probability that Jon Snow will like the arrangement. The probability is of the form , then you need to output a single integer _p_·_q_u2009-u20091 _mod_ (109u2009+u20097). Note In the first example _f_u2009u2009=u2009u20091, _w_u2009=u20091 and _h_u2009=u20091, there are only two possible arrangement of stacks and Jon Snow doesn't like any of them. In the second example _f_u2009=u20091, _w_u2009=u20092 and _h_u2009=u20091, there are three arrangements. Jon Snow likes the (1) and (3) arrangement. So the probabilty is .
2,300
true
false
false
false
false
false
true
false
false
false
6,708
1968F
Let us call an array $$$x_1,dots,x_m$$$ interesting if it is possible to divide the array into $$$k>1$$$ parts so that xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le q le 2 cdot 10^5$$$)xa0— the number of elements in the array and the number of queries respectively. The next line contains $$$n$$$ integers $$$a_1,dots,a_n$$$ ($$$0 le a_i < 2^{30}$$$)xa0— elements of the array. Each of the next $$$q$$$ lines contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$) describing the query. It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. It is guaranteed that the sum of $$$q$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. Output For each query, output "YES" if the subarray is interesting and "NO" otherwise. You can output "Yes" and "No" in any case (for example, the strings "yES", "yes", and "Yes" will be recognized as correct answers). Example Input 4 5 5 1 1 2 3 0 1 5 2 4 3 5 1 3 3 4 5 5 1 2 3 4 5 1 5 2 4 3 5 1 3 2 3 7 4 12 9 10 9 10 11 9 1 5 1 7 2 6 2 7 11 4 0 0 1 0 0 1 0 1 1 0 1 1 2 2 5 6 9 7 11 Output YES YES NO NO NO YES NO NO YES NO NO NO NO NO YES NO YES YES Note Explanation for the first test case: The first query is described in the statement. In the second query, we should divide $$$[1,2,3]$$$. A possible division is $$$[1,2],[3]$$$, since $$$1oplus 2=3$$$. It can be shown that for queries $$$3,4,5$$$, the subarrays are not interesting.
1,800
false
false
false
false
true
false
false
true
false
false
495
1534F2
This is the hard version of the problem. The difference between the versions is the constraints on $$$a_i$$$. You can make hacks only if all versions of the problem are solved. Little Dormi has recently received a puzzle from his friend and needs your help to solve it. The puzzle consists of an upright board with $$$n$$$ rows and $$$m$$$ columns of cells, some empty and some filled with blocks of sand, and $$$m$$$ non-negative integers $$$a_1,a_2,ldots,a_m$$$ ($$$0 leq a_i leq n$$$). In this version of the problem, $$$a_i$$$ will always be not greater than the number of blocks of sand in column $$$i$$$. When a cell filled with a block of sand is disturbed, the block of sand will fall from its cell to the sand counter at the bottom of the column (each column has a sand counter). While a block of sand is falling, other blocks of sand that are adjacent at any point to the falling block of sand will also be disturbed and start to fall. Specifically, a block of sand disturbed at a cell $$$(i,j)$$$ will pass through all cells below and including the cell $$$(i,j)$$$ within the column, disturbing all adjacent cells along the way. Here, the cells adjacent to a cell $$$(i,j)$$$ are defined as $$$(i-1,j)$$$, $$$(i,j-1)$$$, $$$(i+1,j)$$$, and $$$(i,j+1)$$$ (if they are within the grid). Note that the newly falling blocks can disturb other blocks. In one operation you are able to disturb any piece of sand. The puzzle is solved when there are at least $$$a_i$$$ blocks of sand counted in the $$$i$$$-th sand counter for each column from $$$1$$$ to $$$m$$$. You are now tasked with finding the minimum amount of operations in order to solve the puzzle. Note that Little Dormi will never give you a puzzle that is impossible to solve. Input The first line consists of two space-separated positive integers $$$n$$$ and $$$m$$$ ($$$1 leq n cdot m leq 400,000$$$). Each of the next $$$n$$$ lines contains $$$m$$$ characters, describing each row of the board. If a character on a line is '.', the corresponding cell is empty. If it is '#', the cell contains a block of sand. The final line contains $$$m$$$ non-negative integers $$$a_1,a_2,ldots,a_m$$$ ($$$0 leq a_i leq n$$$) — the minimum amount of blocks of sand that needs to fall below the board in each column. In this version of the problem, $$$a_i$$$ will always be not greater than the number of blocks of sand in column $$$i$$$. Output Print one non-negative integer, the minimum amount of operations needed to solve the puzzle. Examples Input 5 7 #....#. .#.#... #....#. #....## #.#.... 4 1 1 1 0 3 1 Input 3 3 #.# #.. ##. 3 1 1 Input 7 5 .#..# #.... ..##. ..##. ..### ..#.. #.##. 0 0 2 4 2 Note For example $$$1$$$, by disturbing both blocks of sand on the first row from the top at the first and sixth columns from the left, and the block of sand on the second row from the top and the fourth column from the left, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than $$$3$$$ operations, and as such the answer is $$$3$$$. Here is the puzzle from the first example. For example $$$2$$$, by disturbing the cell on the top row and rightmost column, one can cause all of the blocks of sand in the board to fall into the counters at the bottom. Thus, the answer is $$$1$$$. Here is the puzzle from the second example. For example $$$3$$$, by disturbing the cell on the top row and rightmost column, it is possible to have all the required amounts of sand fall in each column. It can be proved that this is not possible with fewer than $$$1$$$ operation, and as such the answer is $$$1$$$. Here is the puzzle from the third example.
3,000
false
true
false
true
false
false
false
false
false
true
2,977
420A
Problem - 420A - 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 *1000 No tag edit access → Contest materials with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper. There are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not. Input The first line contains a non-empty name that needs to be checked. The name contains at most 105 large English letters. The name will be written with the next sans serif font: Output Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes). Examples Input AHA Output YES Input Z Output NO Input XO Output NO
1,000
false
false
true
false
false
false
false
false
false
false
8,150
1760C
There are $$$n$$$ participants in a competition, participant $$$i$$$ having a strength of $$$s_i$$$. Every participant wonders how much of an advantage they have over the other best participant. In other words, each participant $$$i$$$ wants to know the difference between $$$s_i$$$ and $$$s_j$$$, where $$$j$$$ is the strongest participant in the competition, not counting $$$i$$$ (a difference can be negative). So, they ask you for your help! For each $$$i$$$ ($$$1 leq i leq n$$$) output the difference between $$$s_i$$$ and the maximum strength of any participant other than participant $$$i$$$. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 2cdot10^5$$$)xa0— the length of the array. The following line contains $$$n$$$ space-separated positive integers $$$s_1$$$, $$$s_2$$$, ..., $$$s_n$$$ ($$$1 leq s_i leq 10^9$$$)xa0— the strengths of the participants. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output $$$n$$$ space-separated integers. For each $$$i$$$ ($$$1 leq i leq n$$$) output the difference between $$$s_i$$$ and the maximum strength of any other participant. Example Input 5 4 4 7 3 5 2 1 2 5 1 2 3 4 5 3 4 9 4 4 4 4 4 4 Output -3 2 -4 -2 -1 1 -4 -3 -2 -1 1 -5 5 -5 0 0 0 0 Note For the first test case: The first participant has a strength of $$$4$$$ and the largest strength of a participant different from the first one is $$$7$$$, so the answer for the first participant is $$$4 - 7 = -3$$$. The second participant has a strength of $$$7$$$ and the largest strength of a participant different from the second one is $$$5$$$, so the answer for the second participant is $$$7 - 5 = 2$$$. The third participant has a strength of $$$3$$$ and the largest strength of a participant different from the third one is $$$7$$$, so the answer for the third participant is $$$3 - 7 = -4$$$. The fourth participant has a strength of $$$5$$$ and the largest strength of a participant different from the fourth one is $$$7$$$, so the answer for the fourth participant is $$$5 - 7 = -2$$$.
800
false
false
true
false
true
false
false
false
true
false
1,753
76A
The kingdom of Olympia consists of _N_ cities and _M_ bidirectional roads. Each road connects exactly two cities and two cities can be connected with more than one road. Also it possible that some roads connect city with itself making a loop. All roads are constantly plundered with bandits. After a while bandits became bored of wasting time in road robberies, so they suggested the king of Olympia to pay off. According to the offer, bandits want to get a gift consisted of gold and silver coins. Offer also contains a list of restrictions: for each road it is known _g__i_ — the smallest amount of gold and _s__i_ — the smallest amount of silver coins that should be in the gift to stop robberies on the road. That is, if the gift contains _a_ gold and _b_ silver coins, then bandits will stop robberies on all the roads that _g__i_u2009≤u2009_a_ and _s__i_u2009≤u2009_b_. Unfortunately kingdom treasury doesn't contain neither gold nor silver coins, but there are Olympian tugriks in it. The cost of one gold coin in tugriks is _G_, and the cost of one silver coin in tugriks is _S_. King really wants to send bandits such gift that for any two cities there will exist a safe path between them. Your task is to find the minimal cost in Olympian tugriks of the required gift. Input The first line of the input contains two integers _N_ and _M_ (2u2009≤u2009_N_u2009≤u2009200, 1u2009≤u2009_M_u2009≤u200950u2009000) — the number of cities and the number of roads, respectively. The second line contains two integers _G_ and _S_ (1u2009≤u2009_G_,u2009_S_u2009≤u2009109) — the prices of gold and silver coins in tugriks. The following _M_ lines contain information about the offer. Each of the records in list is given as four integers _x__i_,u2009_y__i_,u2009_g__i_,u2009_s__i_, where _x__i_ and _y__i_ are the numbers of cities that the road connects and _g__i_, _s__i_ are minimal gold and silver coins requirements for the _i_-th road (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_N_, 1u2009≤u2009_g__i_,u2009_s__i_u2009≤u2009109). Cities are numbered from 1 to _N_. It is possible that there are more than one road between a pair of cities. It is possible that a road connects the city with itself. Output The output should contain the minimal cost of the gift in Olympian tugriks. If there is no gift that satisfies the given requirements output . Examples Input 3 3 2 1 1 2 10 15 1 2 4 20 1 3 5 1
2,200
false
false
false
false
false
false
false
false
true
true
9,586
1840G2
The only difference between easy and hard versions is the maximum number of queries. In this version, you are allowed to ask at most $$$1000$$$ queries. This is an interactive problem. You are playing a game. The circle is divided into $$$n$$$ sectors, sectors are numbered from $$$1$$$ to $$$n$$$ in some order. You are in the adjacent room and do not know either the number of sectors or their numbers. There is also an arrow that initially points to some sector. Initially, the host tells you the number of the sector to which the arrow points. After that, you can ask the host to move the arrow $$$k$$$ sectors counterclockwise or clockwise at most $$$1000$$$ times. And each time you are told the number of the sector to which the arrow points. Your task is to determine the integer $$$n$$$xa0— the number of sectors in at most $$$1000$$$ queries. It is guaranteed that $$$1 le n le 10^6$$$. Input The input consists of a single integer $$$x$$$ ($$$1 le x le n$$$)xa0— the number of the initial sector. Output After you determine the integer $$$n$$$xa0— the number of sectors, you should output "! n" ($$$1 le n le 10^6$$$). After that the program should immediately terminate. Note that, printing the answer does not count as a query. It is guaranteed that the integer $$$n$$$ and the numbers of the sectors are fixed initially and will not be changed by the jury program depending on the queries. Interaction After reading the description of the input, you may ask queries. Queries can be of two types: 1. "+ k" ($$$0 le k le 10^9$$$)xa0— ask to move the arrow $$$k$$$ sectors clockwise. 2. "- k" ($$$0 le k le 10^9$$$)xa0— ask to move the arrow $$$k$$$ sectors counterclockwise. After each query, you should read an integer $$$x$$$ ($$$1 le x le n$$$)xa0— the number of the current sector to which the arrow points. You are allowed to make at most $$$1000$$$ queries in total. If you make too many queries, you will get Wrong answer. After printing a query or the answer, do not forget to output a the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Example Input 1 5 6 7 2 10 9 8 4 3 1 Output + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ! 10 Note Hacks To hack, use the following test format. In the first line, output a single integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0— the number of sectors. In the second line, output $$$n$$$ different integers $$$1 le a_1, a_2, dots, a_n le n$$$xa0— the numbers of the sectors in clockwise order, the arrow initially points to the sector with the number $$$a_1$$$.
2,500
true
false
false
false
false
true
false
false
false
false
1,246
1593B
It is given a positive integer $$$n$$$. In $$$1$$$ move, one can select any single digit and remove it (i.e. one selects some position in the number and removes the digit located at this position). The operation cannot be performed if only one digit remains. If the resulting number contains leading zeroes, they are automatically removed. E.g. if one removes from the number $$$32925$$$ the $$$3$$$-rd digit, the resulting number will be $$$3225$$$. If one removes from the number $$$20099050$$$ the first digit, the resulting number will be $$$99050$$$ (the $$$2$$$ zeroes going next to the first digit are automatically removed). What is the minimum number of steps to get a number such that it is divisible by $$$25$$$ and positive? It is guaranteed that, for each $$$n$$$ occurring in the input, the answer exists. It is guaranteed that the number $$$n$$$ has no leading zeros. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing one integer $$$n$$$ ($$$25 le n le 10^{18}$$$). It is guaranteed that, for each $$$n$$$ occurring in the input, the answer exists. It is guaranteed that the number $$$n$$$ has no leading zeros. Output For each test case output on a separate line an integer $$$k$$$ ($$$k ge 0$$$) — the minimum number of steps to get a number such that it is divisible by $$$25$$$ and positive. Example Input 5 100 71345 3259 50555 2050047 Note In the first test case, it is already given a number divisible by $$$25$$$. In the second test case, we can remove the digits $$$1$$$, $$$3$$$, and $$$4$$$ to get the number $$$75$$$. In the third test case, it's enough to remove the last digit to get the number $$$325$$$. In the fourth test case, we can remove the three last digits to get the number $$$50$$$. In the fifth test case, it's enough to remove the digits $$$4$$$ and $$$7$$$.
900
true
true
false
true
false
false
false
false
false
false
2,685
1834A
Given an array $$$a$$$ of length $$$n$$$, which elements are equal to $$$-1$$$ and $$$1$$$. Let's call the array $$$a$$$ good if the following conditions are held at the same time: $$$a_1 + a_2 + ldots + a_n ge 0$$$; $$$a_1 cdot a_2 cdot ldots cdot a_n = 1$$$. In one operation, you can select an arbitrary element of the array $$$a_i$$$ and change its value to the opposite. In other words, if $$$a_i = -1$$$, you can assign the value to $$$a_i := 1$$$, and if $$$a_i = 1$$$, then assign the value to $$$a_i := -1$$$. Determine the minimum number of operations you need to perform to make the array $$$a$$$ good. It can be shown that this is always possible. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i = pm 1$$$) — the elements of the array $$$a$$$. Output For each test case, output a single integer — the minimum number of operations that need to be done to make the $$$a$$$ array good. Example Input 7 4 -1 -1 1 -1 5 -1 -1 -1 1 1 4 -1 1 -1 1 3 -1 -1 -1 5 1 1 1 1 1 1 -1 2 -1 -1 Note In the first test case, we can assign the value $$$a_1 := 1$$$. Then $$$a_1 + a_2 + a_3 + a_4 = 1 + (-1) + 1 + (-1) = 0 ge 0$$$ and $$$a_1 cdot a_2 cdot a_3 cdot a_4 = 1 cdot (-1) cdot 1 cdot (-1) = 1$$$. Thus, we performed $$$1$$$ operation. In the second test case, we can assign $$$a_1 := 1$$$. Then $$$a_1 + a_2 + a_3 + a_4 + a_5 = 1 + (-1) + (-1) + 1 + 1 = 1 ge 0$$$ and $$$a_1 cdot a_2 cdot a_3 cdot a_4 cdot a_5 = 1 cdot (-1) cdot (-1) cdot 1 cdot 1 = 1$$$. Thus, we performed $$$1$$$ operation. In the third test case, $$$a_1 + a_2 + a_3 + a_4 = (-1) + 1 + (-1) + 1 = 0 ge 0$$$ and $$$a_1 cdot a_2 cdot a_3 cdot a_4 = (-1) cdot 1 cdot (-1) cdot 1 = 1$$$. Thus, all conditions are already satisfied and no operations are needed. In the fourth test case, we can assign the values $$$a_1 := 1, a_2 := 1, a_3 := 1$$$. Then $$$a_1 + a_2 + a_3 = 1 + 1 + 1 = 3 ge 0$$$ and $$$a_1 cdot a_2 cdot a_3 = 1 cdot 1 cdot 1 = 1$$$. Thus, we performed $$$3$$$ operations.
800
true
true
false
false
false
false
false
false
false
false
1,284
701A
There are _n_ cards (_n_ is even) in the deck. Each card has a positive integer written on it. _n_u2009/u20092 people will play new card game. At the beginning of the game each player gets two cards, each card is given to exactly one player. Find the way to distribute cards such that the sum of values written of the cards will be equal for each player. It is guaranteed that it is always possible. Input The first line of the input contains integer _n_ (2u2009≤u2009_n_u2009≤u2009100)xa0— the number of cards in the deck. It is guaranteed that _n_ is even. The second line contains the sequence of _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100), where _a__i_ is equal to the number written on the _i_-th card. Output Print _n_u2009/u20092 pairs of integers, the _i_-th pair denote the cards that should be given to the _i_-th player. Each card should be given to exactly one player. Cards are numbered in the order they appear in the input. It is guaranteed that solution exists. If there are several correct answers, you are allowed to print any of them. Note In the first sample, cards are distributed in such a way that each player has the sum of numbers written on his cards equal to 8. In the second sample, all values _a__i_ are equal. Thus, any distribution is acceptable.
800
false
true
true
false
false
false
false
false
false
false
7,020
938E
Problem - 938E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics math *2300 No tag edit access → Contest materials — the size of array _a_. Second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009u2009_a__i_u2009≤u2009u2009109). Output Print the only integer, the sum of _f__a_ over all _n_! permutations of the array _a_ modulo 109u2009+u20097. Examples Input 2 1 3 Output 1 Input 3 1 1 2 Output 4 Note For the second example all the permutations are: _p_u2009=u2009
2,300
true
false
false
false
false
false
false
false
false
false
5,947
129B
Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one. To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student. Determine how many groups of students will be kicked out of the club. Input The first line contains two integers _n_ and _m_ — the initial number of students and laces (). The students are numbered from 1 to _n_, and the laces are numbered from 1 to _m_. Next _m_ lines each contain two integers _a_ and _b_ — the numbers of students tied by the _i_-th lace (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_,u2009_a_u2009≠u2009_b_). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself. Output Print the single number — the number of groups of students that will be kicked out from the club. Note In the first sample Anna and Maria won't kick out any group of students — in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone. In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then — two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club. In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.
1,200
false
false
true
false
false
false
true
false
false
true
9,364
360B
Problem - 360B - 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 *2000 No tag edit access → Contest materials , which can be calculated by the formula: The less value _c_(_a_) is, the more beautiful the array is.It’s time to change the world and Levko is going to change his array for the better. To be exact, Levko wants to change the values of at most _k_ array elements (it is allowed to replace the values by any integers). Of course, the changes should make the array as beautiful as possible. Help Levko and calculate what minimum number _c_(_a_) he can reach. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20092000). The second line contains space-separated integers _a_1,u2009_a_2,u2009... ,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109). Output A single number — the minimum value of _c_(_a_) Levko can get. Examples Input 5 2 4 7 4 7 4 Output 0 Input 3 1 -100 0 100 Output 100 Input 6 3 1 2 3 7 8 9 Output 1 Note In the first sample Levko can change the second and fourth elements and get array: 4, 4, 4, 4, 4. In the third sample he can get array: 1, 2, 3, 4, 5, 6.
2,000
false
false
false
true
false
false
false
true
false
false
8,393
744A
Hongcow is ruler of the world. As ruler of the world, he wants to make it easier for people to travel by road within their own countries. The world can be modeled as an undirected graph with _n_ nodes and _m_ edges. _k_ of the nodes are home to the governments of the _k_ countries that make up the world. There is at most one edge connecting any two nodes and no edge connects a node to itself. Furthermore, for any two nodes corresponding to governments, there is no path between those two nodes. Any graph that satisfies all of these conditions is stable. Hongcow wants to add as many edges as possible to the graph while keeping it stable. Determine the maximum number of edges Hongcow can add. Input The first line of input will contain three integers _n_, _m_ and _k_ (1u2009≤u2009_n_u2009≤u20091u2009000, 0u2009≤u2009_m_u2009≤u2009100u2009000, 1u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of vertices and edges in the graph, and the number of vertices that are homes of the government. The next line of input will contain _k_ integers _c_1,u2009_c_2,u2009...,u2009_c__k_ (1u2009≤u2009_c__i_u2009≤u2009_n_). These integers will be pairwise distinct and denote the nodes that are home to the governments in this world. The following _m_ lines of input will contain two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_). This denotes an undirected edge between nodes _u__i_ and _v__i_. It is guaranteed that the graph described by the input is stable. Output Output a single integer, the maximum number of edges Hongcow can add to the graph while keeping it stable. Note For the first sample test, the graph looks like this: Vertices 1 and 3 are special. The optimal solution is to connect vertex 4 to vertices 1 and 2. This adds a total of 2 edges. We cannot add any more edges, since vertices 1 and 3 cannot have any path between them. For the second sample test, the graph looks like this: We cannot add any more edges to this graph. Note that we are not allowed to add self-loops, and the graph must be simple.
1,500
false
false
false
false
false
false
false
false
false
true
6,824
1895G
You are given a string consisting of characters 0 and/or 1. You have to paint every character of this string into one of two colors, red or blue. If you paint the $$$i$$$-th character red, you get $$$r_i$$$ coins. If you paint it blue, you get $$$b_i$$$ coins. After coloring the string, you remove every blue character from it, and count the number of inversions in the resulting string (i.u2009e. the number of pairs of characters such that the left character in the pair is 1, and the right character in the pair is 0). For each inversion, you have to pay $$$1$$$ coin. What is the maximum number of coins you can earn? Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of four lines: the first line contains one integer $$$n$$$ ($$$1 le n le 4 cdot 10^5$$$) — the length of the string; the second line contains $$$s$$$ — a string of $$$n$$$ characters, where each character is either 0 or 1; the third line contains $$$n$$$ integers $$$r_1, r_2, dots, r_n$$$ ($$$1 le r_i le 10^{12}$$$); the fourth line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 10^{12}$$$). Additional constraint on the input: the sum of values of $$$n$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Output For each test case, print one integer — the maximum number of coins you can earn. Example Input 4 7 0100010 6 6 6 7 7 6 6 3 3 5 4 7 6 7 5 10111 9 8 5 7 5 4 4 7 8 4 10 0100000000 7 7 6 5 2 2 5 3 8 3 8 6 9 6 6 8 9 7 7 9 8 01010000 8 7 7 7 8 7 7 8 4 4 4 2 1 4 4 4 Note Explanations for the test cases for the example (blue characters are underlined, red ones are not): 1. $$$0100underline{0}1underline{0}$$$; 2. $$$10underline{11}1$$$; 3. $$$underline{0}1underline{00000000}$$$; 4. $$$0underline{1}010000$$$.
3,100
false
true
false
true
true
false
false
true
false
false
938
1499C
Let's say you are standing on the $$$XY$$$-plane at point $$$(0, 0)$$$ and you want to reach point $$$(n, n)$$$. You can move only in two directions: to the right, i.xa0e. horizontally and in the direction that increase your $$$x$$$ coordinate, or up, i.xa0e. vertically and in the direction that increase your $$$y$$$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $$$n - 1$$$ direction changes. As a result, your path will be a polygonal chain from $$$(0, 0)$$$ to $$$(n, n)$$$, consisting of at most $$$n$$$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ where $$$c_i$$$ is the cost of the $$$i$$$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i.xa0e. if the path consists of $$$k$$$ segments ($$$k le n$$$), then the cost of the path is equal to $$$sumlimits_{i=1}^{k}{c_i cdot length_i}$$$ (segments are numbered from $$$1$$$ to $$$k$$$ in the order they are in the path). Find the path of the minimum cost and print its cost. Input The first line contains the single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains the single integer $$$n$$$ ($$$2 le n le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^9$$$)xa0— the costs of each segment. It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$10^5$$$. Output For each test case, print the minimum possible cost of the path from $$$(0, 0)$$$ to $$$(n, n)$$$ consisting of at most $$$n$$$ alternating segments. Example Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Note In the first test case, to reach $$$(2, 2)$$$ you need to make at least one turn, so your path will consist of exactly $$$2$$$ segments: one horizontal of length $$$2$$$ and one vertical of length $$$2$$$. The cost of the path will be equal to $$$2 cdot c_1 + 2 cdot c_2 = 26 + 176 = 202$$$. In the second test case, one of the optimal paths consists of $$$3$$$ segments: the first segment of length $$$1$$$, the second segment of length $$$3$$$ and the third segment of length $$$2$$$. The cost of the path is $$$1 cdot 2 + 3 cdot 3 + 2 cdot 1 = 13$$$. In the third test case, one of the optimal paths consists of $$$4$$$ segments: the first segment of length $$$1$$$, the second onexa0— $$$1$$$, the third onexa0— $$$4$$$, the fourth onexa0— $$$4$$$. The cost of the path is $$$1 cdot 4 + 1 cdot 3 + 4 cdot 2 + 4 cdot 1 = 19$$$.
1,500
true
true
false
false
true
false
true
false
false
false
3,173
1003F
You are given a text consisting of $$$n$$$ space-separated words. There is exactly one space character between any pair of adjacent words. There are no spaces before the first word and no spaces after the last word. The length of text is the number of letters and spaces in it. $$$w_i$$$ is the $$$i$$$-th word of text. All words consist only of lowercase Latin letters. Let's denote a segment of words $$$w[i..j]$$$ as a sequence of words $$$w_i, w_{i + 1}, dots, w_j$$$. Two segments of words $$$w[i_1 .. j_1]$$$ and $$$w[i_2 .. j_2]$$$ are considered equal if $$$j_1 - i_1 = j_2 - i_2$$$, $$$j_1 ge i_1$$$, $$$j_2 ge i_2$$$, and for every $$$t in [0, j_1 - i_1]$$$ $$$w_{i_1 + t} = w_{i_2 + t}$$$. For example, for the text "to be or not to be" the segments $$$w[1..2]$$$ and $$$w[5..6]$$$ are equal, they correspond to the words "to be". An abbreviation is a replacement of some segments of words with their first uppercase letters. In order to perform an abbreviation, you have to choose at least two non-intersecting equal segments of words, and replace each chosen segment with the string consisting of first letters of the words in the segment (written in uppercase). For example, for the text "a ab a a b ab a a b c" you can replace segments of words $$$w[2..4]$$$ and $$$w[6..8]$$$ with an abbreviation "AAA" and obtain the text "a AAA b AAA b c", or you can replace segments of words $$$w[2..5]$$$ and $$$w[6..9]$$$ with an abbreviation "AAAB" and obtain the text "a AAAB AAAB c". What is the minimum length of the text after at most one abbreviation? Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 300$$$) — the number of words in the text. The next line contains $$$n$$$ space-separated words of the text $$$w_1, w_2, dots, w_n$$$. Each word consists only of lowercase Latin letters. It is guaranteed that the length of text does not exceed $$$10^5$$$. Note In the first example you can obtain the text "TB or not TB". In the second example you can obtain the text "a AAAB AAAB c". In the third example you can obtain the text "AB aa AB bb".
2,200
false
false
false
true
false
false
false
false
false
false
5,669
1037D
The which denotes the number of nodes in the tree. The following $$$n - 1$$$ lines describe the edges of the tree. Each of them contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$)xa0— the endpoints of the corresponding edge of the tree. It is guaranteed that the given graph is a tree. The last line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the sequence to check. Output Print "Yes" (quotes for clarity) if the sequence corresponds to some valid BFS traversal of the given tree and "No" (quotes for clarity) otherwise. You can print each letter in any case (upper or lower). Note Both sample tests have the same tree in them. In this tree, there are two valid BFS orderings: $$$1, 2, 3, 4$$$, $$$1, 3, 2, 4$$$. The ordering $$$1, 2, 4, 3$$$ doesn't correspond to any valid BFS order.
1,700
false
false
false
false
false
false
false
false
false
true
5,513
954C
There is a matrix _A_ of size _x_u2009×u2009_y_ filled with integers. For every , _A__i_,u2009_j_u2009=u2009_y_(_i_u2009-u20091)u2009+u2009_j_. Obviously, every integer from [1.._xy_] occurs exactly once in this matrix. You have traversed some path in this matrix. Your path can be described as a sequence of visited cells _a_1, _a_2, ..., _a__n_ denoting that you started in the cell containing the number _a_1, then moved to the cell with the number _a_2, and so on. From the cell located in _i_-th line and _j_-th column (we denote this cell as (_i_,u2009_j_)) you can move into one of the following cells: 1. (_i_u2009+u20091,u2009_j_) — only if _i_u2009<u2009_x_; 2. (_i_,u2009_j_u2009+u20091) — only if _j_u2009<u2009_y_; 3. (_i_u2009-u20091,u2009_j_) — only if _i_u2009>u20091; 4. (_i_,u2009_j_u2009-u20091) — only if _j_u2009>u20091. Notice that making a move requires you to go to an adjacent cell. It is not allowed to stay in the same cell. You don't know _x_ and _y_ exactly, but you have to find any possible values for these numbers such that you could start in the cell containing the integer _a_1, then move to the cell containing _a_2 (in one step), then move to the cell containing _a_3 (also in one step) and so on. Can you choose _x_ and _y_ so that they don't contradict with your sequence of moves? Input The first line contains one integer number _n_ (1u2009≤u2009_n_u2009≤u2009200000) — the number of cells you visited on your path (if some cell is visited twice, then it's listed twice). The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the integers in the cells on your path. Output If all possible values of _x_ and _y_ such that 1u2009≤u2009_x_,u2009_y_u2009≤u2009109 contradict with the information about your path, print NO. Otherwise, print YES in the first line, and in the second line print the values _x_ and _y_ such that your path was possible with such number of lines and columns in the matrix. Remember that they must be positive integers not exceeding 109. Note The matrix and the path on it in the first test looks like this: Also there exist multiple correct answers for both the first and the third examples.
1,700
false
false
true
false
false
false
false
false
false
false
5,907
1950E
You are given a string $$$s$$$ of length $$$n$$$ consisting of lowercase Latin characters. Find the length of the shortest string $$$k$$$ such that several (possibly one) copies of $$$k$$$ can be concatenated together to form a string with the same length as $$$s$$$ and, at most, one different character. More formally, find the length of the shortest string $$$k$$$ such that $$$c = underbrace{k + cdots + k}_{x m ext{times}}$$$ for some positive integer $$$x$$$, strings $$$s$$$ and $$$c$$$ has the same length and $$$c_i eq s_i$$$ for at most one $$$i$$$ (i.e. there exist $$$0$$$ or $$$1$$$ such positions). Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 2cdot10^5$$$)xa0— the length of string $$$s$$$. The second line of each test case contains the string $$$s$$$, consisting of lowercase Latin characters. The sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, print the length of the shortest string $$$k$$$ satisfying the constraints in the statement. Example Input 5 4 abaa 4 abba 13 slavicgslavic 8 hshahaha 20 stormflamestornflame Note In the first test case, you can select $$$k = exttt{a}$$$ and $$$k+k+k+k = exttt{aaaa}$$$, which only differs from $$$s$$$ in the second position. In the second test case, you cannot select $$$k$$$ of length one or two. We can have $$$k = exttt{abba}$$$, which is equal to $$$s$$$.
1,500
false
false
true
false
false
false
true
false
false
false
577
1012A
Pavel made a photo of his favourite stars in the sky. His camera takes a photo of all points of the sky that belong to some rectangle with sides parallel to the coordinate axes. Strictly speaking, it makes a photo of all points with coordinates $$$(x, y)$$$, such that $$$x_1 leq x leq x_2$$$ and $$$y_1 leq y leq y_2$$$, where $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ are coordinates of the left bottom and the right top corners of the rectangle being photographed. The area of this rectangle can be zero. After taking the photo, Pavel wrote down coordinates of $$$n$$$ of his favourite stars which appeared in the photo. These points are not necessarily distinct, there can be multiple stars in the same point of the sky. Pavel has lost his camera recently and wants to buy a similar one. Specifically, he wants to know the dimensions of the photo he took earlier. Unfortunately, the photo is also lost. His notes are also of not much help; numbers are written in random order all over his notepad, so it's impossible to tell which numbers specify coordinates of which points. Pavel asked you to help him to determine what are the possible dimensions of the photo according to his notes. As there are multiple possible answers, find the dimensions with the minimal possible area of the rectangle. Input The first line of the input contains an only integer $$$n$$$ ($$$1 leq n leq 100,000$$$), the number of points in Pavel's records. The second line contains $$$2 cdot n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_{2 cdot n}$$$ ($$$1 leq a_i leq 10^9$$$), coordinates, written by Pavel in some order. Note In the first sample stars in Pavel's records can be $$$(1, 3)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$. In this case, the minimal area of the rectangle, which contains all these points is $$$1$$$ (rectangle with corners at $$$(1, 3)$$$ and $$$(2, 4)$$$).
1,500
true
false
true
false
false
false
true
false
true
false
5,627
1846A
There are $$$n$$$ nails driven into the wall, the $$$i$$$-th nail is driven $$$a_i$$$ meters above the ground, one end of the $$$b_i$$$ meters long rope is tied to it. All nails hang at different heights one above the other. One candy is tied to all ropes at once. Candy is tied to end of a rope that is not tied to a nail. To take the candy, you need to lower it to the ground. To do this, Rudolph can cut some ropes, one at a time. Help Rudolph find the minimum number of ropes that must be cut to get the candy. The figure shows an example of the first test: Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 50$$$)xa0— the number of nails. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 200$$$)xa0— the height of the $$$i$$$-th nail and the length of the rope tied to it, all $$$a_i$$$ are different. It is guaranteed that the data is not contradictory, it is possible to build a configuration described in the statement. Output For each test case print one integerxa0— the minimum number of ropes that need to be cut to make the candy fall to the ground. Example Input 4 3 4 3 3 1 1 2 4 9 2 5 2 7 7 3 4 5 11 7 5 10 12 9 3 2 1 5 3 5 6 4 5 7 7
800
true
false
true
false
false
false
false
false
false
false
1,208
34E
Problem - 34E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation math *2000 No tag edit access → Contest materials ") Разбор") and weight _m__i_. The radius of the balls can be ignored. The balls collide elastically, i.e. if two balls weighing _m_1 and _m_2 and with speeds _v_1 and _v_2 collide, their new speeds will be: .Your task is to find out, where each ball will be _t_ seconds after. Input The first line contains two integers _n_ and _t_ (1u2009≤u2009_n_u2009≤u200910,u20090u2009≤u2009_t_u2009≤u2009100) — amount of balls and duration of the process. Then follow _n_ lines, each containing three integers: _x__i_, _v__i_, _m__i_ (1u2009≤u2009_v__i_,u2009_m__i_u2009≤u2009100,u2009_x__i_u2009≤u2009100) — coordinate, speed and weight of the ball with index _i_ at time moment 0. It is guaranteed that no two balls have the same coordinate initially. Also each collision will be a collision of not more than two balls (that is, three or more balls never collide at the same point in all times from segment
2,000
true
false
true
false
false
false
true
false
false
false
9,824
311A
Currently Tiny is learning Computational Geometry. When trying to solve a problem called "The Closest Pair Of Points In The Plane", he found that a code which gave a wrong time complexity got Accepted instead of Time Limit Exceeded. The problem is the follows. Given _n_ points in the plane, find a pair of points between which the distance is minimized. Distance between (_x_1,u2009_y_1) and (_x_2,u2009_y_2) is . The pseudo code of the unexpected code is as follows: input n for i from 1 to n input the i-th point's coordinates into p[i] sort array p[] by increasing of x coordinate first and increasing of y coordinate second d=INF //here INF is a number big enough tot=0 for i from 1 to n for j from (i+1) to n ++tot if (p[j].x-p[i].x>=d) then break //notice that "break" is only to be //out of the loop "for j" d=min(d,distance(p[i],p[j])) output d Here, _tot_ can be regarded as the running time of the code. Due to the fact that a computer can only run a limited number of operations per second, _tot_ should not be more than _k_ in order not to get Time Limit Exceeded. You are a great hacker. Would you please help Tiny generate a test data and let the code get Time Limit Exceeded? Output If there doesn't exist such a data which let the given code get TLE, print "no solution" (without quotes); else print _n_ lines, and the _i_-th line contains two integers _x__i_,u2009_y__i_ (_x__i_,u2009_y__i_u2009≤u2009109) representing the coordinates of the _i_-th point. The conditions below must be held: All the points must be distinct. _x__i_,u2009_y__i_u2009≤u2009109. After running the given code, the value of _tot_ should be larger than _k_.
1,300
false
false
true
false
false
true
false
false
false
false
8,602
141E
After Santa Claus and his assistant Elf delivered all the presents and made all the wishes come true, they returned to the North Pole and found out that it is all covered with snow. Both of them were quite tired and they decided only to remove the snow from the roads connecting huts. The North Pole has _n_ huts connected with _m_ roads. One can go along the roads in both directions. The Elf offered to split: Santa Claus will clear up the wide roads and the Elf will tread out the narrow roads. For each road they decided who will clear it: Santa Claus or the Elf. To minimize the efforts they decided to clear the road so as to fulfill both those conditions: between any two huts should exist exactly one simple path along the cleared roads; Santa Claus and the Elf should clear the same number of roads. At this point Santa Claus and his assistant Elf wondered which roads should they clear up? Input The first input line contains two positive integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009103, 1u2009≤u2009_m_u2009≤u2009105) — the number of huts and the number of roads. Then follow _m_ lines, each of them contains a road description: the numbers of huts it connects — _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_) and the person responsible for clearing out this road ("S" — for the Elf or "M" for Santa Claus). It is possible to go on each road in both directions. Note that there can be more than one road between two huts and a road can begin and end in the same hut. Output Print "-1" without the quotes if it is impossible to choose the roads that will be cleared by the given rule. Otherwise print in the first line how many roads should be cleared and in the second line print the numbers of those roads (the roads are numbered from 1 in the order of occurrence in the input). It is allowed to print the numbers of the roads in any order. Each number should be printed exactly once. As you print the numbers, separate them with spaces. Examples Input 5 6 1 1 S 1 2 M 1 3 S 1 4 M 1 5 M 2 2 S Note A path is called simple if all huts on it are pairwise different.
2,300
false
false
false
true
false
true
false
false
false
true
9,308
1415A
There is a prison that can be represented as a rectangular matrix with $$$n$$$ rows and $$$m$$$ columns. Therefore, there are $$$n cdot m$$$ prison cells. There are also $$$n cdot m$$$ prisoners, one in each prison cell. Let's denote the cell in the $$$i$$$-th row and the $$$j$$$-th column as $$$(i, j)$$$. There's a secret tunnel in the cell $$$(r, c)$$$, that the prisoners will use to escape! However, to avoid the risk of getting caught, they will escape at night. Before the night, every prisoner is in his own cell. When night comes, they can start moving to adjacent cells. Formally, in one second, a prisoner located in cell $$$(i, j)$$$ can move to cells $$$( i - 1 , j )$$$ , $$$( i + 1 , j )$$$ , $$$( i , j - 1 )$$$ , or $$$( i , j + 1 )$$$, as long as the target cell is inside the prison. They can also choose to stay in cell $$$(i, j)$$$. The prisoners want to know the minimum number of seconds needed so that every prisoner can arrive to cell $$$( r , c )$$$ if they move optimally. Note that there can be any number of prisoners in the same cell at the same time. Input The first line contains an integer $$$t$$$ $$$(1 le t le 10^4)$$$, the number of test cases. Each of the next $$$t$$$ lines contains four space-separated integers $$$n$$$, $$$m$$$, $$$r$$$, $$$c$$$ ($$$1 le r le n le 10^9$$$, $$$1 le c le m le 10^9$$$).
800
true
false
false
false
false
false
true
false
false
false
3,608
507E
Breaking Good is a new video game which a lot of gamers want to have. There is a certain level in the game that is really difficult even for experienced gamers. Walter William, the main character of the game, wants to join a gang called Los Hermanos (The Brothers). The gang controls the whole country which consists of _n_ cities with _m_ bidirectional roads connecting them. There is no road is connecting a city to itself and for any two cities there is at most one road between them. The country is connected, in the other words, it is possible to reach any city from any other city using the given roads. The roads aren't all working. There are some roads which need some more work to be performed to be completely functioning. The gang is going to rob a bank! The bank is located in city 1. As usual, the hardest part is to escape to their headquarters where the police can't get them. The gang's headquarters is in city _n_. To gain the gang's trust, Walter is in charge of this operation, so he came up with a smart plan. First of all the path which they are going to use on their way back from city 1 to their headquarters _n_ must be as short as possible, since it is important to finish operation as fast as possible. Then, gang has to blow up all other roads in country that don't lay on this path, in order to prevent any police reinforcements. In case of non-working road, they don't have to blow up it as it is already malfunctional. If the chosen path has some roads that doesn't work they'll have to repair those roads before the operation. Walter discovered that there was a lot of paths that satisfied the condition of being shortest possible so he decided to choose among them a path that minimizes the total number of affected roads (both roads that have to be blown up and roads to be repaired). Can you help Walter complete his task and gain the gang's trust? Input The first line of input contains two integers _n_,u2009_m_ (2u2009≤u2009_n_u2009≤u2009105, ), the number of cities and number of roads respectively. In following _m_ lines there are descriptions of roads. Each description consists of three integers _x_,u2009_y_,u2009_z_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_, ) meaning that there is a road connecting cities number _x_ and _y_. If _z_u2009=u20091, this road is working, otherwise it is not. Output In the first line output one integer _k_, the minimum possible number of roads affected by gang. In the following _k_ lines output three integers describing roads that should be affected. Each line should contain three integers _x_,u2009_y_,u2009_z_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_, ), cities connected by a road and the new state of a road. _z_u2009=u20091 indicates that the road between cities _x_ and _y_ should be repaired and _z_u2009=u20090 means that road should be blown up. You may output roads in any order. Each affected road should appear exactly once. You may output cities connected by a single road in any order. If you output a road, it's original state should be different from _z_. After performing all operations accroding to your plan, there should remain working only roads lying on some certain shortest past between city 1 and _n_. If there are multiple optimal answers output any. Examples Input 4 4 1 2 1 1 3 0 2 3 1 3 4 1 Input 8 9 1 2 0 8 3 0 2 3 1 1 4 1 8 7 0 1 5 1 4 6 1 5 7 0 6 8 0 Note In the first test the only path is 1u2009-u20092 In the second test the only shortest path is 1u2009-u20093u2009-u20094 In the third test there are multiple shortest paths but the optimal is 1u2009-u20094u2009-u20096u2009-u20098
2,100
false
false
false
true
false
false
false
false
false
true
7,812
1501B
This week Arkady wanted to cook some pancakes (to follow ancient traditions) and make a problem about that. But then he remembered that one can't make a problem about stacking pancakes without working at a specific IT company, so he decided to bake the Napoleon cake instead. To bake a Napoleon cake, one has to bake $$$n$$$ dry layers first, and then put them on each other in one stack, adding some cream. Arkady started with an empty plate, and performed the following steps $$$n$$$ times: place a new cake layer on the top of the stack; after the $$$i$$$-th layer is placed, pour $$$a_i$$$ units of cream on top of the stack. When $$$x$$$ units of cream are poured on the top of the stack, top $$$x$$$ layers of the cake get drenched in the cream. If there are less than $$$x$$$ layers, all layers get drenched and the rest of the cream is wasted. If $$$x = 0$$$, no layer gets drenched. The picture represents the first test case of the example. Help Arkady determine which layers of the cake eventually get drenched when the process is over, and which don't. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 20,000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of layers in the cake. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le n$$$)xa0— the amount of cream poured on the cake after adding each layer. 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 a single line with $$$n$$$ integers. The $$$i$$$-th of the integers should be equal to $$$1$$$ if the $$$i$$$-th layer from the bottom gets drenched, and $$$0$$$ otherwise. Example Input 3 6 0 3 0 0 1 3 10 0 0 0 1 0 5 0 0 0 2 3 0 0 0 Output 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 0 0 0
900
false
false
true
true
false
false
false
false
true
false
3,161
1248D1
This is an easier version of the problem. In this version, $$$n le 500$$$. Vasya is an experienced developer of programming competitions' problems. As all great minds at some time, Vasya faced a creative crisis. To improve the situation, Petya gifted him a string consisting of opening and closing brackets only. Petya believes, that the beauty of the bracket string is a number of its cyclical shifts, which form a correct bracket sequence. To digress from his problems, Vasya decided to select two positions of the string (not necessarily distinct) and swap characters located at this positions with each other. Vasya will apply this operation exactly once. He is curious what is the maximum possible beauty he can achieve this way. Please help him. We remind that bracket sequence $$$s$$$ is called correct if: $$$s$$$ is empty; $$$s$$$ is equal to "($$$t$$$)", where $$$t$$$ is correct bracket sequence; $$$s$$$ is equal to $$$t_1 t_2$$$, i.e. concatenation of $$$t_1$$$ and $$$t_2$$$, where $$$t_1$$$ and $$$t_2$$$ are correct bracket sequences. For example, "(()())", "()" are correct, while ")(" and "())" are not. The cyclical shift of the string $$$s$$$ of length $$$n$$$ by $$$k$$$ ($$$0 leq k < n$$$) is a string formed by a concatenation of the last $$$k$$$ symbols of the string $$$s$$$ with the first $$$n - k$$$ symbols of string $$$s$$$. For example, the cyclical shift of string "(())()" by $$$2$$$ equals "()(())". Cyclical shifts $$$i$$$ and $$$j$$$ are considered different, if $$$i e j$$$. Input The first line contains an integer $$$n$$$ ($$$1 le n le 500$$$), the length of the string. The second line contains a string, consisting of exactly $$$n$$$ characters, where each of the characters is either "(" or ")". Output The first line should contain a single integerxa0— the largest beauty of the string, which can be achieved by swapping some two characters. The second line should contain integers $$$l$$$ and $$$r$$$ ($$$1 leq l, r leq n$$$)xa0— the indices of two characters, which should be swapped in order to maximize the string's beauty. In case there are several possible swaps, print any of them. Note In the first example, we can swap $$$7$$$-th and $$$8$$$-th character, obtaining a string "()()()()()". The cyclical shifts by $$$0, 2, 4, 6, 8$$$ of this string form a correct bracket sequence. In the second example, after swapping $$$5$$$-th and $$$10$$$-th character, we obtain a string ")(())()()(()". The cyclical shifts by $$$11, 7, 5, 3$$$ of this string form a correct bracket sequence. In the third example, swap of any two brackets results in $$$0$$$ cyclical shifts being correct bracket sequences.
2,000
false
true
true
true
false
false
true
false
false
false
4,465
1442B
We start with a permutation $$$a_1, a_2, ldots, a_n$$$ and with an empty array $$$b$$$. We apply the following operation $$$k$$$ times. On the $$$i$$$-th iteration, we select an index $$$t_i$$$ ($$$1 le t_i le n-i+1$$$), remove $$$a_{t_i}$$$ from the array, and append one of the numbers $$$a_{t_i-1}$$$ or $$$a_{t_i+1}$$$ (if $$$t_i-1$$$ or $$$t_i+1$$$ are within the array bounds) to the right end of the array $$$b$$$. Then we move elements $$$a_{t_i+1}, ldots, a_n$$$ to the left in order to fill in the empty space. You are given the initial permutation $$$a_1, a_2, ldots, a_n$$$ and the resulting array $$$b_1, b_2, ldots, b_k$$$. All elements of an array $$$b$$$ are distinct. Calculate the number of possible sequences of indices $$$t_1, t_2, ldots, t_k$$$ modulo $$$998,244,353$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 100,000$$$), denoting the number of test cases, followed by a description of the test cases. The first line of each test case contains two integers $$$n, k$$$ ($$$1 le k < n le 200,000$$$): sizes of arrays $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$): elements of $$$a$$$. All elements of $$$a$$$ are distinct. The third line of each test case contains $$$k$$$ integers $$$b_1, b_2, ldots, b_k$$$ ($$$1 le b_i le n$$$): elements of $$$b$$$. All elements of $$$b$$$ are distinct. The sum of all $$$n$$$ among all test cases is guaranteed to not exceed $$$200,000$$$. Output For each test case print one integer: the number of possible sequences modulo $$$998,244,353$$$. Example Input 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 Note $$$ equire{cancel}$$$ Let's denote as $$$a_1 a_2 ldots cancel{a_i} underline{a_{i+1}} ldots a_n ightarrow a_1 a_2 ldots a_{i-1} a_{i+1} ldots a_{n-1}$$$ an operation over an element with index $$$i$$$: removal of element $$$a_i$$$ from array $$$a$$$ and appending element $$$a_{i+1}$$$ to array $$$b$$$. In the first example test, the following two options can be used to produce the given array $$$b$$$: $$$1 2 underline{3} cancel{4} 5 ightarrow 1 underline{2} cancel{3} 5 ightarrow 1 cancel{2} underline{5} ightarrow 1 2$$$; $$$(t_1, t_2, t_3) = (4, 3, 2)$$$; $$$1 2 underline{3} cancel{4} 5 ightarrow cancel{1} underline{2} 3 5 ightarrow 2 cancel{3} underline{5} ightarrow 1 5$$$; $$$(t_1, t_2, t_3) = (4, 1, 2)$$$. In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to $$$4$$$, namely number $$$3$$$, which means that it couldn't be added to array $$$b$$$ on the second step. In the third example test, there are four options to achieve the given array $$$b$$$: $$$1 4 cancel{7} underline{3} 6 2 5 ightarrow 1 4 3 cancel{6} underline{2} 5 ightarrow cancel{1} underline{4} 3 2 5 ightarrow 4 3 cancel{2} underline{5} ightarrow 4 3 5$$$; $$$1 4 cancel{7} underline{3} 6 2 5 ightarrow 1 4 3 cancel{6} underline{2} 5 ightarrow 1 underline{4} cancel{3} 2 5 ightarrow 1 4 cancel{2} underline{5} ightarrow 1 4 5$$$; $$$1 4 7 underline{3} cancel{6} 2 5 ightarrow 1 4 7 cancel{3} underline{2} 5 ightarrow cancel{1} underline{4} 7 2 5 ightarrow 4 7 cancel{2} underline{5} ightarrow 4 7 5$$$; $$$1 4 7 underline{3} cancel{6} 2 5 ightarrow 1 4 7 cancel{3} underline{2} 5 ightarrow 1 underline{4} cancel{7} 2 5 ightarrow 1 4 cancel{2} underline{5} ightarrow 1 4 5$$$;
1,800
false
true
true
false
true
false
false
false
false
false
3,463
802J
Heidi's friend Jenny is asking Heidi to deliver an important letter to one of their common friends. Since Jenny is Irish, Heidi thinks that this might be a prank. More precisely, she suspects that the message she is asked to deliver states: "Send the fool further!", and upon reading it the recipient will ask Heidi to deliver the same message to yet another friend (that the recipient has in common with Heidi), and so on. Heidi believes that her friends want to avoid awkward situations, so she will not be made to visit the same person (including Jenny) twice. She also knows how much it costs to travel between any two of her friends who know each other. She wants to know: what is the maximal amount of money she will waste on travel if it really is a prank? Heidi's _n_ friends are labeled 0 through _n_u2009-u20091, and their network of connections forms a tree. In other words, every two of her friends _a_, _b_ know each other, possibly indirectly (there is a sequence of friends starting from _a_ and ending on _b_ and such that each two consecutive friends in the sequence know each other directly), and there are exactly _n_u2009-u20091 pairs of friends who know each other directly. Jenny is given the number 0. Input The first line of the input contains the number of friends _n_ (3u2009≤u2009_n_u2009≤u2009100). The next _n_u2009-u20091 lines each contain three space-separated integers _u_, _v_ and _c_ (0u2009≤u2009_u_,u2009_v_u2009≤u2009_n_u2009-u20091, 1u2009≤u2009_c_u2009≤u2009104), meaning that _u_ and _v_ are friends (know each other directly) and the cost for travelling between _u_ and _v_ is _c_. It is guaranteed that the social network of the input forms a tree. Output Output a single integer – the maximum sum of costs. Examples Input 6 1 2 3 0 2 100 1 4 2 0 3 7 3 5 10 Input 11 1 0 1664 2 0 881 3 2 4670 4 2 1555 5 1 1870 6 2 1265 7 2 288 8 7 2266 9 2 1536 10 6 3378 Note In the second example, the worst-case scenario goes like this: Jenny sends Heidi to the friend labeled by number 2 (incurring a cost of 100), then friend 2 sends her to friend 1 (costing Heidi 3), and finally friend 1 relays her to friend 4 (incurring an additional cost of 2).
1,400
false
false
false
false
false
false
false
false
false
true
6,558
702F
The big consignment of t-shirts goes on sale in the shop before the beginning of the spring. In all _n_ types of t-shirts go on sale. The t-shirt of the _i_-th type has two integer parameters — _c__i_ and _q__i_, where _c__i_ — is the price of the _i_-th type t-shirt, _q__i_ — is the quality of the _i_-th type t-shirt. It should be assumed that the unlimited number of t-shirts of each type goes on sale in the shop, but in general the quality is not concerned with the price. As predicted, _k_ customers will come to the shop within the next month, the _j_-th customer will get ready to spend up to _b__j_ on buying t-shirts. All customers have the same strategy. First of all, the customer wants to buy the maximum possible number of the highest quality t-shirts, then to buy the maximum possible number of the highest quality t-shirts from residuary t-shirts and so on. At the same time among several same quality t-shirts the customer will buy one that is cheaper. The customers don't like the same t-shirts, so each customer will not buy more than one t-shirt of one type. Determine the number of t-shirts which each customer will buy, if they use the described strategy. All customers act independently from each other, and the purchase of one does not affect the purchase of another. Input The first line contains the positive integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the number of t-shirt types. Each of the following _n_ lines contains two integers _c__i_ and _q__i_ (1u2009≤u2009_c__i_,u2009_q__i_u2009≤u2009109) — the price and the quality of the _i_-th type t-shirt. The next line contains the positive integer _k_ (1u2009≤u2009_k_u2009≤u20092·105) — the number of the customers. The next line contains _k_ positive integers _b_1,u2009_b_2,u2009...,u2009_b__k_ (1u2009≤u2009_b__j_u2009≤u2009109), where the _j_-th number is equal to the sum, which the _j_-th customer gets ready to spend on t-shirts. Output The first line of the input data should contain the sequence of _k_ integers, where the _i_-th number should be equal to the number of t-shirts, which the _i_-th customer will buy. Examples Input 2 100 500 50 499 4 50 200 150 100 Note In the first example the first customer will buy the t-shirt of the second type, then the t-shirt of the first type. He will spend 10 and will not be able to buy the t-shirt of the third type because it costs 4, and the customer will owe only 3. The second customer will buy all three t-shirts (at first, the t-shirt of the second type, then the t-shirt of the first type, and then the t-shirt of the third type). He will spend all money on it.
2,800
false
false
false
false
true
false
false
false
false
false
7,012
14E
Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with _t_ humps, representing them as polylines in the plane. Each polyline consists of _n_ vertices with coordinates (_x_1,u2009_y_1), (_x_2,u2009_y_2), ..., (_x__n_,u2009_y__n_). The first vertex has a coordinate _x_1u2009=u20091, the second — _x_2u2009=u20092, etc. Coordinates _y__i_ might be any, but should satisfy the following conditions: there should be _t_ humps precisely, i.e. such indexes _j_ (2u2009≤u2009_j_u2009≤u2009_n_u2009-u20091), so that _y__j_u2009-u20091u2009<u2009_y__j_u2009>u2009_y__j_u2009+u20091, there should be precisely _t_u2009-u20091 such indexes _j_ (2u2009≤u2009_j_u2009≤u2009_n_u2009-u20091), so that _y__j_u2009-u20091u2009>u2009_y__j_u2009<u2009_y__j_u2009+u20091, no segment of a polyline should be parallel to the _Ox_-axis, all _y__i_ are integers between 1 and 4. For a series of his drawings of camels with _t_ humps Bob wants to buy a notebook, but he doesn't know how many pages he will need. Output the amount of different polylines that can be drawn to represent camels with _t_ humps for a given number _n_. Input The first line contains a pair of integers _n_ and _t_ (3u2009≤u2009_n_u2009≤u200920, 1u2009≤u2009_t_u2009≤u200910). Output Output the required amount of camels with _t_ humps. Note In the first sample test sequences of _y_-coordinates for six camels are: 123421, 123431, 123432, 124321, 134321 и 234321 (each digit corresponds to one value of _y__i_).
1,900
false
false
false
true
false
false
false
false
false
false
9,921
1579A
Casimir has a string $$$s$$$ which consists of capital Latin letters 'A', 'B', and 'C' only. Each turn he can choose to do one of the two following actions: he can either erase exactly one letter 'A' and exactly one letter 'B' from arbitrary places of the string (these letters don't have to be adjacent); or he can erase exactly one letter 'B' and exactly one letter 'C' from arbitrary places in the string (these letters don't have to be adjacent). Therefore, each turn the length of the string is decreased exactly by $$$2$$$. All turns are independent so for each turn, Casimir can choose any of two possible actions. For example, with $$$s$$$xa0$$$=$$$xa0"ABCABC" he can obtain a string $$$s$$$xa0$$$=$$$xa0"ACBC" in one turn (by erasing the first occurrence of 'B' and the second occurrence of 'A'). There are also many other options for a turn aside from this particular example. For a given string $$$s$$$ determine whether there is a sequence of actions leading to an empty string. In other words, Casimir's goal is to erase all letters from the string. Is there a way to do this? Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Each test case is described by one string $$$s$$$, for which you need to determine if it can be fully erased by some sequence of turns. The string $$$s$$$ consists of capital letters 'A', 'B', 'C' and has a length from $$$1$$$ to $$$50$$$ letters, inclusive. Output Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if there is a way to fully erase the corresponding string and NO otherwise. 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 answers). Example Input 6 ABACAB ABBA AC ABC CABCBB BCBCBCBCBCBCBCBC Output NO YES NO NO YES YES
800
true
false
false
false
false
false
false
false
false
false
2,741
1534D
This is an interactive problem. Little Dormi was faced with an awkward problem at the carnival: he has to guess the edges of an unweighted tree of $$$n$$$ nodes! The nodes of the tree are numbered from $$$1$$$ to $$$n$$$. The game master only allows him to ask one type of question: Little Dormi picks a node $$$r$$$ ($$$1 le r le n$$$), and the game master will reply with an array $$$d_1, d_2, ldots, d_n$$$, where $$$d_i$$$ is the length of the shortest path from node $$$r$$$ to $$$i$$$, for all $$$1 le i le n$$$. Additionally, to make the game unfair challenge Little Dormi the game master will allow at most $$$lceilfrac{n}{2} ceil$$$ questions, where $$$lceil x ceil$$$ denotes the smallest integer greater than or equal to $$$x$$$. Faced with the stomach-churning possibility of not being able to guess the tree, Little Dormi needs your help to devise a winning strategy! Note that the game master creates the tree before the game starts, and does not change it during the game. Input The first line of input contains the integer $$$n$$$ ($$$2 le n le 2,000$$$), the number of nodes in the tree. You will then begin interaction. Output When your program has found the tree, first output a line consisting of a single "!" followed by $$$n-1$$$ lines each with two space separated integers $$$a$$$ and $$$b$$$, denoting an edge connecting nodes $$$a$$$ and $$$b$$$ ($$$1 le a, b le n$$$). Once you are done, terminate your program normally immediately after flushing the output stream. You may output the edges in any order and an edge $$$(a,b)$$$ is considered the same as an edge $$$(b,a)$$$. Answering is not considered as a query. Interaction After taking input, you may make at most $$$lceilfrac{n}{2} ceil$$$ queries. Each query is made in the format "? r", where $$$r$$$ is an integer $$$1 le r le n$$$ that denotes the node you want to pick for that query. You will then receive $$$n$$$ space separated integers $$$d_1, d_2, ldots, d_n$$$, where $$$d_i$$$ is the length of the shortest path from node $$$r$$$ to $$$i$$$, followed by a newline. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. If at any point you make an invalid query or try to make more than $$$lceil frac{n}{2} ceil$$$ queries, the interaction will terminate immediately and you will receive a Wrong Answer verdict. Hacks To hack a solution, use the following format. The first line contains the integer $$$n$$$ ($$$2 le n le 2,000$$$). The next $$$n−1$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) denoting an edge between $$$u$$$ and $$$v$$$ ($$$u eq v$$$). These $$$n-1$$$ edges must form a tree. Examples Output ? 1 ? 2 ! 4 2 1 2 2 3 Output ? 5 ! 4 5 3 5 2 4 1 3 Note Here is the tree from the first example. Notice that the edges can be output in any order. Additionally, here are the answers for querying every single node in example $$$1$$$: $$$1$$$: $$$[0,1,2,2]$$$ $$$2$$$: $$$[1,0,1,1]$$$ $$$3$$$: $$$[2,1,0,2]$$$ $$$4$$$: $$$[2,1,2,0]$$$ Below is the tree from the second example interaction. Lastly, here are the answers for querying every single node in example $$$2$$$: $$$1$$$: $$$[0,4,1,3,2]$$$ $$$2$$$: $$$[4,0,3,1,2]$$$ $$$3$$$: $$$[1,3,0,2,1]$$$ $$$4$$$: $$$[3,1,2,0,1]$$$ $$$5$$$: $$$[2,2,1,1,0]$$$
1,800
false
false
false
false
false
true
false
false
false
false
2,980
343C
Mad scientist Mike does not use slow hard disks. His modification of a hard drive has not one, but _n_ different heads that can read data in parallel. When viewed from the side, Mike's hard drive is an endless array of tracks. The tracks of the array are numbered from left to right with integers, starting with 1. In the initial state the _i_-th reading head is above the track number _h__i_. For each of the reading heads, the hard drive's firmware can move the head exactly one track to the right or to the left, or leave it on the current track. During the operation each head's movement does not affect the movement of the other heads: the heads can change their relative order; there can be multiple reading heads above any of the tracks. A track is considered read if at least one head has visited this track. In particular, all of the tracks numbered _h_1, _h_2, ..., _h__n_ have been read at the beginning of the operation. Mike needs to read the data on _m_ distinct tracks with numbers _p_1, _p_2, ..., _p__m_. Determine the minimum time the hard drive firmware needs to move the heads and read all the given tracks. Note that an arbitrary number of other tracks can also be read. Input The first line of the input contains two space-separated integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of disk heads and the number of tracks to read, accordingly. The second line contains _n_ distinct integers _h__i_ in ascending order (1u2009≤u2009_h__i_u2009≤u20091010, _h__i_u2009<u2009_h__i_u2009+u20091) — the initial positions of the heads. The third line contains _m_ distinct integers _p__i_ in ascending order (1u2009≤u2009_p__i_u2009≤u20091010, _p__i_u2009<u2009_p__i_u2009+u20091) - the numbers of tracks to read. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier. Output Print a single number — the minimum time required, in seconds, to read all the needed tracks. Note The first test coincides with the figure. In this case the given tracks can be read in 2 seconds in the following way: 1. during the first second move the 1-st head to the left and let it stay there; 2. move the second head to the left twice; 3. move the third head to the right twice (note that the 6-th track has already been read at the beginning). One cannot read the tracks in 1 second as the 3-rd head is at distance 2 from the 8-th track.
1,900
false
true
false
false
false
false
false
true
false
false
8,461
1506A
Polycarp found a rectangular table consisting of $$$n$$$ rows and $$$m$$$ columns. He noticed that each cell of the table has its number, obtained by the following algorithm "by columns": cells are numbered starting from one; cells are numbered from left to right by columns, and inside each column from top to bottom; number of each cell is an integer one greater than in the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, the table will be numbered as follows: $$$$$$ begin{matrix} 1 & 4 & 7 & 10 & 13 2 & 5 & 8 & 11 & 14 3 & 6 & 9 & 12 & 15 end{matrix} $$$$$$ However, Polycarp considers such numbering inconvenient. He likes the numbering "by rows": cells are numbered starting from one; cells are numbered from top to bottom by rows, and inside each row from left to right; number of each cell is an integer one greater than the number of the previous cell. For example, if $$$n = 3$$$ and $$$m = 5$$$, then Polycarp likes the following table numbering: $$$$$$ begin{matrix} 1 & 2 & 3 & 4 & 5 6 & 7 & 8 & 9 & 10 11 & 12 & 13 & 14 & 15 end{matrix} $$$$$$ Polycarp doesn't have much time, so he asks you to find out what would be the cell number in the numbering "by rows", if in the numbering "by columns" the cell has the number $$$x$$$? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$). Then $$$t$$$ test cases follow. Each test case consists of a single line containing three integers $$$n$$$, $$$m$$$, $$$x$$$ ($$$1 le n, m le 10^6$$$, $$$1 le x le n cdot m$$$), where $$$n$$$ and $$$m$$$ are the number of rows and columns in the table, and $$$x$$$ is the cell number. Note that the numbers in some test cases do not fit into the $$$32$$$-bit integer type, so you must use at least the $$$64$$$-bit integer type of your programming language. Output For each test case, output the cell number in the numbering "by rows". Example Input 5 1 1 1 2 2 3 3 5 11 100 100 7312 1000000 1000000 1000000000000 Output 1 2 9 1174 1000000000000
800
true
false
false
false
false
false
false
false
false
false
3,143
1016G
Since next season are coming, you'd like to form a team from two or three participants. There are $$$n$$$ candidates, the $$$i$$$-th candidate has rank $$$a_i$$$. But you have weird requirements for your teammates: if you have rank $$$v$$$ and have chosen the $$$i$$$-th and $$$j$$$-th candidate, then $$$GCD(v, a_i) = X$$$ and $$$LCM(v, a_j) = Y$$$ must be met. You are very experienced, so you can change your rank to any non-negative integer but $$$X$$$ and $$$Y$$$ are tied with your birthdate, so they are fixed. Now you want to know, how many are there pairs $$$(i, j)$$$ such that there exists an integer $$$v$$$ meeting the following constraints: $$$GCD(v, a_i) = X$$$ and $$$LCM(v, a_j) = Y$$$. It's possible that $$$i = j$$$ and you form a team of two. $$$GCD$$$ is the greatest common divisor of two number, $$$LCM$$$ — the least common multiple. Input First line contains three integers $$$n$$$, $$$X$$$ and $$$Y$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le X le Y le 10^{18}$$$) — the number of candidates and corresponding constants. Second line contains $$$n$$$ space separated integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^{18}$$$) — ranks of candidates. Output Print the only integer — the number of pairs $$$(i, j)$$$ such that there exists an integer $$$v$$$ meeting the following constraints: $$$GCD(v, a_i) = X$$$ and $$$LCM(v, a_j) = Y$$$. It's possible that $$$i = j$$$. Examples Input 12 2 2 1 2 3 4 5 6 7 8 9 10 11 12 Input 12 1 6 1 3 5 7 9 11 12 10 8 6 4 2 Note In the first example next pairs are valid: $$$a_j = 1$$$ and $$$a_i = [2, 4, 6, 8, 10, 12]$$$ or $$$a_j = 2$$$ and $$$a_i = [2, 4, 6, 8, 10, 12]$$$. The $$$v$$$ in both cases can be equal to $$$2$$$. In the second example next pairs are valid: $$$a_j = 1$$$ and $$$a_i = [1, 5, 7, 11]$$$; $$$a_j = 2$$$ and $$$a_i = [1, 5, 7, 11, 10, 8, 4, 2]$$$; $$$a_j = 3$$$ and $$$a_i = [1, 3, 5, 7, 9, 11]$$$; $$$a_j = 6$$$ and $$$a_i = [1, 3, 5, 7, 9, 11, 12, 10, 8, 6, 4, 2]$$$.
2,700
true
false
false
false
false
false
false
false
false
false
5,606
594C
Edo has got a collection of _n_ refrigerator magnets! He decided to buy a refrigerator and hang the magnets on the door. The shop can make the refrigerator with any size of the door that meets the following restrictions: the refrigerator door must be rectangle, and both the length and the width of the door must be positive integers. Edo figured out how he wants to place the magnets on the refrigerator. He introduced a system of coordinates on the plane, where each magnet is represented as a rectangle with sides parallel to the coordinate axes. Now he wants to remove no more than _k_ magnets (he may choose to keep all of them) and attach all remaining magnets to the refrigerator door, and the area of u200bu200bthe door should be as small as possible. A magnet is considered to be attached to the refrigerator door if its center lies on the door or on its boundary. The relative positions of all the remaining magnets must correspond to the plan. Let us explain the last two sentences. Let's suppose we want to hang two magnets on the refrigerator. If the magnet in the plan has coordinates of the lower left corner (_x_1, _y_1) and the upper right corner (_x_2, _y_2), then its center is located at (, ) (may not be integers). By saying the relative position should correspond to the plan we mean that the only available operation is translation, i.e. the vector connecting the centers of two magnets in the original plan, must be equal to the vector connecting the centers of these two magnets on the refrigerator. The sides of the refrigerator door must also be parallel to coordinate axes. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100u2009000, 0u2009≤u2009_k_u2009≤u2009_min_(10,u2009_n_u2009-u20091))xa0— the number of magnets that Edo has and the maximum number of magnets Edo may not place on the refrigerator. Next _n_ lines describe the initial plan of placing magnets. Each line contains four integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2 (1u2009≤u2009_x_1u2009<u2009_x_2u2009≤u2009109, 1u2009≤u2009_y_1u2009<u2009_y_2u2009≤u2009109)xa0— the coordinates of the lower left and upper right corners of the current magnet. The magnets can partially overlap or even fully coincide. Output Print a single integerxa0— the minimum area of the door of refrigerator, which can be used to place at least _n_u2009-u2009_k_ magnets, preserving the relative positions. Examples Input 3 1 1 1 2 2 2 2 3 3 3 3 4 4 Input 4 1 1 1 2 2 1 9 2 10 9 9 10 10 9 1 10 2 Input 3 0 1 1 2 2 1 1 1000000000 1000000000 1 3 8 12 Note In the first test sample it is optimal to remove either the first or the third magnet. If we remove the first magnet, the centers of two others will lie at points (2.5, 2.5) and (3.5, 3.5). Thus, it is enough to buy a fridge with door width 1 and door height 1, the area of the door also equals one, correspondingly. In the second test sample it doesn't matter which magnet to remove, the answer will not change — we need a fridge with door width 8 and door height 8. In the third sample you cannot remove anything as _k_u2009=u20090.
2,300
false
true
true
false
false
false
true
false
false
false
7,470
1371F
2,800
false
false
true
false
true
false
false
false
false
false
3,808
171A
Problem - 171A - Codeforces =============== xa0 , separated by a single space. Output Output a single integer. Examples Input 3 14 Output 44 Input 27 12 Output 48 Input 100 200 Output 102
1,200
false
false
false
false
false
true
false
false
false
false
9,186
683I
A loader works in a warehouse, which is a rectangular field with size _n_u2009×u2009_m_. Some cells of this field are free, others are occupied by pillars on which the roof of the warehouse rests. There is a load in one of the free cells, and the loader in another. At any moment, the loader and the load can not be in the cells with columns, outside the warehouse or in the same cell. The loader can move to the adjacent cell (two cells are considered adjacent if they have a common side), or move the load. To move the load, the loader should reach the cell adjacent to the load and push the load. In this case the load advances to the next cell in the direction in which the loader pushes it and the loader ends up in the cell in which the load was. Your task is to determine a sequence of pushes and loader's movements after which the load will reach the given cell (it is guaranteed that this cell is free). The load is rather heavy, so you need to minimize first the number of pushes and second the number of loader's movements. Input The first line contains two positive integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200940, _n_·_m_u2009≥u20093) — the number of rows and columns in the rectangular field. Each of the next _n_ lines contains _m_ characters — the description of the warehouse. If there is a character in the next cell of the warehouse: "X", it means, that the current cell contains the column; ".", it means, that the current cell is free; "Y", it means, that the loader is in the current cell; "B", it means, that the load is in the current cell; "T", it means, that the load should be moved to this cell. It is guaranteed that there is exactly one load, one loader and one cell to which the load should be moved. Output If the loader is not able to move the load to the given cell, print "NO" (without the quotes) in the first line of the output. Otherwise, print "YES" (without the quotes) in the first line of the output, and in the second line — the sequence of characters that determines movements and pushes of the loader. Characters w, e, n, s shall denote loader's moves to the west, east, north and south, respectively. Characters W, E, N, S must denote loader's pushes in the corresponding directions. First of all you need to minimize the number of pushes of the load and second, the number of movements of the loader. If there are several answers, you are allowed to print any of them.
2,500
false
false
false
false
false
false
false
false
false
true
7,085
1582E
Pchelyonok decided to give Mila a gift. Pchelenok has already bought an array $$$a$$$ of length $$$n$$$, but gifting an array is too common. Instead of that, he decided to gift Mila the segments of that array! Pchelyonok wants his gift to be beautiful, so he decided to choose $$$k$$$ non-overlapping segments of the array $$$[l_1,r_1]$$$, $$$[l_2,r_2]$$$, $$$ldots$$$ $$$[l_k,r_k]$$$ such that: the length of the first segment $$$[l_1,r_1]$$$ is $$$k$$$, the length of the second segment $$$[l_2,r_2]$$$ is $$$k-1$$$, $$$ldots$$$, the length of the $$$k$$$-th segment $$$[l_k,r_k]$$$ is $$$1$$$ for each $$$i<j$$$, the $$$i$$$-th segment occurs in the array earlier than the $$$j$$$-th (i.e. $$$r_i<l_j$$$) the sums in these segments are strictly increasing (i.e. let $$$sum(l ldots r) = sumlimits_{i=l}^{r} a_i$$$ — the sum of numbers in the segment $$$[l,r]$$$ of the array, then $$$sum(l_1 ldots r_1) < sum(l_2 ldots r_2) < ldots < sum(l_k ldots r_k)$$$). Pchelenok also wants his gift to be as beautiful as possible, so he asks you to find the maximal value of $$$k$$$ such that he can give Mila a gift! Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The next $$$2 cdot t$$$ lines contain the descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$) — 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, print the maximum possible value of $$$k$$$. Example Input 5 1 1 3 1 2 3 5 1 1 2 2 3 7 1 2 1 1 3 2 6 5 9 6 7 9 7
2,000
true
true
false
true
true
false
false
true
false
false
2,721
1569C
$$$n$$$ people gathered to hold a jury meeting of the upcoming competition, the $$$i$$$-th member of the jury came up with $$$a_i$$$ tasks, which they want to share with each other. First, the jury decides on the order which they will follow while describing the tasks. Let that be a permutation $$$p$$$ of numbers from $$$1$$$ to $$$n$$$ (an array of size $$$n$$$ where each integer from $$$1$$$ to $$$n$$$ occurs exactly once). Then the discussion goes as follows: If a jury member $$$p_1$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. If a jury member $$$p_2$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. ... If a jury member $$$p_n$$$ has some tasks left to tell, then they tell one task to others. Otherwise, they are skipped. If there are still members with tasks left, then the process repeats from the start. Otherwise, the discussion ends. A permutation $$$p$$$ is nice if none of the jury members tell two or more of their own tasks in a row. Count the number of nice permutations. The answer may be really large, so print it modulo $$$998,244,353$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of the test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— number of jury members. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the number of problems that the $$$i$$$-th member of the jury came up with. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print one integerxa0— the number of nice permutations, taken modulo $$$998,244,353$$$. Example Input 4 2 1 2 3 5 5 5 4 1 3 3 7 6 3 4 2 1 3 3 Note Explanation of the first test case from the example: There are two possible permutations, $$$p = [1, 2]$$$ and $$$p = [2, 1]$$$. For $$$p = [1, 2]$$$, the process is the following: 1. the first jury member tells a task; 2. the second jury member tells a task; 3. the first jury member doesn't have any tasks left to tell, so they are skipped; 4. the second jury member tells a task. So, the second jury member has told two tasks in a row (in succession), so the permutation is not nice. For $$$p = [2, 1]$$$, the process is the following: 1. the second jury member tells a task; 2. the first jury member tells a task; 3. the second jury member tells a task. So, this permutation is nice.
1,500
true
false
false
false
false
false
false
false
false
false
2,796
222D
A boy named Vasya has taken part in an Olympiad. His teacher knows that in total Vasya got at least _x_ points for both tours of the Olympiad. The teacher has the results of the first and the second tour of the Olympiad but the problem is, the results have only points, no names. The teacher has to know Vasya's chances. Help Vasya's teacher, find two numbers — the best and the worst place Vasya could have won. Note that the total results' table sorts the participants by the sum of points for both tours (the first place has the participant who has got the most points). If two or more participants have got the same number of points, it's up to the jury to assign places to them according to their choice. It is guaranteed that each participant of the Olympiad participated in both tours of the Olympiad. Input The first line contains two space-separated integers _n_,u2009_x_ (1u2009≤u2009_n_u2009≤u2009105;xa00u2009≤u2009_x_u2009≤u20092·105) — the number of Olympiad participants and the minimum number of points Vasya earned. The second line contains _n_ space-separated integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009105) — the participants' points in the first tour. The third line contains _n_ space-separated integers: _b_1,u2009_b_2,u2009...,u2009_b__n_ (0u2009≤u2009_b__i_u2009≤u2009105) — the participants' points in the second tour. The participants' points are given in the arbitrary order. It is guaranteed that Vasya was present in the Olympiad — there are two integers _i_,u2009_j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_) such, that _a__i_u2009+u2009_b__j_u2009≥u2009_x_. Output Print two space-separated integers — the best and the worst place Vasya could have got on the Olympiad. Examples Input 5 2 1 1 1 1 1 1 1 1 1 1 Input 6 7 4 3 5 6 4 4 8 6 0 4 3 4 Note In the first text sample all 5 participants earn 2 points each in any case. Depending on the jury's decision, Vasya can get the first (the best) as well as the last (the worst) fifth place. In the second test sample in the best case scenario Vasya wins again: he can win 12 points and become the absolute winner if the total results' table looks like that — {4:8, 6:4, 3:6, 4:4, 4:3, 5:0}. In this table all participants are sorted by decreasing points and we can see how much a participant earned in the first and in the second tour. In the worst case scenario Vasya can get the fifth place if the table looks like that — {4:8, 4:6, 6:4, 5:4, 4:3, 3:0}, and he earned 4 and 3 points in the first and second tours, correspondingly.
1,900
false
true
false
false
false
false
false
true
true
false
8,949