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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
865B | It's another Start[c]up finals, and that means there is pizza to order for the onsite contestants. There are only 2 types of pizza (obviously not, but let's just pretend for the sake of the problem), and all pizzas contain exactly _S_ slices. It is known that the _i_-th contestant will eat _s__i_ slices of pizza, and gain _a__i_ happiness for each slice of type 1 pizza they eat, and _b__i_ happiness for each slice of type 2 pizza they eat. We can order any number of type 1 and type 2 pizzas, but we want to buy the minimum possible number of pizzas for all of the contestants to be able to eat their required number of slices. Given that restriction, what is the maximum possible total happiness that can be achieved? Input The first line of input will contain integers _N_ and _S_ (1u2009≤u2009_N_u2009≤u2009105,u20091u2009≤u2009_S_u2009≤u2009105), the number of contestants and the number of slices per pizza, respectively. _N_ lines follow. The _i_-th such line contains integers _s__i_, _a__i_, and _b__i_ (1u2009≤u2009_s__i_u2009≤u2009105,u20091u2009≤u2009_a__i_u2009≤u2009105,u20091u2009≤u2009_b__i_u2009≤u2009105), the number of slices the _i_-th contestant will eat, the happiness they will gain from each type 1 slice they eat, and the happiness they will gain from each type 2 slice they eat, respectively. Output Print the maximum total happiness that can be achieved. Examples Input 6 10 7 4 7 5 8 8 12 5 8 6 11 6 3 3 7 5 9 6 Note In the first example, you only need to buy one pizza. If you buy a type 1 pizza, the total happiness will be 3·5u2009+u20094·6u2009+u20095·9u2009=u200984, and if you buy a type 2 pizza, the total happiness will be 3·7u2009+u20094·7u2009+u20095·5u2009=u200974. | 1,900 | false | false | false | false | false | false | false | true | true | false | 6,280 |
1136C | Nastya came to her informatics lesson, and her teacher who is, by the way, a little bit famous here gave her the following task. Two matrices $$$A$$$ and $$$B$$$ are given, each of them has size $$$n imes m$$$. Nastya can perform the following operation to matrix $$$A$$$ unlimited number of times: take any square square submatrix of $$$A$$$ and transpose it (i.e. the element of the submatrix which was in the $$$i$$$-th row and $$$j$$$-th column of the submatrix will be in the $$$j$$$-th row and $$$i$$$-th column after transposing, and the transposed submatrix itself will keep its place in the matrix $$$A$$$). Nastya's task is to check whether it is possible to transform the matrix $$$A$$$ to the matrix $$$B$$$. Example of the operation As it may require a lot of operations, you are asked to answer this question for Nastya. A square submatrix of matrix $$$M$$$ is a matrix which consist of all elements which comes from one of the rows with indeces $$$x, x+1, dots, x+k-1$$$ of matrix $$$M$$$ and comes from one of the columns with indeces $$$y, y+1, dots, y+k-1$$$ of matrix $$$M$$$. $$$k$$$ is the size of square submatrix. In other words, square submatrix is the set of elements of source matrix which form a solid square (i.e. without holes). Input The first line contains two integers $$$n$$$ and $$$m$$$ separated by space ($$$1 leq n, m leq 500$$$)xa0— the numbers of rows and columns in $$$A$$$ and $$$B$$$ respectively. Each of the next $$$n$$$ lines contains $$$m$$$ integers, the $$$j$$$-th number in the $$$i$$$-th of these lines denotes the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$A$$$ ($$$1 leq A_{ij} leq 10^{9}$$$). Each of the next $$$n$$$ lines contains $$$m$$$ integers, the $$$j$$$-th number in the $$$i$$$-th of these lines denotes the $$$j$$$-th element of the $$$i$$$-th row of the matrix $$$B$$$ ($$$1 leq B_{ij} leq 10^{9}$$$). Output Print "YES" (without quotes) if it is possible to transform $$$A$$$ to $$$B$$$ and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Examples Input 3 3 1 2 3 4 5 6 7 8 9 1 4 7 2 5 6 3 8 9 Note Consider the third example. The matrix $$$A$$$ initially looks as follows. $$$$$$ begin{bmatrix} 1 & 2 & 3 4 & 5 & 6 7 & 8 & 9 end{bmatrix} $$$$$$ Then we choose the whole matrix as transposed submatrix and it becomes $$$$$$ begin{bmatrix} 1 & 4 & 7 2 & 5 & 8 3 & 6 & 9 end{bmatrix} $$$$$$ Then we transpose the submatrix with corners in cells $$$(2, 2)$$$ and $$$(3, 3)$$$. $$$$$$ begin{bmatrix} 1 & 4 & 7 2 & extbf{5} & extbf{8} 3 & extbf{6} & extbf{9} end{bmatrix} $$$$$$ So matrix becomes $$$$$$ begin{bmatrix} 1 & 4 & 7 2 & 5 & 6 3 & 8 & 9 end{bmatrix} $$$$$$ and it is $$$B$$$. | 1,500 | false | false | false | false | false | true | false | false | true | false | 5,045 |
1070B | Berkomnadzorxa0— Federal Service for Supervision of Communications, Information Technology and Mass Mediaxa0— is a Berland federal executive body that protects ordinary residents of Berland from the threats of modern internet. Berkomnadzor maintains a list of prohibited IPv4 subnets (blacklist) and a list of allowed IPv4 subnets (whitelist). All Internet Service Providers (ISPs) in Berland must configure the network equipment to block access to all IPv4 addresses matching the blacklist. Also ISPs must provide access (that is, do not block) to all IPv4 addresses matching the whitelist. If an IPv4 address does not match either of those lists, it's up to the ISP to decide whether to block it or not. An IPv4 address matches the blacklist (whitelist) if and only if it matches some subnet from the blacklist (whitelist). An IPv4 address can belong to a whitelist and to a blacklist at the same time, this situation leads to a contradiction (see no solution case in the output description). An IPv4 address is a 32-bit unsigned integer written in the form $$$a.b.c.d$$$, where each of the values $$$a,b,c,d$$$ is called an octet and is an integer from $$$0$$$ to $$$255$$$ written in decimal notation. For example, IPv4 address $$$192.168.0.1$$$ can be converted to a 32-bit number using the following expression $$$192 cdot 2^{24} + 168 cdot 2^{16} + 0 cdot 2^8 + 1 cdot 2^0$$$. First octet $$$a$$$ encodes the most significant (leftmost) $$$8$$$ bits, the octets $$$b$$$ and $$$c$$$xa0— the following blocks of $$$8$$$ bits (in this order), and the octet $$$d$$$ encodes the least significant (rightmost) $$$8$$$ bits. The IPv4 network in Berland is slightly different from the rest of the world. There are no reserved or internal addresses in Berland and use all $$$2^{32}$$$ possible values. An IPv4 subnet is represented either as $$$a.b.c.d$$$ or as $$$a.b.c.d/x$$$ (where $$$0 le x le 32$$$). A subnet $$$a.b.c.d$$$ contains a single address $$$a.b.c.d$$$. A subnet $$$a.b.c.d/x$$$ contains all IPv4 addresses with $$$x$$$ leftmost (most significant) bits equal to $$$x$$$ leftmost bits of the address $$$a.b.c.d$$$. It is required that $$$32 - x$$$ rightmost (least significant) bits of subnet $$$a.b.c.d/x$$$ are zeroes. Naturally it happens that all addresses matching subnet $$$a.b.c.d/x$$$ form a continuous range. The range starts with address $$$a.b.c.d$$$ (its rightmost $$$32 - x$$$ bits are zeroes). The range ends with address which $$$x$$$ leftmost bits equal to $$$x$$$ leftmost bits of address $$$a.b.c.d$$$, and its $$$32 - x$$$ rightmost bits are all ones. Subnet contains exactly $$$2^{32-x}$$$ addresses. Subnet $$$a.b.c.d/32$$$ contains exactly one address and can also be represented by just $$$a.b.c.d$$$. For example subnet $$$192.168.0.0/24$$$ contains range of 256 addresses. $$$192.168.0.0$$$ is the first address of the range, and $$$192.168.0.255$$$ is the last one. Berkomnadzor's engineers have devised a plan to improve performance of Berland's global network. Instead of maintaining both whitelist and blacklist they want to build only a single optimised blacklist containing minimal number of subnets. The idea is to block all IPv4 addresses matching the optimised blacklist and allow all the rest addresses. Of course, IPv4 addresses from the old blacklist must remain blocked and all IPv4 addresses from the old whitelist must still be allowed. Those IPv4 addresses which matched neither the old blacklist nor the old whitelist may be either blocked or allowed regardless of their accessibility before. Please write a program which takes blacklist and whitelist as input and produces optimised blacklist. The optimised blacklist must contain the minimal possible number of subnets and satisfy all IPv4 addresses accessibility requirements mentioned above. IPv4 subnets in the source lists may intersect arbitrarily. Please output a single number -1 if some IPv4 address matches both source whitelist and blacklist. Input The first line of the input contains single integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$) — total number of IPv4 subnets in the input. The following $$$n$$$ lines contain IPv4 subnets. Each line starts with either '-' or '+' sign, which indicates if the subnet belongs to the blacklist or to the whitelist correspondingly. It is followed, without any spaces, by the IPv4 subnet in $$$a.b.c.d$$$ or $$$a.b.c.d/x$$$ format ($$$0 le x le 32$$$). The blacklist always contains at least one subnet. All of the IPv4 subnets given in the input are valid. Integer numbers do not start with extra leading zeroes. The provided IPv4 subnets can intersect arbitrarily. Output Output -1, if there is an IPv4 address that matches both the whitelist and the blacklist. Otherwise output $$$t$$$ — the length of the optimised blacklist, followed by $$$t$$$ subnets, with each subnet on a new line. Subnets may be printed in arbitrary order. All addresses matching the source blacklist must match the optimised blacklist. All addresses matching the source whitelist must not match the optimised blacklist. You can print a subnet $$$a.b.c.d/32$$$ in any of two ways: as $$$a.b.c.d/32$$$ or as $$$a.b.c.d$$$. If there is more than one solution, output any. | 2,400 | false | true | false | false | true | false | false | false | false | false | 5,366 |
152D | One day Vasya got hold of a sheet of checkered paper _n_u2009×u2009_m_ squares in size. Our Vasya adores geometrical figures, so he painted two rectangles on the paper. The rectangles' sides are parallel to the coordinates' axes, also the length of each side of each rectangle is no less than 3 squares and the sides are painted by the grid lines. The sides can also be part of the sheet of paper's edge. Then Vasya hatched all squares on the rectangles' frames. Let's define a rectangle's frame as the set of squares inside the rectangle that share at least one side with its border. A little later Vasya found a sheet of paper of exactly the same size and couldn't guess whether it is the same sheet of paper or a different one. So, he asked you to check whether the sheet of paper he had found contains two painted frames and nothing besides them. Please note that the frames painted by Vasya can arbitrarily intersect, overlap or even completely coincide. The coordinates on the sheet of paper are introduced in such a way that the _X_ axis goes from top to bottom, the _x_ coordinates of the squares' numbers take values from 1 to _n_ and the _Y_ axis goes from the left to the right and the _y_ coordinates of the squares' numbers take values from 1 to _m_. Input The first input line contains two integers _n_ and _m_ (3u2009≤u2009_n_,u2009_m_u2009≤u20091000) — the sizes of the sheet of paper Vasya found. Next _n_ lines, each consisting of _m_ symbols "." (dot) and "#" (number sign), describe the found sheet of paper. The symbol "#" represents a hatched square and the symbol "." represents a non-hatched square. Output In the first line print the single word "YES" or "NO", meaning whether it is true that the found sheet of paper has two frames painted on it. If the answer is positive, then print in the second line 4 integers: the coordinates of the upper left and lower right corners of the first frame. In the third line print 4 integers: the coordinates of the upper left and the lower right corners of the second frame. If there are multiple answers, print any of them. Examples Input 4 5 ##### #.#.# ###.# ##### Input 5 6 ...### ...### #####. #...#. #####. Note In the first sample there are two frames on the picture. The first one is: ###.. #.#.. ###.. ..... The second one is: ##### #...# #...# ##### In the second sample the painted figures are not frames. Note that the height and width of valid frames is no less than 3. | 2,600 | false | false | false | false | false | false | true | false | false | false | 9,266 |
1110A | You are given an integer $$$n$$$ ($$$n ge 0$$$) represented with $$$k$$$ digits in base (radix) $$$b$$$. So, $$$$$$n = a_1 cdot b^{k-1} + a_2 cdot b^{k-2} + ldots a_{k-1} cdot b + a_k.$$$$$$ For example, if $$$b=17, k=3$$$ and $$$a=[11, 15, 7]$$$ then $$$n=11cdot17^2+15cdot17+7=3179+255+7=3441$$$. Determine whether $$$n$$$ is even or odd. Input The first line contains two integers $$$b$$$ and $$$k$$$ ($$$2le ble 100$$$, $$$1le kle 10^5$$$)xa0— the base of the number and the number of digits. The second line contains $$$k$$$ integers $$$a_1, a_2, ldots, a_k$$$ ($$$0le a_i < b$$$)xa0— the digits of $$$n$$$. The representation of $$$n$$$ contains no unnecessary leading zero. That is, $$$a_1$$$ can be equal to $$$0$$$ only if $$$k = 1$$$. Output Print "even" if $$$n$$$ is even, otherwise print "odd". You can print each letter in any case (upper or lower). Examples Input 10 9 1 2 3 4 5 6 7 8 9 Note In the first example, $$$n = 3 cdot 13^2 + 2 cdot 13 + 7 = 540$$$, which is even. In the second example, $$$n = 123456789$$$ is odd. In the third example, $$$n = 32 cdot 99^4 + 92 cdot 99^3 + 85 cdot 99^2 + 74 cdot 99 + 4 = 3164015155$$$ is odd. In the fourth example $$$n = 2$$$. | 900 | true | false | false | false | false | false | false | false | false | false | 5,149 |
1659E | There is an undirected, connected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. A walk from vertex $$$u$$$ to vertex $$$v$$$ is defined as a sequence of vertices $$$p_1,p_2,ldots,p_k$$$ (which are not necessarily distinct) starting with $$$u$$$ and ending with $$$v$$$, such that $$$p_i$$$ and $$$p_{i+1}$$$ are connected by an edge for $$$1 leq i < k$$$. We define the length of a walk as follows: take the ordered sequence of edges and write down the weights on each of them in an array. Now, write down the bitwise AND of every nonempty prefix of this array. The length of the walk is the MEX of all these values. More formally, let us have $$$ of a set is the smallest non-negative integer that does not belong to the set. For instance: The MEX of $$${2,1}$$$ is $$$0$$$, because $$$0$$$ does not belong to the set. The MEX of $$${3,1,0}$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the set, but $$$2$$$ does not. The MEX of $$${0,3,1,2}$$$ is $$$4$$$ because $$$0$$$, $$$1$$$, $$$2$$$ and $$$3$$$ belong to the set, but $$$4$$$ does not. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 10^5$$$; $$$n-1 leq m leq min{left(frac{n(n-1)}{2},10^5 ight)}$$$). Each of the next $$$m$$$ lines contains three integers $$$a$$$, $$$b$$$, and $$$w$$$ ($$$1 leq a, b leq n$$$, $$$a eq b$$$; $$$0 leq w < 2^{30}$$$) indicating an undirected edge between vertex $$$a$$$ and vertex $$$b$$$ with weight $$$w$$$. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer $$$q$$$ ($$$1 leq q leq 10^5$$$). Each of the next $$$q$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n$$$, $$$u eq v$$$), the description of each query. Output For each query, print one line containing a single integerxa0— the answer to the query. Examples Input 6 7 1 2 1 2 3 3 3 1 5 4 5 2 5 6 4 6 4 6 3 4 1 3 1 5 1 2 5 3 Input 9 8 1 2 5 2 3 11 3 4 10 3 5 10 5 6 2 5 7 1 7 8 5 7 9 5 10 5 7 2 5 7 1 6 4 5 2 7 6 4 1 6 2 4 7 2 8 Output 0 0 2 0 0 2 1 0 1 1 Note The following is an explanation of the first example. The graph in the first example. Here is one possible walk for the first query: $$$$$$1 overset{5}{ ightarrow} 3 overset{3}{ ightarrow} 2 overset{1}{ ightarrow} 1 overset{5}{ ightarrow} 3 overset{1}{ ightarrow} 4 overset{2}{ ightarrow} 5.$$$$$$ The array of weights is $$$w=[5,3,1,5,1,2]$$$. Now if we take the bitwise AND of every prefix of this array, we get the set $$${5,1,0}$$$. The MEX of this set is $$$2$$$. We cannot get a walk with a smaller length (as defined in the statement). | 2,200 | false | false | false | false | false | true | true | false | false | true | 2,328 |
459C | Problem - 459C - 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 constructive algorithms math *1900 No tag edit access → Contest materials . Each day the company provides all the buses for the trip. Pashmak has to arrange the students in the buses. He wants to arrange the students in a way that no two students become close friends. In his ridiculous idea, two students will become close friends if and only if they are in the same buses for all _d_ days. Please help Pashmak with his weird idea. Assume that each bus has an unlimited capacity. Input The first line of input contains three space-separated integers _n_,u2009_k_,u2009_d_ (1u2009≤u2009_n_,u2009_d_u2009≤u20091000;xa01u2009≤u2009_k_u2009≤u2009109). Output If there is no valid arrangement just print -1. Otherwise print _d_ lines, in each of them print _n_ integers. The _j_-th integer of the _i_-th line shows which bus the _j_-th student has to take on the _i_-th day. You can assume that the buses are numbered from 1 to _k_. Examples Input 3 2 2 Output 1 1 2 1 2 1 Input 3 2 1 Output -1 Note Note that two students become close friends only if they share a bus each day. But the bus they share can differ from day to day. | 1,900 | true | false | false | false | false | true | false | false | false | false | 8,004 |
1032F | Vasya has got a tree consisting of $$$n$$$ vertices. He wants to delete some (possibly zero) edges in this tree such that the maximum matching in the resulting graph is unique. He asks you to calculate the number of ways to choose a set of edges to remove. A matching in the graph is a subset of its edges such that there is no vertex incident to two (or more) edges from the subset. A maximum matching is a matching such that the number of edges in the subset is maximum possible among all matchings in this graph. Since the answer may be large, output it modulo $$$998244353$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$) — the number of vertices in the tree. Each of the next $$$n − 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n, u eq v$$$) denoting an edge between vertex $$$u$$$ and vertex $$$v$$$. It is guaranteed that these edges form a tree. Output Print one integer — the number of ways to delete some (possibly empty) subset of edges so that the maximum matching in the resulting graph is unique. Print the answer modulo $$$998244353$$$. Note Possible ways to delete edges in the first example: delete $$$(1, 2)$$$ and $$$(1, 3)$$$. delete $$$(1, 2)$$$ and $$$(1, 4)$$$. delete $$$(1, 3)$$$ and $$$(1, 4)$$$. delete all edges. Possible ways to delete edges in the second example: delete no edges. delete $$$(1, 2)$$$ and $$$(2, 3)$$$. delete $$$(1, 2)$$$ and $$$(3, 4)$$$. delete $$$(2, 3)$$$ and $$$(3, 4)$$$. delete $$$(2, 3)$$$. delete all edges. | 2,400 | false | false | false | true | false | false | false | false | false | false | 5,537 |
1948G | You are given an undirected connected graph on $$$n$$$ vertices. Each edge of this graph has a weight; the weight of the edge connecting vertices $$$i$$$ and $$$j$$$ is $$$w_{i,j}$$$ (or $$$w_{i,j} = 0$$$ if there is no edge between $$$i$$$ and $$$j$$$). All weights are positive integers. You are also given a positive integer $$$c$$$. You have to build a spanning tree of this graph; i.u2009e. choose exactly $$$(n-1)$$$ edges of this graph in such a way that every vertex can be reached from every other vertex by traversing some of the chosen edges. The cost of the spanning tree is the sum of two values: the sum of weights of all chosen edges; the maximum matching in the spanning tree (i.u2009e. the maximum size of a set of edges such that they all belong to the chosen spanning tree, and no vertex has more than one incident edge in this set), multiplied by the given integer $$$c$$$. Find any spanning tree with the minimum cost. Since the graph is connected, there exists at least one spanning tree. Input The first line contains two integers $$$n$$$ and $$$c$$$ ($$$2 le n le 20$$$; $$$1 le c le 10^6$$$). Then $$$n$$$ lines follow. The $$$i$$$-th of them contains $$$n$$$ integers $$$w_{i,1}, w_{i,2}, dots, w_{i,n}$$$ ($$$0 le w_{i,j} le 10^6$$$), where $$$w_{i,j}$$$ denotes the weight of the edge between $$$i$$$ and $$$j$$$ (or $$$w_{i,j} = 0$$$ if there is no such edge). Additional constraints on the input: for every $$$i in [1, n]$$$, $$$w_{i,i} = 0$$$; for every pair of integers $$$(i, j)$$$ such that $$$i in [1, n]$$$ and $$$j in [1, n]$$$, $$$w_{i,j} = w_{j,i}$$$; the given graph is connected. Output Print one integer — the minimum cost of a spanning tree of the given graph. Examples Input 4 10 0 1 8 0 1 0 1 0 8 1 0 2 0 0 2 0 Input 4 5 0 1 8 0 1 0 1 0 8 1 0 2 0 0 2 0 Note In the first example, the minimum cost spanning tree consists of edges $$$(1, 3)$$$, $$$(2, 3)$$$ and $$$(3, 4)$$$. The maximum matching for it is $$$1$$$. In the second example, the minimum cost spanning tree consists of edges $$$(1, 2)$$$, $$$(2, 3)$$$ and $$$(3, 4)$$$. The maximum matching for it is $$$2$$$. | 3,100 | false | false | false | false | false | false | true | false | false | false | 593 |
665F | Problem - 665F - Codeforces =============== xa0 ]( "Educational Codeforces Round 12") — number of integers between 1 and _n_ (inclusive) which has exactly four positive divisors. Between 1 and 10 only the integers 6, 8 and 10 has exactly four positive divisors. So, _D_(10)u2009=u20093. You are given an integer _n_. You have to calculate _D_(_n_). Input The only line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091011) — the parameter from the problem statement. Output Print the only integer _c_ — the number of integers between 1 and _n_ with exactly four divisors. Examples Input 10 Output 3 Input 20 Output 5 | 2,400 | true | false | false | true | true | false | false | false | true | false | 7,156 |
374C | Inna and Dima bought a table of size _n_u2009×u2009_m_ in the shop. Each cell of the table contains a single letter: "D", "I", "M", "A". Inna loves Dima, so she wants to go through his name as many times as possible as she moves through the table. For that, Inna acts as follows: 1. initially, Inna chooses some cell of the table where letter "D" is written; 2. then Inna can move to some side-adjacent table cell that contains letter "I"; then from this cell she can go to one of the side-adjacent table cells that contains the written letter "M"; then she can go to a side-adjacent cell that contains letter "A". Then Inna assumes that she has gone through her sweetheart's name; 3. Inna's next move can be going to one of the side-adjacent table cells that contains letter "D" and then walk on through name DIMA in the similar manner. Inna never skips a letter. So, from the letter "D" she always goes to the letter "I", from the letter "I" she always goes the to letter "M", from the letter "M" she always goes to the letter "A", and from the letter "A" she always goes to the letter "D". Depending on the choice of the initial table cell, Inna can go through name DIMA either an infinite number of times or some positive finite number of times or she can't go through his name once. Help Inna find out what maximum number of times she can go through name DIMA. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009103). Then follow _n_ lines that describe Inna and Dima's table. Each line contains _m_ characters. Each character is one of the following four characters: "D", "I", "M", "A". Note that it is not guaranteed that the table contains at least one letter "D". Output If Inna cannot go through name DIMA once, print on a single line "Poor Dima!" without the quotes. If there is the infinite number of names DIMA Inna can go through, print "Poor Inna!" without the quotes. Otherwise print a single integer — the maximum number of times Inna can go through name DIMA. Examples Input 5 5 DIMAD DIMAI DIMAM DDMAA AAMID Note Notes to the samples: In the first test sample, Inna cannot go through name DIMA a single time. In the second test sample, Inna can go through the infinite number of words DIMA. For that, she should move in the clockwise direction starting from the lower right corner. In the third test sample the best strategy is to start from the cell in the upper left corner of the table. Starting from this cell, Inna can go through name DIMA four times. | 1,900 | false | false | true | true | false | false | false | false | false | true | 8,334 |
1833G | Ksyusha has a pet chinchilla, a tree on $$$n$$$ vertices and huge scissors. A tree is a connected graph without cycles. During a boring physics lesson Ksyusha thought about how to entertain her pet. Chinchillas like to play with branches. A branch is a tree of $$$3$$$ vertices. The branch looks like this. A cut is the removal of some (not yet cut) edge in the tree. Ksyusha has plenty of free time, so she can afford to make enough cuts so that the tree splits into branches. In other words, after several (possibly zero) cuts, each vertex must belong to exactly one branch. Help Ksyusha choose the edges to be cut or tell that it is impossible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of vertices in the tree. The next $$$n - 1$$$ rows of each testcase contain integers $$$v_i$$$ and $$$u_i$$$ ($$$1 le v_i, u_i le n$$$)xa0— the numbers of vertices that the $$$i$$$-th edge connects. It is guaranteed that this set of edges forms a tree. It is also guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. Output Print the answer for each testcase. If the desired way to cut the tree does not exist, print $$$-1$$$. Otherwise, print an integer $$$k$$$xa0— the number of edges to be cut. In the next line, print $$$k$$$ different integers $$$e_i$$$ ($$$1 le e_i < n$$$)xa0— numbers of the edges to be cut. If $$$k = 0$$$, print an empty string instead. If there are several solutions, you can print any. Examples Input 4 9 1 2 4 3 7 9 5 4 4 6 3 2 8 7 1 7 6 1 2 1 3 4 3 1 5 6 1 6 1 2 3 2 3 4 4 5 6 5 5 1 3 5 3 5 2 3 4 Input 4 2 1 2 3 1 2 3 1 6 1 2 3 1 3 4 3 5 6 1 9 2 6 6 9 9 1 9 7 1 8 7 3 8 5 4 7 Note The first testcase in first test. | 1,800 | false | true | true | true | false | true | false | false | false | false | 1,285 |
870C | Problem - 870C - Codeforces =============== xa0 ") ") xa0— the number of queries. _q_ lines follow. The (_i_u2009+u20091)-th line contains single integer _n__i_ (1u2009≤u2009_n__i_u2009≤u2009109)xa0— the _i_-th query. Output For each query print the maximum possible number of summands in a valid splitting to composite summands, or -1, if there are no such splittings. Examples Input 1 12 Output 3 Input 2 6 8 Output 1 2 Input 3 1 2 3 Output -1 -1 -1 Note 12u2009=u20094u2009+u20094u2009+u20094u2009=u20094u2009+u20098u2009=u20096u2009+u20096u2009=u200912, but the first splitting has the maximum possible number of summands. 8u2009=u20094u2009+u20094, 6 can't be split into several composite summands. 1,u20092,u20093 are less than any composite number, so they do not have valid splittings. | 1,300 | true | true | false | true | false | false | false | false | false | false | 6,259 |
817E | As you might remember from the previous round, Vova is currently playing a strategic game known as Rage of Empires. Vova managed to build a large army, but forgot about the main person in the army - the commander. So he tries to hire a commander, and he wants to choose the person who will be respected by warriors. Each warrior is represented by his personality — an integer number _p__i_. Each commander has two characteristics — his personality _p__j_ and leadership _l__j_ (both are integer numbers). Warrior _i_ respects commander _j_ only if ( is the bitwise excluding OR of _x_ and _y_). Initially Vova's army is empty. There are three different types of events that can happen with the army: 1xa0_p__i_ — one warrior with personality _p__i_ joins Vova's army; 2xa0_p__i_ — one warrior with personality _p__i_ leaves Vova's army; 3xa0_p__i_xa0_l__i_ — Vova tries to hire a commander with personality _p__i_ and leadership _l__i_. For each event of the third type Vova wants to know how many warriors (counting only those who joined the army and haven't left yet) respect the commander he tries to hire. Input The first line contains one integer _q_ (1u2009≤u2009_q_u2009≤u2009100000) — the number of events. Then _q_ lines follow. Each line describes the event: 1xa0_p__i_ (1u2009≤u2009_p__i_u2009≤u2009108) — one warrior with personality _p__i_ joins Vova's army; 2xa0_p__i_ (1u2009≤u2009_p__i_u2009≤u2009108) — one warrior with personality _p__i_ leaves Vova's army (it is guaranteed that there is at least one such warrior in Vova's army by this moment); 3xa0_p__i_xa0_l__i_ (1u2009≤u2009_p__i_,u2009_l__i_u2009≤u2009108) — Vova tries to hire a commander with personality _p__i_ and leadership _l__i_. There is at least one event of this type. Output For each event of the third type print one integer — the number of warriors who respect the commander Vova tries to hire in the event. Example Input 5 1 3 1 4 3 6 3 2 4 3 6 3 Note In the example the army consists of two warriors with personalities 3 and 4 after first two events. Then Vova tries to hire a commander with personality 6 and leadership 3, and only one warrior respects him (, and 2u2009<u20093, but , and 5u2009≥u20093). Then warrior with personality 4 leaves, and when Vova tries to hire that commander again, there are no warriors who respect him. | 2,000 | false | false | false | false | true | false | false | false | false | false | 6,489 |
1778D | You are given two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. In each move, the string $$$a$$$ is modified in the following way. An index $$$i$$$ ($$$1 leq i leq n$$$) is chosen uniformly at random. The character $$$a_i$$$ will be flipped. That is, if $$$a_i$$$ is $$$0$$$, it becomes $$$1$$$, and if $$$a_i$$$ is $$$1$$$, it becomes $$$0$$$. What is the expected number of moves required to make both strings equal for the first time? A binary string is a string, in which the character is either $$$ t{0}$$$ or $$$ t{1}$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 10^6$$$)xa0— the length of the strings. The second line of each test case contains the binary string $$$a$$$ of length $$$n$$$. The third line of each test case contains the binary string $$$b$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output a single line containing the expected number of moves modulo $$$998,244,353$$$. Formally, let $$$M = 998,244,353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Input 4 1 0 1 2 00 00 4 1000 1110 5 01001 10111 Output 1 0 665496254 665496277 Note In the first test case, index $$$1$$$ is chosen randomly and $$$a_1$$$ is flipped. After the move, the strings $$$a$$$ and $$$b$$$ are equal. The expected number of moves is $$$1$$$. The strings $$$a$$$ and $$$b$$$ are already equal in the second test case. So, the expected number of moves is $$$0$$$. The expected number of moves for the third and fourth test cases are $$$frac{56}{3}$$$ and $$$frac{125}{3}$$$ respectively. | 2,100 | true | false | false | true | false | false | false | false | false | false | 1,605 |
1325C | Problem - 1325C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms dfs and similar greedy trees *1500 No tag edit access → Contest materials $$$ over all pairs of nodes $$$(u,v)$$$ is as small as possible. Here, $$$MEX(u,v)$$$ denotes the smallest non-negative integer that isn't written on any edge on the unique simple path from node $$$u$$$ to node $$$v$$$. Input The first line contains the integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of nodes in the tree. Each of the next $$$n-1$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) that mean there's an edge between nodes $$$u$$$ and $$$v$$$. It's guaranteed that the given graph is a tree. Output Output $$$n-1$$$ integers. The $$$i^{th}$$$ of them will be the number written on the $$$i^{th}$$$ edge (in the input order). Examples Input 3 1 2 1 3 Output 0 1 Input 6 1 2 1 3 2 4 2 5 5 6 Output 0 3 2 4 1 Note The tree from the second sample: | 1,500 | false | true | false | false | false | true | false | false | false | false | 4,091 |
1344C | Logical quantifiers are very useful tools for expressing claims about a set. For this problem, let's focus on the set of real numbers specifically. The set of real numbers includes zero and negatives. There are two kinds of quantifiers: universal ($$$forall$$$) and existential ($$$exists$$$). You can read more about them :=(x_{j_1}<x_{k_1})land (x_{j_2}<x_{k_2})land cdotsland (x_{j_m}<x_{k_m}), $$$$$$ where $$$land$$$ denotes logical AND. That is, $$$f(x_1,ldots, x_n)$$$ is true if every inequality $$$x_{j_i}<x_{k_i}$$$ holds. Otherwise, if at least one inequality does not hold, then $$$f(x_1,ldots,x_n)$$$ is false. Your task is to assign quantifiers $$$Q_1,ldots,Q_n$$$ to either universal ($$$forall$$$) or existential ($$$exists$$$) so that the statement $$$$$$ Q_1 x_1, Q_2 x_2, ldots, Q_n x_n, f(x_1,ldots, x_n) $$$$$$ is true, and the number of universal quantifiers is maximized, or determine that the statement is false for every possible assignment of quantifiers. Note that the order the variables appear in the statement is fixed. For example, if $$$f(x_1,x_2):=(x_1<x_2)$$$ then you are not allowed to make $$$x_2$$$ appear first and use the statement $$$forall x_2,exists x_1, x_1<x_2$$$. If you assign $$$Q_1=exists$$$ and $$$Q_2=forall$$$, it will only be interpreted as $$$exists x_1,forall x_2,x_1<x_2$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2le nle 2cdot 10^5$$$; $$$1le mle 2cdot 10^5$$$)xa0— the number of variables and the number of inequalities in the formula, respectively. The next $$$m$$$ lines describe the formula. The $$$i$$$-th of these lines contains two integers $$$j_i$$$,$$$k_i$$$ ($$$1le j_i,k_ile n$$$, $$$j_i e k_i$$$). Output If there is no assignment of quantifiers for which the statement is true, output a single integer $$$-1$$$. Otherwise, on the first line output an integer, the maximum possible number of universal quantifiers. On the next line, output a string of length $$$n$$$, where the $$$i$$$-th character is "A" if $$$Q_i$$$ should be a universal quantifier ($$$forall$$$), or "E" if $$$Q_i$$$ should be an existential quantifier ($$$exists$$$). All letters should be upper-case. If there are multiple solutions where the number of universal quantifiers is maximum, print any. Note For the first test, the statement $$$forall x_1, exists x_2, x_1<x_2$$$ is true. Answers of "EA" and "AA" give false statements. The answer "EE" gives a true statement, but the number of universal quantifiers in this string is less than in our answer. For the second test, we can show that no assignment of quantifiers, for which the statement is true exists. For the third test, the statement $$$forall x_1, forall x_2, exists x_3, (x_1<x_3)land (x_2<x_3)$$$ is true: We can set $$$x_3=max{x_1,x_2}+1$$$. | 2,600 | true | false | false | true | false | false | false | false | false | true | 3,985 |
690D3 | So many wall designs to choose from! Even modulo 106u2009+u20093, it's an enormous number. Given that recently Heidi acquired an unlimited supply of bricks, her choices are endless! She really needs to do something to narrow them down. Heidi is quick to come up with criteria for a useful wall: In a useful wall, at least one segment is wider than _W_ bricks. This should give the zombies something to hit their heads against. Or, in a useful wall, at least one column is higher than _H_ bricks. This provides a lookout from which zombies can be spotted at a distance. This should rule out a fair amount of possibilities, right? Help Heidi compute the number of useless walls that do not confirm to either of these criteria. In other words, a wall is useless if every segment has width at most _W_ and height at most _H_. Parameter _C_, the total width of the wall, has the same meaning as in the easy version. However, note that the number of bricks is now unlimited. Output the number of useless walls modulo 106u2009+u20093. Input The first and the only line of the input contains three space-separated integers _C_, _W_ and _H_ (1u2009≤u2009_C_u2009≤u2009108, 1u2009≤u2009_W_,u2009_H_u2009≤u2009100). Output Output the number of different walls, modulo 106u2009+u20093, which are useless according to Heidi's criteria. Note If there is no brick in any of the columns, the structure is considered as a useless wall. | 2,100 | false | false | false | true | false | false | false | false | false | false | 7,053 |
1453A | Gildong's town has a train system that has $$$100$$$ trains that travel from the bottom end to the top end and $$$100$$$ trains that travel from the left end to the right end. The trains starting from each side are numbered from $$$1$$$ to $$$100$$$, respectively, and all trains have the same speed. Let's take a look at the picture below. The train system can be represented as coordinates on a 2D plane. The $$$i$$$-th train starting at the bottom end is initially at $$$(i,0)$$$ and will be at $$$(i,T)$$$ after $$$T$$$ minutes, and the $$$i$$$-th train starting at the left end is initially at $$$(0,i)$$$ and will be at $$$(T,i)$$$ after $$$T$$$ minutes. All trains arrive at their destinations after $$$101$$$ minutes. However, Gildong found that some trains scheduled to depart at a specific time, simultaneously, are very dangerous. At this time, $$$n$$$ trains are scheduled to depart from the bottom end and $$$m$$$ trains are scheduled to depart from the left end. If two trains are both at $$$(x,y)$$$ at the same time for some $$$x$$$ and $$$y$$$, they will crash into each other. Therefore, he is asking you to find the minimum number of trains that should be cancelled to prevent all such crashes. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Each test case contains three lines. The first line of each test case consists of two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 100$$$) — the number of trains scheduled to depart from the bottom end, and the number of trains scheduled to depart from the left end, respectively. The second line of each test case contains $$$n$$$ integers. Each integer is a train number that is scheduled to start from the bottom end. The numbers are given in strictly increasing order, and are between $$$1$$$ and $$$100$$$, inclusive. The third line of each test case contains $$$m$$$ integers. Each integer is a train number that is scheduled to start from the left end. The numbers are given in strictly increasing order, and are between $$$1$$$ and $$$100$$$, inclusive. Output For each test case, print a single integer: the minimum number of trains that should be canceled in order to prevent all crashes. Example Input 3 1 2 1 3 4 3 2 1 3 4 2 4 9 14 2 7 16 28 33 57 59 86 99 3 9 14 19 25 26 28 35 41 59 85 87 99 100 Note In the first case, we can show that there will be no crashes if the current schedule is followed. Therefore, the answer is zero. In the second case, at $$$T=4$$$, there will be a crash, as can be seen in the picture below. We can prove that after canceling one of these trains, the remaining trains will not crash. Therefore, the answer is one. | 800 | false | false | true | false | false | false | false | false | false | false | 3,414 |
1365E | # Maximum Subsequence Value Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Ridhiman challenged Ashish to find the maximum valued subsequence of an array a of size n consisting of positive integers. The value of a non-empty subsequence of k elements of a is defined as ∑ 2i over all integers i ≥ 0 such that at least max(1 , k − 2) elements of the subsequence have the i-th bit set in their binary representation (value x has the i-th bit set in its binary representation if b x > 2i c mod 2 is equal to 1). Recall that b is a subsequence of a, if b can be obtained by deleting some(possibly zero) elements from a.Help Ashish find the maximum value he can get by choosing some subsequence of a. # Input The first line of the input consists of a single integer n (1 ≤ n ≤ 500) x16 the size of a.The next line consists of n space-separated integers x16 the elements of the array (1 ≤ ai ≤ 10 18 ). # Output Print a single integer x16 the maximum value Ashish can get by choosing some subsequence of a. # Examples standard input standard output 32 1 3 333 1 4 711147 7 1 1 7 # Note For the first test case, Ashish can pick the subsequence {2, 3} of size 2. The binary representation of 2 is 10 and that of 3 is 11 . Since max( k − 2, 1) is equal to 1, the value of the subsequence is 20 + 2 1 (both 2 and 3 have 1-st bit set in their binary representation and 3 has 0-th bit set in its binary representation). Note that he could also pick the subsequence {3} or {2, 1, 3}.For the second test case, Ashish can pick the subsequence {3, 4} with value 7.For the third test case, Ashish can pick the subsequence {1} with value 1.For the fourth test case, Ashish can pick the subsequence {7, 7} with value 7. Page 1 of 1 | 1,900 | false | false | false | false | false | true | true | false | false | false | 3,853 |
609F | There are _n_ frogs sitting on the coordinate axis _Ox_. For each frog two values _x__i_,u2009_t__i_ are known — the position and the initial length of the tongue of the _i_-th frog (it is guaranteed that all positions _x__i_ are different). _m_ mosquitoes one by one are landing to the coordinate axis. For each mosquito two values are known _p__j_ — the coordinate of the position where the _j_-th mosquito lands and _b__j_ — the size of the _j_-th mosquito. Frogs and mosquitoes are represented as points on the coordinate axis. The frog can eat mosquito if mosquito is in the same position with the frog or to the right, and the distance between them is not greater than the length of the tongue of the frog. If at some moment several frogs can eat a mosquito the leftmost frog will eat it (with minimal _x__i_). After eating a mosquito the length of the tongue of a frog increases with the value of the size of eaten mosquito. It's possible that after it the frog will be able to eat some other mosquitoes (the frog should eat them in this case). For each frog print two values — the number of eaten mosquitoes and the length of the tongue after landing all mosquitoes and after eating all possible mosquitoes by frogs. Each mosquito is landing to the coordinate axis only after frogs eat all possible mosquitoes landed before. Mosquitoes are given in order of their landing to the coordinate axis. Input First line contains two integers _n_,u2009_m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105) — the number of frogs and mosquitoes. Each of the next _n_ lines contains two integers _x__i_,u2009_t__i_ (0u2009≤u2009_x__i_,u2009_t__i_u2009≤u2009109) — the position and the initial length of the tongue of the _i_-th frog. It is guaranteed that all _x__i_ are different. Next _m_ lines contain two integers each _p__j_,u2009_b__j_ (0u2009≤u2009_p__j_,u2009_b__j_u2009≤u2009109) — the position and the size of the _j_-th mosquito. Output Print _n_ lines. The _i_-th line should contain two integer values _c__i_,u2009_l__i_ — the number of mosquitoes eaten by the _i_-th frog and the length of the tongue of the _i_-th frog. Examples Input 4 6 10 2 15 0 6 1 0 1 110 10 1 1 6 0 15 10 14 100 12 2 | 2,500 | false | true | false | false | true | false | false | false | false | false | 7,407 |
609B | Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are _n_ books on sale from one of _m_ genres. In the bookshop, Jack decides to buy two books of different genres. Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book. The books are given by indices of their genres. The genres are numbered from 1 to _m_. Input The first line contains two positive integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20092·105,u20092u2009≤u2009_m_u2009≤u200910) — the number of books in the bookstore and the number of genres. The second line contains a sequence _a_1,u2009_a_2,u2009...,u2009_a__n_, where _a__i_ (1u2009≤u2009_a__i_u2009≤u2009_m_) equals the genre of the _i_-th book. It is guaranteed that for each genre there is at least one book of that genre. Output Print the only integer — the number of ways in which Jack can choose books. It is guaranteed that the answer doesn't exceed the value 2·109. Note The answer to the first test sample equals 5 as Sasha can choose: 1. the first and second books, 2. the first and third books, 3. the first and fourth books, 4. the second and third books, 5. the third and fourth books. | 1,100 | false | false | true | false | false | true | false | false | false | false | 7,411 |
1997C | Monocarp had a regular bracket sequence $$$s$$$ of length $$$n$$$ ($$$n$$$ is even). He even came up with his own way to calculate its cost. He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distances between pairs of corresponding bracket pairs. For example, let's look at RBS (())(). It has three pairs of brackets: (__)__: the distance between brackets at position $$$1$$$ and at $$$4$$$ is $$$4 - 1 = 3$$$; _()___: the distance is $$$3 - 2 = 1$$$; ____(): the distance is $$$6 - 5 = 1$$$. So the cost of (())() is $$$3 + 1 + 1 = 5$$$. Unfortunately, due to data corruption, Monocarp lost all characters on odd positions $$$s_1, s_3, dots, s_{n-1}$$$. Only characters on even positions ($$$s_2, s_4, dots, s_{n}$$$) remain. For example, (())() turned to _(_)_). Monocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with minimum cost. It's too hard to do for Monocarp alone, so can you help him? Reminder: A regular bracket sequence is a string consisting of only brackets, such that this sequence, when inserted 1-s and +-s, gives a valid mathematical expression. For example, (), (()) or (()())() are RBS, while ), ()( or ())(() are not. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5000$$$)xa0— the number of test cases. Next $$$t$$$ cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$n$$$ is even)xa0— the length of string $$$s$$$. The second line of each test case contains a string $$$s$$$ of length $$$n$$$, where all characters on the odd positions are '_' and all characters on the even positions are either '(' or ')'. Additional constraints: $$$s$$$ can be restored to at least one regular bracket sequence; the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Note In the first test case, it's optimal to make $$$s$$$ equal to (())(). The cost of $$$s$$$ will be equal to $$$3 + 1 + 1 = 5$$$. In the second test case, the only option is to make $$$s$$$ equal to () with cost $$$1$$$. In the third test case, the only possible RBS is ()()()() with cost $$$1 + 1 + 1 + 1 = 4$$$. In the fourth test case, it's optimal to make $$$s$$$ equal to (())(()) with cost $$$3 + 1 + 3 + 1 = 8$$$. | 1,100 | false | true | false | false | true | true | false | false | false | false | 281 |
1844G | You are given a tree with $$$n$$$ nodes labelled $$$1,2,dots,n$$$. The $$$i$$$-th edge connects nodes $$$u_i$$$ and $$$v_i$$$ and has an unknown positive integer weight $$$w_i$$$. To help you figure out these weights, you are also given the distance $$$d_i$$$ between the nodes $$$i$$$ and $$$i+1$$$ for all $$$1 le i le n-1$$$ (the sum of the weights of the edges on the simple path between the nodes $$$i$$$ and $$$i+1$$$ in the tree). Find the weight of each edge. If there are multiple solutions, print any of them. If there are no weights $$$w_i$$$ consistent with the information, print a single integer $$$-1$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$). The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i,v_i le n$$$, $$$u_i e v_i$$$). The last line contains $$$n-1$$$ integers $$$d_1,dots,d_{n-1}$$$ ($$$1 le d_i le 10^{12}$$$). It is guaranteed that the given edges form a tree. Output If there is no solution, print a single integer $$$-1$$$. Otherwise, output $$$n-1$$$ lines containing the weights $$$w_1,dots,w_{n-1}$$$. If there are multiple solutions, print any of them. Examples Input 5 1 2 1 3 2 4 2 5 31 41 59 26 Input 9 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 2 236 205 72 125 178 216 214 117 Output 31 41 59 26 53 58 97 93 Note In the first sample, the tree is as follows: In the second sample, note that $$$w_2$$$ is not allowed to be $$$0$$$ because it must be a positive integer, so there is no solution. In the third sample, the tree is as follows: | 3,000 | true | false | true | false | true | true | false | false | false | false | 1,216 |
1466B | Athenaeus has just finished creating his latest musical composition and will present it tomorrow to the people of Athens. Unfortunately, the melody is rather dull and highly likely won't be met with a warm reception. His song consists of $$$n$$$ notes, which we will treat as positive integers. The diversity of a song is the number of different notes it contains. As a patron of music, Euterpe watches over composers and guides them throughout the process of creating new melodies. She decided to help Athenaeus by changing his song to make it more diverse. Being a minor goddess, she cannot arbitrarily change the song. Instead, for each of the $$$n$$$ notes in the song, she can either leave it as it is or increase it by $$$1$$$. Given the song as a sequence of integers describing the notes, find out the maximal, achievable diversity. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10,000$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow, each one is described in two lines. In the first line of each test case there is a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) denoting the length of the song. The next line contains a sequence of $$$n$$$ integers $$$x_1, x_2, ldots, x_n$$$ $$$(1 leq x_1 leq x_2 leq ldots leq x_n leq 2 cdot n)$$$, describing the song. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, you should output a single line containing precisely one integer, the maximal diversity of the song, i.e. the maximal possible number of different elements in the final sequence. Example Input 5 6 1 2 2 2 5 6 2 4 4 6 1 1 3 4 4 5 1 1 6 1 1 1 2 2 2 Note In the first test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence $$$1, underline{3}, 2, 2, underline{6}, underline{7}$$$, which has $$$5$$$ different elements (increased elements are underlined). In the second test case, Euterpe can increase the first element to obtain the sequence $$$underline{5}, 4$$$, which has $$$2$$$ different elements. In the third test case, Euterpe can increase the second, fifth and sixth element to obtain the sequence $$$1, underline{2}, 3, 4, underline{5}, underline{6}$$$, which has $$$6$$$ different elements. | 800 | false | true | false | true | false | false | false | false | false | false | 3,365 |
527B | Ford Prefect got a job as a web developer for a small company that makes towels. His current work task is to create a search engine for the website of the company. During the development process, he needs to write a subroutine for comparing strings _S_ and _T_ of equal length to be "similar". After a brief search on the Internet, he learned about the Hamming distance between two strings _S_ and _T_ of the same length, which is defined as the number of positions in which _S_ and _T_ have different characters. For example, the Hamming distance between words "permanent" and "pergament" is two, as these words differ in the fourth and sixth letters. Moreover, as he was searching for information, he also noticed that modern search engines have powerful mechanisms to correct errors in the request to improve the quality of search. Ford doesn't know much about human beings, so he assumed that the most common mistake in a request is swapping two arbitrary letters of the string (not necessarily adjacent). Now he wants to write a function that determines which two letters should be swapped in string _S_, so that the Hamming distance between a new string _S_ and string _T_ would be as small as possible, or otherwise, determine that such a replacement cannot reduce the distance between the strings. Help him do this! Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000) — the length of strings _S_ and _T_. The second line contains string _S_. The third line contains string _T_. Each of the lines only contains lowercase Latin letters. Output In the first line, print number _x_ — the minimum possible Hamming distance between strings _S_ and _T_ if you swap at most one pair of letters in _S_. In the second line, either print the indexes _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_, _i_u2009≠u2009_j_), if reaching the minimum possible distance is possible by swapping letters on positions _i_ and _j_, or print "-1 -1", if it is not necessary to swap characters. If there are multiple possible answers, print any of them. Note In the second test it is acceptable to print _i_u2009=u20092, _j_u2009=u20093. | 1,500 | false | true | false | false | false | false | false | false | false | false | 7,723 |
1344E | That's right. I'm a Purdue student, and I shamelessly wrote a problem about trains. There are $$$n$$$ stations and $$$m$$$ trains. The stations are connected by $$$n-1$$$ one-directional railroads that form a tree rooted at station $$$1$$$. All railroads are pointed in the direction from the root station $$$1$$$ to the leaves. A railroad connects a station $$$u$$$ to a station $$$v$$$, and has a distance $$$d$$$, meaning it takes $$$d$$$ time to travel from $$$u$$$ to $$$v$$$. Each station with at least one outgoing railroad has a switch that determines the child station an incoming train will be directed toward. For example, it might look like this: Here, stations $$$1$$$ and $$$3$$$ have switches directed toward stations $$$2$$$ and $$$4$$$, respectively. Initially, no trains are at any station. Train $$$i$$$ will enter station $$$1$$$ at time $$$t_i$$$. Every unit of time, starting at time $$$1$$$, the following two steps happen: 1. You can switch at most one station to point to a different child station. A switch change takes effect before step $$$2$$$. 2. For every train that is on a station $$$u$$$, it is directed toward the station $$$v$$$ indicated by $$$u$$$'s switch. So, if the railroad from $$$u$$$ to $$$v$$$ has distance $$$d$$$, the train will enter station $$$v$$$ in $$$d$$$ units of time from now. Every train has a destination station $$$s_i$$$. When it enters $$$s_i$$$, it will stop there permanently. If at some point the train is going in the wrong direction, so that it will never be able to reach $$$s_i$$$ no matter where the switches point, it will immediately explode. Find the latest possible time of the first explosion if you change switches optimally, or determine that you can direct every train to its destination so that no explosion occurs. Also, find the minimum number of times you need to change a switch to achieve this. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1le n,mle 10^5$$$)xa0— the number of stations and trains, respectively. The next $$$n-1$$$ lines describe the railroads. The $$$i$$$-th line contains three integers $$$u,v,d$$$ ($$$1le u,vle n$$$, $$$1le dle 10^9$$$), denoting a railroad from station $$$u$$$ to station $$$v$$$ with distance $$$d$$$. It is guaranteed that the railroads form a tree rooted at station $$$1$$$. The switch of a station $$$u$$$ is initially directed towards the last outgoing railroad from $$$u$$$ that appears in the input. The next $$$m$$$ lines describe the trains. The $$$i$$$-th line contains two integers $$$s_i,t_i$$$ ($$$1le s_ile n$$$, $$$1le t_1<t_2<cdots<t_mle 10^9$$$)xa0— the destination station and the time the $$$i$$$-th train enters station $$$1$$$, respectively. Output Output two integers: the latest possible time of the first explosion (or $$$-1$$$ if it is possible to never have an explosion) and the minimum number of switch changes to achieve it. Examples Input 5 4 1 2 1 1 3 2 3 4 1 3 5 3 2 1 4 2 2 6 5 10 Input 5 4 1 2 1 1 3 2 3 4 1 3 5 3 5 1 4 2 4 3 2 4 Input 11 6 1 2 1 1 3 2 3 4 1 3 5 2 5 6 1 5 7 2 7 8 1 7 9 2 9 10 1 9 11 1 2 1 8 3 6 5 10 7 4 9 2 11 Note For the first test, here's an example timeline: At time $$$1$$$, train $$$1$$$ enters station $$$1$$$. We switch station $$$1$$$ to point to station $$$2$$$. Train $$$1$$$ is directed to station $$$2$$$. At time $$$2$$$, train $$$2$$$ enters station $$$1$$$, and train $$$1$$$ enters station $$$2$$$, where it stops permanently. We switch station $$$1$$$ to point to station $$$3$$$. Train $$$2$$$ is directed to station $$$3$$$. At time $$$4$$$, train $$$2$$$ enters station $$$3$$$. We switch station $$$3$$$ to point to station $$$4$$$. Train $$$2$$$ is directed to station $$$4$$$. At time $$$5$$$, train $$$2$$$ enters station $$$4$$$, where it stops permanently. At time $$$6$$$, train $$$3$$$ enters station $$$1$$$. We switch station $$$1$$$ to point to station $$$2$$$. Train $$$3$$$ is directed to station $$$2$$$. At time $$$7$$$, train $$$3$$$ enters station $$$2$$$, where it stops permanently. We switch station $$$3$$$ to point to station $$$5$$$. At time $$$10$$$, train $$$4$$$ enters station $$$1$$$. We switch station $$$1$$$ to point to station $$$3$$$. Train $$$4$$$ is directed to station $$$3$$$. At time $$$12$$$, train $$$4$$$ enters station $$$3$$$. Train $$$4$$$ is directed to station $$$5$$$. At time $$$15$$$, train $$$4$$$ enters station $$$5$$$, where it stops permanently. For the second test, we switch nothing. At time $$$4$$$, train $$$2$$$ is directed to station $$$5$$$ and train $$$4$$$ is directed to station $$$3$$$. They both explode. It is impossible to prevent an explosion by time $$$4$$$. For the third test, denote a switch change by $$$(u o v,t)$$$ if we make station $$$u$$$ point to station $$$v$$$ at time $$$t$$$. One solution is to make these $$$4$$$ switch changes: $$$(1 o 2,1)$$$,$$$(1 o 3,2)$$$,$$$(7 o 8,5)$$$,$$$(5 o 6,8)$$$. At time $$$11$$$, trains $$$4$$$,$$$5$$$, and $$$6$$$ explode. It is impossible to prevent an explosion by time $$$11$$$. | 3,100 | false | false | false | false | true | false | false | false | false | false | 3,983 |
1711B | A club plans to hold a party and will invite some of its $$$n$$$ members. The $$$n$$$ members are identified by the numbers $$$1, 2, dots, n$$$. If member $$$i$$$ is not invited, the party will gain an unhappiness value of $$$a_i$$$. There are $$$m$$$ pairs of friends among the $$$n$$$ members. As per tradition, if both people from a friend pair are invited, they will share a cake at the party. The total number of cakes eaten will be equal to the number of pairs of friends such that both members have been invited. However, the club's oven can only cook two cakes at a time. So, the club demands that the total number of cakes eaten is an even number. What is the minimum possible total unhappiness value of the party, respecting the constraint that the total number of cakes eaten is even? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 10^5$$$, $$$0 leq m leq min(10^5,frac{n(n-1)}{2})$$$) — the number of club members and pairs of friends. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, dots,a_n$$$ ($$$0 leq a_i leq 10^4$$$) — the unhappiness value array. Each of the next $$$m$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 leq x,y leq n$$$, $$$x eq y$$$) indicating that $$$x$$$ and $$$y$$$ are friends. Each unordered pair $$$(x,y)$$$ appears at most once in each test case. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$10^5$$$. Output For each test case, print a line containing a single integer – the minimum possible unhappiness value of a valid party. Example Input 4 1 0 1 3 1 2 1 3 1 3 5 5 1 2 3 4 5 1 2 1 3 1 4 1 5 2 3 5 5 1 1 1 1 1 1 2 2 3 3 4 4 5 5 1 Note In the first test case, all members can be invited. So the unhappiness value is $$$0$$$. In the second test case, the following options are possible: invite $$$1$$$ and $$$2$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$3$$$); invite $$$2$$$ and $$$3$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$2$$$); invite only $$$1$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$4$$$); invite only $$$2$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$5$$$); invite only $$$3$$$ ($$$0$$$ cakes eaten, unhappiness value equal to $$$3$$$); invite nobody ($$$0$$$ cakes eaten, unhappiness value equal to $$$6$$$). The minimum unhappiness value is achieved by inviting $$$2$$$ and $$$3$$$. In the third test case, inviting members $$$3,4,5$$$ generates a valid party with the minimum possible unhappiness value. | 1,300 | false | false | false | false | false | false | true | false | false | true | 2,014 |
818A | There are _n_ students who have taken part in an olympiad. Now it's time to award the students. Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and certificates. The number of certificates must be exactly _k_ times greater than the number of diplomas. The number of winners must not be greater than half of the number of all students (i.e. not be greater than half of _n_). It's possible that there are no winners. You have to identify the maximum possible number of winners, according to these rules. Also for this case you have to calculate the number of students with diplomas, the number of students with certificates and the number of students who are not winners. Input The first (and the only) line of input contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u20091012), where _n_ is the number of students and _k_ is the ratio between the number of certificates and the number of diplomas. Output Output three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible. It's possible that there are no winners. Examples Output 83333333333 416666666665 500000000002 Input 1000000000000 499999999999 Output 1 499999999999 500000000000 | 800 | true | false | true | false | false | false | false | false | false | false | 6,487 |
1560E | Polycarp has a string $$$s$$$. Polycarp performs the following actions until the string $$$s$$$ is empty ($$$t$$$ is initially an empty string): he adds to the right to the string $$$t$$$ the string $$$s$$$, i.e. he does $$$t = t + s$$$, where $$$t + s$$$ is a concatenation of the strings $$$t$$$ and $$$s$$$; he selects an arbitrary letter of $$$s$$$ and removes from $$$s$$$ all its occurrences (the selected letter must occur in the string $$$s$$$ at the moment of performing this action). Polycarp performs this sequence of actions strictly in this order. Note that after Polycarp finishes the actions, the string $$$s$$$ will be empty and the string $$$t$$$ will be equal to some value (that is undefined and depends on the order of removing). E.g. consider $$$s$$$="abacaba" so the actions may be performed as follows: $$$t$$$="abacaba", the letter 'b' is selected, then $$$s$$$="aacaa"; $$$t$$$="abacabaaacaa", the letter 'a' is selected, then $$$s$$$="c"; $$$t$$$="abacabaaacaac", the letter 'c' is selected, then $$$s$$$="" (the empty string). You need to restore the initial value of the string $$$s$$$ using only the final value of $$$t$$$ and find the order of removing letters from $$$s$$$. 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 contains one string $$$t$$$ consisting of lowercase letters of the Latin alphabet. The length of $$$t$$$ doesn't exceed $$$5 cdot 10^5$$$. The sum of lengths of all strings $$$t$$$ in the test cases doesn't exceed $$$5 cdot 10^5$$$. Output For each test case output in a separate line: $$$-1$$$, if the answer doesn't exist; two strings separated by spaces. The first one must contain a possible initial value of $$$s$$$. The second one must contain a sequence of letters — it's in what order one needs to remove letters from $$$s$$$ to make the string $$$t$$$. E.g. if the string "bac" is outputted, then, first, all occurrences of the letter 'b' were deleted, then all occurrences of 'a', and then, finally, all occurrences of 'c'. If there are multiple solutions, print any one. Example Input 7 abacabaaacaac nowyouknowthat polycarppoycarppoyarppyarppyrpprppp isi everywherevrywhrvryhrvrhrvhv haaha qweqeewew Output abacaba bac -1 polycarp lcoayrp is si everywhere ewyrhv -1 -1 Note The first test case is considered in the statement. | 1,800 | false | false | true | false | false | false | false | true | true | false | 2,826 |
1929B | Even in kindergarten, Sasha liked a girl. Therefore, he wanted to give her a drawing and attract her attention. As a drawing, he decided to draw a square grid of size $$$n imes n$$$, in which some cells are colored. But coloring the cells is difficult, so he wants to color as few cells as possible. But at the same time, he wants at least $$$k$$$ diagonals to have at least one colored cell. Note that the square grid of size $$$n imes n$$$ has a total of $$$4n - 2$$$ diagonals. Help little Sasha to make the girl fall in love with him and tell him the minimum number of cells he needs to color. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 10^8$$$, $$$1 leq k leq 4n - 2$$$) — the size of the square grid and the minimum number of diagonals in which there should be at least one colored cell. Output For each test case, output a single integer — the minimum number of cells that need to be colored. Example Input 7 3 4 3 3 3 10 3 9 4 7 7 11 2 3 Note In the pictures below, the colored cells are marked in black, and all diagonals are marked in purple. In the first test case, you can color $$$2$$$ cells so that $$$4$$$ diagonals contain at least one colored cell: In the third test case, you can color $$$6$$$ cells so that all $$$10$$$ diagonals contain at least one colored cell: | 800 | true | true | false | false | false | true | false | false | false | false | 716 |
111D | Problem - 111D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp *2300 No tag edit access → Contest materials in _k_ colors. Besides, the coloring should have the following property: for any vertical line that passes along the grid lines and divides the board in two non-empty parts the number of distinct colors in both these parts should be the same. Help Petya to count these colorings. Input The first line contains space-separated integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000,u20091u2009≤u2009_k_u2009≤u2009106) — the board's vertical and horizontal sizes and the number of colors respectively. Output Print the answer to the problem. As the answer can be quite a large number, you should print it modulo 109u2009+u20097 (1000000007). Examples Input 2 2 1 Output 1 Input 2 2 2 Output 8 Input 3 2 2 Output 40 | 2,300 | false | false | false | true | false | false | false | false | false | false | 9,439 |
720A | The closing ceremony of Squanch Code Cup is held in the big hall with _n_u2009×u2009_m_ seats, arranged in _n_ rows, _m_ seats in a row. Each seat has two coordinates (_x_,u2009_y_) (1u2009≤u2009_x_u2009≤u2009_n_, 1u2009≤u2009_y_u2009≤u2009_m_). There are two queues of people waiting to enter the hall: _k_ people are standing at (0,u20090) and _n_·_m_u2009-u2009_k_ people are standing at (0,u2009_m_u2009+u20091). Each person should have a ticket for a specific seat. If person _p_ at (_x_,u2009_y_) has ticket for seat (_x__p_,u2009_y__p_) then he should walk _x_u2009-u2009_x__p_u2009+u2009_y_u2009-u2009_y__p_ to get to his seat. Each person has a staminaxa0— the maximum distance, that the person agrees to walk. You should find out if this is possible to distribute all _n_·_m_ tickets in such a way that each person has enough stamina to get to their seat. Input The first line of input contains two integers _n_ and _m_ (1u2009≤u2009_n_·_m_u2009≤u2009104)xa0— the size of the hall. The second line contains several integers. The first integer _k_ (0u2009≤u2009_k_u2009≤u2009_n_·_m_)xa0— the number of people at (0,u20090). The following _k_ integers indicate stamina of each person there. The third line also contains several integers. The first integer _l_ (_l_u2009=u2009_n_·_m_u2009-u2009_k_)xa0— the number of people at (0,u2009_m_u2009+u20091). The following _l_ integers indicate stamina of each person there. The stamina of the person is a positive integer less that or equal to _n_u2009+u2009_m_. Output If it is possible to distribute tickets between people in the described manner print "YES", otherwise print "NO". | 2,000 | false | true | false | false | false | false | false | false | false | false | 6,937 |
1867D | Egor has an array $$$a$$$ of length $$$n$$$, initially consisting of zeros. However, he wanted to turn it into another array $$$b$$$ of length $$$n$$$. Since Egor doesn't take easy paths, only the following operation can be used (possibly zero or several times): choose an array $$$l$$$ of length $$$k$$$ ($$$1 leq l_i leq n$$$, all $$$l_i$$$ are distinct) and change each element $$$a_{l_i}$$$ to $$$l_{(i%k)+1}$$$ ($$$1 leq i leq k$$$). He became interested in whether it is possible to get the array $$$b$$$ using only these operations. Since Egor is still a beginner programmer, he asked you to help him solve this problem. The operation $$$%$$$ means taking the remainder, that is, $$$a%b$$$ is equal to the remainder of dividing the number $$$a$$$ by the number $$$b$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 10^5$$$) - the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq k leq n leq 10^5$$$). The second line contains the array $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq n$$$). 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 "YES" (without quotes) if there is a way to get the array $$$b$$$ using only the given operation. Otherwise, output "NO" (without quotes). You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes" and "YES" will be accepted as a positive answer. Example Input 6 5 3 2 3 5 3 4 4 2 2 4 3 1 1 1 1 3 1 1 2 3 5 3 5 4 3 2 1 6 1 1 2 3 1 5 6 Output YES NO YES YES NO NO Note Let's consider the first example: Apply the operation with $$$l$$$ = $$$[1,2,3]$$$. Now $$$a$$$ = $$$[2,3,1,0,0]$$$. Apply the operation with $$$l$$$ = $$$[3,5,4]$$$. Now $$$a$$$ = $$$[2,3,5,3,4]$$$ = $$$b$$$. We see that it is possible to get the array $$$b$$$. Therefore, the answer is YES. In the second example, it can be proven that the array $$$b$$$ cannot be obtained, therefore the answer is NO. | 1,800 | false | true | true | false | false | true | true | false | false | true | 1,072 |
582A | Problem - 582A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms greedy number theory *1700 No tag edit access → Contest materials of two positive integers _x_ and _y_ is the greatest integer that is divisor of both _x_ and _y_, it is denoted as . For example, for array _a_u2009=u2009{4,u20093,u20096,u20092} of length 4 the GCD table will look as follows: Given all the numbers of the GCD table _G_, restore array _a_. Input The first line contains number _n_ (1u2009≤u2009_n_u2009≤u2009500) — the length of array _a_. The second line contains _n_2 space-separated numbers — the elements of the GCD table of _G_ for array _a_. All the numbers in the table are positive integers, not exceeding 109. Note that the elements are given in an arbitrary order. It is guaranteed that the set of the input data corresponds to some array _a_. Output In the single line print _n_ positive integers — the elements of array _a_. If there are multiple possible solutions, you are allowed to print any of them. Examples Input 4 2 1 2 3 4 3 2 6 1 1 2 2 1 2 3 2 Output 4 3 6 2 Input 1 42 Output 42 Input 2 1 1 1 1 Output 1 1 | 1,700 | false | true | false | false | false | true | false | false | false | false | 7,517 |
1527D | You are given a tree with $$$n$$$ nodes, numerated from $$$0$$$ to $$$n-1$$$. For each $$$k$$$ between $$$0$$$ and $$$n$$$, inclusive, you have to count the number of unordered pairs $$$(u,v)$$$, $$$u eq v$$$, such that the MEX of all the node labels in the shortest path from $$$u$$$ to $$$v$$$ (including end points) is $$$k$$$. The MEX of a sequence of integers is the smallest non-negative integer that does not belong to the sequence. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^{5}$$$). The next $$$n-1$$$ lines of each test case describe the tree that has to be constructed. These lines contain two integers $$$u$$$ and $$$v$$$ ($$$0 le u,v le n-1$$$) denoting an edge between $$$u$$$ and $$$v$$$ ($$$u eq v$$$). It is guaranteed that the given edges form a tree. It is also guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^{5}$$$. Output For each test case, print $$$n+1$$$ integers: the number of paths in the tree, such that the MEX of all the node labels in that path is $$$k$$$ for each $$$k$$$ from $$$0$$$ to $$$n$$$. Example Input 2 4 0 1 0 2 2 3 2 1 0 Note 1. In example case $$$1$$$, For $$$k = 0$$$, there is $$$1$$$ path that is from $$$2$$$ to $$$3$$$ as $$$MEX([2, 3]) = 0$$$. For $$$k = 1$$$, there are $$$2$$$ paths that is from $$$0$$$ to $$$2$$$ as $$$MEX([0, 2]) = 1$$$ and $$$0$$$ to $$$3$$$ as $$$MEX([0, 2, 3]) = 1$$$. For $$$k = 2$$$, there is $$$1$$$ path that is from $$$0$$$ to $$$1$$$ as $$$MEX([0, 1]) = 2$$$. For $$$k = 3$$$, there is $$$1$$$ path that is from $$$1$$$ to $$$2$$$ as $$$MEX([1, 0, 2]) = 3$$$ For $$$k = 4$$$, there is $$$1$$$ path that is from $$$1$$$ to $$$3$$$ as $$$MEX([1, 0, 2, 3]) = 4$$$. 2. In example case $$$2$$$, For $$$k = 0$$$, there are no such paths. For $$$k = 1$$$, there are no such paths. For $$$k = 2$$$, there is $$$1$$$ path that is from $$$0$$$ to $$$1$$$ as $$$MEX([0, 1]) = 2$$$. | 2,400 | true | false | true | false | false | false | false | false | false | false | 3,025 |
182E | Vasya has recently bought some land and decided to surround it with a wooden fence. He went to a company called "Wooden board" that produces wooden boards for fences. Vasya read in the catalog of products that the company has at its disposal _n_ different types of wood. The company uses the _i_-th type of wood to produce a board of this type that is a rectangular _a__i_ by _b__i_ block. Vasya decided to order boards in this company and build a fence from them. It turned out that the storehouse of the company is so large that Vasya can order arbitrary number of boards of every type. Note that Vasya is allowed to turn the boards as he builds the fence. However, Vasya cannot turn square boards. Vasya is required to construct a fence of length _l_, however, an arbitrary fence won't do. Vasya wants his fence to look beautiful. We'll say that a fence is beautiful if and only if the following two conditions are fulfilled: there are no two successive boards of the same type the first board of the fence has an arbitrary length, and the length of each subsequent board equals the width of the previous one In other words, the fence is considered beautiful, if the type of the _i_-th board in the fence is different from the _i_u2009-u20091-th board's type; besides, the _i_-th board's length is equal to the _i_u2009-u20091-th board's width (for all _i_, starting from 2). Now Vasya wonders, how many variants of arranging a fence for his land exist. Your task is to count the number of different beautiful fences of length _l_. Two fences will be considered the same if the corresponding sequences of fence boards types and rotations are the same, otherwise the fences are different. Since the sought number can be large enough, you need to calculate the answer modulo 1000000007 (109u2009+u20097). Input The first line contains two integers _n_ and _l_ (1u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_l_u2009≤u20093000) — the number of different board types and the fence length, correspondingly. Next _n_ lines contain descriptions of board types: the _i_-th line contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009100) — the sizes of the board of the _i_-th type. All numbers on the lines are separated by spaces. | 1,800 | false | false | false | true | false | false | false | false | false | false | 9,110 |
1828A | You are given a positive integer $$$n$$$. Please find an array $$$a_1, a_2, ldots, a_n$$$ that is perfect. A perfect array $$$a_1, a_2, ldots, a_n$$$ satisfies the following criteria: $$$1 le a_i le 1000$$$ for all $$$1 le i le n$$$. $$$a_i$$$ is divisible by $$$i$$$ for all $$$1 le i le n$$$. $$$a_1 + a_2 + ldots + a_n$$$ is divisible by $$$n$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 200$$$). The description of the test cases follows. The only line of each test case contains a single positive integer $$$n$$$ ($$$1 le n le 200$$$)xa0— the length of the array $$$a$$$. Output For each test case, output an array $$$a_1, a_2, ldots, a_n$$$ that is perfect. We can show that an answer always exists. If there are multiple solutions, print any. Example Output 1 2 4 1 2 3 2 8 6 4 3 4 9 4 5 1 10 18 8 5 36 3 6 21 24 10 6 14 Note In the third test case: $$$a_1 = 1$$$ is divisible by $$$1$$$. $$$a_2 = 2$$$ is divisible by $$$2$$$. $$$a_3 = 3$$$ is divisible by $$$3$$$. $$$a_1 + a_2 + a_3 = 1 + 2 + 3 = 6$$$ is divisible by $$$3$$$. In the fifth test case: $$$a_1 = 3$$$ is divisible by $$$1$$$. $$$a_2 = 4$$$ is divisible by $$$2$$$. $$$a_3 = 9$$$ is divisible by $$$3$$$. $$$a_4 = 4$$$ is divisible by $$$4$$$. $$$a_5 = 5$$$ is divisible by $$$5$$$. $$$a_1 + a_2 + a_3 + a_4 + a_5 = 3 + 4 + 9 + 4 + 5 = 25$$$ is divisible by $$$5$$$. | 800 | true | false | false | false | false | true | false | false | false | false | 1,316 |
1669H | Let $$$mathsf{AND}$$$ denote the and replace $$$a_i$$$ with $$$a_i$$$ $$$mathsf{OR}$$$ $$$2^j$$$ where $$$j$$$ is any integer between $$$0$$$ and $$$30$$$ inclusive. In other words, in an operation you can choose an index $$$i$$$ ($$$1 leq i leq n$$$) and set the $$$j$$$-th bit of $$$a_i$$$ to $$$1$$$ ($$$0 leq j leq 30$$$). Output the maximum possible value of $$$a_1$$$ $$$mathsf{AND}$$$ $$$a_2$$$ $$$mathsf{AND}$$$ $$$dots$$$ $$$mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le k le 10^9$$$). Then a single line follows, containing $$$n$$$ integers describing the arrays $$$a$$$ ($$$0 leq a_i < 2^{31}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single line containing the maximum possible $$$mathsf{AND}$$$ value of $$$a_1$$$ $$$mathsf{AND}$$$ $$$a_2$$$ $$$mathsf{AND}$$$ $$$dots$$$ $$$mathsf{AND}$$$ $$$a_n$$$ after performing at most $$$k$$$ operations. Example Input 4 3 2 2 1 1 7 0 4 6 6 28 6 6 12 1 30 0 4 4 3 1 3 1 Output 2 4 2147483646 1073741825 Note For the first test case, we can set the bit $$$1$$$ ($$$2^1$$$) of the last $$$2$$$ elements using the $$$2$$$ operations, thus obtaining the array [$$$2$$$, $$$3$$$, $$$3$$$], which has $$$mathsf{AND}$$$ value equal to $$$2$$$. For the second test case, we can't perform any operations so the answer is just the $$$mathsf{AND}$$$ of the whole array which is $$$4$$$. | 1,300 | true | true | false | false | false | false | false | false | false | false | 2,258 |
702E | You are given a functional graph. It is a directed graph, in which from each vertex goes exactly one arc. The vertices are numerated from 0 to _n_u2009-u20091. Graph is given as the array _f_0,u2009_f_1,u2009...,u2009_f__n_u2009-u20091, where _f__i_ — the number of vertex to which goes the only arc from the vertex _i_. Besides you are given array with weights of the arcs _w_0,u2009_w_1,u2009...,u2009_w__n_u2009-u20091, where _w__i_ — the arc weight from _i_ to _f__i_. The graph from the first sample test. Also you are given the integer _k_ (the length of the path) and you need to find for each vertex two numbers _s__i_ and _m__i_, where: _s__i_ — the sum of the weights of all arcs of the path with length equals to _k_ which starts from the vertex _i_; _m__i_ — the minimal weight from all arcs on the path with length _k_ which starts from the vertex _i_. The length of the path is the number of arcs on this path. Input The first line contains two integers _n_,u2009_k_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_k_u2009≤u20091010). The second line contains the sequence _f_0,u2009_f_1,u2009...,u2009_f__n_u2009-u20091 (0u2009≤u2009_f__i_u2009<u2009_n_) and the third — the sequence _w_0,u2009_w_1,u2009...,u2009_w__n_u2009-u20091 (0u2009≤u2009_w__i_u2009≤u2009108). Output Print _n_ lines, the pair of integers _s__i_, _m__i_ in each line. Examples Input 7 3 1 2 3 4 3 2 6 6 3 1 4 2 2 3 Output 10 1 8 1 7 1 10 2 8 2 7 1 9 3 Input 5 3 1 2 3 4 0 4 1 2 14 3 | 2,100 | false | false | false | false | true | false | false | false | false | true | 7,013 |
374B | Inna loves digit 9 very much. That's why she asked Dima to write a small number consisting of nines. But Dima must have misunderstood her and he wrote a very large number _a_, consisting of digits from 1 to 9. Inna wants to slightly alter the number Dima wrote so that in the end the number contained as many digits nine as possible. In one move, Inna can choose two adjacent digits in a number which sum equals 9 and replace them by a single digit 9. For instance, Inna can alter number 14545181 like this: 14545181u2009→u20091945181u2009→u2009194519u2009→u200919919. Also, she can use this method to transform number 14545181 into number 19991. Inna will not transform it into 149591 as she can get numbers 19919 and 19991 which contain more digits nine. Dima is a programmer so he wants to find out how many distinct numbers containing as many digits nine as possible Inna can get from the written number. Help him with this challenging task. Input The first line of the input contains integer _a_ (1u2009≤u2009_a_u2009≤u200910100000). Number _a_ doesn't have any zeroes. Output In a single line print a single integer — the answer to the problem. It is guaranteed that the answer to the problem doesn't exceed 263u2009-u20091. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Note Notes to the samples In the first sample Inna can get the following numbers: 369727u2009→u200999727u2009→u20099997, 369727u2009→u200999727u2009→u20099979. In the second sample, Inna can act like this: 123456789987654321u2009→u200912396789987654321u2009→u20091239678998769321. | 1,500 | false | true | false | false | false | false | false | false | false | false | 8,335 |
1037F | Given an array $$$a$$$ of $$$n$$$ integers and an integer $$$k$$$ ($$$2 le k le n$$$), where each element of the array is denoted by $$$a_i$$$ ($$$0 le i < n$$$). Perform the operation $$$z$$$ given below on $$$a$$$ and print the value of $$$z(a,k)$$$ modulo $$$10^{9}+7$$$. function z(array a, integer k): if length(a) < k: return 0 else: b = empty array ans = 0 for i = 0 .. (length(a) - k): temp = a[i] for j = i .. (i + k - 1): temp = max(temp, a[j]) append temp to the end of b ans = ans + temp return ans + z(b, k) Input The first line of input contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 10^6$$$)xa0— the length of the initial array $$$a$$$ and the parameter $$$k$$$. The second line of input contains $$$n$$$ integers $$$a_0, a_1, ldots, a_{n - 1}$$$ ($$$1 le a_{i} le 10^9$$$)xa0— the elements of the array $$$a$$$. Output Output the only integer, the value of $$$z(a,k)$$$ modulo $$$10^9+7$$$. Note In the first example: for $$$a=(9,1,10)$$$, $$$ans=19$$$ and $$$b=(9,10)$$$, for $$$a=(9,10)$$$, $$$ans=10$$$ and $$$b=(10)$$$, for $$$a=(10)$$$, $$$ans=0$$$. So the returned value is $$$19+10+0=29$$$. In the second example: for $$$a=(5,8,7,1,9)$$$, $$$ans=25$$$ and $$$b=(8,8,9)$$$, for $$$a=(8,8,9)$$$, $$$ans=9$$$ and $$$b=(9)$$$, for $$$a=(9)$$$, $$$ans=0$$$. So the returned value is $$$25+9+0=34$$$. | 2,500 | true | false | false | false | true | false | false | false | false | false | 5,511 |
1660F2 | This is the hard version of Problem F. The only difference between the easy version and the hard version is the constraints. We will call a non-empty string balanced if it contains the same number of plus and minus signs. For example: strings "+--+" and "++-+--" are balanced, and strings "+--", "--" and "" are not balanced. We will call a string promising if the string can be made balanced by several (possibly zero) uses of the following operation: replace two adjacent minus signs with one plus sign. In particular, every balanced string is promising. However, the converse is not true: not every promising string is balanced. For example, the string "-+---" is promising, because you can replace two adjacent minuses with plus and get a balanced string "-++-", or get another balanced string "-+-+". How many non-empty substrings of the given string $$$s$$$ are promising? Each non-empty promising substring must be counted in the answer as many times as it occurs in string $$$s$$$. Recall that a substring is a sequence of consecutive characters of the string. For example, for string "+-+" its substring are: "+-", "-+", "+", "+-+" (the string is a substring of itself) and some others. But the following strings are not its substring: "--", "++", "-++". Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0—the number of test cases in the test. Then the descriptions of test cases follow. Each test case of input data consists of two lines. The first line consists of the number $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$): the length of $$$s$$$. The second line of the test case contains the string $$$s$$$ of length $$$n$$$, consisting only of characters "+" and "-". It is guaranteed that the sum of values $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single number: the number of the promising non-empty substrings of string $$$s$$$. Each non-empty promising substring must be counted in the answer as many times as it occurs in string $$$s$$$. | 2,100 | true | false | true | false | true | false | false | false | false | false | 2,320 |
1845E | There are $$$n$$$ boxes placed in a line. The boxes are numbered from $$$1$$$ to $$$n$$$. Some boxes contain one ball inside of them, the rest are empty. At least one box contains a ball and at least one box is empty. In one move, you have to choose a box with a ball inside and an adjacent empty box and move the ball from one box into another. Boxes $$$i$$$ and $$$i+1$$$ for all $$$i$$$ from $$$1$$$ to $$$n-1$$$ are considered adjacent to each other. Boxes $$$1$$$ and $$$n$$$ are not adjacent. How many different arrangements of balls exist after exactly $$$k$$$ moves are performed? Two arrangements are considered different if there is at least one such box that it contains a ball in one of them and doesn't contain a ball in the other one. Since the answer might be pretty large, print its remainder modulo $$$10^9+7$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 1500$$$; $$$1 le k le 1500$$$)xa0— the number of boxes and the number of moves. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$a_i in {0, 1}$$$)xa0— $$$0$$$ denotes an empty box and $$$1$$$ denotes a box with a ball inside. There is at least one $$$0$$$ and at least one $$$1$$$. Output Print a single integerxa0— the number of different arrangements of balls that can exist after exactly $$$k$$$ moves are performed, modulo $$$10^9+7$$$. Examples Input 10 6 1 0 0 1 0 0 0 1 1 1 Note In the first example, there are the following possible arrangements: 0 1 1 0xa0— obtained after moving the ball from box $$$1$$$ to box $$$2$$$; 1 0 0 1xa0— obtained after moving the ball from box $$$3$$$ to box $$$4$$$; 1 1 0 0xa0— obtained after moving the ball from box $$$3$$$ to box $$$2$$$. In the second example, there are the following possible arrangements: 1 0 1 0xa0— three ways to obtain that: just reverse the operation performed during the first move; 0 1 0 1xa0— obtained from either of the first two arrangements after the first move. | 2,500 | true | false | true | true | false | false | false | false | false | false | 1,210 |
1060B | Problem - 1060B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy *1100 No tag edit access → Contest materials ") ") $$$ be sum of digits in base 10 representation of $$$x$$$, for example, $$$S(123) = 1 + 2 + 3 = 6$$$, $$$S(0) = 0$$$. Your task is to find two integers $$$a, b$$$, such that $$$0 leq a, b leq n$$$, $$$a + b = n$$$ and $$$S(a) + S(b)$$$ is the largest possible among all such pairs. Input The only line of input contains an integer $$$n$$$ $$$(1 leq n leq 10^{12})$$$. Output Print largest $$$S(a) + S(b)$$$ among all pairs of integers $$$a, b$$$, such that $$$0 leq a, b leq n$$$ and $$$a + b = n$$$. Examples Input 35 Output 17 Input 10000000000 Output 91 Note In the first example, you can choose, for example, $$$a = 17$$$ and $$$b = 18$$$, so that $$$S(17) + S(18) = 1 + 7 + 1 + 8 = 17$$$. It can be shown that it is impossible to get a larger answer. In the second test example, you can choose, for example, $$$a = 5000000001$$$ and $$$b = 4999999999$$$, with $$$S(5000000001) + S(4999999999) = 91$$$. It can be shown that it is impossible to get a larger answer. | 1,100 | false | true | false | false | false | false | false | false | false | false | 5,415 |
1814E | You are given an undirected graph consisting of $$$n$$$ vertices and $$$n-1$$$ edges. The $$$i$$$-th edge has weight $$$a_i$$$; it connects the vertices $$$i$$$ and $$$i+1$$$. Initially, each vertex contains a chip. Each chip has an integer written on it; the integer written on the chip in the $$$i$$$-th vertex is $$$i$$$. In one operation, you can choose a chip (if there are multiple chips in a single vertex, you may choose any one of them) and move it along one of the edges of the graph. The cost of this operation is equal to the weight of the edge. The cost of the graph is the minimum cost of a sequence of such operations that meets the following condition: after all operations are performed, each vertex contains exactly one chip, and the integer on each chip is not equal to the index of the vertex where that chip is located. You are given $$$q$$$ queries of the form: $$$k$$$ $$$x$$$ — change the weight of the $$$k$$$-th edge (the one which connects the vertices $$$k$$$ and $$$k+1$$$) to $$$x$$$. After each query, print the cost of the graph. Note that you don't actually move any chips; when you compute the cost, the chips are on their initial positions. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line contains $$$n-1$$$ integers $$$a_1, a_2, dots, a_{n-1}$$$ ($$$1 le a_i le 10^9$$$). The third line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$). Then $$$q$$$ lines follow. The $$$i$$$-th of them contains two integers $$$k$$$ and $$$x$$$ ($$$1 le k le n-1$$$; $$$1 le x le 10^9$$$) for the $$$i$$$-th query. Output For each query, print one integer — the cost of the graph after the query is performed. Example Input 10 12 6 12 15 20 8 17 12 15 8 4 10 7 3 6 14 9 9 2 10 3 5 4 11 7 11 Output 126 108 120 108 112 98 98 114 | 2,300 | false | false | false | true | true | false | false | false | false | false | 1,383 |
1763F | You are given an undirected, connected graph of $$$n$$$ nodes and $$$m$$$ edges. All nodes $$$u$$$ of the graph satisfy the following: Let $$$S_u$$$ be the set of vertices in the longest simple cycle starting and ending at $$$u$$$. Let $$$C_u$$$ be the union of the sets of vertices in any simple cycle starting and ending at $$$u$$$. $$$S_u = C_u$$$. You need to answer $$$q$$$ queries. For each query, you will be given node $$$a$$$ and node $$$b$$$. Out of all the edges that belong to any simple path from $$$a$$$ to $$$b$$$, count the number of edges such that if you remove that edge, $$$a$$$ and $$$b$$$ are reachable from each other. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m le min$$$($$$2 cdot 10^5$$$, $$$(n cdot (n-1))/2$$$)) — the total number of nodes and edges in the graph, respectively. The next $$$m$$$ lines contain two integers $$$u$$$ and $$$v$$$ ($$$1 le$$$ $$$u$$$, $$$v$$$ $$$le n$$$, $$$u eq v$$$) — describing an edge, implying that nodes $$$u$$$ and $$$v$$$ are connected to each other. It is guaranteed that there is at most one edge between any pair of vertices in the graph and the given graph is connected. The next line contains a single integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each representing a query. Each query contains two integers $$$a$$$ and $$$b$$$ ($$$1 le$$$ $$$a$$$, $$$b$$$ $$$le n$$$). Output For each query, output a single integer — answer to the query. Examples Input 10 11 1 2 2 3 3 4 4 5 5 3 2 7 7 9 9 10 10 6 6 7 1 8 5 1 4 5 10 3 5 2 8 7 10 Input 13 15 1 2 2 3 3 4 4 1 2 4 3 5 5 6 6 7 6 8 7 9 9 10 8 7 10 11 10 12 10 13 6 9 11 1 5 1 8 5 2 5 12 12 13 Note The graph in the first sample is : The first query is $$$(1, 4)$$$. There are $$$5$$$ total edges that belong to any simple path from $$$1$$$ to $$$4$$$. Edges $$$(3, 4), (4, 5), (5, 3)$$$ will be counted in the answer to the query. The fourth query is $$$(2, 8)$$$. There is only one simple path from $$$2$$$ to $$$8$$$, thus none of the edges will be counted in the answer to the query. The fifth query is $$$(7, 10)$$$. There are $$$4$$$ total edges that belong to any simple path from $$$7$$$ to $$$10$$$, all of them will be counted in the answer to the query. | 3,000 | false | false | false | true | true | false | false | false | false | true | 1,728 |
713A | Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her _t_ queries, each of one of the following type: 1. u2009+u2009 _a__i_xa0— add non-negative integer _a__i_ to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. u2009-u2009 _a__i_xa0— delete a single occurrence of non-negative integer _a__i_ from the multiset. It's guaranteed, that there is at least one _a__i_ in the multiset. 3. ? _s_xa0— count the number of integers in the multiset (with repetitions) that match some pattern _s_ consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer _x_ matches the pattern _s_, if the parity of the _i_-th from the right digit in decimal notation matches the _i_-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is _s_u2009=u2009010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer _t_ (1u2009≤u2009_t_u2009≤u2009100u2009000)xa0— the number of operation Sonya has to perform. Next _t_ lines provide the descriptions of the queries in order they appear in the input file. The _i_-th row starts with a character _c__i_xa0— the type of the corresponding operation. If _c__i_ is equal to '+' or '-' then it's followed by a space and an integer _a__i_ (0u2009≤u2009_a__i_u2009<u20091018) given without leading zeroes (unless it's 0). If _c__i_ equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000. | 1,400 | false | false | true | false | true | false | false | false | false | false | 6,966 |
1776L | You are at your grandparents' house and you are playing an old video game on a strange console. Your controller has only two buttons and each button has a number written on it. Initially, your score is $$$0$$$. The game is composed of $$$n$$$ rounds. For each $$$1le ile n$$$, the $$$i$$$-th round works as follows. On the screen, a symbol $$$s_i$$$ appears, which is either $$$ exttt{+}$$$ (plus) or $$$ exttt{-}$$$ (minus). Then you must press one of the two buttons on the controller once. Suppose you press a button with the number $$$x$$$ written on it: your score will increase by $$$x$$$ if the symbol was $$$ exttt{+}$$$ and will decrease by $$$x$$$ if the symbol was $$$ exttt{-}$$$. After you press the button, the round ends. After you have played all $$$n$$$ rounds, you win if your score is $$$0$$$. Over the years, your grandparents bought many different controllers, so you have $$$q$$$ of them. The two buttons on the $$$j$$$-th controller have the numbers $$$a_j$$$ and $$$b_j$$$ written on them. For each controller, you must compute whether you can win the game playing with that controller. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2cdot 10^5$$$) — the number of rounds. The second line contains a string $$$s$$$ of length $$$n$$$ — where $$$s_i$$$ is the symbol that will appear on the screen in the $$$i$$$-th round. It is guaranteed that $$$s$$$ contains only the characters $$$ exttt{+}$$$ and $$$ exttt{-}$$$. The third line contains an integer $$$q$$$ ($$$1 le q le 10^5$$$) — the number of controllers. The following $$$q$$$ lines contain two integers $$$a_j$$$ and $$$b_j$$$ each ($$$1 le a_j, b_j le 10^9$$$) — the numbers on the buttons of controller $$$j$$$. Output Output $$$q$$$ lines. On line $$$j$$$ print $$$ exttt{YES}$$$ if the game is winnable using controller $$$j$$$, otherwise print $$$ exttt{NO}$$$. Examples Input 8 +-+---+- 5 2 1 10 3 7 9 10 10 5 3 Input 20 +-----+--+--------+- 2 1000000000 99999997 250000000 1000000000 Note In the first sample, one possible way to get score $$$0$$$ using the first controller is by pressing the button with numnber $$$1$$$ in rounds $$$1$$$, $$$2$$$, $$$4$$$, $$$5$$$, $$$6$$$ and $$$8$$$, and pressing the button with number $$$2$$$ in rounds $$$3$$$ and $$$7$$$. It is possible to show that there is no way to get a score of $$$0$$$ using the second controller. | 1,500 | true | false | false | false | false | false | false | true | false | false | 1,617 |
33D | Berland is facing dark times again. The army of evil lord Van de Mart is going to conquer the whole kingdom. To the council of war called by the Berland's king Valery the Severe came _n_ knights. After long discussions it became clear that the kingdom has exactly _n_ control points (if the enemy conquers at least one of these points, the war is lost) and each knight will occupy one of these points. Berland is divided into _m_u2009+u20091 regions with _m_ fences, and the only way to get from one region to another is to climb over the fence. Each fence is a circle on a plane, no two fences have common points, and no control point is on the fence. You are given _k_ pairs of numbers _a__i_, _b__i_. For each pair you have to find out: how many fences a knight from control point with index _a__i_ has to climb over to reach control point _b__i_ (in case when Van de Mart attacks control point _b__i_ first). As each knight rides a horse (it is very difficult to throw a horse over a fence), you are to find out for each pair the minimum amount of fences to climb over. Input The first input line contains three integers _n_, _m_, _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000, 0u2009≤u2009_k_u2009≤u2009100000). Then follow _n_ lines, each containing two integers _Kx__i_, _Ky__i_ (u2009-u2009109u2009≤u2009_Kx__i_,u2009_Ky__i_u2009≤u2009109) — coordinates of control point with index _i_. Control points can coincide. Each of the following _m_ lines describes fence with index _i_ with three integers _r__i_, _Cx__i_, _Cy__i_ (1u2009≤u2009_r__i_u2009≤u2009109, u2009-u2009109u2009≤u2009_Cx__i_,u2009_Cy__i_u2009≤u2009109) — radius and center of the circle where the corresponding fence is situated. Then follow _k_ pairs of integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_), each in a separate line — requests that you have to answer. _a__i_ and _b__i_ can coincide. Output Output exactly _k_ lines, each containing one integer — the answer to the corresponding request. Examples Input 2 3 1 0 0 4 4 1 0 0 2 0 0 3 0 0 1 2 | 2,000 | false | false | false | false | false | false | false | false | true | true | 9,830 |
1985A | Problem - 1985A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation strings *800 No tag edit access → Contest materials ") Editorial") xa0— the number of test cases. The first and only line of each test case contains two space-separated strings, $$$a$$$ and $$$b$$$, both of length $$$3$$$. The strings only contain lowercase Latin letters. Output For each test case, after the swap, output $$$a$$$ and $$$b$$$, separated by a space. Example Input 6 bit set cat dog hot dog uwu owo cat cat zzz zzz Output sit bet dat cog dot hog owu uwo cat cat zzz zzz | 800 | false | false | true | false | false | false | false | false | false | false | 374 |
2021D | Pak Chanek has a friend who runs a drink stall in a canteen. His friend will sell drinks for $$$n$$$ days, numbered from day $$$1$$$ to day $$$n$$$. There are also $$$m$$$ types of drinks, numbered from $$$1$$$ to $$$m$$$. The profit gained from selling a drink on a particular day can vary. On day $$$i$$$, the projected profit from selling drink of type $$$j$$$ is $$$A_{i, j}$$$. Note that $$$A_{i, j}$$$ can be negative, meaning that selling the drink would actually incur a loss. Pak Chanek wants to help his friend plan the sales over the $$$n$$$ days. On day $$$i$$$, Pak Chanek must choose to sell at least one type of drink. Furthermore, the types of drinks sold on a single day must form a subarray. In other words, in each day, Pak Chanek will select $$$i$$$ and $$$j$$$ such that $$$1 leq i leq j leq m$$$. Then all types of drinks between $$$i$$$ and $$$j$$$ (inclusive) will be sold. However, to ensure that customers from the previous day keep returning, the selection of drink types sold on day $$$i$$$ ($$$i>1$$$) must meet the following conditions: At least one drink type sold on day $$$i$$$ must also have been sold on day $$$i-1$$$. At least one drink type sold on day $$$i$$$ must not have been sold on day $$$i-1$$$. The daily profit is the sum of the profits from all drink types sold on that day. The total profit from the sales plan is the sum of the profits over $$$n$$$ days. What is the maximum total profit that can be achieved if Pak Chanek plans the sales optimally? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 2 cdot 10^5$$$; $$$3 leq m leq 2 cdot 10^5$$$; $$$n cdot m leq 2 cdot 10^5$$$)xa0— the number of rows and columns in a grid. The next $$$n$$$ lines of each test case contain $$$m$$$ integers each, where the $$$i$$$-th line contains the integers $$$A_{i,1} A_{i,2}, ldots, A_{i,m}$$$ ($$$-10^9 leq A_{i,j} leq 10^9$$$)xa0— project profits of each drink type on the $$$i$$$-th day. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note Here is Pak Chanek's optimal plan: On day $$$1$$$, Pak Chanek sells drink types $$$1$$$ to $$$3$$$. Generating a profit of $$$79+20+49 = 148$$$. On day $$$2$$$, Pak Chanek sells drink types $$$2$$$ to $$$4$$$. Generating a profit of $$$9+109+24 = 142$$$ On day $$$3$$$, Pak Chanek sells drink types $$$1$$$ to $$$6$$$. Generating a profit of $$$185$$$. So, the total profit of Pak Chanek's plan is $$$148 + 142 + 185 = 475$$$. | 2,500 | false | true | true | true | false | false | false | false | false | false | 142 |
1992F | In his favorite cafe Kmes once again wanted to try the herring under a fur coat. Previously, it would not have been difficult for him to do this, but the cafe recently introduced a new purchasing policy. Now, in order to make a purchase, Kmes needs to solve the following problem: $$$n$$$ cards with prices for different positions are laid out in front of him, on the $$$i$$$-th card there is an integer $$$a_i$$$, among these prices there is no whole positive integer $$$x$$$. Kmes is asked to divide these cards into the minimum number of bad segments (so that each card belongs to exactly one segment). A segment is considered bad if it is impossible to select a subset of cards with a product equal to $$$x$$$. All segments, in which Kmes will divide the cards, must be bad. Formally, the segment $$$(l, r)$$$ is bad if there are no indices $$$i_1 < i_2 < ldots < i_k$$$ such that $$$l le i_1, i_k le r$$$, and $$$a_{i_1} cdot a_{i_2} ldots cdot a_{i_k} = x$$$. Help Kmes determine the minimum number of bad segments in order to enjoy his favorite dish. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^3$$$) — the number of test cases. The first line of each set of input data gives you $$$2$$$ integers $$$n$$$ and $$$x$$$ ($$$1 le n le 10^5, 2 le x le 10^5$$$) — the number of cards and the integer, respectively. The second line of each set of input data contains $$$n$$$ integers $$$a_i$$$ ($$$1 le a_i le 2 cdot 10^5, a_i eq x$$$) — the prices on the cards. It is guaranteed that the sum of $$$n$$$ over all sets of test data does not exceed $$$10^5$$$. | 1,900 | false | true | false | true | false | false | true | false | false | false | 314 |
1773E | Emma loves playing with blocks. She has several cubic blocks of the same size that are numbered with distinct integers written on them. She assembles towers from those blocks by stacking them vertically. A configuration of her game is a set of towers that she has assembled from the blocks. Emma can perform two kinds of operations on a configuration of towers: Split any tower with more than one block in it by taking any number of blocks from the top of the tower and moving them to a new tower keeping their order, so that the top block of the old tower becomes the top block of the new tower. As a result of this operation, the number of towers increases by one. Combine any two towers by moving blocks from one tower on top of the other tower in the same order. As a result of this operation, the number of towers decreases by one. Emma wants to stack all the blocks into a single tower so that all blocks come in order sorted by the numbersxa0— from the block with the minimal number at the top to the block with the maximal number at the bottom. Emma wants to do as little of splitting and combining operations as possible. Your task is to find the minimal number of operations she has to make and output how many splits and combines are needed. Input The first line of the input file contains an integer $$$n$$$ ($$$1 le n le 10,000$$$)xa0— the number of towers in the initial configuration. Next $$$n$$$ lines describe towers. Each tower $$$i$$$ is described by a line that starts with the number $$$k_i$$$ ($$$k_i ge 1$$$; $$$sum_1^n{k_i} le 10,000$$$)xa0— the number of blocks in the tower, followed by $$$k_i$$$ numbers $$$b_{i,j}$$$ ($$$1 le b_{i,j} le 10^9$$$)xa0— numbers written on the blocks in the $$$i$$$-th tower, listed from top to bottom. All block numbers listed in the input are different. Output Output a line with two integers $$$s$$$ and $$$c$$$xa0— the number of split and combine operations Emma should make to get a single tower with blocks sorted by their numbers, so that the total number of operations is minimized. Note The example needs the following operations (1 split and 2 combines). | 1,400 | false | true | false | false | false | false | false | false | true | false | 1,652 |
1036D | Vasya has two arrays $$$A$$$ and $$$B$$$ of lengths $$$n$$$ and $$$m$$$, respectively. He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array $$$[1, 10, 100, 1000, 10000]$$$ Vasya can obtain array $$$[1, 1110, 10000]$$$, and from array $$$[1, 2, 3]$$$ Vasya can obtain array $$$[6]$$$. Two arrays $$$A$$$ and $$$B$$$ are considered equal if and only if they have the same length and for each valid $$$i$$$ $$$A_i = B_i$$$. Vasya wants to perform some of these operations on array $$$A$$$, some on array $$$B$$$, in such a way that arrays $$$A$$$ and $$$B$$$ become equal. Moreover, the lengths of the resulting arrays should be maximal possible. Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays $$$A$$$ and $$$B$$$ equal. Input The first line contains a single integer $$$n~(1 le n le 3 cdot 10^5)$$$ — the length of the first array. The second line contains $$$n$$$ integers $$$a_1, a_2, cdots, a_n~(1 le a_i le 10^9)$$$ — elements of the array $$$A$$$. The third line contains a single integer $$$m~(1 le m le 3 cdot 10^5)$$$ — the length of the second array. The fourth line contains $$$m$$$ integers $$$b_1, b_2, cdots, b_m~(1 le b_i le 10^9)$$$ - elements of the array $$$B$$$. Output Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays $$$A$$$ and $$$B$$$ in such a way that they became equal. If there is no way to make array equal, print "-1". | 1,600 | false | true | false | false | false | false | false | false | false | false | 5,520 |
1981E | Turtle just received $$$n$$$ segments and a sequence $$$a_1, a_2, ldots, a_n$$$. The $$$i$$$-th segment is $$$[l_i, r_i]$$$. Turtle will create an undirected graph $$$G$$$. If segment $$$i$$$ and segment $$$j$$$ intersect, then Turtle will add an undirected edge between $$$i$$$ and $$$j$$$ with a weight of $$$a_i - a_j$$$, for every $$$i e j$$$. Turtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph $$$G$$$, or report that the graph $$$G$$$ has no spanning tree. We say two segments $$$[l_1, r_1]$$$ and $$$[l_2, r_2]$$$ intersect if and only if $$$max(l_1, l_2) le min(r_1, r_2)$$$. 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 a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^5$$$) — the number of segments. The $$$i$$$-th of the following $$$n$$$ lines contains three integers $$$l_i, r_i, a_i$$$ ($$$1 le l_i le r_i le 10^9, 1 le a_i le 10^9$$$) — the $$$i$$$-th segment and the $$$i$$$-th element of the sequence. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output a single integer — the sum of the weights of the edges of the minimum spanning tree of the graph $$$G$$$. If the graph $$$G$$$ has no spanning tree, output $$$-1$$$. Example Input 4 5 1 7 3 2 4 6 3 5 5 6 7 9 3 4 4 5 2 7 3 1 3 6 4 5 5 6 7 9 1 1 4 4 1 4 3 1 2 1 3 4 5 1 4 4 3 1 3 1 2 3 3 4 5 8 Note In the first test case, the graph $$$G$$$ is as follows: One of the minimum spanning trees of $$$G$$$ is as follows: The sum of the weights of the edges of the minimum spanning tree is $$$9$$$. In the second test case, the graph $$$G$$$ is as follows: $$$G$$$ is already a tree, and the sum of the weights of the tree is $$$13$$$. In the third test case, the graph $$$G$$$ is as follows: In the fourth test case, the graph $$$G$$$ is as follows: It's easy to see that $$$G$$$ is not connected, so $$$G$$$ has no spanning tree. | 2,600 | false | true | false | false | true | false | false | false | false | true | 398 |
584C | Problem - 584C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms greedy strings *1700 No tag edit access → Contest materials the number of characters in which strings _a_ and _b_ are different. Then your task will be to find any string _s_3 of length _n_, such that _f_(_s_1,u2009_s_3)u2009=u2009_f_(_s_2,u2009_s_3)u2009=u2009_t_. If there is no such string, print u2009-u20091. Input The first line contains two integers _n_ and _t_ (1u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_t_u2009≤u2009_n_). The second line contains string _s_1 of length _n_, consisting of lowercase English letters. The third line contain string _s_2 of length _n_, consisting of lowercase English letters. Output Print a string of length _n_, differing from string _s_1 and from _s_2 in exactly _t_ characters. Your string should consist only from lowercase English letters. If such string doesn't exist, print -1. Examples Input 3 2 abc xyc Output ayd Input 1 0 c b Output -1 | 1,700 | false | true | false | false | false | true | false | false | false | false | 7,508 |
772E | This is an interactive problem. The judge has a hidden rooted full binary tree with _n_ leaves. A full binary tree is one where every node has either 0 or 2 children. The nodes with 0 children are called the leaves of the tree. Since this is a full binary tree, there are exactly 2_n_u2009-u20091 nodes in the tree. The leaves of the judge's tree has labels from 1 to _n_. You would like to reconstruct a tree that is isomorphic to the judge's tree. To do this, you can ask some questions. A question consists of printing the label of three distinct leaves _a_1,u2009_a_2,u2009_a_3. Let the depth of a node be the shortest distance from the node to the root of the tree. Let _LCA_(_a_,u2009_b_) denote the node with maximum depth that is a common ancestor of the nodes _a_ and _b_. Consider _X_u2009=u2009_LCA_(_a_1,u2009_a_2),u2009_Y_u2009=u2009_LCA_(_a_2,u2009_a_3),u2009_Z_u2009=u2009_LCA_(_a_3,u2009_a_1). The judge will tell you which one of _X_,u2009_Y_,u2009_Z_ has the maximum depth. Note, this pair is uniquely determined since the tree is a binary tree; there can't be any ties. More specifically, if _X_ (or _Y_, _Z_ respectively) maximizes the depth, the judge will respond with the string "X" (or "Y", "Z" respectively). You may only ask at most 10·_n_ questions. Input The first line of input will contain a single integer _n_ (3u2009≤u2009_n_u2009≤u20091u2009000)xa0— the number of leaves in the tree. Output To print the final answer, print out the string "-1" on its own line. Then, the next line should contain 2_n_u2009-u20091 integers. The _i_-th integer should be the parent of the _i_-th node, or -1, if it is the root. Your answer will be judged correct if your output is isomorphic to the judge's tree. In particular, the labels of the leaves do not need to be labeled from 1 to _n_. Here, isomorphic means that there exists a permutation π such that node _i_ is the parent of node _j_ in the judge tree if and only node π(_i_) is the parent of node π(_j_) in your tree. Interaction To ask a question, print out three distinct integers _a_1,u2009_a_2,u2009_a_3. These integers should be between 1 and _n_, inclusive. The judge will respond with a single character, either "X", "Y", "Z". If the string is "X" (or "Y", "Z" respectively), that means the pair (_a_1,u2009_a_2) (or (_a_2,u2009_a_3), (_a_3,u2009_a_1) respectively) has the deepest _LCA_ among the three pairs. You may only ask a question at most 10·_n_ times, otherwise, you will get Wrong Answer. When you are ready to answer, print out a single integer "-1" on its own line. The next line should contain 2_n_u2009-u20091 integers. The _i_-th integer should be the parent of the _i_-th node, or -1, if it is the root. Do not forget to flush the final answer as well. Printing the answer does not count as asking a question. You will get Wrong Answer verdict if Your question or answers are not in the format described in this statement. You ask strictly more than 10·_n_ questions. Your question contains duplicate indices. Your final answer is not isomorphic to the judge tree. You will get Idleness Limit Exceeded if you don't print anything or if you forget to flush the output, including for the final answer (more info about flushing output below). To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. If at any moment your program reads -1 as an answer, it should immediately exit normally (for example, by calling exit(0)). You will get Wrong Answer in this case, it means that you made more queries than allowed, or made an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Hacking To hack someone, use the following format n p_1 p_2 ... p_{2n-1} This denotes a tree where the parent of the _i_-th node is _p__i_ (_p__i_u2009=u2009u2009-u20091 or _n_u2009<u2009_p__i_u2009≤u20092_n_u2009-u20091). If _p__i_ is equal to -1, then node _i_ is the root. This input must describe a valid full rooted binary tree. Of course, contestant programs will not be able to see this input. Example Output 1 4 2 1 2 4 2 4 1 2 3 5 2 4 3 -1 -1 1 1 2 2 3 3 6 6 Note For the first sample, the judge has the hidden tree: Here is a more readable format of the interaction: The last line can also be 8 6 9 8 9 7 -1 6 7. | 3,200 | false | false | false | false | false | false | false | true | false | false | 6,688 |
1092B | There are $$$n$$$ students in a university. The number of students is even. The $$$i$$$-th student has programming skill equal to $$$a_i$$$. The coach wants to form $$$frac{n}{2}$$$ teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal (otherwise they cannot understand each other and cannot form a team). Students can solve problems to increase their skill. One solved problem increases the skill by one. The coach wants to know the minimum total number of problems students should solve to form exactly $$$frac{n}{2}$$$ teams (i.e. each pair of students should form a team). Your task is to find this number. Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 100$$$) — the number of students. It is guaranteed that $$$n$$$ is even. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 100$$$), where $$$a_i$$$ is the skill of the $$$i$$$-th student. Output Print one number — the minimum total number of problems students should solve to form exactly $$$frac{n}{2}$$$ teams. Note In the first example the optimal teams will be: $$$(3, 4)$$$, $$$(1, 6)$$$ and $$$(2, 5)$$$, where numbers in brackets are indices of students. Then, to form the first team the third student should solve $$$1$$$ problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve $$$4$$$ problems so the answer is $$$1 + 4 = 5$$$. In the second example the first student should solve $$$99$$$ problems to form a team with the second one. | 800 | false | false | false | false | false | false | false | false | true | false | 5,250 |
289A | Problem - 289A - 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 *1100 No tag edit access → Contest materials . He has a set that consists of _n_ integer segments: | 1,100 | false | false | true | false | false | false | true | false | false | false | 8,679 |
487E | There are _n_ cities in Cyberland, numbered from 1 to _n_, connected by _m_ bidirectional roads. The _j_-th road connects city _a__j_ and _b__j_. For tourists, souvenirs are sold in every city of Cyberland. In particular, city _i_ sell it at a price of _w__i_. Now there are _q_ queries for you to handle. There are two types of queries: "C _a_ _w_": The price in city _a_ is changed to _w_. "A _a_ _b_": Now a tourist will travel from city _a_ to _b_. He will choose a route, he also doesn't want to visit a city twice. He will buy souvenirs at the city where the souvenirs are the cheapest (possibly exactly at city _a_ or _b_). You should output the minimum possible price that he can buy the souvenirs during his travel. More formally, we can define routes as follow: A route is a sequence of cities [_x_1,u2009_x_2,u2009...,u2009_x__k_], where _k_ is a certain positive integer. For any 1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_k_,u2009_x__i_u2009≠u2009_x__j_. For any 1u2009≤u2009_i_u2009<u2009_k_, there is a road connecting _x__i_ and _x__i_u2009+u20091. The minimum price of the route is _min_(_w__x_1,u2009_w__x_2,u2009...,u2009_w__x__k_). The required answer is the minimum value of the minimum prices of all valid routes from _a_ to _b_. Input The first line of input contains three integers _n_,u2009_m_,u2009_q_ (1u2009≤u2009_n_,u2009_m_,u2009_q_u2009≤u2009105), separated by a single space. Next _n_ lines contain integers _w__i_ (1u2009≤u2009_w__i_u2009≤u2009109). Next _m_ lines contain pairs of space-separated integers _a__j_ and _b__j_ (1u2009≤u2009_a__j_,u2009_b__j_u2009≤u2009_n_,u2009_a__j_u2009≠u2009_b__j_). It is guaranteed that there is at most one road connecting the same pair of cities. There is always at least one valid route between any two cities. Next _q_ lines each describe a query. The format is "C _a_ _w_" or "A _a_ _b_" (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_,u20091u2009≤u2009_w_u2009≤u2009109). Output For each query of type "A", output the corresponding answer. Examples Input 3 3 3 1 2 3 1 2 2 3 1 3 A 2 3 C 1 5 A 2 3 Input 7 9 4 1 2 3 4 5 6 7 1 2 2 5 1 5 2 3 3 4 2 4 5 6 6 7 5 7 A 2 3 A 6 4 A 6 7 A 3 3 Note For the second sample, an optimal routes are: From 2 to 3 it is [2,u20093]. From 6 to 4 it is [6,u20095,u20091,u20092,u20094]. From 6 to 7 it is [6,u20095,u20097]. From 3 to 3 it is [3]. | 3,200 | false | false | false | false | true | false | false | false | false | true | 7,886 |
851A | Problem - 851A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation math *800 No tag edit access → Contest materials ") -th spectator stands and the first spectator sits. At time _k_u2009+u20092, the (_k_u2009+u20092)-th spectator stands and the second spectator sits. ... At time _n_, the _n_-th spectator stands and the (_n_u2009-u2009_k_)-th spectator sits. At time _n_u2009+u20091, the (_n_u2009+u20091u2009-u2009_k_)-th spectator sits. ... At time _n_u2009+u2009_k_, the _n_-th spectator sits. Arpa wants to know how many spectators are standing at time _t_. Input The first line contains three integers _n_, _k_, _t_ (1u2009≤u2009_n_u2009≤u2009109, 1u2009≤u2009_k_u2009≤u2009_n_, 1u2009≤u2009_t_u2009<u2009_n_u2009+u2009_k_). Output Print single integer: how many spectators are standing at time _t_. Examples Input 10 5 3 Output 3 Input 10 5 7 Output 5 Input 10 5 12 Output 3 Note In the following a sitting spectator is represented as -, a standing spectator is represented as ^. At _t_u2009=u20090u2002 ---------- number of standing spectators = 0. At _t_u2009=u20091u2002 ^--------- number of standing spectators = 1. At _t_u2009=u20092u2002 ^^-------- number of standing spectators = 2. At _t_u2009=u20093u2002 ^^^------- number of standing spectators = 3. At _t_u2009=u20094u2002 ^^^^------ number of standing spectators = 4. At _t_u2009=u20095u2002 ^^^^^----- number of standing spectators = 5. At _t_u2009=u20096u2002 -^^^^^---- number of standing spectators = 5. At _t_u2009=u20097u2002 --^^^^^--- number of standing spectators = 5. At _t_u2009=u20098u2002 ---^^^^^-- number of standing spectators = 5. At _t_u2009=u20099u2002 ----^^^^^- number of standing spectators = 5. At _t_u2009=u200910 -----^^^^^ number of standing spectators = 5. At _t_u2009=u200911 ------^^^^ number of standing spectators = 4. At _t_u2009=u200912 -------^^^ number of standing spectators = 3. At _t_u2009=u200913 --------^^ number of standing spectators = 2. At _t_u2009=u200914 ---------^ number of standing spectators = 1. At _t_u2009=u200915 ---------- number of standing spectators = 0. | 800 | true | false | true | false | false | false | false | false | false | false | 6,345 |
254B | In 2013, the writers of Berland State University should prepare problems for _n_ Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to _n_. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number _i_ should be prepared by _p__i_ people for _t__i_ days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it. For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd. In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time. Input The first line contains integer _n_ — the number of Olympiads in 2013 (1u2009≤u2009_n_u2009≤u2009100). Each of the following _n_ lines contains four integers _m__i_, _d__i_, _p__i_ and _t__i_ — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the _i_-th Olympiad (1u2009≤u2009_m__i_u2009≤u200912, _d__i_u2009≥u20091, 1u2009≤u2009_p__i_,u2009_t__i_u2009≤u2009100), _d__i_ doesn't exceed the number of days in month _m__i_. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day. Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year. Output Print a single number — the minimum jury size. Examples Input 3 12 9 2 1 12 8 1 3 12 8 2 2 | 1,500 | false | false | true | false | false | false | true | false | false | false | 8,818 |
592A | Galois is one of the strongest chess players of Byteforces. He has even invented a new variant of chess, which he named «PawnChess». This new game is played on a board consisting of 8 rows and 8 columns. At the beginning of every game some black and white pawns are placed on the board. The number of black pawns placed is not necessarily equal to the number of white pawns placed. Lets enumerate rows and columns with integers from 1 to 8. Rows are numbered from top to bottom, while columns are numbered from left to right. Now we denote as (_r_,u2009_c_) the cell located at the row _r_ and at the column _c_. There are always two players A and B playing the game. Player A plays with white pawns, while player B plays with black ones. The goal of player A is to put any of his pawns to the row 1, while player B tries to put any of his pawns to the row 8. As soon as any of the players completes his goal the game finishes immediately and the succeeded player is declared a winner. Player A moves first and then they alternate turns. On his move player A must choose exactly one white pawn and move it one step upward and player B (at his turn) must choose exactly one black pawn and move it one step down. Any move is possible only if the targeted cell is empty. It's guaranteed that for any scenario of the game there will always be at least one move available for any of the players. Moving upward means that the pawn located in (_r_,u2009_c_) will go to the cell (_r_u2009-u20091,u2009_c_), while moving down means the pawn located in (_r_,u2009_c_) will go to the cell (_r_u2009+u20091,u2009_c_). Again, the corresponding cell must be empty, i.e. not occupied by any other pawn of any color. Given the initial disposition of the board, determine who wins the game if both players play optimally. Note that there will always be a winner due to the restriction that for any game scenario both players will have some moves available. Input The input consists of the board description given in eight lines, each line contains eight characters. Character 'B' is used to denote a black pawn, and character 'W' represents a white pawn. Empty cell is marked with '.'. It's guaranteed that there will not be white pawns on the first row neither black pawns on the last row. Output Print 'A' if player A wins the game on the given board, and 'B' if player B will claim the victory. Again, it's guaranteed that there will always be a winner on the given board. Note In the first sample player A is able to complete his goal in 3 steps by always moving a pawn initially located at (4,u20095). Player B needs at least 5 steps for any of his pawns to reach the row 8. Hence, player A will be the winner. | 1,200 | false | false | true | false | false | false | false | false | false | false | 7,482 |
862F | Mahmoud and Ehab solved Dr. Evil's questions so he gave them the password of the door of the evil land. When they tried to open the door using it, the door gave them a final question to solve before they leave (yes, the door is digital, Dr. Evil is modern). If they don't solve it, all the work will be useless and they won't leave the evil land forever. Will you help them? Mahmoud and Ehab are given _n_ strings _s_1,u2009_s_2,u2009... ,u2009_s__n_ numbered from 1 to _n_ and _q_ queries, Each query has one of the following forms: 1 _a_ _b_ (1u2009≤u2009_a_u2009≤u2009_b_u2009≤u2009_n_), For all the intervals [_l_;_r_] where (_a_u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_b_) find the maximum value of this expression:(_r_u2009-u2009_l_u2009+u20091)u2009*u2009_LCP_(_s__l_,u2009_s__l_u2009+u20091,u2009... ,u2009_s__r_u2009-u20091,u2009_s__r_) where _LCP_(_str_1,u2009_str_2,u2009_str_3,u2009... ) is the length of the longest common prefix of the strings _str_1,u2009_str_2,u2009_str_3,u2009... . 2 _x_ _y_ (1u2009≤u2009_x_u2009≤u2009_n_) where _y_ is a string, consisting of lowercase English letters. Change the string at position _x_ to _y_. Input The first line of input contains 2 integers _n_ and _q_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_q_u2009≤u2009105)xa0– The number of strings and the number of queries, respectively. The second line contains _n_ strings _str__i_ consisting of lowercase English letters. The next _q_ lines describe the queries and may have one of the 2 forms: 1 _a_ _b_ (1u2009≤u2009_a_u2009≤u2009_b_u2009≤u2009_n_). 2 _x_ _y_ (1u2009≤u2009_x_u2009≤u2009_n_), where _y_ is a string consisting of lowercase English letters. the total length of all strings in input won't exceed 105 Output For each query of first type output its answer in a new line. Example Input 5 9 mahmoud mahmoudbadawy drmahmoud drevil mahmoud 1 1 5 1 1 2 1 2 3 2 3 mahmoud 2 4 mahmoud 2 2 mahmouu 1 1 5 1 2 3 1 1 1 | 2,900 | false | false | false | false | true | false | false | false | false | false | 6,295 |
169A | Petya and Vasya are brothers. Today is a special day for them as their parents left them home alone and commissioned them to do _n_ chores. Each chore is characterized by a single parameter — its complexity. The complexity of the _i_-th chore equals _h__i_. As Petya is older, he wants to take the chores with complexity larger than some value _x_ (_h__i_u2009>u2009_x_) to leave to Vasya the chores with complexity less than or equal to _x_ (_h__i_u2009≤u2009_x_). The brothers have already decided that Petya will do exactly _a_ chores and Vasya will do exactly _b_ chores (_a_u2009+u2009_b_u2009=u2009_n_). In how many ways can they choose an integer _x_ so that Petya got exactly _a_ chores and Vasya got exactly _b_ chores? Input The first input line contains three integers _n_,u2009_a_ and _b_ (2u2009≤u2009_n_u2009≤u20092000; _a_,u2009_b_u2009≥u20091; _a_u2009+u2009_b_u2009=u2009_n_) — the total number of chores, the number of Petya's chores and the number of Vasya's chores. The next line contains a sequence of integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≤u2009_h__i_u2009≤u2009109), _h__i_ is the complexity of the _i_-th chore. The numbers in the given sequence are not necessarily different. All numbers on the lines are separated by single spaces. Output Print the required number of ways to choose an integer value of _x_. If there are no such ways, print 0. Note In the first sample the possible values of _x_ are 3, 4 or 5. In the second sample it is impossible to find such _x_, that Petya got 3 chores and Vasya got 4. | 800 | false | false | false | false | false | false | false | false | true | false | 9,188 |
1551D1 | The only difference between this problem and D2 is that you don't have to provide the way to construct the answer in this problem, but you have to do it in D2. There's a table of $$$n imes m$$$ cells ($$$n$$$ rows and $$$m$$$ columns). The value of $$$n cdot m$$$ is even. A domino is a figure that consists of two cells having a common side. It may be horizontal (one of the cells is to the right of the other) or vertical (one of the cells is above the other). You need to find out whether it is possible to place $$$frac{nm}{2}$$$ dominoes on the table so that exactly $$$k$$$ of them are horizontal and all the other dominoes are vertical. The dominoes cannot overlap and must fill the whole table. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of a single line. The line contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 le n,m le 100$$$, $$$0 le k le frac{nm}{2}$$$, $$$n cdot m$$$ is even) — the number of rows, columns and horizontal dominoes, respectively. Output For each test case output "YES", if it is possible to place dominoes in the desired way, or "NO" otherwise. You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer). Example Input 8 4 4 2 2 3 0 3 2 3 1 2 0 2 4 2 5 2 2 2 17 16 2 1 1 Output YES YES YES NO YES NO YES NO | 1,700 | true | false | false | false | false | true | false | false | false | false | 2,888 |
1706D1 | This is the easy version of the problem. The only difference between the versions is the constraints on $$$n$$$, $$$k$$$, $$$a_i$$$, and the sum of $$$n$$$ over all test cases. You can make hacks only if both versions of the problem are solved. Note the unusual memory limit. You are given an array of integers $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$, and an integer $$$k$$$. The cost of an array of integers $$$p_1, p_2, ldots, p_n$$$ of length $$$n$$$ is $$$$$$maxlimits_{1 le i le n}left(left lfloor frac{a_i}{p_i} ight floor ight) - minlimits_{1 le i le n}left(left lfloor frac{a_i}{p_i} ight floor ight).$$$$$$ Here, $$$lfloor frac{x}{y} floor$$$ denotes the integer part of the division of $$$x$$$ by $$$y$$$. Find the minimum cost of an array $$$p$$$ such that $$$1 le p_i le k$$$ for all $$$1 le i le n$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 3000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_1 le a_2 le ldots le a_n le 3000$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3000$$$. Output For each test case, print a single integerxa0— the minimum possible cost of an array $$$p$$$ satisfying the condition above. Example Input 7 5 2 4 5 6 8 11 5 12 4 5 6 8 11 3 1 2 9 15 7 3 2 3 5 5 6 9 10 6 56 54 286 527 1436 2450 2681 3 95 16 340 2241 2 2 1 3 Note In the first test case, the optimal array is $$$p = [1, 1, 1, 2, 2]$$$. The resulting array of values of $$$lfloor frac{a_i}{p_i} floor$$$ is $$$[4, 5, 6, 4, 5]$$$. The cost of $$$p$$$ is $$$maxlimits_{1 le i le n}(lfloor frac{a_i}{p_i} floor) - minlimits_{1 le i le n}(lfloor frac{a_i}{p_i} floor) = 6 - 4 = 2$$$. We can show that there is no array (satisfying the condition from the statement) with a smaller cost. In the second test case, one of the optimal arrays is $$$p = [12, 12, 12, 12, 12]$$$, which results in all $$$lfloor frac{a_i}{p_i} floor$$$ being $$$0$$$. In the third test case, the only possible array is $$$p = [1, 1, 1]$$$. | 1,700 | false | true | false | false | false | true | true | true | false | false | 2,037 |
798C | Mike has a sequence _A_u2009=u2009[_a_1,u2009_a_2,u2009...,u2009_a__n_] of length _n_. He considers the sequence _B_u2009=u2009[_b_1,u2009_b_2,u2009...,u2009_b__n_] beautiful if the _gcd_ of all its elements is bigger than 1, i.e. . Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index _i_ (1u2009≤u2009_i_u2009<u2009_n_), delete numbers _a__i_,u2009_a__i_u2009+u20091 and put numbers _a__i_u2009-u2009_a__i_u2009+u20091,u2009_a__i_u2009+u2009_a__i_u2009+u20091 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence _A_ beautiful if it's possible, or tell him that it is impossible to do so. is the biggest non-negative number _d_ such that _d_ divides _b__i_ for every _i_ (1u2009≤u2009_i_u2009≤u2009_n_). Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009100u2009000) — length of sequence _A_. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — elements of sequence _A_. Output Output on the first line "YES" (without quotes) if it is possible to make sequence _A_ beautiful by performing operations described above, and "NO" (without quotes) otherwise. If the answer was "YES", output the minimal number of moves needed to make sequence _A_ beautiful. Note In the first example you can simply make one move to obtain sequence [0,u20092] with . In the second example the _gcd_ of the sequence is already greater than 1. | 1,700 | false | true | false | true | false | false | false | false | false | false | 6,579 |
1447B | You are given a rectangular grid with $$$n$$$ rows and $$$m$$$ columns. The cell located on the $$$i$$$-th row from the top and the $$$j$$$-th column from the left has a value $$$a_{ij}$$$ written in it. You can perform the following operation any number of times (possibly zero): Choose any two adjacent cells and multiply the values in them by $$$-1$$$. Two cells are called adjacent if they share a side. Note that you can use a cell more than once in different operations. You are interested in $$$X$$$, the sum of all the numbers in the grid. What is the maximum $$$X$$$ you can achieve with these operations? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$,$$$m$$$ ($$$2 le n$$$, $$$m le 10$$$). The following $$$n$$$ lines contain $$$m$$$ integers each, the $$$j$$$-th element in the $$$i$$$-th line is $$$a_{ij}$$$ ($$$-100leq a_{ij}le 100$$$). Output For each testcase, print one integer $$$X$$$, the maximum possible sum of all the values in the grid after applying the operation as many times as you want. Example Input 2 2 2 -1 1 1 1 3 4 0 -1 -2 -3 -1 -2 -3 -4 -2 -3 -4 -5 Note In the first test case, there will always be at least one $$$-1$$$, so the answer is $$$2$$$. In the second test case, we can use the operation six times to elements adjacent horizontally and get all numbers to be non-negative. So the answer is: $$$2 imes 1 + 3 imes2 + 3 imes 3 + 2 imes 4 + 1 imes 5 = 30$$$. | 1,000 | true | true | false | false | false | false | false | false | false | false | 3,439 |
1468K | There is a robot on a checkered field that is endless in all directions. Initially, the robot is located in the cell with coordinates $$$(0, 0)$$$. He will execute commands which are described by a string of capital Latin letters 'L', 'R', 'D', 'U'. When a command is executed, the robot simply moves in the corresponding direction: 'L': one cell to the left (the $$$x$$$-coordinate of the current cell decreases by $$$1$$$); 'R': one cell to the right (the $$$x$$$-coordinate of the current cell is increased by $$$1$$$); 'D': one cell down (the $$$y$$$-coordinate of the current cell decreases by $$$1$$$); 'U': one cell up (the $$$y$$$-coordinate of the current cell is increased by $$$1$$$). Your task is to put an obstacle in one cell of the field so that after executing the commands, the robot will return to the original cell of its path $$$(0, 0)$$$. Of course, an obstacle cannot be placed in the starting cell $$$(0, 0)$$$. It is guaranteed that if the obstacle is not placed, then the robot will not return to the starting cell. An obstacle affects the movement of the robot in the following way: if it tries to go in a certain direction, and there is an obstacle, then it simply remains in place (the obstacle also remains, that is, it does not disappear). Find any such cell of the field (other than $$$(0, 0)$$$) that if you put an obstacle there, the robot will return to the cell $$$(0, 0)$$$ after the execution of all commands. If there is no solution, then report it. Input The first line contains one integer $$$t$$$ ($$$1 le t le 500$$$) — the number of test cases. Each test case consists of a single line containing $$$s$$$ — the sequence of commands, which are uppercase Latin letters 'L', 'R', 'D', 'U' only. The length of $$$s$$$ is between $$$1$$$ and $$$5000$$$, inclusive. Additional constraint on $$$s$$$: executing this sequence of commands leads the robot to some cell other than $$$(0, 0)$$$, if there are no obstacles. The sum of lengths of all $$$s$$$ in a test doesn't exceed $$$5000$$$. Output For each test case print a single line: if there is a solution, print two integers $$$x$$$ and $$$y$$$ ($$$-10^9 le x,y le 10^9$$$) such that an obstacle in $$$(x, y)$$$ will force the robot to return back to the cell $$$(0, 0)$$$; otherwise, print two zeroes (i.u2009e. 0 0). If there are multiple answers, you can print any of them. | 1,600 | false | false | true | false | false | false | true | false | false | false | 3,342 |
1243A | Ujan decided to make a new wooden roof for the house. He has $$$n$$$ rectangular planks numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th plank has size $$$a_i imes 1$$$ (that is, the width is $$$1$$$ and the height is $$$a_i$$$). Now, Ujan wants to make a square roof. He will first choose some of the planks and place them side by side in some order. Then he will glue together all of these planks by their vertical sides. Finally, he will cut out a square from the resulting shape in such a way that the sides of the square are horizontal and vertical. For example, if Ujan had planks with lengths $$$4$$$, $$$3$$$, $$$1$$$, $$$4$$$ and $$$5$$$, he could choose planks with lengths $$$4$$$, $$$3$$$ and $$$5$$$. Then he can cut out a $$$3 imes 3$$$ square, which is the maximum possible. Note that this is not the only way he can obtain a $$$3 imes 3$$$ square. What is the maximum side length of the square Ujan can get? Input The first line of input contains a single integer $$$k$$$ ($$$1 leq k leq 10$$$), the number of test cases in the input. For each test case, the first line contains a single integer $$$n$$$ ($$$1 leq n leq 1,000$$$), the number of planks Ujan has in store. The next line contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 leq a_i leq n$$$), the lengths of the planks. Output For each of the test cases, output a single integer, the maximum possible side length of the square. Example Input 4 5 4 3 1 4 5 4 4 4 4 4 3 1 1 1 5 5 5 1 1 5 Note The first sample corresponds to the example in the statement. In the second sample, gluing all $$$4$$$ planks will result in a $$$4 imes 4$$$ square. In the third sample, the maximum possible square is $$$1 imes 1$$$ and can be taken simply as any of the planks. | 800 | false | false | true | false | false | false | false | false | false | false | 4,484 |
286D | A double tourist path, located at a park in Ultima Thule, is working by the following principle: We introduce the Cartesian coordinate system. At some points of time there are two tourists going (for a walk) from points (u2009-u20091,u20090) and (1,u20090) simultaneously. The first one is walking from (u2009-u20091,u20090), the second one is walking from (1,u20090). Both tourists in a pair move at the same speed 1 (distance unit per second), the first one moves along line _x_u2009=u2009u2009-u20091, the second one moves along line _x_u2009=u20091, both of them are moving in the positive direction of the _Oy_ axis. At some points of time walls appear. Wall (_l__i_,u2009_r__i_) is a segment between points (0,u2009_l__i_) and (0,u2009_r__i_). Each wall appears immediately. The Ultima Thule government wants to learn this for each pair of tourists that walk simultaneously: for how long (in seconds) will they not see each other? Two tourists don't see each other if the segment that connects their positions on the plane intersects at least one wall. Two segments intersect if they share at least one point. We assume that the segments' ends belong to the segments. Help the government count the required time. Note that the walls can intersect (in any way) or coincide. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of pairs of tourists and the number of built walls. The next _m_ lines contain three space-separated integers _l__i_, _r__i_ and _t__i_ each (0u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009109, 0u2009≤u2009_t__i_u2009≤u2009109) — the wall ends and the time it appeared. The last line contains _n_ distinct space-separated strictly increasing integers _q_1,u2009_q_2,u2009...,u2009_q__n_ (0u2009≤u2009_q__i_u2009≤u2009109) — the points of time when pairs of tourists walk. All points of time are given in seconds. Output For each pair of tourists print on a single line a single integer — the time in seconds when the two tourists from the corresponding pair won't see each other. Print the numbers in the order in which the they go in the input. Examples Input 3 3 0 3 4 0 1 2 2 4 0 1 3 4 | 2,600 | false | false | false | false | true | false | false | false | true | false | 8,688 |
1328D | The round carousel consists of $$$n$$$ figures of animals. Figures are numbered from $$$1$$$ to $$$n$$$ in order of the carousel moving. Thus, after the $$$n$$$-th figure the figure with the number $$$1$$$ follows. Each figure has its own type — the type of the animal corresponding to this figure (the horse, the tiger and so on). The type of animal of the $$$i$$$-th figure equals $$$t_i$$$. The example of the carousel for $$$n=9$$$ and $$$t=[5, 5, 1, 15, 1, 5, 5, 1, 1]$$$. You want to color each figure in one of the colors. You think that it's boring if the carousel contains two different figures (with the distinct types of animals) going one right after another and colored in the same color. Your task is to color the figures in such a way that the number of distinct colors used is the minimum possible and there are no figures of the different types going one right after another and colored in the same color. If you use exactly $$$k$$$ distinct colors, then the colors of figures should be denoted with integers from $$$1$$$ to $$$k$$$. Input The input contains one or more test cases. The first line contains one integer $$$q$$$ ($$$1 le q le 10^4$$$) — the number of test cases in the test. Then $$$q$$$ test cases follow. One test case is given on two lines. The first line of the test case contains one integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the number of figures in the carousel. Figures are numbered from $$$1$$$ to $$$n$$$ in order of carousel moving. Assume that after the $$$n$$$-th figure the figure $$$1$$$ goes. The second line of the test case contains $$$n$$$ integers $$$t_1, t_2, dots, t_n$$$ ($$$1 le t_i le 2 cdot 10^5$$$), where $$$t_i$$$ is the type of the animal of the $$$i$$$-th figure. The sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output Print $$$q$$$ answers, for each test case print two lines. In the first line print one integer $$$k$$$ — the minimum possible number of distinct colors of figures. In the second line print $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le k$$$), where $$$c_i$$$ is the color of the $$$i$$$-th figure. If there are several answers, you can print any. Example Input 4 5 1 2 1 2 2 6 1 2 2 1 2 2 5 1 2 1 2 3 3 10 10 10 Output 2 1 2 1 2 2 2 2 1 2 1 2 1 3 2 3 2 3 1 1 1 1 1 | 1,800 | true | true | false | true | false | true | false | false | false | true | 4,068 |
107A | The German University in Cairo (GUC) dorm houses are numbered from 1 to _n_. Underground water pipes connect these houses together. Each pipe has certain direction (water can flow only in this direction and not vice versa), and diameter (which characterizes the maximal amount of water it can handle). For each house, there is at most one pipe going into it and at most one pipe going out of it. With the new semester starting, GUC student and dorm resident, Lulu, wants to install tanks and taps at the dorms. For every house with an outgoing water pipe and without an incoming water pipe, Lulu should install a water tank at that house. For every house with an incoming water pipe and without an outgoing water pipe, Lulu should install a water tap at that house. Each tank house will convey water to all houses that have a sequence of pipes from the tank to it. Accordingly, each tap house will receive water originating from some tank house. In order to avoid pipes from bursting one week later (like what happened last semester), Lulu also has to consider the diameter of the pipes. The amount of water each tank conveys should not exceed the diameter of the pipes connecting a tank to its corresponding tap. Lulu wants to find the maximal amount of water that can be safely conveyed from each tank to its corresponding tap. Input The first line contains two space-separated integers _n_ and _p_ (1u2009≤u2009_n_u2009≤u20091000,u20090u2009≤u2009_p_u2009≤u2009_n_) — the number of houses and the number of pipes correspondingly. Then _p_ lines follow — the description of _p_ pipes. The _i_-th line contains three integers _a__i_ _b__i_ _d__i_, indicating a pipe of diameter _d__i_ going from house _a__i_ to house _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_,u2009_a__i_u2009≠u2009_b__i_,u20091u2009≤u2009_d__i_u2009≤u2009106). It is guaranteed that for each house there is at most one pipe going into it and at most one pipe going out of it. Output Print integer _t_ in the first line — the number of tank-tap pairs of houses. For the next _t_ lines, print 3 integers per line, separated by spaces: _tank__i_, _tap__i_, and _diameter__i_, where _tank__i_u2009≠u2009_tap__i_ (1u2009≤u2009_i_u2009≤u2009_t_). Here _tank__i_ and _tap__i_ are indexes of tank and tap houses respectively, and _diameter__i_ is the maximum amount of water that can be conveyed. All the _t_ lines should be ordered (increasingly) by _tank__i_. Examples Input 3 3 1 2 20 2 3 10 3 1 5 | 1,400 | false | false | false | false | false | false | false | false | false | true | 9,456 |
883L | Berland.Taxi is a new taxi company with _k_ cars which started operating in the capital of Berland just recently. The capital has _n_ houses on a straight line numbered from 1 (leftmost) to _n_ (rightmost), and the distance between any two neighboring houses is the same. You have to help the company schedule all the taxi rides which come throughout the day according to the following rules: All cars are available for picking up passengers. Initially the _j_-th car is located next to the house with the number _x__j_ at time 0. All cars have the same speed. It takes exactly 1 minute for any car to travel between neighboring houses _i_ and _i_u2009+u20091. The _i_-th request for taxi ride comes at the time _t__i_, asking for a passenger to be picked up at the house _a__i_ and dropped off at the house _b__i_. All requests for taxi rides are given in the increasing order of _t__i_. All _t__i_ are distinct. When a request for taxi ride is received at time _t__i_, Berland.Taxi operator assigns a car to it as follows: Out of cars which are currently available, operator assigns the car which is the closest to the pick up spot _a__i_. Needless to say, if a car is already on a ride with a passenger, it won't be available for any rides until that passenger is dropped off at the corresponding destination. If there are several such cars, operator will pick one of them which has been waiting the most since it became available. If there are several such cars, operator will pick one of them which has the lowest number. After a car gets assigned to the taxi ride request: The driver immediately starts driving from current position to the house _a__i_. Once the car reaches house _a__i_, the passenger is immediately picked up and the driver starts driving to house _b__i_. Once house _b__i_ is reached, the passenger gets dropped off and the car becomes available for new rides staying next to the house _b__i_. It is allowed for multiple cars to be located next to the same house at the same point in time, while waiting for ride requests or just passing by. If there are no available cars at time _t__i_ when a request for taxi ride comes, then: The _i_-th passenger will have to wait for a car to become available. When a car becomes available, operator will immediately assign it to this taxi ride request. If multiple cars become available at once while the passenger is waiting, operator will pick a car out of them according to the rules described above. Operator processes taxi ride requests one by one. So if multiple passengers are waiting for the cars to become available, operator will not move on to processing the (_i_u2009+u20091)-th ride request until the car gets assigned to the _i_-th ride request. Your task is to write a program that will process the given list of _m_ taxi ride requests. For each request you have to find out which car will get assigned to it, and how long the passenger will have to wait for a car to arrive. Note, if there is already car located at the house _a__i_, then the corresponding wait time will be 0. Input The first line of input contains integers _n_, _k_ and _m_ (2u2009≤u2009_n_u2009≤u20092·105, 1u2009≤u2009_k_,u2009_m_u2009≤u20092·105) — number of houses, number of cars, and number of taxi ride requests. The second line contains integers _x_1,u2009_x_2,u2009...,u2009_x__k_ (1u2009≤u2009_x__i_u2009≤u2009_n_) — initial positions of cars. _x__i_ is a house number at which the _i_-th car is located initially. It's allowed for more than one car to be located next to the same house. The following _m_ lines contain information about ride requests. Each ride request is represented by integers _t__j_, _a__j_ and _b__j_ (1u2009≤u2009_t__j_u2009≤u20091012, 1u2009≤u2009_a__j_,u2009_b__j_u2009≤u2009_n_, _a__j_u2009≠u2009_b__j_), where _t__j_ is time in minutes when a request is made, _a__j_ is a house where passenger needs to be picked up, and _b__j_ is a house where passenger needs to be dropped off. All taxi ride requests are given in the increasing order of _t__j_. All _t__j_ are distinct. Output Print _m_ lines: the _j_-th line should contain two integer numbers, the answer for the _j_-th ride request — car number assigned by the operator and passenger wait time. Examples Input 5 2 2 1 5 10 3 5 20 4 1 Note In the first sample test, a request comes in at time 5 and the car needs to get from house 3 to house 2 to pick up the passenger. Therefore wait time will be 1 and the ride will be completed at time 5u2009+u20091u2009+u20096u2009=u200912. The second request comes in at time 9, so the passenger will have to wait for the car to become available at time 12, and then the car needs another 2 minutes to get from house 8 to house 10. So the total wait time is 3u2009+u20092u2009=u20095. In the second sample test, cars 1 and 2 are located at the same distance from the first passenger and have the same "wait time since it became available". Car 1 wins a tiebreaker according to the rules because it has the lowest number. It will come to house 3 at time 3, so the wait time will be 2. | 2,500 | false | false | false | false | true | false | false | false | false | false | 6,216 |
1249A | You are a coach of a group consisting of $$$n$$$ students. The $$$i$$$-th student has programming skill $$$a_i$$$. All students have distinct programming skills. You want to divide them into teams in such a way that: No two students $$$i$$$ and $$$j$$$ such that $$$a_i - a_j = 1$$$ belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than $$$1$$$); the number of teams is the minimum possible. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 100$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 100$$$) — the number of students in the query. The second line of the query contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 100$$$, all $$$a_i$$$ are distinct), where $$$a_i$$$ is the programming skill of the $$$i$$$-th student. Output For each query, print the answer on it — the minimum number of teams you can form if no two students $$$i$$$ and $$$j$$$ such that $$$a_i - a_j = 1$$$ may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than $$$1$$$) Example Input 4 4 2 10 1 20 2 3 6 5 2 3 4 99 100 1 42 Note In the first query of the example, there are $$$n=4$$$ students with the skills $$$a=[2, 10, 1, 20]$$$. There is only one restriction here: the $$$1$$$-st and the $$$3$$$-th students can't be in the same team (because of $$$a_1 - a_3=2-1=1$$$). It is possible to divide them into $$$2$$$ teams: for example, students $$$1$$$, $$$2$$$ and $$$4$$$ are in the first team and the student $$$3$$$ in the second team. In the second query of the example, there are $$$n=2$$$ students with the skills $$$a=[3, 6]$$$. It is possible to compose just a single team containing both students. | 800 | true | false | false | false | false | false | false | false | false | false | 4,464 |
1239D | In the Catowice city next weekend the cat contest will be held. However, the jury members and the contestants haven't been selected yet. There are $$$n$$$ residents and $$$n$$$ cats in the Catowice, and each resident has exactly one cat living in his house. The residents and cats are numbered with integers from $$$1$$$ to $$$n$$$, where the $$$i$$$-th cat is living in the house of $$$i$$$-th resident. Each Catowice resident is in friendship with several cats, including the one living in his house. In order to conduct a contest, at least one jury member is needed and at least one cat contestant is needed. Of course, every jury member should know none of the contestants. For the contest to be successful, it's also needed that the number of jury members plus the number of contestants is equal to $$$n$$$. Please help Catowice residents to select the jury and the contestants for the upcoming competition, or determine that it's impossible to do. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100,000$$$), the number of test cases. Then description of $$$t$$$ test cases follow, where each description is as follows: The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 le n le m le 10^6$$$), the number of Catowice residents and the number of friendship pairs between residents and cats. Each of the next $$$m$$$ lines contains integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$), denoting that $$$a_i$$$-th resident is acquaintances with $$$b_i$$$-th cat. It's guaranteed that each pair of some resident and some cat is listed at most once. It's guaranteed, that for every $$$i$$$ there exists a pair between $$$i$$$-th resident and $$$i$$$-th cat. Different test cases are separated with an empty line. It's guaranteed, that the sum of $$$n$$$ over all test cases is at most $$$10^6$$$ and that the sum of $$$m$$$ over all test cases is at most $$$10^6$$$. Output For every test case print: "No", if it's impossible to select the jury and contestants. Otherwise print "Yes".In the second line print two integers $$$j$$$ and $$$p$$$ ($$$1 le j$$$, $$$1 le p$$$, $$$j + p = n$$$)xa0— the number of jury members and the number of contest participants. In the third line print $$$j$$$ distinct integers from $$$1$$$ to $$$n$$$, the indices of the residents forming a jury. In the fourth line print $$$p$$$ distinct integers from $$$1$$$ to $$$n$$$, the indices of the cats, which will participate in the contest. In case there are several correct answers, print any of them. Example Input 4 3 4 1 1 2 2 3 3 1 3 3 7 1 1 1 2 1 3 2 2 3 1 3 2 3 3 1 1 1 1 2 4 1 1 1 2 2 1 2 2 Output Yes 2 1 1 3 2 Yes 1 2 2 1 3 No No Note In the first test case, we can select the first and the third resident as a jury. Both of them are not acquaintances with a second cat, so we can select it as a contestant. In the second test case, we can select the second resident as a jury. He is not an acquaintances with a first and a third cat, so they can be selected as contestants. In the third test case, the only resident is acquaintances with the only cat, so they can't be in the contest together. So it's not possible to make a contest with at least one jury and at least one cat. In the fourth test case, each resident is acquaintances with every cat, so it's again not possible to make a contest with at least one jury and at least one cat. | 2,400 | false | false | false | false | false | false | false | false | false | true | 4,493 |
1907A | Problem - 1907A - 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 ") Editorial") xa0— the number of test cases. The descriptions of test cases follow. Each test case contains one string of two characters, description of the square where rook is positioned. The first character is a letter from a to h, the label of column, and the second character is a digit from 1 to 8, the label of row. The same position may occur in more than one test case. Output For each test case, output descriptions of all squares where the rook can move, in the same format as in the input. You can output squares in any order per test case. Example Input 1 d5 Output d1 d2 b5 g5 h5 d3 e5 f5 d8 a5 d6 d7 c5 d4 | 800 | false | false | true | false | false | false | false | false | false | false | 864 |
1615A | There are $$$n$$$ block towers in a row, where tower $$$i$$$ has a height of $$$a_i$$$. You're part of a building crew, and you want to make the buildings look as nice as possible. In a single day, you can perform the following operation: Choose two indices $$$i$$$ and $$$j$$$ ($$$1 leq i, j leq n$$$; $$$i eq j$$$), and move a block from tower $$$i$$$ to tower $$$j$$$. This essentially decreases $$$a_i$$$ by $$$1$$$ and increases $$$a_j$$$ by $$$1$$$. You think the ugliness of the buildings is the height difference between the tallest and shortest buildings. Formally, the ugliness is defined as $$$max(a)-min(a)$$$. What's the minimum possible ugliness you can achieve, after any number of days? Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. Then $$$t$$$ cases follow. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 100$$$)xa0— the number of buildings. The second line of each test case contains $$$n$$$ space separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^7$$$)xa0— the heights of the buildings. Output For each test case, output a single integerxa0— the minimum possible ugliness of the buildings. Example Input 3 3 10 10 10 4 3 2 1 2 5 1 2 3 1 5 Note In the first test case, the ugliness is already $$$0$$$. In the second test case, you should do one operation, with $$$i = 1$$$ and $$$j = 3$$$. The new heights will now be $$$[2, 2, 2, 2]$$$, with an ugliness of $$$0$$$. In the third test case, you may do three operations: 1. with $$$i = 3$$$ and $$$j = 1$$$. The new array will now be $$$[2, 2, 2, 1, 5]$$$, 2. with $$$i = 5$$$ and $$$j = 4$$$. The new array will now be $$$[2, 2, 2, 2, 4]$$$, 3. with $$$i = 5$$$ and $$$j = 3$$$. The new array will now be $$$[2, 2, 3, 2, 3]$$$. The resulting ugliness is $$$1$$$. It can be proven that this is the minimum possible ugliness for this test. | 800 | true | true | false | false | false | false | false | false | false | false | 2,564 |
1909E | You have $$$n$$$ lamps, numbered from $$$1$$$ to $$$n$$$. Initially, all the lamps are turned off. You also have $$$n$$$ buttons. The $$$i$$$-th button toggles all the lamps whose index is a multiple of $$$i$$$. When a lamp is toggled, if it was off it turns on, and if it was on it turns off. You have to press some buttons according to the following rules. You have to press at least one button. You cannot press the same button multiple times. You are given $$$m$$$ pairs $$$(u_i, v_i)$$$. If you press the button $$$u_i$$$, you also have to press the button $$$v_i$$$ (at any moment, not necessarily after pressing the button $$$u_i$$$). Note that, if you press the button $$$v_i$$$, you don't need to press the button $$$u_i$$$. You don't want to waste too much electricity. Find a way to press buttons such that at the end at most $$$lfloor n/5 floor$$$ lamps are on, or print $$$-1$$$ if it is impossible. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$0 leq m leq 2 cdot 10^5$$$)xa0— the number of lamps and the number of pairs, respectively. Each of the next $$$m$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$). If you press the button $$$u_i$$$, you also have to press the button $$$v_i$$$. It is guaranteed that the pairs $$$(u_i, v_i)$$$ are distinct. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Output For each test case: If there is no choice of buttons that makes at most $$$lfloor n/5 floor$$$ lamps on at the end, output a single line containing $$$-1$$$. Otherwise, output two lines. The first line should contain an integer $$$k$$$ ($$$1 le k le n$$$)xa0— the number of pressed buttons. The second line should contain $$$k$$$ integers $$$b_1, b_2, dots, b_k$$$ ($$$1 le b_i le n$$$)xa0— the indices of the pressed buttons (in any order). The $$$b_i$$$ must be distinct, and at the end at most $$$lfloor n/5 floor$$$ lamps must be turned on. Example Input 4 4 0 5 2 4 1 5 1 15 9 7 8 8 9 9 10 10 9 11 1 12 2 13 3 14 4 15 5 5 4 1 2 2 3 3 4 4 5 Output -1 4 3 5 1 2 3 8 9 10 1 5 Note In the first test case, you need to turn at most $$$lfloor 4/5 floor$$$ lamps on, which means that no lamp can be turned on. You can show that no choice of at least one button turns $$$0$$$ lamps on. In the second test case, you can press buttons $$$3$$$, $$$5$$$, $$$1$$$, $$$2$$$. Initially, all the lamps are off; after pressing button $$$3$$$, the lamps whose index is a multiple of $$$3$$$ (i.e., $$$3$$$) are toggled, so lamp $$$3$$$ is turned on; after pressing button $$$5$$$, the lamps whose index is a multiple of $$$5$$$ (i.e., $$$5$$$) are toggled, so lamps $$$3$$$, $$$5$$$ are turned on; after pressing button $$$1$$$, the lamps whose index is a multiple of $$$1$$$ (i.e., $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$) are toggled, so lamps $$$1$$$, $$$2$$$, $$$4$$$ are turned on; after pressing button $$$2$$$, the lamps whose index is a multiple of $$$2$$$ (i.e., $$$2$$$, $$$4$$$) are toggled, so lamp $$$1$$$ is turned on. This is valid because you pressed at least one button; you pressed all the buttons at most once; you pressed button $$$u_2 = 5$$$, which means that you had to also press button $$$v_2 = 1$$$: in fact, button $$$1$$$ has been pressed; at the end, only lamp $$$1$$$ is on. In the third test case, pressing the buttons $$$8$$$, $$$9$$$, $$$10$$$ turns only the lamps $$$8$$$, $$$9$$$, $$$10$$$ on. | 2,400 | true | false | false | false | false | true | true | false | false | false | 853 |
1466F | You may know that Euclid was a mathematician. Well, as it turns out, Morpheus knew it too. So when he wanted to play a mean trick on Euclid, he sent him an appropriate nightmare. In his bad dream Euclid has a set $$$S$$$ of $$$n$$$ $$$m$$$-dimensional vectors over the $$$mathbb{Z}_2$$$ field and can perform vector addition on them. In other words he has vectors with $$$m$$$ coordinates, each one equal either $$$0$$$ or $$$1$$$. Vector addition is defined as follows: let $$$u+v = w$$$, then $$$w_i = (u_i + v_i) bmod 2$$$. Euclid can sum any subset of $$$S$$$ and archive another $$$m$$$-dimensional vector over $$$mathbb{Z}_2$$$. In particular, he can sum together an empty subset; in such a case, the resulting vector has all coordinates equal $$$0$$$. Let $$$T$$$ be the set of all the vectors that can be written as a sum of some vectors from $$$S$$$. Now Euclid wonders the size of $$$T$$$ and whether he can use only a subset $$$S'$$$ of $$$S$$$ to obtain all the vectors from $$$T$$$. As it is usually the case in such scenarios, he will not wake up until he figures this out. So far, things are looking rather grim for the philosopher. But there is hope, as he noticed that all vectors in $$$S$$$ have at most $$$2$$$ coordinates equal $$$1$$$. Help Euclid and calculate $$$T$$$, the number of $$$m$$$-dimensional vectors over $$$mathbb{Z}_2$$$ that can be written as a sum of some vectors from $$$S$$$. As it can be quite large, calculate it modulo $$$10^9+7$$$. You should also find $$$S'$$$, the smallest such subset of $$$S$$$, that all vectors in $$$T$$$ can be written as a sum of vectors from $$$S'$$$. In case there are multiple such sets with a minimal number of elements, output the lexicographically smallest one with respect to the order in which their elements are given in the input. Consider sets $$$A$$$ and $$$B$$$ such that $$$A = B$$$. Let $$$a_1, a_2, dots a_{A}$$$ and $$$b_1, b_2, dots b_{B}$$$ be increasing arrays of indices elements of $$$A$$$ and $$$B$$$ correspondingly. $$$A$$$ is lexicographically smaller than $$$B$$$ iff there exists such $$$i$$$ that $$$a_j = b_j$$$ for all $$$j < i$$$ and $$$a_i < b_i$$$. Input In the first line of input, there are two integers $$$n$$$, $$$m$$$ ($$$1 leq n, m leq 5 cdot 10^5$$$) denoting the number of vectors in $$$S$$$ and the number of dimensions. Next $$$n$$$ lines contain the description of the vectors in $$$S$$$. In each of them there is an integer $$$k$$$ ($$$1 leq k leq 2$$$) and then follow $$$k$$$ distinct integers $$$x_1, dots x_k$$$ ($$$1 leq x_i leq m$$$). This encodes an $$$m$$$-dimensional vector having $$$1$$$s on coordinates $$$x_1, dots x_k$$$ and $$$0$$$s on the rest of them. Among the $$$n$$$ vectors, no two are the same. Output In the first line, output two integers: remainder modulo $$$10^9+7$$$ of $$$T$$$ and $$$S'$$$. In the second line, output $$$S'$$$ numbers, indices of the elements of $$$S'$$$ in ascending order. The elements of $$$S$$$ are numbered from $$$1$$$ in the order they are given in the input. Note In the first example we are given three vectors: $$$10$$$ $$$01$$$ $$$11$$$ It turns out that we can represent all vectors from our $$$2$$$-dimensional space using these vectors: $$$00$$$ is a sum of the empty subset of above vectors; $$$01 = 11 + 10$$$, is a sum of the first and third vector; $$$10 = 10$$$, is just the first vector; $$$11 = 10 + 01$$$, is a sum of the first and the second vector. Hence, $$$T = {00, 01, 10, 11}$$$. We can choose any two of the three vectors from $$$S$$$ and still be able to obtain all the vectors in $$$T$$$. In such a case, we choose the two vectors which appear first in the input. Since we cannot obtain all vectors in $$$T$$$ using only a single vector from $$$S$$$, $$$S' = 2$$$ and $$$S' = {10, 01}$$$ (indices $$$1$$$ and $$$2$$$), as set $$${1, 2 }$$$ is lexicographically the smallest. We can represent all vectors from $$$T$$$, using only vectors from $$$S'$$$, as shown below: $$$00$$$ is a sum of the empty subset; $$$01 = 01$$$ is just the second vector; $$$10 = 10$$$ is just the first vector; $$$11 = 10 + 01$$$ is a sum of the first and the second vector. | 2,100 | true | true | false | false | false | false | false | false | true | true | 3,361 |
2023E | In the heart of an ancient kingdom grows the legendary Tree of Life — the only one of its kind and the source of magical power for the entire world. The tree consists of $$$n$$$ nodes. Each node of this tree is a magical source, connected to other such sources through magical channels (edges). In total, there are $$$n-1$$$ channels in the tree, with the $$$i$$$-th channel connecting nodes $$$v_i$$$ and $$$u_i$$$. Moreover, there exists a unique simple path through the channels between any two nodes in the tree. However, the magical energy flowing through these channels must be balanced; otherwise, the power of the Tree of Life may disrupt the natural order and cause catastrophic consequences. The sages of the kingdom discovered that when two magical channels converge at a single node, a dangerous "magical resonance vibration" occurs between them. To protect the Tree of Life and maintain its balance, it is necessary to select several paths and perform special rituals along them. A path is a sequence of distinct nodes $$$v_1, v_2, ldots, v_k$$$, where each pair of adjacent nodes $$$v_i$$$ and $$$v_{i+1}$$$ is connected by a channel. When the sages perform a ritual along such a path, the resonance vibration between the channels $$$(v_i, v_{i+1})$$$ and $$$(v_{i+1}, v_{i+2})$$$ is blocked for each $$$1 leq i leq k - 2$$$. The sages' task is to select the minimum number of paths and perform rituals along them to block all resonance vibrations. This means that for every pair of channels emanating from a single node, there must exist at least one selected path that contains both of these channels. Help the sages find the minimum number of such paths so that the magical balance of the Tree of Life is preserved, and its power continues to nourish the entire world! Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 4 cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 5 cdot 10^5$$$) — the number of nodes in the Tree of Life. The $$$i$$$-th of the following $$$n - 1$$$ lines of each test case contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 leq v_i < u_i leq n$$$) — the channel connecting nodes $$$v_i$$$ and $$$u_i$$$. It is guaranteed that there exists a unique simple path through the channels between any two nodes. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output a single integer — the minimum number of paths that the sages need to select to prevent a catastrophe. Example Input 5 4 1 2 2 3 3 4 2 1 2 4 1 2 1 3 1 4 8 3 7 2 4 1 2 2 5 3 6 1 3 3 8 6 2 3 1 2 3 6 1 5 1 4 Note In the first test case, there are two pairs of channels emanating from a single node: $$$(1, 2)$$$ and $$$(2, 3)$$$, $$$(2, 3)$$$ and $$$(3, 4)$$$. It is sufficient to perform the ritual along the path $$$1-2-3-4$$$. Thus, the answer is $$$1$$$. In the second test case, there are no pairs of channels emanating from a single node, so the answer is $$$0$$$. In the third test case, rituals can be performed along the paths $$$2-1-3$$$, $$$2-1-4$$$, and $$$3-1-4$$$. | 3,300 | false | true | false | true | false | false | false | false | false | false | 127 |
1666E | A revolution has recently happened in Segmentland. The new government is committed to equality, and they hired you to help with land redistribution in the country. Segmentland is a segment of length $$$l$$$ kilometers, with the capital in one of its ends. There are $$$n$$$ citizens in Segmentland, the home of $$$i$$$-th citizen is located at the point $$$a_i$$$ kilometers from the capital. No two homes are located at the same point. Each citizen should receive a segment of positive length with ends at integer distances from the capital that contains her home. The union of these segments should be thexa0whole of Segmentland, and they should not have common points besides their ends. To ensure equality, the difference between the lengths of the longest and the shortest segments should be as small as possible. Input The first line of the input contains two integers $$$l$$$ and $$$n$$$ ($$$2 leq l leq 10^9; 1 leq n leq 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 < a_1 < a_2 < dots < a_n < l$$$). Output Output $$$n$$$ pairs of numbers $$$s_i, f_i$$$ ($$$0 leq s_i < f_i leq l$$$), one pair per line. The pair on $$$i$$$-th line denotes thexa0ends of the $$$[s_i, f_i]$$$ segment that $$$i$$$-th citizen receives. If there are many possible arrangements with the same difference between the lengths of the longest and the shortest segments, you can output any of them. Note In the first example, it is possible to make all segments equal. Viva la revolucion! In the second example, citizens live close to the capital, so the length of the shortest segment is 2 and thexa0length of the longest segment is 8. | 2,500 | true | true | false | false | false | true | false | true | false | false | 2,281 |
496D | Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores _t_ points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of _s_ sets, he wins the match and the match is over. Here _s_ and _t_ are some positive integer numbers. To spice it up, Petya and Gena choose new numbers _s_ and _t_ before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores _t_ points and the match is over as soon as one of the players wins _s_ sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers _s_ and _t_ for the given match are also lost. The players now wonder what values of _s_ and _t_ might be. Can you determine all the possible options? Input The first line contains a single integer _n_xa0— the length of the sequence of games (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ space-separated integers _a__i_. If _a__i_u2009=u20091, then the _i_-th serve was won by Petya, if _a__i_u2009=u20092, then the _i_-th serve was won by Gena. It is not guaranteed that at least one option for numbers _s_ and _t_ corresponds to the given record. Output In the first line print a single number _k_xa0— the number of options for numbers _s_ and _t_. In each of the following _k_ lines print two integers _s__i_ and _t__i_xa0— the option for numbers _s_ and _t_. Print the options in the order of increasing _s__i_, and for equal _s__i_xa0— in the order of increasing _t__i_. | 1,900 | false | false | false | false | false | false | false | true | false | false | 7,848 |
515A | Problem - 515A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *1000 No tag edit access → Contest materials and Varda's home is located in point (_a_,u2009_b_). In each step, he can move in a unit distance in horizontal or vertical direction. In other words, from position (_x_,u2009_y_) he can go to positions (_x_u2009+u20091,u2009_y_), (_x_u2009-u20091,u2009_y_), (_x_,u2009_y_u2009+u20091) or (_x_,u2009_y_u2009-u20091). Unfortunately, Drazil doesn't have sense of direction. So he randomly chooses the direction he will go to in each step. He may accidentally return back to his house during his travel. Drazil may even not notice that he has arrived to (_a_,u2009_b_) and continue travelling. Luckily, Drazil arrived to the position (_a_,u2009_b_) successfully. Drazil said to Varda: "It took me exactly _s_ steps to travel from my house to yours". But Varda is confused about his words, she is not sure that it is possible to get from (0,u20090) to (_a_,u2009_b_) in exactly _s_ steps. Can you find out if it is possible for Varda? Input You are given three integers _a_, _b_, and _s_ (u2009-u2009109u2009≤u2009_a_,u2009_b_u2009≤u2009109, 1u2009≤u2009_s_u2009≤u20092·109) in a single line. Output If you think Drazil made a mistake and it is impossible to take exactly _s_ steps and get from his home to Varda's home, print "No" (without quotes). Otherwise, print "Yes". Examples Input 5 5 11 Output No Input 10 15 25 Output Yes Input 0 5 1 Output No Input 0 0 2 Output Yes Note In fourth sample case one possible route is: . | 1,000 | true | false | false | false | false | false | false | false | false | false | 7,775 |
151B | Winters are just damn freezing cold in Nvodsk! That's why a group of _n_ friends prefers to take a taxi, order a pizza and call girls. The phone numbers in the city consist of three pairs of digits (for example, 12-34-56). Each friend has a phonebook of size _s__i_ (that's the number of phone numbers). We know that taxi numbers consist of six identical digits (for example, 22-22-22), the numbers of pizza deliveries should necessarily be decreasing sequences of six different digits (for example, 98-73-21), all other numbers are the girls' numbers. You are given your friends' phone books. Calculate which friend is best to go to when you are interested in each of those things (who has maximal number of phone numbers of each type). If the phone book of one person contains some number two times, you should count it twice. That is, each number should be taken into consideration the number of times it occurs in the phone book. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of friends. Then follow _n_ data blocks that describe each friend's phone books. Each block is presented in the following form: first goes the line that contains integer _s__i_ and string _name__i_ (0u2009≤u2009_s__i_u2009≤u2009100) — the number of phone numbers in the phone book of the _i_-th friend and the name of the _i_-th friend. The name is a non-empty sequence of uppercase and lowercase Latin letters, containing no more than 20 characters. Next _s__i_ lines contain numbers as "XX-XX-XX", where X is arbitrary digits from 0 to 9. Output In the first line print the phrase "If you want to call a taxi, you should call: ". Then print names of all friends whose phone books contain maximal number of taxi phone numbers. In the second line print the phrase "If you want to order a pizza, you should call: ". Then print names of all friends who have maximal number of pizza phone numbers. In the third line print the phrase "If you want to go to a cafe with a wonderful girl, you should call: ". Then print names of all friends who have maximal number of girls' phone numbers. Print the names in the order in which they are given in the input data. Separate two consecutive names with a comma and a space. Each line should end with exactly one point. For clarifications concerning the output form, see sample tests. It is necessary that you follow the output form strictly. Extra spaces are not allowed. Examples Input 4 2 Fedorov 22-22-22 98-76-54 3 Melnikov 75-19-09 23-45-67 99-99-98 7 Rogulenko 22-22-22 11-11-11 33-33-33 44-44-44 55-55-55 66-66-66 95-43-21 3 Kaluzhin 11-11-11 99-99-99 98-65-32 Output If you want to call a taxi, you should call: Rogulenko. If you want to order a pizza, you should call: Fedorov, Rogulenko, Kaluzhin. If you want to go to a cafe with a wonderful girl, you should call: Melnikov. Input 3 5 Gleb 66-66-66 55-55-55 01-01-01 65-43-21 12-34-56 3 Serega 55-55-55 87-65-43 65-55-21 5 Melnik 12-42-12 87-73-01 36-04-12 88-12-22 82-11-43 Output If you want to call a taxi, you should call: Gleb. If you want to order a pizza, you should call: Gleb, Serega. If you want to go to a cafe with a wonderful girl, you should call: Melnik. Input 3 3 Kulczynski 22-22-22 65-43-21 98-12-00 4 Pachocki 11-11-11 11-11-11 11-11-11 98-76-54 0 Smietanka Output If you want to call a taxi, you should call: Pachocki. If you want to order a pizza, you should call: Kulczynski, Pachocki. If you want to go to a cafe with a wonderful girl, you should call: Kulczynski. Note In the first sample you are given four friends. Fedorov's phone book contains one taxi number and one pizza delivery number, Melnikov's phone book only has 3 numbers of girls, Rogulenko's one has 6 taxi numbers and one pizza delivery number, Kaluzhin's one contains 2 taxi numbers and one pizza delivery number. Thus, if you need to order a taxi, you should obviously call Rogulenko, if you need to order a pizza you should call anybody of the following: Rogulenko, Fedorov, Kaluzhin (each of them has one number). Melnikov has maximal number of phone numbers of girls. | 1,200 | false | false | true | false | false | false | false | false | false | false | 9,270 |
1204D2 | The only difference between easy and hard versions is the length of the string. You can hack this problem if you solve it. But you can hack the previous problem only if you solve both problems. Kirk has a binary string $$$s$$$ (a string which consists of zeroes and ones) of length $$$n$$$ and he is asking you to find a binary string $$$t$$$ of the same length which satisfies the following conditions: For any $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$) the length of the longest non-decreasing subsequence of the substring $$$s_{l}s_{l+1} ldots s_{r}$$$ is equal to the length of the longest non-decreasing subsequence of the substring $$$t_{l}t_{l+1} ldots t_{r}$$$; The number of zeroes in $$$t$$$ is the maximum possible. A non-decreasing subsequence of a string $$$p$$$ is a sequence of indices $$$i_1, i_2, ldots, i_k$$$ such that $$$i_1 < i_2 < ldots < i_k$$$ and $$$p_{i_1} leq p_{i_2} leq ldots leq p_{i_k}$$$. The length of the subsequence is $$$k$$$. If there are multiple substrings which satisfy the conditions, output any. Input The first line contains a binary string of length not more than $$$10^5$$$. Output Output a binary string which satisfied the above conditions. If there are many such strings, output any of them. Examples Input 0111001100111011101000 Output 0011001100001011101000 Note In the first example: For the substrings of the length $$$1$$$ the length of the longest non-decreasing subsequnce is $$$1$$$; For $$$l = 1, r = 2$$$ the longest non-decreasing subsequnce of the substring $$$s_{1}s_{2}$$$ is $$$11$$$ and the longest non-decreasing subsequnce of the substring $$$t_{1}t_{2}$$$ is $$$01$$$; For $$$l = 1, r = 3$$$ the longest non-decreasing subsequnce of the substring $$$s_{1}s_{3}$$$ is $$$11$$$ and the longest non-decreasing subsequnce of the substring $$$t_{1}t_{3}$$$ is $$$00$$$; For $$$l = 2, r = 3$$$ the longest non-decreasing subsequnce of the substring $$$s_{2}s_{3}$$$ is $$$1$$$ and the longest non-decreasing subsequnce of the substring $$$t_{2}t_{3}$$$ is $$$1$$$; The second example is similar to the first one. | 2,100 | true | true | false | false | true | false | false | false | false | false | 4,671 |
1884B | You are given a number in binary representation consisting of exactly $$$n$$$ bits, possibly, with leading zeroes. For example, for $$$n = 5$$$ the number $$$6$$$ will be given as $$$00110$$$, and for $$$n = 4$$$ the number $$$9$$$ will be given as $$$1001$$$. Let's fix some integer $$$i$$$ such that $$$1 le i le n$$$. In one operation you can swap any two adjacent bits in the binary representation. Your goal is to find the smallest number of operations you are required to perform to make the number divisible by $$$2^i$$$, or say that it is impossible. Please note that for each $$$1 le i le n$$$ you are solving the problem independently. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the binary representation of the number. The second line of each test case contains a string of length $$$n$$$, consisting of $$$0$$$ and $$$1$$$,xa0— the binary representation of the number. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, for each $$$1 le i le n$$$ output the smallest number of operations required to make the number divisible by $$$2^i$$$, or output $$$-1$$$ if it is impossible. Example Input 6 1 1 2 01 3 010 5 10101 7 0000111 12 001011000110 Output -1 1 -1 0 1 -1 1 3 -1 -1 -1 3 6 9 12 -1 -1 -1 0 2 4 6 10 15 20 -1 -1 -1 -1 -1 Note In the first test case, we cannot swap any elements, and the number $$$1$$$ is not divisible by $$$2$$$. In the second test case, the initial number is $$$1$$$. It is not divisible by $$$2$$$, but if we perform the operation, then we obtain the number with binary representation $$$10$$$, which is equal to $$$2$$$ in decimal representation, thus, it is divisible by $$$2$$$. But this number is not divisible by $$$4$$$ and we cannot obtain any other number using the operations. In the third test case, the initial number is $$$2$$$. For $$$i = 1$$$ we do not have to perform any operations since the initial number is divisible by $$$2$$$. For $$$i = 2$$$ we can perform one operation swapping the first two bits (we would obtain $$$100$$$ in binary representation, which corresponds to number $$$4$$$). There is no answer for $$$i = 3$$$. | 1,100 | true | true | false | false | false | false | false | true | false | false | 982 |
1632B | It has finally been decided to build a roof over the football field in School 179. Its construction will require placing $$$n$$$ consecutive vertical pillars. Furthermore, the headmaster wants the heights of all the pillars to form a permutation $$$p$$$ of integers from $$$0$$$ to $$$n - 1$$$, where $$$p_i$$$ is the height of the $$$i$$$-th pillar from the left $$$(1 le i le n)$$$. As the chief, you know that the cost of construction of consecutive pillars is equal to the maximum value of the bitwise XOR of heights of all pairs of adjacent pillars. In other words, the cost of construction is equal to $$$maxlimits_{1 le i le n - 1}{p_i oplus p_{i + 1}}$$$, where $$$oplus$$$ denotes the and $$$[1,0,3]$$$ is also not a permutation ($$$n=3$$$, but $$$3$$$ is in the array). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The only line for each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of pillars for the construction of the roof. 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 $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, $$$ldots$$$, $$$p_n$$$ — the sequence of pillar heights with the smallest construction cost. If there are multiple answers, print any of them. | 1,000 | false | false | false | false | false | true | false | false | false | false | 2,457 |
67B | Problem - 67B - Codeforces =============== xa0 . For example, if _n_u2009=u20095, a possible _A_ is {5,u20091,u20094,u20092,u20093}. For _k_u2009=u20092, _B_ is given by {1,u20092,u20091,u20090,u20090}. But if _k_u2009=u20093, then _B_u2009=u2009{1,u20091,u20090,u20090,u20090}. For two sequences _X_u2009=u2009{_x_1,u2009_x_2,u2009...,u2009_x__n_} and _Y_u2009=u2009{_y_1,u2009_y_2,u2009...,u2009_y__n_}, let _i_-th elements be the first elements such that _x__i_u2009≠u2009_y__i_. If _x__i_u2009<u2009_y__i_, then _X_ is lexicographically smaller than _Y_, while if _x__i_u2009>u2009_y__i_, then _X_ is lexicographically greater than _Y_. Given _n_, _k_ and _B_, you need to determine the lexicographically smallest _A_. Input The first line contains two space separated integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_k_u2009≤u2009_n_). On the second line are _n_ integers specifying the values of _B_u2009=u2009{_b_1,u2009_b_2,u2009...,u2009_b__n_}. Output Print on a single line _n_ integers of _A_u2009=u2009{_a_1,u2009_a_2,u2009...,u2009_a__n_} such that _A_ is lexicographically minimal. It is guaranteed that the solution exists. Examples Input 5 2 1 2 1 0 0 Output 4 1 5 2 3 Input 4 2 1 0 0 0 Output 2 3 1 4 | 1,800 | false | true | false | false | false | false | false | false | false | false | 9,635 |
1365A | Ashish and Vivek play a game on a matrix consisting of $$$n$$$ rows and $$$m$$$ columns, where they take turns claiming cells. Unclaimed cells are represented by $$$0$$$, while claimed cells are represented by $$$1$$$. The initial state of the matrix is given. There can be some claimed cells in the initial state. In each turn, a player must claim a cell. A cell may be claimed if it is unclaimed and does not share a row or column with any other already claimed cells. When a player is unable to make a move, he loses and the game ends. If Ashish and Vivek take turns to move and Ashish goes first, determine the winner of the game if both of them are playing optimally. Optimal play between two players means that both players choose the best possible strategy to achieve the best possible outcome for themselves. Input The first line consists of a single integer $$$t$$$ $$$(1 le t le 50)$$$xa0— the number of test cases. The description of the test cases follows. The first line of each test case consists of two space-separated integers $$$n$$$, $$$m$$$ $$$(1 le n, m le 50)$$$xa0— the number of rows and columns in the matrix. The following $$$n$$$ lines consist of $$$m$$$ integers each, the $$$j$$$-th integer on the $$$i$$$-th line denoting $$$a_{i,j}$$$ $$$(a_{i,j} in {0, 1})$$$. Output For each test case if Ashish wins the game print "Ashish" otherwise print "Vivek" (without quotes). Example Input 4 2 2 0 0 0 0 2 2 0 0 0 1 2 3 1 0 1 1 1 0 3 3 1 0 0 0 0 0 1 0 0 Output Vivek Ashish Vivek Ashish Note For the first case: One possible scenario could be: Ashish claims cell $$$(1, 1)$$$, Vivek then claims cell $$$(2, 2)$$$. Ashish can neither claim cell $$$(1, 2)$$$, nor cell $$$(2, 1)$$$ as cells $$$(1, 1)$$$ and $$$(2, 2)$$$ are already claimed. Thus Ashish loses. It can be shown that no matter what Ashish plays in this case, Vivek will win. For the second case: Ashish claims cell $$$(1, 1)$$$, the only cell that can be claimed in the first move. After that Vivek has no moves left. For the third case: Ashish cannot make a move, so Vivek wins. For the fourth case: If Ashish claims cell $$$(2, 3)$$$, Vivek will have no moves left. | 1,100 | false | true | true | false | false | false | false | false | false | false | 3,857 |
1824B1 | This is the easy version of the problem. The only difference is that in this version $$$klemin(n,3)$$$. You can make hacks only if both versions of the problem are solved. Chtholly and the floating islands. LuoTianyi now lives in a world with $$$n$$$ floating islands. The floating islands are connected by $$$n-1$$$ undirected air routes, and any two of them can reach each other by passing the routes. That means, the $$$n$$$ floating islands form a tree. One day, LuoTianyi wants to meet her friends: Chtholly, Nephren, William, .... Totally, she wants to meet $$$k$$$ people. She doesn't know the exact positions of them, but she knows that they are in pairwise distinct islands. She define an island is good if and only if the sum of the distances$$$^{dagger}$$$ from it to the islands with $$$k$$$ people is the minimal among all the $$$n$$$ islands. Now, LuoTianyi wants to know that, if the $$$k$$$ people are randomly set in $$$k$$$ distinct of the $$$n$$$ islands, then what is the expect number of the good islands? You just need to tell her the expect number modulo $$$10^9+7$$$. $$$^{dagger}$$$The distance between two islands is the minimum number of air routes you need to take to get from one island to the other. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1le k le min(n,3), 1le n le 2cdot 10^5$$$) — the number of the islands and people respectively. Next $$$n−1$$$ lines describe the air routes. The $$$i$$$-th of them contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i,v_i le n, u_i eq v_i$$$) — the islands connected by the $$$i$$$-th air route. Output Print a single integer — the expect number of the good islands modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0$$$ ($$$operatorname{mod} M$$$). Output the integer equal to $$$p cdot q^{-1}$$$ $$$operatorname{mod} M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p$$$ ($$$operatorname{mod} M$$$). Note In the first example the air routes form the following tree: If the people are in the islands $$$1$$$ and $$$2$$$, then islands $$$1$$$ and $$$2$$$ will be good. The sum of the distances from island $$$1$$$ or $$$2$$$ to all the people is $$$1+0=1$$$, which is the minimal. While the sum of the distances from island $$$3$$$ to all the people is $$$2+1=3$$$, which is greater than $$$1$$$. Like this, when the people are in island $$$1$$$ and $$$3$$$, then islands $$$1,2$$$ and $$$3$$$ will be good. When the people are in islands $$$1$$$ and $$$4$$$, then islands $$$1,2,3$$$ and $$$4$$$ will be good. When the people are in islands $$$2$$$ and $$$3$$$, then islands $$$2$$$ and $$$3$$$ will be good. When the people are in islands $$$2$$$ and $$$4$$$, then islands $$$2,3$$$ and $$$4$$$ will be good. When the people are in islands $$$3$$$ and $$$4$$$, then islands $$$3$$$ and $$$4$$$ will be good. So the expect of the number of the good islands is $$$frac{16}{6}$$$, which equals to $$$666666674$$$ modulo $$$10^9+7$$$. In the second example the air routes form the following tree: There is always the only good island, so the expected number is $$$1$$$. | 1,800 | true | false | false | false | false | false | false | false | false | false | 1,336 |
1183A | Problem - 1183A - 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 . Output Print the nearest greater or equal interesting number for the given number $$$a$$$. In other words, print the interesting number $$$n$$$ such that $$$n ge a$$$ and $$$n$$$ is minimal. Examples Input 432 Output 435 Input 99 Output 103 Input 237 Output 237 Input 42 Output 44 | 800 | false | false | true | false | false | false | false | false | false | false | 4,802 |
1839D | There are $$$n$$$ colorful balls arranged in a row. The balls are painted in $$$n$$$ distinct colors, denoted by numbers from $$$1$$$ to $$$n$$$. The $$$i$$$-th ball from the left is painted in color $$$c_i$$$. You want to reorder the balls so that the $$$i$$$-th ball from the left has color $$$i$$$. Additionally, you have $$$k ge 1$$$ balls of color $$$0$$$ that you can use in the reordering process. Due to the strange properties of the balls, they can be reordered only by performing the following operations: 1. Place a ball of color $$$0$$$ anywhere in the sequence (between any two consecutive balls, before the leftmost ball or after the rightmost ball) while keeping the relative order of other balls. You can perform this operation no more than $$$k$$$ times, because you have only $$$k$$$ balls of color $$$0$$$. 2. Choose any ball of non-zero color such that at least one of the balls adjacent to him has color $$$0$$$, and move that ball (of non-zero color) anywhere in the sequence (between any two consecutive balls, before the leftmost ball or after the rightmost ball) while keeping the relative order of other balls. You can perform this operation as many times as you want, but for each operation you should pay $$$1$$$ coin. You can perform these operations in any order. After the last operation, all balls of color $$$0$$$ magically disappear, leaving a sequence of $$$n$$$ balls of non-zero colors. What is the minimum amount of coins you should spend on the operations of the second type, so that the $$$i$$$-th ball from the left has color $$$i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$ after the disappearance of all balls of color zero? It can be shown that under the constraints of the problem, it is always possible to reorder the balls in the required way. Solve the problem for all $$$k$$$ from $$$1$$$ to $$$n$$$. Note In the first test case there are $$$n = 6$$$ balls. The colors of the balls from left to right are $$$[, 2, 3, 1, 4, 6, 5 ,]$$$. Let's suppose $$$k = 1$$$. One of the ways to reorder the balls in the required way for $$$3$$$ coins: $$$[, 2, 3, 1, 4, 6, 5 ,]$$$ $$$xrightarrow{, 1 ,}$$$ $$$[, 2, 3, 1, 4, color{red}{0}, 6, 5 ,]$$$ $$$xrightarrow{, 2 ,}$$$ $$$[, 2, 3, color{blue}{4}, 1, 0, 6, 5 ,]$$$ $$$xrightarrow{, 2 ,}$$$ $$$[, color{blue}{1}, 2, 3, 4, 0, 6, 5 ,]$$$ $$$xrightarrow{, 2,}$$$ $$$[, 1, 2, 3, 4, 0, 5, color{blue}{6} ,]$$$ The number above the arrow is the operation type. Balls inserted on the operations of the first type are highlighted red; balls moved on the operations of second type are highlighted blue. It can be shown that for $$$k = 1$$$ it is impossible to rearrange balls in correct order for less than $$$3$$$ coins. Let's suppose $$$k = 2$$$. One of the ways to reorder the balls in the required way for $$$2$$$ coins: $$$[, 2, 3, 1, 4, 6, 5 ,]$$$ $$$xrightarrow{, 1 ,}$$$ $$$[, 2, 3, 1, 4, 6, color{red}{0}, 5,]$$$ $$$xrightarrow{, 2 ,}$$$ $$$[, 2, 3, 1, 4, 0, 5, color{blue}{6},]$$$ $$$xrightarrow{, 1 ,}$$$ $$$[, 2, 3, color{red}{0}, 1, 4, 0, 5, 6 ,]$$$ $$$xrightarrow{, 2 ,}$$$ $$$[, color{blue}{1}, 2, 3, 0, 4, 0, 5, 6,]$$$ Note that this sequence of operations is also correct for $$$k$$$ greater than $$$2$$$. It can be shown that for $$$k$$$ from $$$2$$$ to $$$6$$$ it is impossible to rearrange balls in correct order for less than $$$2$$$ coins. In the second test case the balls are already placed in the correct order, so answers for all $$$k$$$ are equal to $$$0$$$. | 2,100 | false | false | false | true | true | false | false | false | true | false | 1,255 |
1141B | Each day in Berland consists of $$$n$$$ hours. Polycarp likes time management. That's why he has a fixed schedule for each day — it is a sequence $$$a_1, a_2, dots, a_n$$$ (each $$$a_i$$$ is either $$$0$$$ or $$$1$$$), where $$$a_i=0$$$ if Polycarp works during the $$$i$$$-th hour of the day and $$$a_i=1$$$ if Polycarp rests during the $$$i$$$-th hour of the day. Days go one after another endlessly and Polycarp uses the same schedule for each day. What is the maximal number of continuous hours during which Polycarp rests? It is guaranteed that there is at least one working hour in a day. Input The first line contains $$$n$$$ ($$$1 le n le 2cdot10^5$$$) — number of hours per day. The second line contains $$$n$$$ integer numbers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 1$$$), where $$$a_i=0$$$ if the $$$i$$$-th hour in a day is working and $$$a_i=1$$$ if the $$$i$$$-th hour is resting. It is guaranteed that $$$a_i=0$$$ for at least one $$$i$$$. Output Print the maximal number of continuous hours during which Polycarp rests. Remember that you should consider that days go one after another endlessly and Polycarp uses the same schedule for each day. Note In the first example, the maximal rest starts in last hour and goes to the first hour of the next day. In the second example, Polycarp has maximal rest from the $$$4$$$-th to the $$$5$$$-th hour. In the third example, Polycarp has maximal rest from the $$$3$$$-rd to the $$$5$$$-th hour. In the fourth example, Polycarp has no rest at all. | 900 | false | false | true | false | false | false | false | false | false | false | 5,020 |
1361D | # Johnny and James Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes James Bond, Johnny’s favorite secret agent, has a new mission. There are n enemy bases, each of them is described by its coordinates so that we can think about them as points in the Cartesian plane. The bases can communicate with each other, sending a signal, which is the ray directed from the chosen point to the origin or in the opposite direction. The exception is the central base, which lies at the origin and can send a signal in any direction. When some two bases want to communicate, there are two possible scenarios. If they lie on the same line with the origin, one of them can send a signal directly to the other one. Otherwise, the signal is sent from the first base to the central, and then the central sends it to the second base. We denote the distance between two bases as the total Euclidean distance that a signal sent between them has to travel. Bond can damage all but some k bases, which he can choose arbitrarily. A damaged base can’t send or receive the direct signal but still can pass it between two working bases. In particular, James can damage the central base, and the signal can still be sent between any two undamaged bases as before, so the distance between them remains the same. What is the maximal sum of the distances between all pairs of remaining bases that 007 can achieve by damaging exactly n − k of them? # Input The first line contains two integers n and k (2 ≤ k ≤ n ≤ 5 · 10 5) — the total number of bases and number of bases that have to remain, respectively. Each of the next n lines contains two integers x and y (−10 9 ≤ x, y ≤ 10 9), i-th line contains coordinates of the i-th base. You can assume that no two points coincide and that one of them is (0 , 0) . # Output You should output one number — the maximal possible sum of distances between all pairs of some k from given bases. Your answer will be accepted if the absolute or relative error is less than 10 −6. Page 1 of 3 Examples standard input standard output 6 2 0 0 1 1 2 2 3 3 0 1 0 2 6.24264069 6 5 0 0 1 1 2 2 3 3 0 1 0 2 32.62741700 13 10 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 0 -2 0 1 0 2 237.00000000 10 5 2 2 4 4 3 5 6 10 0 5 0 0 5 0 10 0 0 10 4 7 181.52406315 # Note In the first example, in an optimal solution Bond doesn’t destroy bases with indices 4 and 6 (marked in orange): Page 2 of 3 123456The following picture represents an optimal solution for the second example. These bases are are not destroyed: 2, 3, 4, 5, 6 (marked in orange). 123456 An optimal solution for the third test is visible in the picture. Only bases 3, 4, 5 are destroyed. Again, the not destroyed bases are marked in orange. 1 2 3 4 5 6 7 8 9 10 11 12 13 Page 3 of 3 | 2,900 | true | true | true | false | false | false | false | false | false | false | 3,874 |
1971G | You are given an array $$$a$$$ consisting of $$$n$$$ nonnegative integers. You can swap the elements at positions $$$i$$$ and $$$j$$$ if $$$a_i~mathsf{XOR}~a_j < 4$$$, where $$$mathsf{XOR}$$$ is the 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 the array. The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 leq a_i leq 10^9$$$)xa0— the elements of the array. 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 $$$n$$$ integersxa0— the lexicographically smallest array that can be made with any number of swaps. Example Input 4 4 1 0 3 2 5 2 7 1 5 6 8 1 2 1 2 1 2 1 2 4 16 4 1 64 Output 0 1 2 3 1 5 2 6 7 1 1 1 1 2 2 2 2 16 4 1 64 Note For the first test case, you can swap any two elements, so we can produce the sorted array. For the second test case, you can swap $$$2$$$ and $$$1$$$ (their $$$mathsf{XOR}$$$ is $$$3$$$), $$$7$$$ and $$$5$$$ (their $$$mathsf{XOR}$$$ is $$$2$$$), and $$$7$$$ and $$$6$$$ (their $$$mathsf{XOR}$$$ is $$$1$$$) to get the lexicographically smallest array. | 1,700 | false | false | false | false | true | false | false | false | true | false | 459 |
313C | Ilya is a very good-natured lion. He likes maths. Of all mathematical objects, his favourite one is matrices. Now he's faced a complicated matrix problem he needs to solve. He's got a square 2_n_u2009×u20092_n_-sized matrix and 4_n_ integers. You need to arrange all these numbers in the matrix (put each number in a single individual cell) so that the beauty of the resulting matrix with numbers is maximum. The beauty of a 2_n_u2009×u20092_n_-sized matrix is an integer, obtained by the following algorithm: 1. Find the maximum element in the matrix. Let's denote it as _m_. 2. If _n_u2009=u20090, then the beauty of the matrix equals _m_. Otherwise, a matrix can be split into 4 non-intersecting 2_n_u2009-u20091u2009×u20092_n_u2009-u20091-sized submatrices, then the beauty of the matrix equals the sum of number _m_ and other four beauties of the described submatrices. As you can see, the algorithm is recursive. Help Ilya, solve the problem and print the resulting maximum beauty of the matrix. Input The first line contains integer 4_n_ (1u2009≤u20094_n_u2009≤u20092·106). The next line contains 4_n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109) — the numbers you need to arrange in the 2_n_u2009×u20092_n_-sized matrix. Output On a single line print the maximum value of the beauty of the described matrix. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Note Consider the second sample. You need to arrange the numbers in the matrix as follows: 1 2 3 4 Then the beauty of the matrix will equal: 4 + 1 + 2 + 3 + 4 = 14. | 1,400 | false | true | true | false | false | true | false | false | true | false | 8,593 |
1798A | You are given two arrays $$$a_1, a_2, ldots, a_n$$$ and $$$b_1, b_2, ldots, b_n$$$. In one operation, you can choose any integer $$$i$$$ from $$$1$$$ to $$$n$$$ and swap the numbers $$$a_i$$$ and $$$b_i$$$. Determine whether, after using any (possibly zero) number of operations, the following two conditions can be satisfied simultaneously: $$$a_n = max(a_1, a_2, ldots, a_n)$$$, $$$b_n = max(b_1, b_2, ldots, b_n)$$$. Here $$$max(c_1, c_2, ldots, c_k)$$$ denotes the maximum number among $$$c_1, c_2, ldots, c_k$$$. For example, $$$max(3, 5, 4) = 5$$$, $$$max(1, 7, 7) = 7$$$, $$$max(6, 2) = 6$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 200$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the length of the arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 100$$$)xa0— elements of the first array. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 100$$$)xa0— elements of the second array. Output For each test case, print "Yes" if after using any (possibly zero) number of operations the conditions described above are satisfied. Otherwise, print "No". You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 7 3 7 9 7 7 6 9 4 10 10 15 15 10 16 15 15 2 100 99 99 100 1 1 1 9 1 2 3 4 5 6 7 8 9 9 9 9 9 9 9 6 6 6 7 1 1 2 2 1 1 2 1 2 1 2 1 2 1 2 30 4 5 30 Output Yes No Yes Yes Yes No No Note In the first test case, you can swap the numbers $$$a_3$$$ and $$$b_3$$$, after which the array $$$a$$$ becomes equal to $$$[7, 9, 9]$$$, and the array $$$b$$$ becomes equal to $$$[7, 6, 7]$$$, and both conditions are met. In the second test case, it can be proved that it is impossible to satisfy both conditions. In the third test case, you can swap the numbers $$$a_1$$$ and $$$b_1$$$, after which the array $$$a$$$ becomes equal to $$$[99, 99]$$$, and the array $$$b$$$ becomes equal to $$$[100, 100]$$$, and both conditions are satisfied. In fifth test case, you can swap $$$a_7$$$ and $$$b_7$$$, $$$a_8$$$ and $$$b_8$$$, $$$a_9$$$ and $$$b_9$$$, after which the array $$$a$$$ becomes equal to $$$[1, 2, 3, 4, 5, 6, 6, 6, 6]$$$, and the array $$$b$$$ becomes equal to $$$[9, 9, 9, 9, 9, 9, 7, 8, 9]$$$, and both conditions are satisfied. | 800 | false | true | true | false | false | false | false | false | true | false | 1,490 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.