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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1015C | Ivan has $$$n$$$ songs on his phone. The size of the $$$i$$$-th song is $$$a_i$$$ bytes. Ivan also has a flash drive which can hold at most $$$m$$$ bytes in total. Initially, his flash drive is empty. Ivan wants to copy all $$$n$$$ songs to the flash drive. He can compress the songs. If he compresses the $$$i$$$-th song, the size of the $$$i$$$-th song reduces from $$$a_i$$$ to $$$b_i$$$ bytes ($$$b_i < a_i$$$). Ivan can compress any subset of the songs (possibly empty) and copy all the songs to his flash drive if the sum of their sizes is at most $$$m$$$. He can compress any subset of the songs (not necessarily contiguous). Ivan wants to find the minimum number of songs he needs to compress in such a way that all his songs fit on the drive (i.e. the sum of their sizes is less than or equal to $$$m$$$). If it is impossible to copy all the songs (even if Ivan compresses all the songs), print "-1". Otherwise print the minimum number of songs Ivan needs to compress. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5, 1 le m le 10^9$$$) — the number of the songs on Ivan's phone and the capacity of Ivan's flash drive. The next $$$n$$$ lines contain two integers each: the $$$i$$$-th line contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le 10^9$$$, $$$a_i > b_i$$$) — the initial size of the $$$i$$$-th song and the size of the $$$i$$$-th song after compression. Output If it is impossible to compress a subset of the songs in such a way that all songs fit on the flash drive, print "-1". Otherwise print the minimum number of the songs to compress. Note In the first example Ivan can compress the first and the third songs so after these moves the sum of sizes will be equal to $$$8 + 7 + 1 + 5 = 21 le 21$$$. Also Ivan can compress the first and the second songs, then the sum of sizes will be equal $$$8 + 4 + 3 + 5 = 20 le 21$$$. Note that compressing any single song is not sufficient to copy all the songs on the flash drive (for example, after compressing the second song the sum of sizes will be equal to $$$10 + 4 + 3 + 5 = 22 > 21$$$). In the second example even if Ivan compresses all the songs the sum of sizes will be equal $$$8 + 4 + 1 + 4 = 17 > 16$$$. | 1,100 | false | false | false | false | false | false | false | false | true | false | 5,617 |
1985B | Problem - 1985B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math number theory *800 No tag edit access → Contest materials ") Editorial") xa0— the number of test cases. Each test case contains a single integer $$$n$$$ ($$$2 leq n leq 100$$$). Output For each test case, output an integer, the optimal value of $$$x$$$. It can be shown there is only one unique answer. Example Input 2 3 15 Output 3 2 Note For $$$n = 3$$$, the possible values of $$$x$$$ are $$$2$$$ and $$$3$$$. The sum of all multiples of $$$2$$$ less than or equal to $$$n$$$ is just $$$2$$$, and the sum of all multiples of $$$3$$$ less than or equal to $$$n$$$ is $$$3$$$. Therefore, $$$3$$$ is the optimal value of $$$x$$$. For $$$n = 15$$$, the optimal value of $$$x$$$ is $$$2$$$. The sum of all multiples of $$$2$$$ less than or equal to $$$n$$$ is $$$2 + 4 + 6 + 8 + 10 + 12 + 14 = 56$$$, which can be proven to be the maximal over all other possible values of $$$x$$$. | 800 | true | false | false | false | false | false | true | false | false | false | 373 |
1000G | You are given a weighted tree (undirected connected graph with no cycles, loops or multiple edges) with $$$n$$$ vertices. The edge $$${u_j, v_j}$$$ has weight $$$w_j$$$. Also each vertex $$$i$$$ has its own value $$$a_i$$$ assigned to it. Let's call a path starting in vertex $$$u$$$ and ending in vertex $$$v$$$, where each edge can appear no more than twice (regardless of direction), a 2-path. Vertices can appear in the 2-path multiple times (even start and end vertices). For some 2-path $$$p$$$ profit $$$ ext{Pr}(p) = sumlimits_{v in ext{distinct vertices in } p}{a_v} - sumlimits_{e in ext{distinct edges in } p}{k_e cdot w_e}$$$, where $$$k_e$$$ is the number of times edge $$$e$$$ appears in $$$p$$$. That is, vertices are counted once, but edges are counted the number of times they appear in $$$p$$$. You are about to answer $$$m$$$ queries. Each query is a pair of vertices $$$(qu, qv)$$$. For each query find 2-path $$$p$$$ from $$$qu$$$ to $$$qv$$$ with maximal profit $$$ ext{Pr}(p)$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 3 cdot 10^5$$$, $$$1 le q le 4 cdot 10^5$$$) — the number of vertices in the tree and the number of queries. The second line contains $$$n$$$ space-separated integers $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le 10^9)$$$ — the values of the vertices. Next $$$n - 1$$$ lines contain descriptions of edges: each line contains three space separated integers $$$u_i$$$, $$$v_i$$$ and $$$w_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$, $$$1 le w_i le 10^9$$$) — there is edge $$${u_i, v_i}$$$ with weight $$$w_i$$$ in the tree. Next $$$q$$$ lines contain queries (one per line). Each query contains two integers $$$qu_i$$$ and $$$qv_i$$$ $$$(1 le qu_i, qv_i le n)$$$ — endpoints of the 2-path you need to find. Output For each query print one integer per line — maximal profit $$$ ext{Pr}(p)$$$ of the some 2-path $$$p$$$ with the corresponding endpoints. Example Input 7 6 6 5 5 3 2 1 2 1 2 2 2 3 2 2 4 1 4 5 1 6 4 2 7 3 25 1 1 4 4 5 6 6 4 3 4 3 7 Note Explanation of queries: 1. $$$(1, 1)$$$ — one of the optimal 2-paths is the following: $$$1 ightarrow 2 ightarrow 4 ightarrow 5 ightarrow 4 ightarrow 2 ightarrow 3 ightarrow 2 ightarrow 1$$$. $$$ ext{Pr}(p) = (a_1 + a_2 + a_3 + a_4 + a_5) - (2 cdot w(1,2) + 2 cdot w(2,3) + 2 cdot w(2,4) + 2 cdot w(4,5)) = 21 - 2 cdot 12 = 9$$$. 2. $$$(4, 4)$$$: $$$4 ightarrow 2 ightarrow 1 ightarrow 2 ightarrow 3 ightarrow 2 ightarrow 4$$$. $$$ ext{Pr}(p) = (a_1 + a_2 + a_3 + a_4) - 2 cdot (w(1,2) + w(2,3) + w(2,4)) = 19 - 2 cdot 10 = 9$$$. 3. $$$(5, 6)$$$: $$$5 ightarrow 4 ightarrow 2 ightarrow 3 ightarrow 2 ightarrow 1 ightarrow 2 ightarrow 4 ightarrow 6$$$. 4. $$$(6, 4)$$$: $$$6 ightarrow 4 ightarrow 2 ightarrow 1 ightarrow 2 ightarrow 3 ightarrow 2 ightarrow 4$$$. 5. $$$(3, 4)$$$: $$$3 ightarrow 2 ightarrow 1 ightarrow 2 ightarrow 4$$$. 6. $$$(3, 7)$$$: $$$3 ightarrow 2 ightarrow 1 ightarrow 2 ightarrow 4 ightarrow 5 ightarrow 4 ightarrow 2 ightarrow 3 ightarrow 7$$$. | 2,700 | false | false | false | true | true | false | false | false | false | false | 5,699 |
678E | The rules of Sith Tournament are well known to everyone. _n_ Sith take part in the Tournament. The Tournament starts with the random choice of two Sith who will fight in the first battle. As one of them loses, his place is taken by the next randomly chosen Sith who didn't fight before. Does it need to be said that each battle in the Sith Tournament ends with a death of one of opponents? The Tournament ends when the only Sith remains alive. Jedi Ivan accidentally appeared in the list of the participants in the Sith Tournament. However, his skills in the Light Side of the Force are so strong so he can influence the choice of participants either who start the Tournament or who take the loser's place after each battle. Of course, he won't miss his chance to take advantage of it. Help him to calculate the probability of his victory. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200918)xa0— the number of participants of the Sith Tournament. Each of the next _n_ lines contains _n_ real numbers, which form a matrix _p__ij_ (0u2009≤u2009_p__ij_u2009≤u20091). Each its element _p__ij_ is the probability that the _i_-th participant defeats the _j_-th in a duel. The elements on the main diagonal _p__ii_ are equal to zero. For all different _i_, _j_ the equality _p__ij_u2009+u2009_p__ji_u2009=u20091 holds. All probabilities are given with no more than six decimal places. Jedi Ivan is the number 1 in the list of the participants. Output Output a real numberxa0— the probability that Jedi Ivan will stay alive after the Tournament. Absolute or relative error of the answer must not exceed 10u2009-u20096. Examples Input 3 0.0 0.5 0.8 0.5 0.0 0.4 0.2 0.6 0.0 | 2,200 | true | false | false | true | false | false | false | false | false | false | 7,112 |
166D | The warehouse in your shop has _n_ shoe pairs. Each pair is characterized by two integers: its price _c__i_ and its size _s__i_. We know that on this very day all numbers _s__i_ are different, that is, there is no more than one pair of each size. The shop has _m_ customers who came at the same time. The customer number _i_ has _d__i_ money and the size of his feet equals _l__i_. The customer number _i_ can buy the pair number _j_, if _c__j_u2009≤u2009_d__i_, and also if _l__i_u2009=u2009_s__j_ or _l__i_u2009=u2009_s__j_u2009-u20091; that is, it is necessary that he has enough money to pay for the shoes. It is also necessary that the size of his feet equals to or is less by 1 than the size of the shoes he chooses. Your task is to sell some customers pairs of shoes (a pair per person) so as to maximize the sum of the sold pairs _c__j_ that is, the profit. It is guaranteed that each customer buys no more than one pair and each pair will be bought by no more than one customer. Input The first input line contains the only integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of shoe pairs in the warehouse. Then _n_ lines contain the descriptions of pairs of shoes as two integers _c__i_ and _s__i_ (1u2009≤u2009_c__i_,u2009_s__i_u2009≤u2009109), the numbers are separated by a space. It is guaranteed that all numbers _s__i_ are different. The next line contains an integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of customers in the shop. Next _m_ lines contain the customers' descriptions as two integers _d__i_ and _l__i_ (1u2009≤u2009_d__i_,u2009_l__i_u2009≤u2009109), the numbers are separated by a space. Output In the first line print the only integer — the maximum profit you can get from selling shoes. In the second line print an integer _k_ — the number of shoe pairs you will sell. In the following _k_ lines print the descriptions of the sold pairs — two space-separated integers where the first number is the customer's number and the second number is the number of the shoes the customer will buy. You can print pairs of numbers "the customer's number and the shoes' number" in any order, the customers and the pairs of shoes are numbered starting from 1 in the order in which they are given in the input. If there are several optimal answers, you are allowed to print any of them. Please do not use the %lld specificator to read or write 64-bit numbers in С++. It is preferred to use the cin, cout streams or the %I64d specificator instead. Examples Input 3 10 1 30 2 20 3 2 20 1 20 2 Input 3 10 4 20 5 30 6 2 70 4 50 5 | 2,500 | false | true | false | true | false | false | false | false | true | false | 9,197 |
1678B1 | This is the easy version of the problem. The only difference between the two versions is that the harder version asks additionally for a minimum number of subsegments. Tokitsukaze has a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones, $$$n$$$ is even. Now Tokitsukaze divides $$$s$$$ into the minimum number of contiguous subsegments, and for each subsegment, all bits in each subsegment are the same. After that, $$$s$$$ is considered good if the lengths of all subsegments are even. For example, if $$$s$$$ is "11001111", it will be divided into "11", "00" and "1111". Their lengths are $$$2$$$, $$$2$$$, $$$4$$$ respectively, which are all even numbers, so "11001111" is good. Another example, if $$$s$$$ is "1110011000", it will be divided into "111", "00", "11" and "000", and their lengths are $$$3$$$, $$$2$$$, $$$2$$$, $$$3$$$. Obviously, "1110011000" is not good. Tokitsukaze wants to make $$$s$$$ good by changing the values of some positions in $$$s$$$. Specifically, she can perform the operation any number of times: change the value of $$$s_i$$$ to '0' or '1'($$$1 leq i leq n$$$). Can you tell her the minimum number of operations to make $$$s$$$ good? Input The first contains a single positive integer $$$t$$$ ($$$1 leq t leq 10,000$$$)xa0— the number of test cases. For each test case, the first line contains a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$)xa0— the length of $$$s$$$, it is guaranteed that $$$n$$$ is even. The second line contains a binary string $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, one of the ways to make $$$s$$$ good is the following. Change $$$s_3$$$, $$$s_6$$$ and $$$s_7$$$ to '0', after that $$$s$$$ becomes "1100000000", it can be divided into "11" and "00000000", which lengths are $$$2$$$ and $$$8$$$ respectively. There are other ways to operate $$$3$$$ times to make $$$s$$$ good, such as "1111110000", "1100001100", "1111001100". In the second, third and fourth test cases, $$$s$$$ is good initially, so no operation is required. | 800 | false | false | true | false | false | false | false | false | false | false | 2,199 |
1693A | We have an array of length $$$n$$$. Initially, each element is equal to $$$0$$$ and there is a pointer located on the first element. We can do the following two kinds of operations any number of times (possibly zero) in any order: 1. If the pointer is not on the last element, increase the element the pointer is currently on by $$$1$$$. Then move it to the next element. 2. If the pointer is not on the first element, decrease the element the pointer is currently on by $$$1$$$. Then move it to the previous element. But there is one additional rule. After we are done, the pointer has to be on the first element. You are given an array $$$a$$$. Determine whether it's possible to obtain $$$a$$$ after some operations or not. Input The first line contains a single integer $$$t$$$ $$$(1le tle 1000)$$$ xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1le nle 2 cdot 10^5)$$$ xa0— the size of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$)xa0— elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print "Yes" (without quotes) if it's possible to obtain $$$a$$$ after some operations, and "No" (without quotes) otherwise. You can output "Yes" and "No" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). Example Input 7 2 1 0 4 2 -1 -1 0 4 1 -4 3 0 4 1 -1 1 -1 5 1 2 3 4 -10 7 2 -1 1 -2 0 0 0 1 0 Output No Yes No No Yes Yes Yes Note In the first test case we can obtain the array after some operations, but the pointer won't be on the first element. One way of obtaining the array in the second test case is shown below. $$$langle underline{0}, 0, 0, 0 angle o langle 1, underline{0}, 0, 0 angle o langle underline{1}, -1, 0, 0 angle o langle 2, underline{-1}, 0, 0 angle o langle 2, 0, underline{0}, 0 angle o langle 2, underline{0}, -1, 0 angle o langle underline{2}, -1, -1, 0 angle$$$ | 1,300 | false | true | false | false | false | false | false | false | false | false | 2,122 |
215D | The official capital and the cultural capital of Berland are connected by a single road running through _n_ regions. Each region has a unique climate, so the _i_-th (1u2009≤u2009_i_u2009≤u2009_n_) region has a stable temperature of _t__i_ degrees in summer. This summer a group of _m_ schoolchildren wants to get from the official capital to the cultural capital to visit museums and sights. The trip organizers transport the children between the cities in buses, but sometimes it is very hot. Specifically, if the bus is driving through the _i_-th region and has _k_ schoolchildren, then the temperature inside the bus is _t__i_u2009+u2009_k_ degrees. Of course, nobody likes it when the bus is hot. So, when the bus drives through the _i_-th region, if it has more than _T__i_ degrees inside, each of the schoolchild in the bus demands compensation for the uncomfortable conditions. The compensation is as large as _x__i_ rubles and it is charged in each region where the temperature in the bus exceeds the limit. To save money, the organizers of the trip may arbitrarily add or remove extra buses in the beginning of the trip, and between regions (of course, they need at least one bus to pass any region). The organizers can also arbitrarily sort the children into buses, however, each of buses in the _i_-th region will cost the organizers _cost__i_ rubles. Please note that sorting children into buses takes no money. Your task is to find the minimum number of rubles, which the organizers will have to spend to transport all schoolchildren. Input The first input line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009105;xa01u2009≤u2009_m_u2009≤u2009106) — the number of regions on the way and the number of schoolchildren in the group, correspondingly. Next _n_ lines contain four integers each: the _i_-th line contains _t__i_, _T__i_, _x__i_ and _cost__i_ (1u2009≤u2009_t__i_,u2009_T__i_,u2009_x__i_,u2009_cost__i_u2009≤u2009106). The numbers in the lines are separated by single spaces. Output Print the only integer — the minimum number of roubles the organizers will have to spend to transport all schoolchildren. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier. Note In the first sample the organizers will use only one bus to travel through the first region. However, the temperature in the bus will equal 30u2009+u200910u2009=u200940 degrees and each of 10 schoolchildren will ask for compensation. Only one bus will transport the group through the second region too, but the temperature inside won't exceed the limit. Overall, the organizers will spend 100u2009+u200910u2009+u200910u2009=u2009120 rubles. | 1,900 | false | true | false | false | false | false | false | false | false | false | 8,978 |
479D | Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler! However, there is no reason for disappointment, as Valery has found another ruler, its length is _l_ centimeters. The ruler already has _n_ marks, with which he can make measurements. We assume that the marks are numbered from 1 to _n_ in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance _l_ from the origin. This ruler can be repesented by an increasing sequence _a_1,u2009_a_2,u2009...,u2009_a__n_, where _a__i_ denotes the distance of the _i_-th mark from the origin (_a_1u2009=u20090, _a__n_u2009=u2009_l_). Valery believes that with a ruler he can measure the distance of _d_ centimeters, if there is a pair of integers _i_ and _j_ (1u2009≤u2009_i_u2009≤u2009_j_u2009≤u2009_n_), such that the distance between the _i_-th and the _j_-th mark is exactly equal to _d_ (in other words, _a__j_u2009-u2009_a__i_u2009=u2009_d_). Under the rules, the girls should be able to jump at least _x_ centimeters, and the boys should be able to jump at least _y_ (_x_u2009<u2009_y_) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances _x_ and _y_. Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances _x_ and _y_. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler. Input The first line contains four positive space-separated integers _n_, _l_, _x_, _y_ (2u2009≤u2009_n_u2009≤u2009105, 2u2009≤u2009_l_u2009≤u2009109, 1u2009≤u2009_x_u2009<u2009_y_u2009≤u2009_l_) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly. The second line contains a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009=u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_u2009=u2009_l_), where _a__i_ shows the distance from the _i_-th mark to the origin. Output In the first line print a single non-negative integer _v_ — the minimum number of marks that you need to add on the ruler. In the second line print _v_ space-separated integers _p_1,u2009_p_2,u2009...,u2009_p__v_ (0u2009≤u2009_p__i_u2009≤u2009_l_). Number _p__i_ means that the _i_-th mark should be at the distance of _p__i_ centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them. Examples Input 3 250 185 230 0 185 250 Input 4 250 185 230 0 20 185 250 Note In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark. In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks. In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills. | 1,700 | false | true | true | false | false | false | false | true | false | false | 7,913 |
2008H | Sakurako will soon take a test. The test can be described as an array of integers $$$n$$$ and a task on it: Given an integer $$$x$$$, Sakurako can perform the following operation any number of times: Choose an integer $$$i$$$ ($$$1le ile n$$$) such that $$$a_ige x$$$; Change the value of $$$a_i$$$ to $$$a_i-x$$$. Using this operation any number of times, she must find the minimum possible median$$$^{ ext{∗}}$$$ of the array $$$a$$$. Sakurako knows the array but does not know the integer $$$x$$$. Someone let it slip that one of the $$$q$$$ values of $$$x$$$ will be in the next test, so Sakurako is asking you what the answer is for each such $$$x$$$. $$$^{ ext{∗}}$$$The median of an array of length $$$n$$$ is the element that stands in the middle of the sorted array (at the $$$frac{n+2}{2}$$$-th position for even $$$n$$$, and at the $$$frac{n+1}{2}$$$-th for odd) Input The first line contains one integer $$$t$$$ ($$$1le tle 10^4$$$) xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1le n,qle 10^5$$$) xa0— the number of elements in the array and the number of queries. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1le a_ile n$$$) xa0— the elements of the array. The following $$$q$$$ lines each contain one integer $$$x$$$ ($$$1le xle n$$$). It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$. The same guarantee applies to the sum of $$$q$$$ across all test cases. Output For each test case, output $$$q$$$ integers xa0— the answer for each query. Example Input 2 5 5 1 2 3 4 5 1 2 3 4 5 6 3 1 2 6 4 1 3 2 1 5 | 2,100 | true | true | false | false | false | false | true | true | false | false | 201 |
329A | You are an adventurer currently journeying inside an evil temple. After defeating a couple of weak zombies, you arrived at a square room consisting of tiles forming an _n_u2009×u2009_n_ grid. The rows are numbered 1 through _n_ from top to bottom, and the columns are numbered 1 through _n_ from left to right. At the far side of the room lies a door locked with evil magical forces. The following inscriptions are written on the door: The cleaning of all evil will awaken the door! Being a very senior adventurer, you immediately realize what this means. You notice that every single cell in the grid are initially evil. You should purify all of these cells. The only method of tile purification known to you is by casting the "Purification" spell. You cast this spell on a single tile — then, all cells that are located in the same row and all cells that are located in the same column as the selected tile become purified (including the selected tile)! It is allowed to purify a cell more than once. You would like to purify all _n_u2009×u2009_n_ cells while minimizing the number of times you cast the "Purification" spell. This sounds very easy, but you just noticed that some tiles are particularly more evil than the other tiles. You cannot cast the "Purification" spell on those particularly more evil tiles, not even after they have been purified. They can still be purified if a cell sharing the same row or the same column gets selected by the "Purification" spell. Please find some way to purify all the cells with the minimum number of spells cast. Print -1 if there is no such way. Input The first line will contain a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100). Then, _n_ lines follows, each contains _n_ characters. The _j_-th character in the _i_-th row represents the cell located at row _i_ and column _j_. It will be the character 'E' if it is a particularly more evil cell, and '.' otherwise. Output If there exists no way to purify all the cells, output -1. Otherwise, if your solution casts _x_ "Purification" spells (where _x_ is the minimum possible number of spells), output _x_ lines. Each line should consist of two integers denoting the row and column numbers of the cell on which you should cast the "Purification" spell. Examples Input 5 EE.EE E.EE. E...E .EE.E EE.EE Note The first example is illustrated as follows. Purple tiles are evil tiles that have not yet been purified. Red tile is the tile on which "Purification" is cast. Yellow tiles are the tiles being purified as a result of the current "Purification" spell. Green tiles are tiles that have been purified previously. In the second example, it is impossible to purify the cell located at row 1 and column 1. For the third example: | 1,500 | false | true | false | false | false | true | false | false | false | false | 8,529 |
1360G | # A/B Matrix Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes You are given four positive integers n, m, a, b (1 ≤ b ≤ n ≤ 50 ; 1 ≤ a ≤ m ≤ 50 ). Find any such rectangular matrix of size n × m that satisfies all of the following conditions: • each row of the matrix contains exactly a ones; • each column of the matrix contains exactly b ones; • all other elements are zeros. If the desired matrix does not exist, indicate this. For example, for n = 3 , m = 6 , a = 2 , b = 1 , there exists a matrix satisfying the conditions above: ∣∣∣∣∣∣ 0 1 0 0 0 11 0 0 1 0 00 0 1 0 1 0 ∣∣∣∣∣∣ # Input The first line contains an integer t (1 ≤ t ≤ 1000 ) x16 the number of test cases. Then t test cases follow. Each test case is described by four positive integers n, m, a, b (1 ≤ b ≤ n ≤ 50 ; 1 ≤ a ≤ m ≤ 50 ), where n and m are the sizes of the matrix, and a and b are the number of ones for rows and columns, respectively. # Output For each test case print: • “YES” (without quotes) and the required matrix (if there are several answers, print any) if it exists, or • “NO” (without quotes) if it does not exist. To print the matrix n × m, print n rows, each of which consists of m numbers 0 or 1 describing a row of the matrix. Numbers must be printed without spaces . Page 1 of 2 Example standard input standard output 53 6 2 1 2 2 2 1 2 2 2 2 4 4 2 2 2 1 1 2 YES 010001 100100 001010 NO YES 11 11 YES 1100 1100 0011 0011 YES 11 Page 2 of 2 | 1,900 | true | true | false | false | false | true | false | false | false | false | 3,879 |
1406B | You are given an array of integers $$$a_1,a_2,ldots,a_n$$$. Find the maximum possible value of $$$a_ia_ja_ka_la_t$$$ among all five indices $$$(i, j, k, l, t)$$$ ($$$i<j<k<l<t$$$). Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1le tle 2 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$$$ ($$$5le nle 10^5$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$-3 imes 10^3le a_ile 3 imes 10^3$$$) — given array. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print one integer — the answer to the problem. Example Input 4 5 -1 -2 -3 -4 -5 6 -1 -2 -3 1 2 -1 6 -1 0 0 0 -1 -1 6 -9 -7 -5 -3 -2 1 Note In the first test case, choosing $$$a_1,a_2,a_3,a_4,a_5$$$ is a best choice: $$$(-1)cdot (-2) cdot (-3)cdot (-4)cdot (-5)=-120$$$. In the second test case, choosing $$$a_1,a_2,a_3,a_5,a_6$$$ is a best choice: $$$(-1)cdot (-2) cdot (-3)cdot 2cdot (-1)=12$$$. In the third test case, choosing $$$a_1,a_2,a_3,a_4,a_5$$$ is a best choice: $$$(-1)cdot 0cdot 0cdot 0cdot (-1)=0$$$. In the fourth test case, choosing $$$a_1,a_2,a_3,a_4,a_6$$$ is a best choice: $$$(-9)cdot (-7) cdot (-5)cdot (-3)cdot 1=945$$$. | 1,200 | false | true | true | true | false | false | true | false | true | false | 3,645 |
843B | This is an interactive problem. You are given a sorted in increasing order singly linked list. You should find the minimum integer in the list which is greater than or equal to _x_. More formally, there is a singly liked list built on an array of _n_ elements. Element with index _i_ contains two integers: _value__i_ is the integer value in this element, and _next__i_ that is the index of the next element of the singly linked list (or -1, if the current element is the last). The list is sorted, i.e. if _next__i_u2009≠u2009u2009-u20091, then _value__next__i_u2009>u2009_value__i_. You are given the number of elements in the list _n_, the index of the first element _start_, and the integer _x_. You can make up to 2000 queries of the following two types: ? i (1u2009≤u2009_i_u2009≤u2009_n_)xa0— ask the values _value__i_ and _next__i_, ! ansxa0— give the answer for the problem: the minimum integer, greater than or equal to _x_, or ! -1, if there are no such integers. Your program should terminate after this query. Write a program that solves this problem. Input The first line contains three integers _n_, _start_, _x_ (1u2009≤u2009_n_u2009≤u200950000, 1u2009≤u2009_start_u2009≤u2009_n_, 0u2009≤u2009_x_u2009≤u2009109)xa0— the number of elements in the list, the index of the first element and the integer _x_. Output To print the answer for the problem, print ! ans, where ans is the minimum integer in the list greater than or equal to _x_, or -1, if there is no such integer. Interaction To make a query of the first type, print ? i (1u2009≤u2009_i_u2009≤u2009_n_), where i is the index of element you want to know information about. After each query of type ? read two integers _value__i_ and _next__i_ (0u2009≤u2009_value__i_u2009≤u2009109, u2009-u20091u2009≤u2009_next__i_u2009≤u2009_n_, _next__i_u2009≠u20090). It is guaranteed that if _next__i_u2009≠u2009u2009-u20091, then _value__next__i_u2009>u2009_value__i_, and that the array values give a valid singly linked list with _start_ being the first element. Note that you can't ask more than 1999 queries of the type ?. If _next__i_u2009=u2009u2009-u20091 and _value__i_u2009=u2009u2009-u20091, then it means that you asked more queries than allowed, or asked an invalid query. Your program should immediately terminate (for example, by calling exit(0)). You will receive "Wrong Answer", it means that you asked more queries than allowed, or asked an invalid query. If you ignore this, you can get other verdicts since your program will continue to read from a closed stream. Your solution will get "Idleness Limit Exceeded", if you don't print anything or forget to flush the output, including the final answer. To flush you can use (just after printing a query and line end): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; For other languages see documentation. Hacks format For hacks, use the following format: In the first line print three integers _n_, _start_, _x_ (1u2009≤u2009_n_u2009≤u200950000, 1u2009≤u2009_start_u2009≤u2009_n_, 0u2009≤u2009_x_u2009≤u2009109). In the next _n_ lines print the description of the elements of the list: in the _i_-th line print two integers _value__i_ and _next__i_ (0u2009≤u2009_value__i_u2009≤u2009109, u2009-u20091u2009≤u2009_next__i_u2009≤u2009_n_, _next__i_u2009≠u20090). The printed structure should be a valid singly linked list. In particular, it should be possible to reach all elements from _start_ by following links _next__i_, and the last element _end_ should have -1 in the _next__end_. | 2,000 | false | false | false | false | false | false | true | false | false | false | 6,390 |
1800C2 | This is a hard version of the problem. It differs from the easy one only by constraints on $$$n$$$ and $$$t$$$. There is a deck of $$$n$$$ cards, each of which is characterized by its power. There are two types of cards: a hero card, the power of such a card is always equal to $$$0$$$; a bonus card, the power of such a card is always positive. You can do the following with the deck: take a card from the top of the deck; if this card is a bonus card, you can put it on top of your bonus deck or discard; if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards. Your task is to use such actions to gather an army with the maximum possible total power. Input The first line of input data contains single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases in the test. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of cards in the deck. The second line of each test case contains $$$n$$$ integers $$$s_1, s_2, dots, s_n$$$ ($$$0 le s_i le 10^9$$$)xa0— card powers in top-down order. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ numbers, each of which is the answer to the corresponding test case — the maximum possible total power of the army that can be achieved. Example Input 5 5 3 3 3 0 0 6 0 3 3 0 0 3 7 1 2 3 0 4 5 0 7 1 2 5 0 4 3 0 5 3 1 0 0 4 Note In the first sample, you can take bonuses $$$1$$$ and $$$2$$$. Both hero cards will receive $$$3$$$ power. If you take all the bonuses, one of them will remain unused. In the second sample, the hero's card on top of the deck cannot be powered up, and the rest can be powered up with $$$2$$$ and $$$3$$$ bonuses and get $$$6$$$ total power. In the fourth sample, you can take bonuses $$$1$$$, $$$2$$$, $$$3$$$, $$$5$$$ and skip the bonus $$$6$$$, then the hero $$$4$$$ will be enhanced with a bonus $$$3$$$ by $$$5$$$, and the hero $$$7$$$ with a bonus $$$5$$$ by $$$4$$$. $$$4+5=9$$$. | 1,100 | false | true | false | false | true | false | false | false | false | false | 1,472 |
789B | Masha really loves algebra. On the last lesson, her strict teacher Dvastan gave she new exercise. You are given geometric progression _b_ defined by two integers _b_1 and _q_. Remind that a geometric progression is a sequence of integers _b_1,u2009_b_2,u2009_b_3,u2009..., where for each _i_u2009>u20091 the respective term satisfies the condition _b__i_u2009=u2009_b__i_u2009-u20091·_q_, where _q_ is called the common ratio of the progression. Progressions in Uzhlyandia are unusual: both _b_1 and _q_ can equal 0. Also, Dvastan gave Masha _m_ "bad" integers _a_1,u2009_a_2,u2009...,u2009_a__m_, and an integer _l_. Masha writes all progression terms one by one onto the board (including repetitive) while condition _b__i_u2009≤u2009_l_ is satisfied (_x_ means absolute value of _x_). There is an exception: if a term equals one of the "bad" integers, Masha skips it (doesn't write onto the board) and moves forward to the next term. But the lesson is going to end soon, so Masha has to calculate how many integers will be written on the board. In order not to get into depression, Masha asked you for help: help her calculate how many numbers she will write, or print "inf" in case she needs to write infinitely many integers. Input The first line of input contains four integers _b_1, _q_, _l_, _m_ (-109u2009≤u2009_b_1,u2009_q_u2009≤u2009109, 1u2009≤u2009_l_u2009≤u2009109, 1u2009≤u2009_m_u2009≤u2009105)xa0— the initial term and the common ratio of progression, absolute value of maximal number that can be written on the board and the number of "bad" integers, respectively. The second line contains _m_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (-109u2009≤u2009_a__i_u2009≤u2009109)xa0— numbers that will never be written on the board. Output Print the only integer, meaning the number of progression terms that will be written on the board if it is finite, or "inf" (without quotes) otherwise. Examples Input 123 1 2143435 4 123 11 -5453 141245 Input 123 1 2143435 4 54343 -13 6 124 Note In the first sample case, Masha will write integers 3,u200912,u200924. Progression term 6 will be skipped because it is a "bad" integer. Terms bigger than 24 won't be written because they exceed _l_ by absolute value. In the second case, Masha won't write any number because all terms are equal 123 and this is a "bad" integer. In the third case, Masha will write infinitely integers 123. | 1,700 | true | false | true | false | false | false | true | false | false | false | 6,615 |
1976E | Initially, we had one array, which was a permutation of size $$$n$$$ (an array of size $$$n$$$ where each integer from $$$1$$$ to $$$n$$$ appears exactly once). We performed $$$q$$$ operations. During the $$$i$$$-th operation, we did the following: choose any array we have with at least $$$2$$$ elements; split it into two non-empty arrays (prefix and suffix); write two integers $$$l_i$$$ and $$$r_i$$$, where $$$l_i$$$ is the maximum element in the left part which we get after the split, and $$$r_i$$$ is the maximum element in the right part; remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool. For example, suppose the initial array was $$$[6, 3, 4, 1, 2, 5]$$$, and we performed the following operations: 1. choose the array $$$[6, 3, 4, 1, 2, 5]$$$ and split it into $$$[6, 3]$$$ and $$$[4, 1, 2, 5]$$$. Then we write $$$l_1 = 6$$$ and $$$r_1 = 5$$$, and the arrays we have are $$$[6, 3]$$$ and $$$[4, 1, 2, 5]$$$; 2. choose the array $$$[4, 1, 2, 5]$$$ and split it into $$$[4, 1, 2]$$$ and $$$[5]$$$. Then we write $$$l_2 = 4$$$ and $$$r_2 = 5$$$, and the arrays we have are $$$[6, 3]$$$, $$$[4, 1, 2]$$$ and $$$[5]$$$; 3. choose the array $$$[4, 1, 2]$$$ and split it into $$$[4]$$$ and $$$[1, 2]$$$. Then we write $$$l_3 = 4$$$ and $$$r_3 = 2$$$, and the arrays we have are $$$[6, 3]$$$, $$$[4]$$$, $$$[1, 2]$$$ and $$$[5]$$$. You are given two integers $$$n$$$ and $$$q$$$, and two sequences $$$[l_1, l_2, dots, l_q]$$$ and $$$[r_1, r_2, dots, r_q]$$$. A permutation of size $$$n$$$ is called valid if we can perform $$$q$$$ operations and produce the given sequences $$$[l_1, l_2, dots, l_q]$$$ and $$$[r_1, r_2, dots, r_q]$$$. Calculate the number of valid permutations. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le q < n le 3 cdot 10^5$$$). The second line contains $$$q$$$ integers $$$l_1, l_2, dots, l_q$$$ ($$$1 le l_i le n$$$). The third line contains $$$q$$$ integers $$$r_1, r_2, dots, r_q$$$ ($$$1 le r_i le n$$$). Additional constraint on the input: there exists at least one permutation which can produce the given sequences $$$[l_1, l_2, dots, l_q]$$$ and $$$[r_1, r_2, dots, r_q]$$$. | 2,500 | true | true | false | false | true | false | false | false | false | false | 429 |
1051D | Problem - 1051D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks dp *1700 No tag edit access → Contest materials — the number of columns in a grid and the number of components required. Output Print a single integer — the number of beautiful bicolorings modulo $$$998244353$$$. Examples Input 3 4 Output 12 Input 4 1 Output 2 Input 1 2 Output 2 Note One of possible bicolorings in sample $$$1$$$: | 1,700 | false | false | false | true | false | false | false | false | false | false | 5,452 |
1084B | The Fair Nut likes kvass very much. On his birthday parents presented him $$$n$$$ kegs of kvass. There are $$$v_i$$$ liters of kvass in the $$$i$$$-th keg. Each keg has a lever. You can pour your glass by exactly $$$1$$$ liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by $$$s$$$ liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by $$$s$$$ liters of kvass. Input The first line contains two integers $$$n$$$ and $$$s$$$ ($$$1 le n le 10^3$$$, $$$1 le s le 10^{12}$$$)xa0— the number of kegs and glass volume. The second line contains $$$n$$$ integers $$$v_1, v_2, ldots, v_n$$$ ($$$1 le v_i le 10^9$$$)xa0— the volume of $$$i$$$-th keg. Output If the Fair Nut cannot pour his glass by $$$s$$$ liters of kvass, print $$$-1$$$. Otherwise, print a single integerxa0— how much kvass in the least keg can be. Note In the first example, the answer is $$$3$$$, the Fair Nut can take $$$1$$$ liter from the first keg and $$$2$$$ liters from the third keg. There are $$$3$$$ liters of kvass in each keg. In the second example, the answer is $$$2$$$, the Fair Nut can take $$$3$$$ liters from the first keg and $$$1$$$ liter from the second keg. In the third example, the Fair Nut can't pour his cup by $$$7$$$ liters, so the answer is $$$-1$$$. | 1,200 | false | true | true | false | false | false | false | false | false | false | 5,301 |
1425A | Lately, Mr. Chanek frequently plays the game Arena of Greed. As the name implies, the game's goal is to find the greediest of them all, who will then be crowned king of Compfestnesia. The game is played by two people taking turns, where Mr. Chanek takes the first turn. Initially, there is a treasure chest containing $$$N$$$ gold coins. The game ends if there are no more gold coins in the chest. In each turn, the players can make one of the following moves: Take one gold coin from the chest. Take half of the gold coins on the chest. This move is only available if the number of coins in the chest is even. Both players will try to maximize the number of coins they have. Mr. Chanek asks your help to find the maximum number of coins he can get at the end of the game if both he and the opponent plays optimally. Input The first line contains a single integer $$$T$$$ $$$(1 le T le 10^5)$$$ denotes the number of test cases. The next $$$T$$$ lines each contain a single integer $$$N$$$ $$$(1 le N le 10^{18})$$$. Output $$$T$$$ lines, each line is the answer requested by Mr. Chanek. Note For the first case, the game is as follows: 1. Mr. Chanek takes one coin. 2. The opponent takes two coins. 3. Mr. Chanek takes one coin. 4. The opponent takes one coin. For the second case, the game is as follows: 1. Mr. Chanek takes three coins. 2. The opponent takes one coin. 3. Mr. Chanek takes one coin. 4. The opponent takes one coin. | 1,400 | false | true | false | false | false | false | false | false | false | false | 3,547 |
1368H1 | # Breadboard Capacity (easy version) Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes This is an easier version of the problem H without modification queries. Lester and Delbert work at an electronics company. They are currently working on a microchip component serving to connect two independent parts of a large supercomputer. The component is built on top of a breadboard x16 a grid-like base for a microchip. The breadboard has n rows and m columns, and each row-column intersection contains a node. Also, on each side of the breadboard there are ports that can be attached to adjacent nodes. Left and right side have n ports each, and top and bottom side have m ports each. Each of the ports is connected on the outside to one of the parts bridged by the breadboard, and is colored red or blue respectively. Ports can be connected by wires going inside the breadboard. However, there are a few rules to follow: • Each wire should connect a red port with a blue port, and each port should be connected to at most one wire. • Each part of the wire should be horizontal or vertical, and turns are only possible at one of the nodes. • To avoid interference, wires can not have common parts of non-zero length (but may have common nodes). Also, a wire can not cover the same segment of non-zero length twice. The capacity of the breadboard is the largest number of red-blue wire connections that can be made subject to the rules above. For example, the breadboard above has capacity 7, and one way to make seven connections is pictured below. Page 1 of 2 Up to this point statements of both versions are identical. Differences follow below. Given the current breadboard configuration, help Lester and Delbert find its capacity efficiently. # Input The first line contains three integers n, m, q (1 ≤ n, m ≤ 10 5, q = 0 q = 0 q = 0 ). n and m are the number of rows and columns of the breadboard respectively. In this version q is always zero, and is only present for consistency with the harder version. The next four lines describe initial coloring of the ports. Each character in these lines is either R or B,depending on the coloring of the respective port. The first two of these lines contain n characters each, and describe ports on the left and right sides respectively from top to bottom. The last two lines contain m characters each, and describe ports on the top and bottom sides respectively from left to right. # Output Print a single integer x16 the given breadboard capacity. # Example standard input standard output 4 5 0 BBRR RBBR BBBBB RRRRR 7 Page 2 of 2 | 3,300 | false | true | false | true | false | false | false | false | false | false | 3,829 |
1462C | Problem - 1462C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force greedy math *900 No tag edit access → Contest materials ") Editorial") . Input The first line contains a single positive integer $$$t$$$ ($$$1 le t le 50$$$)xa0— the number of test cases in the test. Then $$$t$$$ test cases follow. Each test case consists of a single integer number $$$x$$$ ($$$1 le x le 50$$$). Output Output $$$t$$$ answers to the test cases: if a positive integer number with the sum of digits equal to $$$x$$$ and all digits are different exists, print the smallest such number; otherwise print -1. Example Input 4 1 5 15 50 Output 1 5 69 -1 | 900 | true | true | false | false | false | false | true | false | false | false | 3,378 |
482C | You play the game with your friend. The description of this game is listed below. Your friend creates _n_ distinct strings of the same length _m_ and tells you all the strings. Then he randomly chooses one of them. He chooses strings equiprobably, i.e. the probability of choosing each of the _n_ strings equals . You want to guess which string was chosen by your friend. In order to guess what string your friend has chosen, you are allowed to ask him questions. Each question has the following form: «What character stands on position _pos_ in the string you have chosen?» A string is considered guessed when the answers to the given questions uniquely identify the string. After the string is guessed, you stop asking questions. You do not have a particular strategy, so as each question you equiprobably ask about a position that hasn't been yet mentioned. Your task is to determine the expected number of questions needed to guess the string chosen by your friend. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u200950)xa0— the number of strings your friend came up with. The next _n_ lines contain the strings that your friend has created. It is guaranteed that all the strings are distinct and only consist of large and small English letters. Besides, the lengths of all strings are the same and are between 1 to 20 inclusive. Output Print the single number — the expected value. Your answer will be considered correct if its absolute or relative error doesn't exceed 10u2009-u20099. Note In the first sample the strings only differ in the character in the third position. So only the following situations are possible: Thus, the expected value is equal to In the second sample we need at most two questions as any pair of questions uniquely identifies the string. So the expected number of questions is . In the third sample whatever position we ask about in the first question, we immediately identify the string. | 2,600 | false | false | false | true | false | false | false | false | false | false | 7,907 |
57B | Chris the Rabbit found the traces of an ancient Martian civilization. The brave astronomer managed to see through a small telescope an architecture masterpiece — "A Road to the Sun". The building stands on cubical stones of the same size. The foundation divides the entire "road" into cells, into which the cubical stones are fit tightly. Thus, to any cell of the foundation a coordinate can be assigned. To become the leader of the tribe, a Martian should build a Road to the Sun, that is to build from those cubical stones on a given foundation a stairway. The stairway should be described by the number of stones in the initial coordinate and the coordinates of the stairway's beginning and end. Each following cell in the coordinate's increasing order should contain one cubical stone more than the previous one. At that if the cell has already got stones, they do not count in this building process, the stairways were simply built on them. In other words, let us assume that a stairway is built with the initial coordinate of _l_, the final coordinate of _r_ and the number of stones in the initial coordinate _x_. That means that _x_ stones will be added in the cell _l_, _x_u2009+u20091 stones will be added in the cell _l_u2009+u20091, ..., _x_u2009+u2009_r_u2009-u2009_l_ stones will be added in the cell _r_. Chris managed to find an ancient manuscript, containing the descriptions of all the stairways. Now he wants to compare the data to be sure that he has really found "A Road to the Sun". For that he chose some road cells and counted the total number of cubical stones that has been accumulated throughout the Martian history and then asked you to count using the manuscript to what the sum should ideally total. Input The first line contains three space-separated integers: _n_,u2009_m_,u2009_k_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105,u20091u2009≤u2009_k_u2009≤u2009_min_(_n_,u2009100)) which is the number of cells, the number of "Roads to the Sun" and the number of cells in the query correspondingly. Each of the following _m_ roads contain three space-separated integers: _a__i_,u2009_b__i_,u2009_c__i_ (1u2009≤u2009_a__i_u2009≤u2009_b__i_u2009≤u2009_n_,u20091u2009≤u2009_c__i_u2009≤u20091000) which are the stairway's description, its beginning, end and the initial cell's height. Then follow a line, containing _k_ different space-separated integers _b__i_. All these numbers ranging from 1 to _n_ are cells, the number of stones in which interests Chris. Output You have to print a single number on a single line which is the sum of stones in all the cells Chris is interested in. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d). | 1,600 | false | false | true | false | false | false | false | false | false | false | 9,689 |
401A | Vanya loves playing. He even has a special set of cards to play with. Each card has a single integer. The number on the card can be positive, negative and can even be equal to zero. The only limit is, the number on each card doesn't exceed _x_ in the absolute value. Natasha doesn't like when Vanya spends a long time playing, so she hid all of his cards. Vanya became sad and started looking for the cards but he only found _n_ of them. Vanya loves the balance, so he wants the sum of all numbers on found cards equal to zero. On the other hand, he got very tired of looking for cards. Help the boy and say what is the minimum number of cards does he need to find to make the sum equal to zero? You can assume that initially Vanya had infinitely many cards with each integer number from u2009-u2009_x_ to _x_. Input The first line contains two integers: _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of found cards and _x_ (1u2009≤u2009_x_u2009≤u20091000) — the maximum absolute value of the number on a card. The second line contains _n_ space-separated integers — the numbers on found cards. It is guaranteed that the numbers do not exceed _x_ in their absolute value. Output Print a single number — the answer to the problem. Note In the first sample, Vanya needs to find a single card with number -2. In the second sample, Vanya needs to find two cards with number 2. He can't find a single card with the required number as the numbers on the lost cards do not exceed 3 in their absolute value. | 800 | true | false | true | false | false | false | false | false | false | false | 8,222 |
1999A | Problem - 1999A - 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 ") Editorial") xa0— the number of test cases. The only line of each test case contains a single two-digit positive integer $$$n$$$ ($$$10 leq n leq 99$$$). Output For each test case, output a single integerxa0— the sum of the digits of $$$n$$$. Example Input 8 77 21 40 34 19 84 10 99 Output 14 3 4 7 10 12 1 18 | 800 | true | false | true | false | false | false | false | false | false | false | 271 |
864F | There are _n_ cities in Berland. Some pairs of them are connected with _m_ directed roads. One can use only these roads to move from one city to another. There are no roads that connect a city to itself. For each pair of cities (_x_,u2009_y_) there is at most one road from _x_ to _y_. A path from city _s_ to city _t_ is a sequence of cities _p_1, _p_2, ... , _p__k_, where _p_1u2009=u2009_s_, _p__k_u2009=u2009_t_, and there is a road from city _p__i_ to city _p__i_u2009+u20091 for each _i_ from 1 to _k_u2009-u20091. The path can pass multiple times through each city except _t_. It can't pass through _t_ more than once. A path _p_ from _s_ to _t_ is ideal if it is the lexicographically minimal such path. In other words, _p_ is ideal path from _s_ to _t_ if for any other path _q_ from _s_ to _t_ _p__i_u2009<u2009_q__i_, where _i_ is the minimum integer such that _p__i_u2009≠u2009_q__i_. There is a tourist agency in the country that offers _q_ unusual excursions: the _j_-th excursion starts at city _s__j_ and ends in city _t__j_. For each pair _s__j_, _t__j_ help the agency to study the ideal path from _s__j_ to _t__j_. Note that it is possible that there is no ideal path from _s__j_ to _t__j_. This is possible due to two reasons: there is no path from _s__j_ to _t__j_; there are paths from _s__j_ to _t__j_, but for every such path _p_ there is another path _q_ from _s__j_ to _t__j_, such that _p__i_u2009>u2009_q__i_, where _i_ is the minimum integer for which _p__i_u2009≠u2009_q__i_. The agency would like to know for the ideal path from _s__j_ to _t__j_ the _k__j_-th city in that path (on the way from _s__j_ to _t__j_). For each triple _s__j_, _t__j_, _k__j_ (1u2009≤u2009_j_u2009≤u2009_q_) find if there is an ideal path from _s__j_ to _t__j_ and print the _k__j_-th city in that path, if there is any. Input The first line contains three integers _n_, _m_ and _q_ (2u2009≤u2009_n_u2009≤u20093000,0u2009≤u2009_m_u2009≤u20093000, 1u2009≤u2009_q_u2009≤u20094·105) — the number of cities, the number of roads and the number of excursions. Each of the next _m_ lines contains two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_, _x__i_u2009≠u2009_y__i_), denoting that the _i_-th road goes from city _x__i_ to city _y__i_. All roads are one-directional. There can't be more than one road in each direction between two cities. Each of the next _q_ lines contains three integers _s__j_, _t__j_ and _k__j_ (1u2009≤u2009_s__j_,u2009_t__j_u2009≤u2009_n_, _s__j_u2009≠u2009_t__j_, 1u2009≤u2009_k__j_u2009≤u20093000). Output In the _j_-th line print the city that is the _k__j_-th in the ideal path from _s__j_ to _t__j_. If there is no ideal path from _s__j_ to _t__j_, or the integer _k__j_ is greater than the length of this path, print the string '-1' (without quotes) in the _j_-th line. | 2,700 | false | false | false | false | false | false | false | false | false | true | 6,282 |
2006D | Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $$$k$$$ together. Otherwise, her brain may explode! Her teacher sets a difficult task every day as her daily summer holiday homework. Now she is given an array $$$a$$$ consisting of $$$n$$$ elements, and she needs to calculate the product of each two adjacent elements (that is, $$$a_1 cdot a_2$$$, $$$a_2 cdot a_3$$$, and so on). Iris wants her brain to work safely, and in order to do that, she would like to modify the array $$$a$$$ in such a way that $$$a_i cdot a_{i + 1} leq k$$$ holds for every $$$1 leq i < n$$$. There are two types of operations she can perform: 1. She can rearrange the elements of the array $$$a$$$ in an arbitrary way. 2. She can select an arbitrary element of the array $$$a$$$ and change its value to an arbitrary integer from $$$1$$$ to $$$k$$$. Iris wants to minimize the number of operations of type $$$2$$$ that she uses. However, that's completely not the end of the summer holiday! Summer holiday lasts for $$$q$$$ days, and on the $$$i$$$-th day, Iris is asked to solve the Math homework for the subarray $$$b_{l_i}, b_{l_i + 1}, ldots, b_{r_i}$$$. Help Iris and tell her the minimum number of type $$$2$$$ operations she needs to perform for each day. Note that the operations are independent for each day, i.e. the array $$$b$$$ is not changed. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5cdot 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$q$$$ and $$$k$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq q leq 10^5$$$, $$$1 leq k leq 10^6$$$)xa0— the length of array $$$b$$$, the number of days, and the upper bound for the multiplication calculation. The second line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq k$$$)xa0— the elements of the array $$$b$$$. Then $$$q$$$ lines follow, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i < r_i leq n$$$) — the boundaries of the subarray on the $$$i$$$-th day. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$. Output For each test, output a single line containing $$$q$$$ integers — the minimum number of operations of type $$$2$$$ needed for each day. Example Input 5 3 1 1 1 1 1 1 3 3 2 10 1 10 9 1 3 2 3 5 4 2 2 2 2 2 2 1 2 2 4 2 5 1 5 6 5 10 3 2 5 10 10 1 1 4 3 6 1 6 2 5 5 6 10 10 10 10 9 8 7 6 5 4 3 2 1 1 10 1 9 1 8 1 7 2 10 3 10 4 10 5 10 3 9 6 8 Output 0 0 1 1 1 2 2 1 1 1 1 0 3 3 4 3 2 2 1 1 2 1 Note In the first test case, as Iris can always multiply $$$1$$$ and $$$1$$$ together, no operations are needed, so the answer is $$$0$$$. In the second test case, the first day's homework is $$$[1, 10, 9]$$$. Iris can rearrange its elements to get $$$[9, 1, 10]$$$, so no operations of type $$$2$$$ are needed. The second day's homework is $$$[10, 9]$$$, and she can change one element to get the array $$$[1, 9]$$$, so one operation of type $$$2$$$ is needed. | 2,600 | true | true | true | false | true | false | false | false | false | false | 214 |
1650F | Vitaly enrolled in the course Advanced Useless Algorithms. The course consists of $$$n$$$ tasks. Vitaly calculated that he has $$$a_i$$$ hours to do the task $$$i$$$ from the day he enrolled in the course. That is, the deadline before the $$$i$$$-th task is $$$a_i$$$ hours. The array $$$a$$$ is sorted in ascending order, in other words, the job numbers correspond to the order in which the assignments are turned in. Vitaly does everything conscientiously, so he wants to complete each task by $$$100$$$ percent, or more. Initially, his completion rate for each task is $$$0$$$ percent. Vitaly has $$$m$$$ training options, each option can be used not more than once. The $$$i$$$th option is characterized by three integers: $$$e_i, t_i$$$ and $$$p_i$$$. If Vitaly uses the $$$i$$$th option, then after $$$t_i$$$ hours (from the current moment) he will increase the progress of the task $$$e_i$$$ by $$$p_i$$$ percent. For example, let Vitaly have $$$3$$$ of tasks to complete. Let the array $$$a$$$ have the form: $$$a = [5, 7, 8]$$$. Suppose Vitaly has $$$5$$$ of options: $$$[e_1=1, t_1=1, p_1=30]$$$, $$$[e_2=2, t_2=3, p_2=50]$$$, $$$[e_3=2, t_3=3, p_3=100]$$$, $$$[e_4=1, t_4=1, p_4=80]$$$, $$$[e_5=3, t_5=3, p_5=100]$$$. Then, if Vitaly prepares in the following way, he will be able to complete everything in time: Vitaly chooses the $$$4$$$-th option. Then in $$$1$$$ hour, he will complete the $$$1$$$-st task at $$$80$$$ percent. He still has $$$4$$$ hours left before the deadline for the $$$1$$$-st task. Vitaly chooses the $$$3$$$-rd option. Then in $$$3$$$ hours, he will complete the $$$2$$$-nd task in its entirety. He has another $$$1$$$ hour left before the deadline for the $$$1$$$-st task and $$$4$$$ hours left before the deadline for the $$$3$$$-rd task. Vitaly chooses the $$$1$$$-st option. Then after $$$1$$$ hour, he will complete the $$$1$$$-st task for $$$110$$$ percent, which means that he will complete the $$$1$$$-st task just in time for the deadline. Vitaly chooses the $$$5$$$-th option. He will complete the $$$3$$$-rd task for $$$2$$$ hours, and after another $$$1$$$ hour, Vitaly will complete the $$$3$$$-rd task in its entirety. Thus, Vitaly has managed to complete the course completely and on time, using the $$$4$$$ options. Help Vitalyxa0— print the options for Vitaly to complete the tasks in the correct order. Please note: each option can be used not more than once. If there are several possible answers, it is allowed to output any of them. Input The first line of input data contains an integer $$$T$$$ ($$$1 le T le 10^4$$$)xa0—the number of input test cases in the test. The descriptions of the input test case follow. The first line of each test case description contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n,m le 10^5$$$)xa0—the number of jobs and the number of training options, respectively. The next line contains $$$n$$$ numbers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the time before the deadline of job $$$i$$$. The array valuesxa0— are non-decreasing, that is $$$a_1 le a_2 le dots le a_n$$$. The following $$$m$$$ lines contain triples of numbers $$$e_i, t_i, p_i$$$ ($$$1 le e_i le n$$$, $$$1 le t_i le 10^9$$$, $$$1 le p_i le 100$$$)xa0— if Vitaly chooses this option, then after $$$t_i$$$ hours he will increase the progress of the task $$$e_i$$$ by $$$p_i$$$ percent. The options are numbered from $$$1$$$ to $$$m$$$ in order in the input data. It is guaranteed that the sum of $$$n+m$$$ on all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print on the first line the number $$$k$$$, meaning that for $$$k$$$ of options, Vitaly will be able to complete each task by $$$100$$$ percent or more on time. The options should not be repeated. Or print -1 if Vitaly is unable to complete all tasks in time. If there is an answer, on the next line print $$$k$$$ of different integers from $$$1$$$ to $$$m$$$xa0— the numbers of the options in the order you want. If there is more than one answer, it is allowed to print any of them. | 2,200 | false | true | true | true | false | false | false | false | false | false | 2,370 |
1114D | You are given a line of $$$n$$$ colored squares in a row, numbered from $$$1$$$ to $$$n$$$ from left to right. The $$$i$$$-th square initially has the color $$$c_i$$$. Let's say, that two squares $$$i$$$ and $$$j$$$ belong to the same connected component if $$$c_i = c_j$$$, and $$$c_i = c_k$$$ for all $$$k$$$ satisfying $$$i < k < j$$$. In other words, all squares on the segment from $$$i$$$ to $$$j$$$ should have the same color. For example, the line $$$[3, 3, 3]$$$ has $$$1$$$ connected component, while the line $$$[5, 2, 4, 4]$$$ has $$$3$$$ connected components. The game "flood fill" is played on the given line as follows: At the start of the game you pick any starting square (this is not counted as a turn). Then, in each game turn, change the color of the connected component containing the starting square to any other color. Find the minimum number of turns needed for the entire line to be changed into a single color. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 5000$$$)xa0— the number of squares. The second line contains integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 le c_i le 5000$$$)xa0— the initial colors of the squares. Output Print a single integerxa0— the minimum number of the turns needed. Note In the first example, a possible way to achieve an optimal answer is to pick square with index $$$2$$$ as the starting square and then play as follows: $$$[5, 2, 2, 1]$$$ $$$[5, 5, 5, 1]$$$ $$$[1, 1, 1, 1]$$$ In the second example, a possible way to achieve an optimal answer is to pick square with index $$$5$$$ as the starting square and then perform recoloring into colors $$$2, 3, 5, 4$$$ in that order. In the third example, the line already consists of one color only. | 1,900 | false | false | false | true | false | false | false | false | false | false | 5,131 |
1736D | Everool has a binary string $$$s$$$ of length $$$2n$$$. Note that a binary string is a string consisting of only characters $$$0$$$ and $$$1$$$. He wants to partition $$$s$$$ into two disjoint equal subsequences. He needs your help to do it. You are allowed to do the following operation exactly once. You can choose any subsequence (possibly empty) of $$$s$$$ and rotate it right by one position. In other words, you can select a sequence of indices $$$b_1, b_2, ldots, b_m$$$, where $$$1 le b_1 < b_2 < ldots < b_m le 2n$$$. After that you simultaneously set $$$$$$s_{b_1} := s_{b_m},$$$$$$ $$$$$$s_{b_2} := s_{b_1},$$$$$$ $$$$$$ldots,$$$$$$ $$$$$$s_{b_m} := s_{b_{m-1}}.$$$$$$ Can you partition $$$s$$$ into two disjoint equal subsequences after performing the allowed operation exactly once? A partition of $$$s$$$ into two disjoint equal subsequences $$$s^p$$$ and $$$s^q$$$ is two increasing arrays of indices $$$p_1, p_2, ldots, p_n$$$ and $$$q_1, q_2, ldots, q_n$$$, such that each integer from $$$1$$$ to $$$2n$$$ is encountered in either $$$p$$$ or $$$q$$$ exactly once, $$$s^p = s_{p_1} s_{p_2} ldots s_{p_n}$$$, $$$s^q = s_{q_1} s_{q_2} ldots s_{q_n}$$$, and $$$s^p = s^q$$$. If it is not possible to partition after performing any kind of operation, report $$$-1$$$. If it is possible to do the operation and partition $$$s$$$ into two disjoint subsequences $$$s^p$$$ and $$$s^q$$$, such that $$$s^p = s^q$$$, print elements of $$$b$$$ and indices of $$$s^p$$$, i.xa0e. the values $$$p_1, p_2, ldots, p_n$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$), where $$$2n$$$ is the length of the binary string. The second line of each test case contains the binary string $$$s$$$ of length $$$2n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, follow the following output format. If there is no solution, print $$$-1$$$. Otherwise, In the first line, print an integer $$$m$$$ ($$$0 leq m leq 2n$$$), followed by $$$m$$$ distinct indices $$$b_1$$$, $$$b_2$$$, ..., $$$b_m$$$(in increasing order). In the second line, print $$$n$$$ distinct indices $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (in increasing order). If there are multiple solutions, print any. Note In the first test case, $$$b$$$ is empty. So string $$$s$$$ is not changed. Now $$$s^p = s_1 s_2 = mathtt{10}$$$, and $$$s^q = s_3s_4 = mathtt{10}$$$. In the second test case, $$$b=[3,5]$$$. Initially $$$s_3=mathtt{0}$$$, and $$$s_5=mathtt{1}$$$. On performing the operation, we simultaneously set $$$s_3=mathtt{1}$$$, and $$$s_5=mathtt{0}$$$. So $$$s$$$ is updated to 101000 on performing the operation. Now if we take characters at indices $$$[1,2,5]$$$ in $$$s^p$$$, we get $$$s_1=mathtt{100}$$$. Also characters at indices $$$[3,4,6]$$$ are in $$$s^q$$$. Thus $$$s^q=100$$$. We are done as $$$s^p=s^q$$$. In fourth test case, it can be proved that it is not possible to partition the string after performing any operation. | 2,200 | false | true | true | false | false | true | false | false | false | false | 1,870 |
1223A | Let's denote correct match equation (we will denote it as CME) an equation $$$a + b = c$$$ there all integers $$$a$$$, $$$b$$$ and $$$c$$$ are greater than zero. For example, equations $$$2 + 2 = 4$$$ (+=) and $$$1 + 2 = 3$$$ (+=) are CME but equations $$$1 + 2 = 4$$$ (+=), $$$2 + 2 = 3$$$ (+=), and $$$0 + 1 = 1$$$ (+=) are not. Now, you have $$$n$$$ matches. You want to assemble a CME using all your matches. Unfortunately, it is possible that you can't assemble the CME using all matches. But you can buy some extra matches and then assemble CME! For example, if $$$n = 2$$$, you can buy two matches and assemble +=, and if $$$n = 5$$$ you can buy one match and assemble +=. Calculate the minimum number of matches which you have to buy for assembling CME. Note, that you have to answer $$$q$$$ independent queries. Input The first line contains one integer $$$q$$$ ($$$1 le q le 100$$$)xa0— the number of queries. The only line of each query contains one integer $$$n$$$ ($$$2 le n le 10^9$$$)xa0— the number of matches. Output For each test case print one integer in single linexa0— the minimum number of matches which you have to buy for assembling CME. Note The first and second queries are explained in the statement. In the third query, you can assemble $$$1 + 3 = 4$$$ (+=) without buying matches. In the fourth query, buy one match and assemble $$$2 + 4 = 6$$$ (+=). | 800 | true | false | false | false | false | false | false | false | false | false | 4,559 |
27B | The tournament «Sleepyhead-2010» in the rapid falling asleep has just finished in Berland. _n_ best participants from the country have participated in it. The tournament consists of games, each of them is a match between two participants. _n_·(_n_u2009-u20091)u2009/u20092 games were played during the tournament, and each participant had a match with each other participant. The rules of the game are quite simple — the participant who falls asleep first wins. The secretary made a record of each game in the form «_x__i_ _y__i_», where _x__i_ and _y__i_ are the numbers of participants. The first number in each pair is a winner (i.e. _x__i_ is a winner and _y__i_ is a loser). There is no draws. Recently researches form the «Institute Of Sleep» have found that every person is characterized by a value _p__j_ — the speed of falling asleep. The person who has lower speed wins. Every person has its own value _p__j_, constant during the life. It is known that all participants of the tournament have distinct speeds of falling asleep. Also it was found that the secretary made records about all the games except one. You are to find the result of the missing game. Input The first line contains one integer _n_ (3u2009≤u2009_n_u2009≤u200950) — the number of participants. The following _n_·(_n_u2009-u20091)u2009/u20092u2009-u20091 lines contain the results of the games. Each game is described in a single line by two integers _x__i_,u2009_y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_,u2009_x__i_u2009≠u2009_y__i_), where _x__i_ и _y__i_ are the numbers of the opponents in this game. It is known that during the tournament each of the _n_ participants played _n_u2009-u20091 games, one game with each other participant. Output Output two integers _x_ and _y_ — the missing record. If there are several solutions, output any of them. | 1,300 | false | true | false | false | false | false | true | false | false | false | 9,862 |
1770G | Chiyuu has a bracket sequence$$$^dagger$$$ $$$s$$$ of length $$$n$$$. Let $$$k$$$ be the minimum number of characters that Chiyuu has to remove from $$$s$$$ to make $$$s$$$ balanced$$$^ddagger$$$. Now, Koxia wants you to count the number of ways to remove $$$k$$$ characters from $$$s$$$ so that $$$s$$$ becomes balanced, modulo $$$998,244,353$$$. Note that two ways of removing characters are considered distinct if and only if the set of indices removed is different. $$$^dagger$$$ A bracket sequence is a string containing only the characters "(" and ")". $$$^ddagger$$$ A bracket sequence is called balanced if one can turn it into a valid math expression by adding characters + and 1. For example, sequences (())(), (), (()(())) and the empty string are balanced, while )(, ((), and (()))( are not. Input The first line of input contains a string $$$s$$$ ($$$1 leq s leq 5 cdot {10}^5$$$) — the bracket sequence. It is guaranteed that $$$s$$$ only contains the characters "(" and ")". Output Output a single integer — the number of ways to remove $$$k$$$ characters from $$$s$$$ so that $$$s$$$ becomes balanced, modulo $$$998,244,353$$$. Note In the first test case, it can be proved that the minimum number of characters that Chiyuu has to remove is $$$2$$$. There are $$$4$$$ ways to remove $$$2$$$ characters to make $$$s$$$ balanced as follows. Deleted characters are noted as red. $$$ exttt{(} color{Red}{ exttt{)}} exttt{)} color{Red}{ exttt{(}} exttt{(} exttt{)}$$$, $$$ exttt{(} exttt{)} color{Red}{ exttt{)}} color{Red}{ exttt{(}} exttt{(} exttt{)}$$$, $$$ exttt{(} color{Red}{ exttt{)}} exttt{)} exttt{(} color{Red}{ exttt{(}} exttt{)}$$$, $$$ exttt{(} exttt{)} color{Red}{ exttt{)}} exttt{(} color{Red}{ exttt{(}} exttt{)}$$$. In the second test case, the only way to make $$$s$$$ balanced is by deleting the only character to get an empty bracket sequence, which is considered balanced. | 3,400 | true | false | false | false | false | false | false | false | false | false | 1,671 |
2038L | Problem - 2038L - 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 dp greedy math two pointers *1400 No tag edit access → Contest materials xa0— the number of planks required for each of the three bridges. Output Print a single integerxa0— the minimum number of planks of standard length ($$$60$$$ units) Monocarp needs to cover all three bridges if the planks can be cut into parts. Examples Input 1 Output 2 Input 3 Output 4 Input 1000 Output 1167 Note In the first example, it is possible to cut one plank of length $$$60$$$ into three planks with lengths $$$25$$$, $$$18$$$ and $$$17$$$, and cut another plank of length $$$60$$$ into two planks with lengths $$$39$$$ and $$$21$$$. That way, Monocarp will have all the required planks. | 1,400 | true | true | false | true | false | false | true | false | false | false | 27 |
302A | Problem - 302A - 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 . The response to the query will be integer 1, if the elements of array _a_ can be rearranged so as the sum _a__l__i_u2009+u2009_a__l__i_u2009+u20091u2009+u2009...u2009+u2009_a__r__i_u2009=u20090, otherwise the response to the query will be integer 0. Help Eugeny, answer all his queries. Input The first line contains integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009=u2009-1,u20091). Next _m_ lines contain Eugene's queries. The _i_-th line contains integers _l__i_,u2009_r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_). Output Print _m_ integers — the responses to Eugene's queries in the order they occur in the input. Examples Input 2 3 1 -1 1 1 1 2 2 2 Output 0 1 0 Input 5 5 -1 1 1 1 -1 1 1 2 3 3 5 2 5 1 5 Output 0 1 0 1 0 | 800 | false | false | true | false | false | false | false | false | false | false | 8,625 |
1852D | Miriany's matchstick is a $$$2 imes n$$$ grid that needs to be filled with characters A or B. He has already filled in the first row of the grid and would like you to fill in the second row. You must do so in a way such that the number of adjacent pairs of cells with different characters$$$^dagger$$$ is equal to $$$k$$$. If it is impossible, report so. $$$^dagger$$$ An adjacent pair of cells with different characters is a pair of cells $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ ($$$1 le r_1, r_2 le 2$$$, $$$1 le c_1, c_2 le n$$$) such that $$$r_1 - r_2 + c_1 - c_2 = 1$$$ and the characters in $$$(r_1, c_1)$$$ and $$$(r_2, c_2)$$$ are different. Input The first line consists of an integer $$$t$$$, the number of test cases ($$$1 leq t leq 1000$$$). The description of the test cases follows. The first line of each test case has two integers, $$$n$$$ and $$$k$$$ ($$$1 leq n leq 2 cdot 10^5, 0 leq k leq 3 cdot n$$$)xa0– the number of columns of the matchstick, and the number of adjacent pairs of cells with different characters required. The following line contains string $$$s$$$ of $$$n$$$ characters ($$$s_i$$$ is either A or B)xa0– Miriany's top row of the matchstick. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, if there is no way to fill the second row with the number of adjacent pairs of cells with different characters equals $$$k$$$, output "NO". Otherwise, output "YES". Then, print $$$n$$$ characters that a valid bottom row for Miriany's matchstick consists of. If there are several answers, output any of them. Example Input 4 10 1 ABBAAABBAA 4 5 AAAA 9 17 BAAABBAAB 4 9 ABAB Output NO YES BABB YES ABABAABAB NO Note In the first test case, it can be proved that there exists no possible way to fill in row $$$2$$$ of the grid such that $$$k = 1$$$. For the second test case, BABB is one possible answer. The grid below is the result of filling in BABB as the second row. $$$begin{array}{cc} hline A & A & A & A cr hline B & A & B & B cr hline end{array}$$$ The pairs of different characters are shown below in red: $$$begin{array}{cc} hline color{red}{A} & A & A & A cr hline color{red}{B} & A & B & B cr hline end{array}$$$————————————————— $$$begin{array}{cc} hline A & A & color{red}{A} & A cr hline B & A & color{red}{B} & B cr hline end{array}$$$ ————————————————— $$$begin{array}{cc} hline A & A & A & color{red}{A} cr hline B & A & B & color{red}{B} cr hline end{array}$$$ ————————————————— $$$begin{array}{cc} hline A & A & A & A cr hline color{red}{B} & color{red}{A} & B & B cr hline end{array}$$$ ————————————————— $$$begin{array}{cc} hline A & A & A & A cr hline B & color{red}{A} & color{red}{B} & B cr hline end{array}$$$ There are a total of $$$5$$$ pairs, which satisfies $$$k$$$. | 2,800 | false | true | false | true | false | true | false | false | false | false | 1,164 |
1137A | Dora loves adventures quite a lot. During some journey she encountered an amazing city, which is formed by $$$n$$$ streets along the Eastern direction and $$$m$$$ streets across the Southern direction. Naturally, this city has $$$nm$$$ intersections. At any intersection of $$$i$$$-th Eastern street and $$$j$$$-th Southern street there is a monumental skyscraper. Dora instantly became curious and decided to explore the heights of the city buildings. When Dora passes through the intersection of the $$$i$$$-th Eastern and $$$j$$$-th Southern street she examines those two streets. After Dora learns the heights of all the skyscrapers on those two streets she wonders: how one should reassign heights to the skyscrapers on those two streets, so that the maximum height would be as small as possible and the result of comparing the heights of any two skyscrapers on one street wouldn't change. Formally, on every of $$$nm$$$ intersections Dora solves an independent problem. She sees $$$n + m - 1$$$ skyscrapers and for each of them she knows its real height. Moreover, any two heights can be compared to get a result "greater", "smaller" or "equal". Now Dora wants to select some integer $$$x$$$ and assign every skyscraper a height from $$$1$$$ to $$$x$$$. When assigning heights, Dora wants to preserve the relative order of the skyscrapers in both streets. That is, the result of any comparison of heights of two skyscrapers in the current Eastern street shouldn't change and the result of any comparison of heights of two skyscrapers in current Southern street shouldn't change as well. Note that skyscrapers located on the Southern street are not compared with skyscrapers located on the Eastern street only. However, the skyscraper located at the streets intersection can be compared with both Southern and Eastern skyscrapers. For every intersection Dora wants to independently calculate the minimum possible $$$x$$$. For example, if the intersection and the two streets corresponding to it look as follows: Then it is optimal to replace the heights of the skyscrapers as follows (note that all comparisons "less", "equal", "greater" inside the Eastern street and inside the Southern street are preserved) The largest used number is $$$5$$$, hence the answer for this intersection would be $$$5$$$. Help Dora to compute the answers for each intersection. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 1000$$$)xa0— the number of streets going in the Eastern direction and the number of the streets going in Southern direction. Each of the following $$$n$$$ lines contains $$$m$$$ integers $$$a_{i,1}$$$, $$$a_{i,2}$$$, ..., $$$a_{i,m}$$$ ($$$1 le a_{i,j} le 10^9$$$). The integer $$$a_{i,j}$$$, located on $$$j$$$-th position in the $$$i$$$-th line denotes the height of the skyscraper at the intersection of the $$$i$$$-th Eastern street and $$$j$$$-th Southern direction. Output Print $$$n$$$ lines containing $$$m$$$ integers each. The integer $$$x_{i,j}$$$, located on $$$j$$$-th position inside the $$$i$$$-th line is an answer for the problem at the intersection of $$$i$$$-th Eastern street and $$$j$$$-th Southern street. Note In the first example, it's not possible to decrease the maximum used height for the problem at any intersection, hence we don't have to change any heights. In the second example, the answers are as follows: For the intersection of the first line and the first column For the intersection of the first line and the second column For the intersection of the second line and the first column For the intersection of the second line and the second column | 1,600 | false | false | true | false | false | false | false | false | true | false | 5,042 |
1555B | You have an axis-aligned rectangle room with width $$$W$$$ and height $$$H$$$, so the lower left corner is in point $$$(0, 0)$$$ and the upper right corner is in $$$(W, H)$$$. There is a rectangular table standing in this room. The sides of the table are parallel to the walls, the lower left corner is in $$$(x_1, y_1)$$$, and the upper right corner in $$$(x_2, y_2)$$$. You want to place another rectangular table in this room with width $$$w$$$ and height $$$h$$$ with the width of the table parallel to the width of the room. The problem is that sometimes there is not enough space to place the second table without intersecting with the first one (there are no problems with tables touching, though). You can't rotate any of the tables, but you can move the first table inside the room. Example of how you may move the first table. What is the minimum distance you should move the first table to free enough space for the second one? Input The first line contains the single integer $$$t$$$ ($$$1 le t le 5000$$$)xa0— the number of the test cases. The first line of each test case contains two integers $$$W$$$ and $$$H$$$ ($$$1 le W, H le 10^8$$$)xa0— the width and the height of the room. The second line contains four integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$0 le x_1 < x_2 le W$$$; $$$0 le y_1 < y_2 le H$$$)xa0— the coordinates of the corners of the first table. The third line contains two integers $$$w$$$ and $$$h$$$ ($$$1 le w le W$$$; $$$1 le h le H$$$)xa0— the width and the height of the second table. Output For each test case, print the minimum distance you should move the first table, or $$$-1$$$ if there is no way to free enough space for the second table. Your answer will be considered correct if its absolute or relative error doesn't exceed $$$10^{-6}$$$. Example Input 5 8 5 2 1 7 4 4 2 5 4 2 2 5 4 3 3 1 8 0 3 1 6 1 5 8 1 3 0 6 1 5 1 8 10 4 5 7 8 8 5 Output 1.000000000 -1 2.000000000 2.000000000 0.000000000 Note The configuration of the first test case is shown in the picture. But the movement of the first table is not optimal. One of the optimal movement, for example, is to move the table by $$$(0, -1)$$$, so the lower left corner will move from $$$(2, 1)$$$ to $$$(2, 0)$$$. Then you can place the second table at $$$(0, 3)-(4, 5)$$$. In the second test case, there is no way to fit both tables in the room without intersecting. In the third test case, you can move the first table by $$$(0, 2)$$$, so the lower left corner will move from $$$(0, 3)$$$ to $$$(0, 5)$$$. | 1,300 | false | false | false | false | false | false | true | false | false | false | 2,860 |
165D | Let's define a non-oriented connected graph of _n_ vertices and _n_u2009-u20091 edges as a beard, if all of its vertices except, perhaps, one, have the degree of 2 or 1 (that is, there exists no more than one vertex, whose degree is more than two). Let us remind you that the degree of a vertex is the number of edges that connect to it. Let each edge be either black or white. Initially all edges are black. You are given the description of the beard graph. Your task is to analyze requests of the following types: paint the edge number _i_ black. The edge number _i_ is the edge that has this number in the description. It is guaranteed that by the moment of this request the _i_-th edge is white paint the edge number _i_ white. It is guaranteed that by the moment of this request the _i_-th edge is black find the length of the shortest path going only along the black edges between vertices _a_ and _b_ or indicate that no such path exists between them (a path's length is the number of edges in it) The vertices are numbered with integers from 1 to _n_, and the edges are numbered with integers from 1 to _n_u2009-u20091. Input The first line of the input contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009105) — the number of vertices in the graph. Next _n_u2009-u20091 lines contain edges described as the numbers of vertices _v__i_, _u__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_, _v__i_u2009≠u2009_u__i_) connected by this edge. It is guaranteed that the given graph is connected and forms a beard graph, and has no self-loops or multiple edges. The next line contains an integer _m_ (1u2009≤u2009_m_u2009≤u20093·105) — the number of requests. Next _m_ lines contain requests in the following form: first a line contains an integer _type_, which takes values u200bu200bfrom 1 to 3, and represents the request type. If _type_u2009=u20091, then the current request is a request to paint the edge black. In this case, in addition to number _type_ the line should contain integer _id_ (1u2009≤u2009_id_u2009≤u2009_n_u2009-u20091), which represents the number of the edge to paint. If _type_u2009=u20092, then the current request is a request to paint the edge white, its form is similar to the previous request. If _type_u2009=u20093, then the current request is a request to find the distance. In this case, in addition to _type_, the line should contain two integers _a_, _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_, _a_ can be equal to _b_) — the numbers of vertices, the distance between which must be found. The numbers in all lines are separated by exactly one space. The edges are numbered in the order in which they are given in the input. Output For each request to "find the distance between vertices _a_ and _b_" print the result. If there is no path going only along the black edges between vertices _a_ and _b_, then print "-1" (without the quotes). Print the results in the order of receiving the requests, separate the numbers with spaces or line breaks. Note In the first sample vertices 1 and 2 are connected with edge number 1, and vertices 2 and 3 are connected with edge number 2. Before the repainting edge number 2 each vertex is reachable from each one along the black edges. Specifically, the shortest path between 1 and 3 goes along both edges. If we paint edge number 2 white, vertex 3 will end up cut off from other vertices, that is, no path exists from it to any other vertex along the black edges. | 2,100 | false | false | false | false | true | false | false | false | false | false | 9,202 |
1411E | You've got a string $$$S$$$ consisting of $$$n$$$ lowercase English letters from your friend. It turned out that this is a number written in poman numerals. The poman numeral system is long forgotten. All that's left is the algorithm to transform number from poman numerals to the numeral system familiar to us. Characters of $$$S$$$ are numbered from $$$1$$$ to $$$n$$$ from left to right. Let's denote the value of $$$S$$$ as $$$f(S)$$$, it is defined as follows: If $$$S > 1$$$, an arbitrary integer $$$m$$$ ($$$1 le m < S$$$) is chosen, and it is defined that $$$f(S) = -f(S[1, m]) + f(S[m + 1, S])$$$, where $$$S[l, r]$$$ denotes the substring of $$$S$$$ from the $$$l$$$-th to the $$$r$$$-th position, inclusively. Otherwise $$$S = c$$$, where $$$c$$$ is some English letter. Then $$$f(S) = 2^{pos(c)}$$$, where $$$pos(c)$$$ is the position of letter $$$c$$$ in the alphabet ($$$pos($$$a$$$) = 0$$$, $$$pos($$$z$$$) = 25$$$). Note that $$$m$$$ is chosen independently on each step. Your friend thinks it is possible to get $$$f(S) = T$$$ by choosing the right $$$m$$$ on every step. Is he right? Input The first line contains two integers $$$n$$$ and $$$T$$$ ($$$2 leq n leq 10^5$$$, $$$-10^{15} leq T leq 10^{15}$$$). The second line contains a string $$$S$$$ consisting of $$$n$$$ lowercase English letters. Output Print "Yes" if it is possible to get the desired value. Otherwise, print "No". You can print each letter in any case (upper or lower). Note In the second example, you cannot get $$$-7$$$. But you can get $$$1$$$, for example, as follows: 1. First choose $$$m = 1$$$, then $$$f($$$abc$$$) = -f($$$a$$$) + f($$$bc$$$)$$$ 2. $$$f($$$a$$$) = 2^0 = 1$$$ 3. $$$f($$$bc$$$) = -f($$$b$$$) + f($$$c$$$) = -2^1 + 2^2 = 2$$$ 4. In the end $$$f($$$abc$$$) = -1 + 2 = 1$$$ | 2,300 | true | true | false | false | false | false | false | false | false | false | 3,617 |
1375H | You are given a permutation $$$a_1, a_2, dots, a_n$$$ of numbers from $$$1$$$ to $$$n$$$. Also, you have $$$n$$$ sets $$$S_1,S_2,dots, S_n$$$, where $$$S_i={a_i}$$$. Lastly, you have a variable $$$cnt$$$, representing the current number of sets. Initially, $$$cnt = n$$$. We define two kinds of functions on sets: $$$f(S)=minlimits_{uin S} u$$$; $$$g(S)=maxlimits_{uin S} u$$$. You can obtain a new set by merging two sets $$$A$$$ and $$$B$$$, if they satisfy $$$g(A)<f(B)$$$ (Notice that the old sets do not disappear). Formally, you can perform the following sequence of operations: $$$cntgets cnt+1$$$; $$$S_{cnt}=S_ucup S_v$$$, you are free to choose $$$u$$$ and $$$v$$$ for which $$$1le u, v < cnt$$$ and which satisfy $$$g(S_u)<f(S_v)$$$. You are required to obtain some specific sets. There are $$$q$$$ requirements, each of which contains two integers $$$l_i$$$,$$$r_i$$$, which means that there must exist a set $$$S_{k_i}$$$ ($$$k_i$$$ is the ID of the set, you should determine it) which equals $$${a_umid l_ileq uleq r_i}$$$, which is, the set consisting of all $$$a_i$$$ with indices between $$$l_i$$$ and $$$r_i$$$. In the end you must ensure that $$$cntleq 2.2 imes 10^6$$$. Note that you don't have to minimize $$$cnt$$$. It is guaranteed that a solution under given constraints exists. Input The first line contains two integers $$$n,q$$$ $$$(1leq n leq 2^{12},1 leq q leq 2^{16})$$$ xa0— the length of the permutation and the number of needed sets correspondently. The next line consists of $$$n$$$ integers $$$a_1,a_2,cdots, a_n$$$ ($$$1leq a_ileq n$$$, $$$a_i$$$ are pairwise distinct) xa0— given permutation. $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i,r_i$$$ $$$(1leq l_ileq r_ileq n)$$$, describing a requirement of the $$$i$$$-th set. Output It is guaranteed that a solution under given constraints exists. The first line should contain one integer $$$cnt_E$$$ $$$(nleq cnt_Eleq 2.2 imes 10^6)$$$, representing the number of sets after all operations. $$$cnt_E-n$$$ lines must follow, each line should contain two integers $$$u$$$, $$$v$$$ ($$$1leq u, vleq cnt'$$$, where $$$cnt'$$$ is the value of $$$cnt$$$ before this operation), meaning that you choose $$$S_u$$$, $$$S_v$$$ and perform a merging operation. In an operation, $$$g(S_u)<f(S_v)$$$ must be satisfied. The last line should contain $$$q$$$ integers $$$k_1,k_2,cdots,k_q$$$ $$$(1leq k_ileq cnt_E)$$$, representing that set $$$S_{k_i}$$$ is the $$$i$$$th required set. Please notice the large amount of output. Examples Input 2 4 2 1 1 2 1 2 1 2 1 1 Output 5 2 1 2 1 2 1 5 3 3 1 Note In the first sample: We have $$$S_1={1},S_2={3},S_3={2}$$$ initially. In the first operation, because $$$g(S_3)=2<f(S_2)=3$$$, we can merge $$$S_3,S_2$$$ into $$$S_4={2,3}$$$. In the second operation, because $$$g(S_1)=1<f(S_3)=2$$$, we can merge $$$S_1,S_3$$$ into $$$S_5={1,2}$$$. In the third operation, because $$$g(S_5)=2<f(S_2)=3$$$, we can merge $$$S_5,S_2$$$ into $$$S_6={1,2,3}$$$. For the first requirement, $$$S_4={2,3}={a_2,a_3}$$$, satisfies it, thus $$$k_1=4$$$. For the second requirement, $$$S_6={1,2,3}={a_1,a_2,a_3}$$$, satisfies it, thus $$$k_2=6$$$ Notice that unused sets, identical sets, outputting the same set multiple times, and using sets that are present initially are all allowed. | 3,300 | false | false | false | false | false | true | false | false | false | false | 3,780 |
188C | Problem - 188C - Codeforces =============== xa0 of two numbers is the smallest positive integer which is divisible by both of them. You are given integers _a_ and _b_. Calculate their LCM. Input The input contains two integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009103), separated by a single space. Output Output LCM(_a_, _b_). Examples Input 10 42 Output 210 Input 123 41 Output 123 | 1,400 | true | false | true | false | false | false | false | false | false | false | 9,090 |
1526C2 | This is the hard version of the problem. The only difference is that in this version $$$n leq 200000$$$. You can make hacks only if both versions of the problem are solved. There are $$$n$$$ potions in a line, with potion $$$1$$$ on the far left and potion $$$n$$$ on the far right. Each potion will increase your health by $$$a_i$$$ when drunk. $$$a_i$$$ can be negative, meaning that potion will decrease will health. You start with $$$0$$$ health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative. What is the largest number of potions you can drink? Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 200000$$$) — the number of potions. The next line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ... ,$$$a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$) which represent the change in health after drinking that potion. Output Output a single integer, the maximum number of potions you can drink without your health becoming negative. Note For the sample, you can drink $$$5$$$ potions by taking potions $$$1$$$, $$$3$$$, $$$4$$$, $$$5$$$ and $$$6$$$. It is not possible to drink all $$$6$$$ potions because your health will go negative at some point | 1,600 | false | true | false | false | true | false | false | false | false | false | 3,033 |
1631B | You are given an array $$$a$$$ of $$$n$$$ elements. You can apply the following operation to it any number of times: Select some subarray from $$$a$$$ of even size $$$2k$$$ that begins at position $$$l$$$ ($$$1le l le l+2cdot{k}-1le n$$$, $$$k ge 1$$$) and for each $$$i$$$ between $$$0$$$ and $$$k-1$$$ (inclusive), assign the value $$$a_{l+k+i}$$$ to $$$a_{l+i}$$$. For example, if $$$a = [2, 1, 3, 4, 5, 3]$$$, then choose $$$l = 1$$$ and $$$k = 2$$$, applying this operation the array will become $$$a = [3, 4, 3, 4, 5, 3]$$$. Find the minimum number of operations (possibly zero) needed to make all the elements of the array equal. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 2 cdot 10^4$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$) — the length of the array. The second line of each test case consists of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq n$$$) — the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test, all elements are equal, therefore no operations are needed. In the second test, you can apply one operation with $$$k=1$$$ and $$$l=1$$$, set $$$a_1 := a_2$$$, and the array becomes $$$[1, 1]$$$ with $$$1$$$ operation. In the third test, you can apply one operation with $$$k=1$$$ and $$$l=4$$$, set $$$a_4 := a_5$$$, and the array becomes $$$[4, 4, 4, 4, 4]$$$. In the fourth test, you can apply one operation with $$$k=1$$$ and $$$l=3$$$, set $$$a_3 := a_4$$$, and the array becomes $$$[4, 2, 3, 3]$$$, then you can apply another operation with $$$k=2$$$ and $$$l=1$$$, set $$$a_1 := a_3$$$, $$$a_2 := a_4$$$, and the array becomes $$$[3, 3, 3, 3]$$$. In the fifth test, there is only one element, therefore no operations are needed. | 1,100 | false | true | false | true | false | false | false | false | false | false | 2,459 |
575C | Note the unusual memory limit for the problem. People working in MDCS (Microsoft Development Center Serbia) like partying. They usually go to night clubs on Friday and Saturday. There are _N_ people working in MDCS and there are _N_ clubs in the city. Unfortunately, if there is more than one Microsoft employee in night club, level of coolness goes infinitely high and party is over, so club owners will never let more than one Microsoft employee enter their club in the same week (just to be sure). You are organizing night life for Microsoft employees and you have statistics about how much every employee likes Friday and Saturday parties for all clubs. You need to match people with clubs maximizing overall sum of their happiness (they are happy as much as they like the club), while half of people should go clubbing on Friday and the other half on Saturday. Input The first line contains integer _N_ — number of employees in MDCS. Then an _N_u2009×u2009_N_ matrix follows, where element in _i_-th row and _j_-th column is an integer number that represents how much _i_-th person likes _j_-th club’s Friday party. Then another _N_u2009×u2009_N_ matrix follows, where element in _i_-th row and _j_-th column is an integer number that represents how much _i_-th person likes _j_-th club’s Saturday party. 2u2009≤u2009_N_u2009≤u200920 _N_ is even 0u2009≤u2009 level of likeness u2009≤u2009106 All values are integers Output Output should contain a single integer — maximum sum of happiness possible. Examples Input 4 1 2 3 4 2 3 4 1 3 4 1 2 4 1 2 3 5 8 7 1 6 9 81 3 55 78 1 6 1 1 1 1 Note Here is how we matched people with clubs: Friday: 1st person with 4th club (4 happiness) and 4th person with 1st club (4 happiness). Saturday: 2nd person with 3rd club (81 happiness) and 3rd person with 2nd club (78 happiness). 4+4+81+78 = 167 | 2,700 | false | false | false | false | false | false | true | false | false | false | 7,550 |
1450B | You have $$$n$$$ distinct points $$$(x_1, y_1),ldots,(x_n,y_n)$$$ on the plane and a non-negative integer parameter $$$k$$$. Each point is a microscopic steel ball and $$$k$$$ is the attract power of a ball when it's charged. The attract power is the same for all balls. In one operation, you can select a ball $$$i$$$ to charge it. Once charged, all balls with Manhattan distance at most $$$k$$$ from ball $$$i$$$ move to the position of ball $$$i$$$. Many balls may have the same coordinate after an operation. More formally, for all balls $$$j$$$ such that $$$x_i - x_j + y_i - y_j le k$$$, we assign $$$x_j:=x_i$$$ and $$$y_j:=y_i$$$. An example of an operation. After charging the ball in the center, two other balls move to its position. On the right side, the red dot in the center is the common position of those balls. Your task is to find the minimum number of operations to move all balls to the same position, or report that this is impossible. 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$$$, $$$k$$$ ($$$2 le n le 100$$$, $$$0 le k le 10^6$$$)xa0— the number of balls and the attract power of all balls, respectively. The following $$$n$$$ lines describe the balls' coordinates. The $$$i$$$-th of these lines contains two integers $$$x_i$$$, $$$y_i$$$ ($$$0 le x_i, y_i le 10^5$$$)xa0— the coordinates of the $$$i$$$-th ball. It is guaranteed that all points are distinct. Output For each test case print a single integerxa0— the minimum number of operations to move all balls to the same position, or $$$-1$$$ if it is impossible. Example Input 3 3 2 0 0 3 3 1 1 3 3 6 7 8 8 6 9 4 1 0 0 0 1 0 2 0 3 Note In the first test case, there are three balls at $$$(0, 0)$$$, $$$(3, 3)$$$, and $$$(1, 1)$$$ and the attract power is $$$2$$$. It is possible to move two balls together with one operation, but not all three balls together with any number of operations. In the second test case, there are three balls at $$$(6, 7)$$$, $$$(8, 8)$$$, and $$$(6, 9)$$$ and the attract power is $$$3$$$. If we charge any ball, the other two will move to the same position, so we only require one operation. In the third test case, there are four balls at $$$(0, 0)$$$, $$$(0, 1)$$$, $$$(0, 2)$$$, and $$$(0, 3)$$$, and the attract power is $$$1$$$. We can show that it is impossible to move all balls to the same position with a sequence of operations. | 1,000 | false | true | false | false | false | false | true | false | false | false | 3,437 |
1249B2 | The only difference between easy and hard versions is constraints. There are $$$n$$$ kids, each of them is reading a unique book. At the end of any day, the $$$i$$$-th kid will give his book to the $$$p_i$$$-th kid (in case of $$$i = p_i$$$ the kid will give his book to himself). It is guaranteed that all values of $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.e. $$$p$$$ is a permutation). The sequence $$$p$$$ doesn't change from day to day, it is fixed. For example, if $$$n=6$$$ and $$$p=[4, 6, 1, 3, 5, 2]$$$ then at the end of the first day the book of the $$$1$$$-st kid will belong to the $$$4$$$-th kid, the $$$2$$$-nd kid will belong to the $$$6$$$-th kid and so on. At the end of the second day the book of the $$$1$$$-st kid will belong to the $$$3$$$-th kid, the $$$2$$$-nd kid will belong to the $$$2$$$-th kid and so on. Your task is to determine the number of the day the book of the $$$i$$$-th child is returned back to him for the first time for every $$$i$$$ from $$$1$$$ to $$$n$$$. Consider the following example: $$$p = [5, 1, 2, 4, 3]$$$. The book of the $$$1$$$-st kid will be passed to the following kids: after the $$$1$$$-st day it will belong to the $$$5$$$-th kid, after the $$$2$$$-nd day it will belong to the $$$3$$$-rd kid, after the $$$3$$$-rd day it will belong to the $$$2$$$-nd kid, after the $$$4$$$-th day it will belong to the $$$1$$$-st kid. So after the fourth day, the book of the first kid will return to its owner. The book of the fourth kid will return to him for the first time after exactly one day. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 1000$$$) — the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of kids in the query. The second line of the query contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ are distinct, i.e. $$$p$$$ is a permutation), where $$$p_i$$$ is the kid which will get the book of the $$$i$$$-th kid. It is guaranteed that $$$sum n le 2 cdot 10^5$$$ (sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$). Output For each query, print the answer on it: $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$, where $$$a_i$$$ is the number of the day the book of the $$$i$$$-th child is returned back to him for the first time in this query. | 1,300 | true | false | false | false | false | false | false | false | false | false | 4,462 |
1811G1 | This is an easy version of the problem, it differs from the hard one only by constraints on $$$n$$$ and $$$k$$$. Vlad found a row of $$$n$$$ tiles and the integer $$$k$$$. The tiles are indexed from left to right and the $$$i$$$-th tile has the color $$$c_i$$$. After a little thought, he decided what to do with it. You can start from any tile and jump to any number of tiles right, forming the path $$$p$$$. Let's call the path $$$p$$$ of length $$$m$$$ nice if: $$$p$$$ can be divided into blocks of length exactly $$$k$$$, that is, $$$m$$$ is divisible by $$$k$$$; $$$c_{p_1} = c_{p_2} = ldots = c_{p_k}$$$; $$$c_{p_{k+1}} = c_{p_{k+2}} = ldots = c_{p_{2k}}$$$; $$$ldots$$$ $$$c_{p_{m-k+1}} = c_{p_{m-k+2}} = ldots = c_{p_{m}}$$$; Your task is to find the number of nice paths of maximum length. Since this number may be too large, print it modulo $$$10^9 + 7$$$. Input The first line of each test contains the integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases in the test. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 100$$$) — the number of tiles in a row and the length of the block. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, c_3, dots, c_n$$$ ($$$1 le c_i le n$$$) — tile colors. It is guaranteed that the sum of $$$n^3$$$ over all test cases does not exceed $$$5 cdot 10^6$$$. Output Print $$$t$$$ numbers, each of which is the answer to the corresponding test casexa0— the number of nice paths of maximum length modulo $$$10^9 + 7$$$. Example Input 5 5 2 1 2 3 4 5 7 2 1 3 1 3 3 1 3 11 4 1 1 1 1 1 1 1 1 1 1 1 5 2 1 1 2 2 2 5 1 1 2 3 4 5 Note In the first sample, it is impossible to make a nice path with a length greater than $$$0$$$. In the second sample, we are interested in the following paths: $$$1 ightarrow 3 ightarrow 4 ightarrow 5$$$ $$$2 ightarrow 4 ightarrow 5 ightarrow 7$$$ $$$1 ightarrow 3 ightarrow 5 ightarrow 7$$$ $$$1 ightarrow 3 ightarrow 4 ightarrow 7$$$ In the third example, any path of length $$$8$$$ is nice. | 2,100 | true | false | false | true | false | false | false | false | false | false | 1,399 |
1922C | There are $$$n$$$ cities located on the number line, the $$$i$$$-th city is in the point $$$a_i$$$. The coordinates of the cities are given in ascending order, so $$$a_1 < a_2 < dots < a_n$$$. The distance between two cities $$$x$$$ and $$$y$$$ is equal to $$$a_x - a_y$$$. For each city $$$i$$$, let's define the closest city $$$j$$$ as the city such that the distance between $$$i$$$ and $$$j$$$ is not greater than the distance between $$$i$$$ and each other city $$$k$$$. For example, if the cities are located in points $$$[0, 8, 12, 15, 20]$$$, then: the closest city to the city $$$1$$$ is the city $$$2$$$; the closest city to the city $$$2$$$ is the city $$$3$$$; the closest city to the city $$$3$$$ is the city $$$4$$$; the closest city to the city $$$4$$$ is the city $$$3$$$; the closest city to the city $$$5$$$ is the city $$$4$$$. The cities are located in such a way that for every city, the closest city is unique. For example, it is impossible for the cities to be situated in points $$$[1, 2, 3]$$$, since this would mean that the city $$$2$$$ has two closest cities ($$$1$$$ and $$$3$$$, both having distance $$$1$$$). You can travel between cities. Suppose you are currently in the city $$$x$$$. Then you can perform one of the following actions: travel to any other city $$$y$$$, paying $$$a_x - a_y$$$ coins; travel to the city which is the closest to $$$x$$$, paying $$$1$$$ coin. You are given $$$m$$$ queries. In each query, you will be given two cities, and you have to calculate the minimum number of coins you have to spend to travel from one city to the other city. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case is given in the following format: the first line contains one integer $$$n$$$ ($$$2 le n le 10^5$$$); the second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_1 < a_2 < dots < a_n le 10^9$$$); the third line contains one integer $$$m$$$ ($$$1 le m le 10^5$$$); then $$$m$$$ lines follow; the $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$; $$$x_i e y_i$$$), denoting that in the $$$i$$$-th query, you have to calculate the minimum number of coins you have to spend to travel from the city $$$x_i$$$ to the city $$$y_i$$$. Additional constraints on the input: in every test case, for each city, the closest city is determined uniquely; the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$; the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$. | 1,300 | true | true | true | false | false | false | false | false | false | false | 756 |
946C | Problem - 946C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy strings *1300 No tag edit access → Contest materials . You cannot replace letter z with any other letter. Your target is to make some number of moves (not necessary minimal) to get string abcdefghijklmnopqrstuvwxyz (english alphabet) as a subsequence. Subsequence of the string is the string that is obtained by deleting characters at some positions. You need to print the string that will be obtained from the given string and will be contain english alphabet as a subsequence or say that it is impossible. Input The only one line of the input consisting of the string _s_ consisting of _s_ (1u2009≤u2009_s_u2009≤u2009105) small english letters. Output If you can get a string that can be obtained from the given string and will contain english alphabet as a subsequence, print it. Otherwise print «-1» (without quotes). Examples Input aacceeggiikkmmooqqssuuwwyy Output abcdefghijklmnopqrstuvwxyz Input thereisnoanswer Output -1 | 1,300 | false | true | false | false | false | false | false | false | false | false | 5,930 |
1340C | If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the girl's house is a road of $$$n$$$ lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light. Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up $$$g$$$ seconds green, and then $$$r$$$ seconds red, then again $$$g$$$ seconds green and so on. Formally, the road can be represented as a segment $$$[0, n]$$$. Initially, Denis is at point $$$0$$$. His task is to get to point $$$n$$$ in the shortest possible time. He knows many different integers $$$d_1, d_2, ldots, d_m$$$, where $$$0 leq d_i leq n$$$ xa0— are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on. Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed: He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by $$$pm 1$$$ in $$$1$$$ second. While doing so, he must always stay inside the segment $$$[0, n]$$$. He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by $$$+1$$$ and he walked on a safety island, then he can change his position by $$$pm 1$$$. Otherwise, he can change his position only by $$$+1$$$. Similarly, if in the previous second he changed his position by $$$-1$$$, on a safety island he can change position by $$$pm 1$$$, and at any other point by $$$-1$$$. At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on. Denis has crossed the road as soon as his coordinate becomes equal to $$$n$$$. This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do. Input The first line contains two integers $$$n$$$ and $$$m$$$ $$$(1 leq n leq 10^6, 2 leq m leq min(n + 1, 10^4))$$$ xa0— road width and the number of safety islands. The second line contains $$$m$$$ distinct integers $$$d_1, d_2, ldots, d_m$$$ $$$(0 leq d_i leq n)$$$ xa0— the points where the safety islands are located. It is guaranteed that there are $$$0$$$ and $$$n$$$ among them. The third line contains two integers $$$g, r$$$ $$$(1 leq g, r leq 1000)$$$ xa0— the time that the green light stays on and the time that the red light stays on. Note In the first test, the optimal route is: xa0xa0xa0xa0 for the first green light, go to $$$7$$$ and return to $$$3$$$. In this case, we will change the direction of movement at the point $$$7$$$, which is allowed, since there is a safety island at this point. In the end, we will be at the point of $$$3$$$, where there is also a safety island. The next $$$11$$$ seconds we have to wait for the red light. xa0xa0xa0xa0 for the second green light reaches $$$14$$$. Wait for the red light again. xa0xa0xa0xa0 for $$$1$$$ second go to $$$15$$$. As a result, Denis is at the end of the road. In total, $$$45$$$ seconds are obtained. In the second test, it is impossible to cross the road according to all the rules. | 2,400 | false | false | false | true | false | false | false | false | false | true | 4,005 |
1830A | Copil Copac is given a list of $$$n-1$$$ edges describing a tree of $$$n$$$ vertices. He decides to draw it using the following algorithm: Step $$$0$$$: Draws the first vertex (vertex $$$1$$$). Go to step $$$1$$$. Step $$$1$$$: For every edge in the input, in order: if the edge connects an already drawn vertex $$$u$$$ to an undrawn vertex $$$v$$$, he will draw the undrawn vertex $$$v$$$ and the edge. After checking every edge, go to step $$$2$$$. Step $$$2$$$: If all the vertices are drawn, terminate the algorithm. Else, go to step $$$1$$$. The number of readings is defined as the number of times Copil Copac performs step $$$1$$$. Find the number of readings needed by Copil Copac to draw the tree. Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of vertices of the tree. The following $$$n - 1$$$ lines of each test case contain two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$)xa0— indicating that $$$(u_i,v_i)$$$ is the $$$i$$$-th edge in the list. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the number of readings Copil Copac needs to draw the tree. Example Input 2 6 4 5 1 3 1 2 3 4 1 6 7 5 6 2 4 2 7 1 3 1 2 4 5 Note In the first test case: After the first reading, the tree will look like this: After the second reading: Therefore, Copil Copac needs $$$2$$$ readings to draw the tree. | 1,400 | false | false | false | true | false | false | false | false | false | true | 1,306 |
1696D | A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). You are given a permutation of $$$1,2,dots,n$$$, $$$[a_1,a_2,dots,a_n]$$$. For integers $$$i$$$, $$$j$$$ such that $$$1le i<jle n$$$, define $$$operatorname{mn}(i,j)$$$ as $$$minlimits_{k=i}^j a_k$$$, and define $$$operatorname{mx}(i,j)$$$ as $$$maxlimits_{k=i}^j a_k$$$. Let us build an undirected graph of $$$n$$$ vertices, numbered $$$1$$$ to $$$n$$$. For every pair of integers $$$1le i<jle n$$$, if $$$operatorname{mn}(i,j)=a_i$$$ and $$$operatorname{mx}(i,j)=a_j$$$ both holds, or $$$operatorname{mn}(i,j)=a_j$$$ and $$$operatorname{mx}(i,j)=a_i$$$ both holds, add an undirected edge of length $$$1$$$ between vertices $$$i$$$ and $$$j$$$. In this graph, find the length of the shortest path from vertex $$$1$$$ to vertex $$$n$$$. We can prove that $$$1$$$ and $$$n$$$ will always be connected via some path, so a shortest path always exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 5cdot 10^4$$$). Description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1le nle 2.5cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_n$$$ ($$$1le a_ile n$$$). It's guaranteed that $$$a$$$ is a permutation of $$$1$$$, $$$2$$$, $$$dots$$$, $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5cdot 10^5$$$. | 1,900 | false | true | false | false | true | true | false | true | false | false | 2,105 |
1995E2 | This is the hard version of a problem. The only difference between an easy and a hard version is the constraints on $$$t$$$ and $$$n$$$. You can make hacks only if both versions of the problem are solved. Arthur is giving a lesson to his famous $$$2 n$$$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $$$2 i - 1$$$ is sitting at the desk with the knight $$$2 i$$$. Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $$$i$$$-th knight as $$$a_i$$$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $$$maxlimits_{1 le i le n} (a_{2 i - 1} + a_{2 i}) - minlimits_{1 le i le n} (a_{2 i - 1} + a_{2 i})$$$. However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $$$a_i := a_{i + n}$$$, $$$a_{i + n} := a_i$$$ for any $$$1 le i le n$$$. Arthur can make any number of such swaps. What is the best result he can achieve? Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0— the number of test cases. It is followed by descriptions of the test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100,000$$$)xa0— the number of desks. The second line consists of $$$2n$$$ integers $$$a_1, a_2, ldots, a_{2 n}$$$ ($$$1 le a_i le 10^9$$$)xa0— the intelligence values of the knights. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100,000$$$. Output For each test case, output a single line containing one integerxa0— the minimal difference Arthur can achieve. Example Input 5 2 6 6 4 4 1 10 17 3 1 10 1 10 1 10 3 3 3 4 5 5 4 5 1 2 3 4 5 6 7 8 9 10 Note In the first test case, Arthur can swap the second and the fourth knights. Then the total intelligence at both desks will be $$$10$$$. In the third test case, Arthur can make $$$0$$$ operations, which will result in the total intelligence of $$$11$$$ at each of the desks. In the fourth test case, Arthur can swap knights with indices $$$2$$$ and $$$5$$$ and achieve the difference of $$$2$$$. It can be proven that he cannot improve his result any further. | 2,900 | false | false | false | true | true | false | false | false | false | false | 291 |
34B | Problem - 34B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy sortings *900 No tag edit access → Contest materials ") Разбор") — amount of TV sets at the sale, and amount of TV sets that Bob can carry. The following line contains _n_ space-separated integers _a__i_ (u2009-u20091000u2009≤u2009_a__i_u2009≤u20091000) — prices of the TV sets. Output Output the only number — the maximum sum of money that Bob can earn, given that he can carry at most _m_ TV sets. Examples Input 5 3 -6 0 35 -2 4 Output 8 Input 4 2 7 0 0 -7 Output 7 | 900 | false | true | false | false | false | false | false | false | true | false | 9,827 |
862B | Problem - 862B - 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 dfs and similar graphs trees *1300 No tag edit access → Contest materials ") that belongs to the graph, _u_ and _v_ belong to different sets. You can find more formal definitions of a tree and a bipartite graph in the notes section below. Dr. Evil gave Mahmoud and Ehab a tree consisting of _n_ nodes and asked them to add edges to it in such a way, that the graph is still bipartite. Besides, after adding these edges the graph should be simple (doesn't contain loops or multiple edges). What is the maximum number of edges they can add? A loop is an edge, which connects a node with itself. Graph doesn't contain multiple edges when for each pair of nodes there is no more than one edge between them. A cycle and a loop aren't the same . Input The first line of input contains an integer _n_xa0— the number of nodes in the tree (1u2009≤u2009_n_u2009≤u2009105). The next _n_u2009-u20091 lines contain integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_, _u_u2009≠u2009_v_)xa0— the description of the edges of the tree. It's guaranteed that the given graph is a tree. Output Output one integerxa0— the maximum number of edges that Mahmoud and Ehab can add to the tree while fulfilling the conditions. Examples Input 3 1 2 1 3 Output 0 Input 5 1 2 2 3 3 4 4 5 Output 2 Note Tree definition: | 1,300 | false | false | false | false | false | false | false | false | false | true | 6,299 |
297C | Polar bears like unique arrays — that is, arrays without repeated elements. You have got a unique array _s_ with length _n_ containing non-negative integers. Since you are good friends with Alice and Bob, you decide to split the array in two. Precisely, you need to construct two arrays _a_ and _b_ that are also of length _n_, with the following conditions for all _i_ (1u2009≤u2009_i_u2009≤u2009_n_): _a__i_,u2009_b__i_ are non-negative integers; _s__i_u2009=u2009_a__i_u2009+u2009_b__i_ . Ideally, _a_ and _b_ should also be unique arrays. However, life in the Arctic is hard and this is not always possible. Fortunately, Alice and Bob are still happy if their arrays are almost unique. We define an array of length _n_ to be almost unique, if and only if it can be turned into a unique array by removing no more than entries. For example, the array [1,u20092,u20091,u20093,u20092] is almost unique because after removing the first two entries, it becomes [1,u20093,u20092]. The array [1,u20092,u20091,u20093,u20091,u20092] is not almost unique because we need to remove at least 3 entries to turn it into a unique array. So, your task is to split the given unique array _s_ into two almost unique arrays _a_ and _b_. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ distinct integers _s_1,u2009_s_2,u2009... _s__n_ (0u2009≤u2009_s__i_u2009≤u2009109). Output If it is possible to make Alice and Bob happy (if you can split the given array), print "YES" (without quotes) in the first line. In the second line, print the array _a_. In the third line, print the array _b_. There may be more than one solution. Any of them will be accepted. If it is impossible to split _s_ into almost unique arrays _a_ and _b_, print "NO" (without quotes) in the first line. | 2,400 | false | false | false | false | false | true | false | false | false | false | 8,642 |
1364C | # Ehab and Prefix MEXs Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes Given an array a of length n, find another array, b, of length n such that: • for each i (1 ≤ i ≤ n) M EX ({b1, b2, . . . , bi}) = ai.The M EX of a set of integers is the smallest non-negative integer that doesn’t belong to this set. If such array doesn’t exist, determine this. ## Input The first line contains an integer n (1 ≤ n ≤ 10 5) x16 the length of the array a.The second line contains n integers a1, a2, . . . , an (0 ≤ ai ≤ i) x16 the elements of the array a. It’s guaranteed that ai ≤ ai+1 for 1 ≤ i < n . ## Output If there’s no such array, print a single line containing −1.Otherwise, print a single line containing n integers b1, b2, . . . , bn (0 ≤ bi ≤ 10 6)If there are multiple answers, print any. ## Examples standard input standard output 31 2 3 0 1 2 40 0 0 2 1 3 4 0 31 1 3 0 2 1 ## Note In the second test case, other answers like [1 , 1, 1, 0] , for example, are valid. Page 1 of 1 | 1,600 | false | true | false | false | false | true | true | false | false | false | 3,860 |
622A | Problem - 622A - Codeforces =============== xa0 ]( "Educational Codeforces Round 7") . Find the number on the _n_-th position of the sequence. Input The only line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091014) — the position of the number to find. Note that the given number is too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type. Output Print the element in the _n_-th position of the sequence (the elements are numerated from one). Examples Input 3 Output 2 Input 5 Output 2 Input 10 Output 4 Input 55 Output 10 Input 56 Output 1 | 1,000 | true | false | true | false | false | false | false | false | false | false | 7,346 |
1107D | You are given a binary matrix $$$A$$$ of size $$$n imes n$$$. Let's denote an $$$x$$$-compression of the given matrix as a matrix $$$B$$$ of size $$$frac{n}{x} imes frac{n}{x}$$$ such that for every $$$i in [1, n], j in [1, n]$$$ the condition $$$A[i][j] = B[lceil frac{i}{x} ceil][lceil frac{j}{x} ceil]$$$ is met. Obviously, $$$x$$$-compression is possible only if $$$x$$$ divides $$$n$$$, but this condition is not enough. For example, the following matrix of size $$$2 imes 2$$$ does not have any $$$2$$$-compression: $$$01$$$ $$$10$$$ For the given matrix $$$A$$$, find maximum $$$x$$$ such that an $$$x$$$-compression of this matrix is possible. Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input. Input The first line contains one number $$$n$$$ ($$$4 le n le 5200$$$) — the number of rows and columns in the matrix $$$A$$$. It is guaranteed that $$$n$$$ is divisible by $$$4$$$. Then the representation of matrix follows. Each of $$$n$$$ next lines contains $$$frac{n}{4}$$$ one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from $$$0$$$ to $$$9$$$ or as uppercase Latin letters from $$$A$$$ to $$$F$$$). Binary representation of each of these numbers denotes next $$$4$$$ elements of the matrix in the corresponding row. For example, if the number $$$B$$$ is given, then the corresponding elements are 1011, and if the number is $$$5$$$, then the corresponding elements are 0101. Elements are not separated by whitespaces. Output Print one number: maximum $$$x$$$ such that an $$$x$$$-compression of the given matrix is possible. Examples Input 8 E7 E7 E7 00 00 E7 E7 E7 Note The first example corresponds to the matrix: $$$11100111$$$ $$$11100111$$$ $$$11100111$$$ $$$00000000$$$ $$$00000000$$$ $$$11100111$$$ $$$11100111$$$ $$$11100111$$$ It is easy to see that the answer on this example is $$$1$$$. | 1,800 | true | false | true | true | false | false | false | false | false | false | 5,166 |
42A | Problem - 42A - Codeforces =============== xa0 . This should also improve his algebra skills. According to the borscht recipe it consists of _n_ ingredients that have to be mixed in proportion litres (thus, there should be _a_1u2009·_x_,u2009...,u2009_a__n_u2009·_x_ litres of corresponding ingredients mixed for some non-negative _x_). In the kitchen Volodya found out that he has _b_1,u2009...,u2009_b__n_ litres of these ingredients at his disposal correspondingly. In order to correct his algebra mistakes he ought to cook as much soup as possible in a _V_ litres volume pan (which means the amount of soup cooked can be between 0 and _V_ litres). What is the volume of borscht Volodya will cook ultimately? Input The first line of the input contains two space-separated integers _n_ and _V_ (1u2009≤u2009_n_u2009≤u200920,u20091u2009≤u2009_V_u2009≤u200910000). The next line contains _n_ space-separated integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009100). Finally, the last line contains _n_ space-separated integers _b__i_ (0u2009≤u2009_b__i_u2009≤u2009100). Output Your program should output just one real number — the volume of soup that Volodya will cook. Your answer must have a relative or absolute error less than 10u2009-u20094. Examples Input 1 100 1 40 Output 40.0 Input 2 100 1 1 25 30 Output 50.0 Input 2 100 1 1 60 60 Output 100.0 | 1,400 | false | true | true | false | false | false | false | false | false | false | 9,779 |
1254B1 | This is the easier version of the problem. In this version, $$$1 le n le 10^5$$$ and $$$0 le a_i le 1$$$. You can hack this problem only if you solve and lock both problems. Christmas is coming, and our protagonist, Bob, is preparing a spectacular present for his long-time best friend Alice. This year, he decides to prepare $$$n$$$ boxes of chocolate, numbered from $$$1$$$ to $$$n$$$. Initially, the $$$i$$$-th box contains $$$a_i$$$ chocolate pieces. Since Bob is a typical nice guy, he will not send Alice $$$n$$$ empty boxes. In other words, at least one of $$$a_1, a_2, ldots, a_n$$$ is positive. Since Alice dislikes coprime sets, she will be happy only if there exists some integer $$$k > 1$$$ such that the number of pieces in each box is divisible by $$$k$$$. Note that Alice won't mind if there exists some empty boxes. Charlie, Alice's boyfriend, also is Bob's second best friend, so he decides to help Bob by rearranging the chocolate pieces. In one second, Charlie can pick up a piece in box $$$i$$$ and put it into either box $$$i-1$$$ or box $$$i+1$$$ (if such boxes exist). Of course, he wants to help his friend as quickly as possible. Therefore, he asks you to calculate the minimum number of seconds he would need to make Alice happy. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of chocolate boxes. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 1$$$)xa0— the number of chocolate pieces in the $$$i$$$-th box. It is guaranteed that at least one of $$$a_1, a_2, ldots, a_n$$$ is positive. Output If there is no way for Charlie to make Alice happy, print $$$-1$$$. Otherwise, print a single integer $$$x$$$xa0— the minimum number of seconds for Charlie to help Bob make Alice happy. | 1,800 | true | true | false | false | false | true | false | false | false | false | 4,415 |
870F | Problem - 870F - Codeforces =============== xa0 ") ") be the shortest distance between _u_ and _v_, or 0 if there is no path between them. Compute the sum of values _d_(_u_,u2009_v_) over all 1u2009≤u2009_u_u2009<u2009_v_u2009≤u2009_n_. The _gcd_ (greatest common divisor) of two positive integers is the maximum positive integer that divides both of the integers. Input Single integer _n_ (1u2009≤u2009_n_u2009≤u2009107). Output Print the sum of _d_(_u_,u2009_v_) over all 1u2009≤u2009_u_u2009<u2009_v_u2009≤u2009_n_. Examples Input 6 Output 8 Input 10 Output 44 Note All shortest paths in the first example: There are no paths between other pairs of vertices. The total distance is 2u2009+u20091u2009+u20091u2009+u20092u2009+u20091u2009+u20091u2009=u20098. | 2,700 | false | false | false | false | true | false | false | false | false | false | 6,256 |
1845B | Bob and Carol hanged out with Alice the whole day, but now it's time to go home. Alice, Bob and Carol live on an infinite 2D grid in cells $$$A$$$, $$$B$$$, and $$$C$$$ respectively. Right now, all of them are in cell $$$A$$$. If Bob (or Carol) is in some cell, he (she) can move to one of the neighboring cells. Two cells are called neighboring if they share a side. For example, the cell $$$(3, 5)$$$ has four neighboring cells: $$$(2, 5)$$$, $$$(4, 5)$$$, $$$(3, 6)$$$ and $$$(3, 4)$$$. Bob wants to return to the cell $$$B$$$, Carolxa0— to the cell $$$C$$$. Both of them want to go along the shortest path, i.xa0e. along the path that consists of the minimum possible number of cells. But they would like to walk together as well. What is the maximum possible number of cells that Bob and Carol can walk together if each of them walks home using one of the shortest paths? Input The first line contains the single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$x_A$$$ and $$$y_A$$$ ($$$1 le x_A, y_A le 10^8$$$)xa0— the position of cell $$$A$$$, where both Bob and Carol are right now. The second line contains two integers $$$x_B$$$ and $$$y_B$$$ ($$$1 le x_B, y_B le 10^8$$$)xa0— the position of cell $$$B$$$ (Bob's house). The third line contains two integers $$$x_C$$$ and $$$y_C$$$ ($$$1 le x_C, y_C le 10^8$$$)xa0— the position of cell $$$C$$$ (Carol's house). Additional constraint on the input: the cells $$$A$$$, $$$B$$$, and $$$C$$$ are pairwise distinct in each test case. Output For each test case, print the single integerxa0— the maximum number of cells Bob and Carol can walk together if each of them goes home along one of the shortest paths. Example Input 3 3 1 1 3 6 4 5 2 2 2 7 2 1 1 4 3 5 5 Note In all pictures, red color denotes cells belonging only to Bob's path, light blue color — cells belonging only to Carol's path, and dark blue color — cells belonging to both paths. One of the optimal routes for the first test case is shown below: Bob's route contains $$$5$$$ cells, Carol's routexa0— $$$7$$$ cells, and they will visit $$$3$$$ cells together. The optimal answer for the second test case is shown below: Bob's route contains $$$4$$$ cells, Carol's routexa0— $$$3$$$ cells, and they will visit only $$$1$$$ cell together. One of the optimal answers for the third test case is shown below: Bob's route contains $$$6$$$ cells, Carol's routexa0— $$$9$$$ cells, and they will visit $$$6$$$ cells together. | 900 | true | false | true | false | false | false | false | false | false | false | 1,213 |
1542A | You are given a multiset (i.xa0e. a set that can contain multiple equal integers) containing $$$2n$$$ integers. Determine if you can split it into exactly $$$n$$$ pairs (i.xa0e. each element should be in exactly one pair) so that the sum of the two elements in each pair is odd (i.xa0e. when divided by $$$2$$$, the remainder is $$$1$$$). Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1leq tleq 100$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1leq nleq 100$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1,a_2,dots, a_{2n}$$$ ($$$0leq a_ileq 100$$$) — the numbers in the set. Output For each test case, print "Yes" if it can be split into exactly $$$n$$$ pairs so that the sum of the two elements in each pair is odd, and "No" otherwise. You can print each letter in any case. Example Input 5 2 2 3 4 5 3 2 3 4 5 5 5 1 2 4 1 2 3 4 1 5 3 2 6 7 3 4 Note In the first test case, a possible way of splitting the set is $$$(2,3)$$$, $$$(4,5)$$$. In the second, third and fifth test case, we can prove that there isn't any possible way. In the fourth test case, a possible way of splitting the set is $$$(2,3)$$$. | 800 | true | false | false | false | false | false | false | false | false | false | 2,934 |
632C | Problem - 632C - Codeforces =============== xa0 . Each of the next _n_ lines contains one string _a__i_ (1u2009≤u2009_a__i_u2009≤u200950) consisting of only lowercase English letters. The sum of string lengths will not exceed 5·104. Output Print the only string _a_ — the lexicographically smallest string concatenation. Examples Input 4 abba abacaba bcd er Output abacabaabbabcder Input 5 x xx xxa xxaa xxaaa Output xxaaaxxaaxxaxxx Input 3 c cb cba Output cbacbc | 1,700 | false | false | false | false | false | false | false | false | true | false | 7,279 |
629A | Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning to make his birthday cake weird! The cake is a _n_u2009×u2009_n_ square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door's happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors's family is wondering what is the amount of happiness of Famil going to be? Please, note that any pair can be counted no more than once, as two different cells can't share both the same row and the same column. Input In the first line of the input, you are given a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the length of the side of the cake. Then follow _n_ lines, each containing _n_ characters. Empty cells are denoted with '.', while cells that contain chocolates are denoted by 'C'. Output Print the value of Famil Door's happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column. Note If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are: 1. (1,u20092) and (1,u20093) 2. (3,u20091) and (3,u20093) Pieces that share the same column are: 1. (2,u20091) and (3,u20091) 2. (1,u20093) and (3,u20093) | 800 | false | false | true | false | false | true | true | false | false | false | 7,309 |
1878C | Aca and Milovan, two fellow competitive programmers, decided to give Vasilije a problem to test his skills. Vasilije is given three positive integers: $$$n$$$, $$$k$$$, and $$$x$$$, and he has to determine if he can choose $$$k$$$ distinct integers between $$$1$$$ and $$$n$$$, such that their sum is equal to $$$x$$$. Since Vasilije is now in the weirdest city in Serbia where Aca and Milovan live, Cacak, the problem seems weird to him. So he needs your help with this problem. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The only line of each test case contains three integers $$$n$$$, $$$k$$$ and $$$x$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le k le n$$$, $$$1 le x le 4 cdot 10^{10}$$$)xa0— the maximum element he can choose, the number of elements he can choose and the sum he has to reach. Note that the sum of $$$n$$$ over all test cases may exceed $$$2 cdot 10^5$$$. Output For each test case output one line: "YES", if it is possible to choose $$$k$$$ distinct integers between $$$1$$$ and $$$n$$$, such that their sum is equal to $$$x$$$, and "NO", if it isn't. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 12 5 3 10 5 3 3 10 10 55 6 5 20 2 1 26 187856 87856 2609202300 200000 190000 19000000000 28 5 2004 2 2 2006 9 6 40 47202 32455 613407217 185977 145541 15770805980 Output YES NO YES YES NO NO YES NO NO NO YES YES Note In the first test case $$$n = 5, k=3, x=10$$$, so we can choose the numbers: $$$2$$$, $$$3$$$, $$$5$$$, whose sum is $$$10$$$, so the answer is "YES". In the second test case $$$n = 5, k=3, x=3$$$, there is no three numbers which satisfies the condition, so the answer is "NO". It can be shown that there are no three numbers whose sum is $$$3$$$. | 900 | true | false | false | false | false | false | false | false | false | false | 1,015 |
750C | Every Codeforces user has rating, described with one integer, possibly negative or zero. Users are divided into two divisions. The first division is for users with rating 1900 or higher. Those with rating 1899 or lower belong to the second division. In every contest, according to one's performance, his or her rating changes by some value, possibly negative or zero. Limak competed in _n_ contests in the year 2016. He remembers that in the _i_-th contest he competed in the division _d__i_ (i.e. he belonged to this division just before the start of this contest) and his rating changed by _c__i_ just after the contest. Note that negative _c__i_ denotes the loss of rating. What is the maximum possible rating Limak can have right now, after all _n_ contests? If his rating may be arbitrarily big, print "Infinity". If there is no scenario matching the given information, print "Impossible". Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000). The _i_-th of next _n_ lines contains two integers _c__i_ and _d__i_ (u2009-u2009100u2009≤u2009_c__i_u2009≤u2009100, 1u2009≤u2009_d__i_u2009≤u20092), describing Limak's rating change after the _i_-th contest and his division during the _i_-th contest contest. Output If Limak's current rating can be arbitrarily big, print "Infinity" (without quotes). If the situation is impossible, print "Impossible" (without quotes). Otherwise print one integer, denoting the maximum possible value of Limak's current rating, i.e. rating after the _n_ contests. Note In the first sample, the following scenario matches all information Limak remembers and has maximum possible final rating: Limak has rating 1901 and belongs to the division 1 in the first contest. His rating decreases by 7. With rating 1894 Limak is in the division 2. His rating increases by 5. Limak has rating 1899 and is still in the division 2. In the last contest of the year he gets u2009+u20098 and ends the year with rating 1907. In the second sample, it's impossible that Limak is in the division 1, his rating increases by 57 and after that Limak is in the division 2 in the second contest. | 1,600 | true | true | false | false | false | false | false | true | false | false | 6,791 |
261C | Problem - 261C - 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 dp math *2000 No tag edit access → Contest materials u2009×u2009(_m_u2009+u20091): Maxim asks you to count, how many numbers _m_ (1u2009≤u2009_m_u2009≤u2009_n_) are there, such that the sum of values in the cells in the row number _m_u2009+u20091 of the resulting matrix equals _t_. Expression (_x_ _xor_ _y_) means applying the operation of bitwise excluding "OR" to numbers _x_ and _y_. The given operation exists in all modern programming languages. For example, in languages C++ and Java it is represented by character "^", in Pascal — by "xor". Input A single line contains two integers _n_ and _t_ (1u2009≤u2009_n_,u2009_t_u2009≤u20091012,u2009_t_u2009≤u2009_n_u2009+u20091). Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Output In a single line print a single integer — the answer to the problem. Examples Input 1 1 Output 1 Input 3 2 Output 1 Input 3 3 Output 0 Input 1000000000000 1048576 Output 118606527258 | 2,000 | true | false | false | true | false | true | false | false | false | false | 8,788 |
1076C | Problem - 1076C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search math *1300 No tag edit access → Contest materials — the number of test cases. Each test case contains one integer $$$d$$$ $$$(0 le d le 10^3)$$$. Output For each test print one line. If there is an answer for the $$$i$$$-th test, print "Y", and then the numbers $$$a$$$ and $$$b$$$. If there is no answer for the $$$i$$$-th test, print "N". Your answer will be considered correct if $$$(a + b) - a cdot b le 10^{-6}$$$ and $$$(a + b) - d le 10^{-6}$$$. Example Input 7 69 0 1 4 5 999 1000 Output Y 67.985071301 1.014928699 Y 0.000000000 0.000000000 N Y 2.000000000 2.000000000 Y 3.618033989 1.381966011 Y 997.998996990 1.001003010 Y 998.998997995 1.001002005 | 1,300 | true | false | false | false | false | false | false | true | false | false | 5,342 |
1295E | You are given a permutation $$$p_1, p_2, dots , p_n$$$ (an array where each integer from $$$1$$$ to $$$n$$$ appears exactly once). The weight of the $$$i$$$-th element of this permutation is $$$a_i$$$. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements $$$p_1, p_2, dots , p_k$$$, the second — $$$p_{k+1}, p_{k+2}, dots , p_n$$$, where $$$1 le k < n$$$. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay $$$a_i$$$ dollars to move the element $$$p_i$$$. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if $$$p = [3, 1, 2]$$$ and $$$a = [7, 1, 4]$$$, then the optimal strategy is: separate $$$p$$$ into two parts $$$[3, 1]$$$ and $$$[2]$$$ and then move the $$$2$$$-element into first set (it costs $$$4$$$). And if $$$p = [3, 5, 1, 6, 2, 4]$$$, $$$a = [9, 1, 9, 9, 1, 9]$$$, then the optimal strategy is: separate $$$p$$$ into two parts $$$[3, 5, 1]$$$ and $$$[6, 2, 4]$$$, and then move the $$$2$$$-element into first set (it costs $$$1$$$), and $$$5$$$-element into second set (it also costs $$$1$$$). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the length of permutation. The second line contains $$$n$$$ integers $$$p_1, p_2, dots , p_n$$$ ($$$1 le p_i le n$$$). It's guaranteed that this sequence contains each element from $$$1$$$ to $$$n$$$ exactly once. The third line contains $$$n$$$ integers $$$a_1, a_2, dots , a_n$$$ ($$$1 le a_i le 10^9$$$). | 2,200 | false | false | false | false | true | false | false | false | false | false | 4,219 |
888F | There are _n_ points marked on the plane. The points are situated in such a way that they form a regular polygon (marked points are its vertices, and they are numbered in counter-clockwise order). You can draw _n_u2009-u20091 segments, each connecting any two marked points, in such a way that all points have to be connected with each other (directly or indirectly). But there are some restrictions. Firstly, some pairs of points cannot be connected directly and have to be connected undirectly. Secondly, the segments you draw must not intersect in any point apart from the marked points (that is, if any two segments intersect and their intersection is not a marked point, then the picture you have drawn is invalid). How many ways are there to connect all vertices with _n_u2009-u20091 segments? Two ways are considered different iff there exist some pair of points such that a segment is drawn between them in the first way of connection, but it is not drawn between these points in the second one. Since the answer might be large, output it modulo 109u2009+u20097. Input The first line contains one number _n_ (3u2009≤u2009_n_u2009≤u2009500) — the number of marked points. Then _n_ lines follow, each containing _n_ elements. _a__i_,u2009_j_ (_j_-th element of line _i_) is equal to 1 iff you can connect points _i_ and _j_ directly (otherwise _a__i_,u2009_j_u2009=u20090). It is guaranteed that for any pair of points _a__i_,u2009_j_u2009=u2009_a__j_,u2009_i_, and for any point _a__i_,u2009_i_u2009=u20090. Output Print the number of ways to connect points modulo 109u2009+u20097. Examples Input 4 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0 | 2,500 | false | false | false | true | false | false | false | false | false | true | 6,191 |
1619D | Vlad has $$$n$$$ friends, for each of whom he wants to buy one gift for the New Year. There are $$$m$$$ shops in the city, in each of which he can buy a gift for any of his friends. If the $$$j$$$-th friend ($$$1 le j le n$$$) receives a gift bought in the shop with the number $$$i$$$ ($$$1 le i le m$$$), then the friend receives $$$p_{ij}$$$ units of joy. The rectangular table $$$p_{ij}$$$ is given in the input. Vlad has time to visit at most $$$n-1$$$ shops (where $$$n$$$ is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them. Let the $$$j$$$-th friend receive $$$a_j$$$ units of joy from Vlad's gift. Let's find the value $$$alpha=min{a_1, a_2, dots, a_n}$$$. Vlad's goal is to buy gifts so that the value of $$$alpha$$$ is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends. For example, let $$$m = 2$$$, $$$n = 2$$$. Let the joy from the gifts that we can buy in the first shop: $$$p_{11} = 1$$$, $$$p_{12}=2$$$, in the second shop: $$$p_{21} = 3$$$, $$$p_{22}=4$$$. Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy $$$3$$$, and for the secondxa0— bringing joy $$$4$$$. In this case, the value $$$alpha$$$ will be equal to $$$min{3, 4} = 3$$$ Help Vlad choose gifts for his friends so that the value of $$$alpha$$$ is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most $$$n-1$$$ shops (where $$$n$$$ is the number of friends). In the shop, he can buy any number of gifts. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. An empty line is written before each test case. Then there is a line containing integers $$$m$$$ and $$$n$$$ ($$$2 le n$$$, $$$2 le n cdot m le 10^5$$$) separated by a spacexa0— the number of shops and the number of friends, where $$$n cdot m$$$ is the product of $$$n$$$ and $$$m$$$. Then $$$m$$$ lines follow, each containing $$$n$$$ numbers. The number in the $$$i$$$-th row of the $$$j$$$-th column $$$p_{ij}$$$ ($$$1 le p_{ij} le 10^9$$$) is the joy of the product intended for friend number $$$j$$$ in shop number $$$i$$$. It is guaranteed that the sum of the values $$$n cdot m$$$ over all test cases in the test does not exceed $$$10^5$$$. Output Print $$$t$$$ lines, each line must contain the answer to the corresponding test casexa0— the maximum possible value of $$$alpha$$$, where $$$alpha$$$ is the minimum of the joys from a gift for all of Vlad's friends. | 1,800 | false | true | false | false | false | false | false | true | true | false | 2,532 |
2025E | In the most popular card game in Berland, a deck of $$$n imes m$$$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $$$1$$$ to $$$n$$$, and ranks are numbered from $$$1$$$ to $$$m$$$. There is exactly one card in the deck for each combination of suit and rank. A card with suit $$$a$$$ and rank $$$b$$$ can beat a card with suit $$$c$$$ and rank $$$d$$$ in one of two cases: $$$a = 1$$$, $$$c e 1$$$ (a card of suit $$$1$$$ can beat a card of any other suit); $$$a = c$$$, $$$b > d$$$ (a card can beat any other card of the same suit but of a lower rank). Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i.u2009e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins. Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $$$998244353$$$. Input The only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 500$$$). Additional constraint on the input: $$$m$$$ is even. Output Print a single integerxa0— the number of ways to distribute the cards so that the first player wins, taken modulo $$$998244353$$$. | 2,200 | true | true | false | true | false | false | false | false | false | false | 119 |
297A | Problem - 297A - 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 *1700 No tag edit access → Contest materials _a_ and _b_. Then you try to turn _a_ into _b_ using two types of operations: Write _parity_(_a_) to the end of _a_. For example, . Remove the first character of _a_. For example, . You cannot perform this operation if _a_ is empty. You can use as many operations as you want. The problem is, is it possible to turn _a_ into _b_? The _parity_ of a 01-string is 1 if there is an odd number of "1"s in the string, and 0 otherwise. Input The first line contains the string _a_ and the second line contains the string _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u20091000). Both strings contain only the characters "0" and "1". Here _x_ denotes the length of the string _x_. Output Print "YES" (without quotes) if it is possible to turn _a_ into _b_, and "NO" (without quotes) otherwise. Examples Input 01011 0110 Output YES Input 0011 1110 Output NO Note In the first sample, the steps are as follows: 01011u2009→u20091011u2009→u2009011u2009→u20090110 | 1,700 | false | false | false | false | false | true | false | false | false | false | 8,644 |
7A | A famous Berland's painter Kalevitch likes to shock the public. One of his last obsessions is chess. For more than a thousand years people have been playing this old game on uninteresting, monotonous boards. Kalevitch decided to put an end to this tradition and to introduce a new attitude to chessboards. As before, the chessboard is a square-checkered board with the squares arranged in a 8u2009×u20098 grid, each square is painted black or white. Kalevitch suggests that chessboards should be painted in the following manner: there should be chosen a horizontal or a vertical line of 8 squares (i.e. a row or a column), and painted black. Initially the whole chessboard is white, and it can be painted in the above described way one or more times. It is allowed to paint a square many times, but after the first time it does not change its colour any more and remains black. Kalevitch paints chessboards neatly, and it is impossible to judge by an individual square if it was painted with a vertical or a horizontal stroke. Kalevitch hopes that such chessboards will gain popularity, and he will be commissioned to paint chessboards, which will help him ensure a comfortable old age. The clients will inform him what chessboard they want to have, and the painter will paint a white chessboard meeting the client's requirements. It goes without saying that in such business one should economize on everything — for each commission he wants to know the minimum amount of strokes that he has to paint to fulfill the client's needs. You are asked to help Kalevitch with this task. Input The input file contains 8 lines, each of the lines contains 8 characters. The given matrix describes the client's requirements, W character stands for a white square, and B character — for a square painted black. It is guaranteed that client's requirments can be fulfilled with a sequence of allowed strokes (vertical/column or horizontal/row). | 1,100 | false | false | false | false | false | true | true | false | false | false | 9,960 |
1472E | Polycarp has invited $$$n$$$ friends to celebrate the New Year. During the celebration, he decided to take a group photo of all his friends. Each friend can stand or lie on the side. Each friend is characterized by two values $$$h_i$$$ (their height) and $$$w_i$$$ (their width). On the photo the $$$i$$$-th friend will occupy a rectangle $$$h_i imes w_i$$$ (if they are standing) or $$$w_i imes h_i$$$ (if they are lying on the side). The $$$j$$$-th friend can be placed in front of the $$$i$$$-th friend on the photo if his rectangle is lower and narrower than the rectangle of the $$$i$$$-th friend. Formally, at least one of the following conditions must be fulfilled: $$$h_j < h_i$$$ and $$$w_j < w_i$$$ (both friends are standing or both are lying); $$$w_j < h_i$$$ and $$$h_j < w_i$$$ (one of the friends is standing and the other is lying). For example, if $$$n = 3$$$, $$$h=[3,5,3]$$$ and $$$w=[4,4,3]$$$, then: the first friend can be placed in front of the second: $$$w_1 < h_2$$$ and $$$h_1 < w_2$$$ (one of the them is standing and the other one is lying); the third friend can be placed in front of the second: $$$h_3 < h_2$$$ and $$$w_3 < w_2$$$ (both friends are standing or both are lying). In other cases, the person in the foreground will overlap the person in the background. Help Polycarp for each $$$i$$$ find any $$$j$$$, such that the $$$j$$$-th friend can be located in front of the $$$i$$$-th friend (i.e. at least one of the conditions above is fulfilled). Please note that you do not need to find the arrangement of all people for a group photo. You just need to find for each friend $$$i$$$ any other friend $$$j$$$ who can be located in front of him. Think about it as you need to solve $$$n$$$ separate independent subproblems. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of friends. This is followed by $$$n$$$ lines, each of which contains a description of the corresponding friend. Each friend is described by two integers $$$h_i$$$ and $$$w_i$$$ ($$$1 leq h_i, w_i leq 10^9$$$)xa0— height and width of the $$$i$$$-th friend, respectively. 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$$$ integers on a separate line, where the $$$i$$$-th number is the index of a friend that can be placed in front of the $$$i$$$-th. If there is no such friend, then output -1. If there are several answers, output any. Note The first test case is described in the statement. In the third test case, the following answers are also correct: $$$[-1, -1, 1, 2]$$$; $$$[-1, -1, 1, 1]$$$; $$$[-1, -1, 2, 1]$$$. | 1,700 | false | false | false | true | true | false | false | true | true | false | 3,320 |
1217F | You are given an undirected graph with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Initially there are no edges. You are asked to perform some queries on the graph. Let $$$last$$$ be the answer to the latest query of the second type, it is set to $$$0$$$ before the first such query. Then the queries are the following: $$$1~x~y$$$ ($$$1 le x, y le n$$$, $$$x e y$$$) — add an undirected edge between the vertices $$$(x + last - 1)~mod~n + 1$$$ and $$$(y + last - 1)~mod~n + 1$$$ if it doesn't exist yet, otherwise remove it; $$$2~x~y$$$ ($$$1 le x, y le n$$$, $$$x e y$$$) — check if there exists a path between the vertices $$$(x + last - 1)~mod~n + 1$$$ and $$$(y + last - 1)~mod~n + 1$$$, which goes only through currently existing edges, and set $$$last$$$ to $$$1$$$ if so and $$$0$$$ otherwise. Good luck! Input The first line contains two integer numbers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 2 cdot 10^5$$$) — the number of vertices and the number of queries, respectively. Each of the following $$$m$$$ lines contains a query of one of two aforementioned types. It is guaranteed that there is at least one query of the second type. Output Print a string, consisting of characters '0' and '1'. The $$$i$$$-th character should be the answer to the $$$i$$$-th query of the second type. Therefore the length of the string should be equal to the number of queries of the second type. Examples Input 5 9 1 1 2 1 1 3 2 3 2 1 2 4 2 3 4 1 2 4 2 3 4 1 1 3 2 4 3 Input 3 9 1 1 2 1 2 3 1 3 1 2 1 3 1 3 2 2 2 3 1 1 2 2 1 2 2 1 2 Note The converted queries in the first example are: 1 1 2 1 1 3 2 3 2 1 3 5 2 4 5 1 2 4 2 3 4 1 2 4 2 5 4 The converted queries in the second example are: 1 1 2 1 2 3 1 3 1 2 1 3 1 1 3 2 3 1 1 2 3 2 2 3 2 1 2 | 2,600 | false | false | false | false | true | false | false | false | false | true | 4,585 |
610A | Pasha has a wooden stick of some positive integer length _n_. He wants to perform exactly three cuts to get four parts of the stick. Each part must have some positive integer length and the sum of these lengths will obviously be _n_. Pasha likes rectangles but hates squares, so he wonders, how many ways are there to split a stick into four parts so that it's possible to form a rectangle using these parts, but is impossible to form a square. Your task is to help Pasha and count the number of such ways. Two ways to cut the stick are considered distinct if there exists some integer _x_, such that the number of parts of length _x_ in the first way differ from the number of parts of length _x_ in the second way. Input The first line of the input contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u20092·109) — the length of Pasha's stick. Output The output should contain a single integerxa0— the number of ways to split Pasha's stick into four parts of positive integer length so that it's possible to make a rectangle by connecting the ends of these parts, but is impossible to form a square. Note There is only one way to divide the stick in the first sample {1, 1, 2, 2}. Four ways to divide the stick in the second sample are {1, 1, 9, 9}, {2, 2, 8, 8}, {3, 3, 7, 7} and {4, 4, 6, 6}. Note that {5, 5, 5, 5} doesn't work. | 1,000 | true | false | false | false | false | false | false | false | false | false | 7,406 |
507C | Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of height _h_. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node. Let's index all the leaf nodes from the left to the right from 1 to 2_h_. The exit is located at some node _n_ where 1u2009≤u2009_n_u2009≤u20092_h_, the player doesn't know where the exit is so he has to guess his way out! Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules: Character 'L' means "go to the left child of the current node"; Character 'R' means "go to the right child of the current node"; If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node; If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command; If he reached a leaf node that is not the exit, he returns to the parent of the current node; If he reaches an exit, the game is finished. Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit? Input Input consists of two integers _h_,u2009_n_ (1u2009≤u2009_h_u2009≤u200950, 1u2009≤u2009_n_u2009≤u20092_h_). Output Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm. Note A perfect binary tree of height _h_ is a binary tree consisting of _h_u2009+u20091 levels. Level 0 consists of a single node called root, level _h_ consists of 2_h_ nodes called leaves. Each node that is not a leaf has exactly two children, left and right one. Following picture illustrates the sample test number 3. Nodes are labeled according to the order of visit. | 1,700 | true | false | true | false | false | false | false | false | false | false | 7,814 |
1530F | Getting ready for VK Fest 2021, you prepared a table with $$$n$$$ rows and $$$n$$$ columns, and filled each cell of this table with some event related with the festival that could either happen or not: for example, whether you will win a prize on the festival, or whether it will rain. Forecasting algorithms used in VK have already estimated the probability for each event to happen. Event in row $$$i$$$ and column $$$j$$$ will happen with probability $$$a_{i, j} cdot 10^{-4}$$$. All of the events are mutually independent. Let's call the table winning if there exists a line such that all $$$n$$$ events on it happen. The line could be any horizontal line (cells $$$(i, 1), (i, 2), ldots, (i, n)$$$ for some $$$i$$$), any vertical line (cells $$$(1, j), (2, j), ldots, (n, j)$$$ for some $$$j$$$), the main diagonal (cells $$$(1, 1), (2, 2), ldots, (n, n)$$$), or the antidiagonal (cells $$$(1, n), (2, n - 1), ldots, (n, 1)$$$). Find the probability of your table to be winning, and output it modulo $$$31,607$$$ (see Output section). Input The first line contains a single integer $$$n$$$xa0($$$2 le n le 21$$$)xa0— the dimensions of the table. The $$$i$$$-th of the next $$$n$$$ lines contains $$$n$$$ integers $$$a_{i, 1}, a_{i, 2}, ldots, a_{i, n}$$$ ($$$0 < a_{i, j} < 10^4$$$). The probability of event in cell $$$(i, j)$$$ to happen is $$$a_{i, j} cdot 10^{-4}$$$. Output Print the probability that your table will be winning, modulo $$$31,607$$$. Formally, let $$$M = 31,607$$$. 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}$$$. Note In the first example, any two events form a line, and the table will be winning if any two events happen. The probability of this is $$$frac{11}{16}$$$, and $$$5927 cdot 16 equiv 11 pmod{31,607}$$$. | 2,600 | true | false | false | true | false | false | false | false | false | false | 3,010 |
1326D2 | This is the hard version of the problem. The difference is the constraint on the sum of lengths of strings and the number of test cases. You can make hacks only if you solve all versions of this task. You are given a string $$$s$$$, consisting of lowercase English letters. Find the longest string, $$$t$$$, which satisfies the following conditions: The length of $$$t$$$ does not exceed the length of $$$s$$$. $$$t$$$ is a palindrome. There exists two strings $$$a$$$ and $$$b$$$ (possibly empty), such that $$$t = a + b$$$ ( "$$$+$$$" represents concatenation), and $$$a$$$ is prefix of $$$s$$$ while $$$b$$$ is suffix of $$$s$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case is a non-empty string $$$s$$$, consisting of lowercase English letters. It is guaranteed that the sum of lengths of strings over all test cases does not exceed $$$10^6$$$. Output For each test case, print the longest string which satisfies the conditions described above. If there exists multiple possible solutions, print any of them. Example Input 5 a abcdfdcecba abbaxyzyx codeforces acbba Output a abcdfdcba xyzyx c abba Note In the first test, the string $$$s = $$$"a" satisfies all conditions. In the second test, the string "abcdfdcba" satisfies all conditions, because: Its length is $$$9$$$, which does not exceed the length of the string $$$s$$$, which equals $$$11$$$. It is a palindrome. "abcdfdcba" $$$=$$$ "abcdfdc" $$$+$$$ "ba", and "abcdfdc" is a prefix of $$$s$$$ while "ba" is a suffix of $$$s$$$. It can be proven that there does not exist a longer string which satisfies the conditions. In the fourth test, the string "c" is correct, because "c" $$$=$$$ "c" $$$+$$$ "" and $$$a$$$ or $$$b$$$ can be empty. The other possible solution for this test is "s". | 1,800 | false | true | false | false | false | false | false | true | false | false | 4,083 |
204E | The Little Elephant loves strings very much. He has an array _a_ from _n_ strings, consisting of lowercase English letters. Let's number the elements of the array from 1 to _n_, then let's denote the element number _i_ as _a__i_. For each string _a__i_ (1u2009≤u2009_i_u2009≤u2009_n_) the Little Elephant wants to find the number of pairs of integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_a__i_) such that substring _a__i_[_l_... _r_] is a substring to at least _k_ strings from array _a_ (including the _i_-th string). Help the Little Elephant solve this problem. If you are not familiar with the basic notation in string problems, you can find the corresponding definitions in the notes. Input The first line contains two space-separated integers — _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009105). Next _n_ lines contain array _a_. The _i_-th line contains a non-empty string _a__i_, consisting of lowercase English letter. The total length of all strings _a__i_ does not exceed 105. Output On a single line print _n_ space-separated integers — the _i_-th number is the answer for string _a__i_. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 7 4 rubik furik abab baba aaabbbababa abababababa zero Note Let's assume that you are given string _a_u2009=u2009_a_1_a_2... _a__a_, then let's denote the string's length as _a_ and the string's _i_-th character as _a__i_. A substring _a_[_l_... _r_] (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_a_) of string _a_ is string _a__l__a__l_u2009+u20091... _a__r_. String _a_ is a substring of string _b_, if there exists such pair of integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_b_), that _b_[_l_... _r_]u2009=u2009_a_. | 2,800 | false | false | true | false | true | false | false | false | false | false | 9,023 |
1131B | You still have partial information about the score during the historic football match. You are given a set of pairs $$$(a_i, b_i)$$$, indicating that at some point during the match the score was "$$$a_i$$$: $$$b_i$$$". It is known that if the current score is «$$$x$$$:$$$y$$$», then after the goal it will change to "$$$x+1$$$:$$$y$$$" or "$$$x$$$:$$$y+1$$$". What is the largest number of times a draw could appear on the scoreboard? The pairs "$$$a_i$$$:$$$b_i$$$" are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10000$$$) — the number of known moments in the match. Each of the next $$$n$$$ lines contains integers $$$a_i$$$ and $$$b_i$$$ ($$$0 le a_i, b_i le 10^9$$$), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team). All moments are given in chronological order, that is, sequences $$$x_i$$$ and $$$y_j$$$ are non-decreasing. The last score denotes the final result of the match. Output Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted. Note In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4. | 1,400 | false | true | true | false | false | false | false | false | false | false | 5,067 |
272E | Problem - 272E - 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 graphs *2200 No tag edit access → Contest materials . The horse land isn't very hostile, so the number of enemies of each horse is at most 3. Right now the horse land is going through an election campaign. So the horses trusted Dima to split them into two parts. At that the horses want the following condition to hold: a horse shouldn't have more than one enemy in its party. Help Dima split the horses into parties. Note that one of the parties can turn out to be empty. Input The first line contains two integers _n_,u2009_m_ — the number of horses in the horse land and the number of enemy pairs. Next _m_ lines define the enemy pairs. The _i_-th line contains integers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa0_a__i_u2009≠u2009_b__i_), which mean that horse _a__i_ is the enemy of horse _b__i_. Consider the horses indexed in some way from 1 to _n_. It is guaranteed that each horse has at most three enemies. No pair of enemies occurs more than once in the input. Output Print a line, consisting of _n_ characters: the _i_-th character of the line must equal "0", if the horse number _i_ needs to go to the first party, otherwise this character should equal "1". If there isn't a way to divide the horses as required, print -1. Examples Input 3 3 1 2 3 2 3 1 Output 100 Input 2 1 2 1 Output 00 Input 10 6 1 2 1 3 1 4 2 3 2 4 3 4 Output 0110000000 | 2,200 | false | false | false | false | false | true | false | false | false | true | 8,742 |
1329C | Drazil likes heap very much. So he created a problem with heap: There is a max heap with a height $$$h$$$ implemented on the array. The details of this heap are the following: This heap contains exactly $$$2^h - 1$$$ distinct positive non-zero integers. All integers are distinct. These numbers are stored in the array $$$a$$$ indexed from $$$1$$$ to $$$2^h-1$$$. For any $$$1 < i < 2^h$$$, $$$a[i] < a[left lfloor{frac{i}{2}} ight floor]$$$. Now we want to reduce the height of this heap such that the height becomes $$$g$$$ with exactly $$$2^g-1$$$ numbers in heap. To reduce the height, we should perform the following action $$$2^h-2^g$$$ times: Choose an index $$$i$$$, which contains an element and call the following function $$$f$$$ in index $$$i$$$: Note that we suppose that if $$$a[i]=0$$$, then index $$$i$$$ don't contain an element. After all operations, the remaining $$$2^g-1$$$ element must be located in indices from $$$1$$$ to $$$2^g-1$$$. Now Drazil wonders what's the minimum possible sum of the remaining $$$2^g-1$$$ elements. Please find this sum and find a sequence of the function calls to achieve this value. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 70,000$$$): the number of test cases. Each test case contain two lines. The first line contains two integers $$$h$$$ and $$$g$$$ ($$$1 leq g < h leq 20$$$). The second line contains $$$n = 2^h-1$$$ distinct positive integers $$$a[1], a[2], ldots, a[n]$$$ ($$$1 leq a[i] < 2^{20}$$$). For all $$$i$$$ from $$$2$$$ to $$$2^h - 1$$$, $$$a[i] < a[left lfloor{frac{i}{2}} ight floor]$$$. The total sum of $$$n$$$ is less than $$$2^{20}$$$. Output For each test case, print two lines. The first line should contain one integer denoting the minimum sum after reducing the height of heap to $$$g$$$. The second line should contain $$$2^h - 2^g$$$ integers $$$v_1, v_2, ldots, v_{2^h-2^g}$$$. In $$$i$$$-th operation $$$f(v_i)$$$ should be called. Example Input 2 3 2 7 6 3 5 4 2 1 3 2 7 6 5 4 3 2 1 Output 10 3 2 3 1 8 2 1 3 1 | 2,400 | false | true | true | false | true | true | false | false | false | false | 4,063 |
1382B | There are $$$n$$$ piles of stones, where the $$$i$$$-th pile has $$$a_i$$$ stones. Two people play a game, where they take alternating turns removing stones. In a move, a player may remove a positive number of stones from the first non-empty pile (the pile with the minimal index, that has at least one stone). The first player who cannot make a move (because all piles are empty) loses the game. If both players play optimally, determine the winner of the game. Input The first line contains a single integer $$$t$$$ ($$$1le tle 1000$$$) xa0— the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1le nle 10^5$$$) xa0— the number of piles. The second line of each test case contains $$$n$$$ integers $$$a_1,ldots,a_n$$$ ($$$1le a_ile 10^9$$$) xa0— $$$a_i$$$ is equal to the number of stones in the $$$i$$$-th pile. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. Output For each test case, if the player who makes the first move will win, output "First". Otherwise, output "Second". Example Input 7 3 2 5 4 8 1 1 1 1 1 1 1 1 6 1 2 3 4 5 6 6 1 1 2 1 2 2 1 1000000000 5 1 2 2 1 1 3 1 1 1 Output First Second Second First First Second First Note In the first test case, the first player will win the game. His winning strategy is: 1. The first player should take the stones from the first pile. He will take $$$1$$$ stone. The numbers of stones in piles will be $$$[1, 5, 4]$$$. 2. The second player should take the stones from the first pile. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 5, 4]$$$. 3. The first player should take the stones from the second pile because the first pile is empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 1, 4]$$$. 4. The second player should take the stones from the second pile because the first pile is empty. He will take $$$1$$$ stone because he can't take any other number of stones. The numbers of stones in piles will be $$$[0, 0, 4]$$$. 5. The first player should take the stones from the third pile because the first and second piles are empty. He will take $$$4$$$ stones. The numbers of stones in piles will be $$$[0, 0, 0]$$$. 6. The second player will lose the game because all piles will be empty. | 1,100 | false | false | false | true | false | false | false | false | false | false | 3,757 |
1326A | You are given a integer $$$n$$$ ($$$n > 0$$$). Find any integer $$$s$$$ which satisfies these conditions, or report that there are no such numbers: In the decimal representation of $$$s$$$: $$$s > 0$$$, $$$s$$$ consists of $$$n$$$ digits, no digit in $$$s$$$ equals $$$0$$$, $$$s$$$ is not divisible by any of it's digits. Input The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 400$$$), the number of test cases. The next $$$t$$$ lines each describe a test case. Each test case contains one positive integer $$$n$$$ ($$$1 leq n leq 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$10^5$$$. Output For each test case, print an integer $$$s$$$ which satisfies the conditions described above, or "-1" (without quotes), if no such number exists. If there are multiple possible solutions for $$$s$$$, print any solution. Note In the first test case, there are no possible solutions for $$$s$$$ consisting of one digit, because any such solution is divisible by itself. For the second test case, the possible solutions are: $$$23$$$, $$$27$$$, $$$29$$$, $$$34$$$, $$$37$$$, $$$38$$$, $$$43$$$, $$$46$$$, $$$47$$$, $$$49$$$, $$$53$$$, $$$54$$$, $$$56$$$, $$$57$$$, $$$58$$$, $$$59$$$, $$$67$$$, $$$68$$$, $$$69$$$, $$$73$$$, $$$74$$$, $$$76$$$, $$$78$$$, $$$79$$$, $$$83$$$, $$$86$$$, $$$87$$$, $$$89$$$, $$$94$$$, $$$97$$$, and $$$98$$$. For the third test case, one possible solution is $$$239$$$ because $$$239$$$ is not divisible by $$$2$$$, $$$3$$$ or $$$9$$$ and has three digits (none of which equals zero). | 1,000 | false | false | false | false | false | true | false | false | false | false | 4,087 |
413B | The R2 company has _n_ employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke. R2 has _m_ Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the _k_-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification. The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee. Input The first line contains three space-separated integers _n_, _m_ and _k_ (2u2009≤u2009_n_u2009≤u20092·104;xa01u2009≤u2009_m_u2009≤u200910;xa01u2009≤u2009_k_u2009≤u20092·105) — the number of the employees, the number of chats and the number of events in the log, correspondingly. Next _n_ lines contain matrix _a_ of size _n_u2009×u2009_m_, consisting of numbers zero and one. The element of this matrix, recorded in the _j_-th column of the _i_-th line, (let's denote it as _a__ij_) equals 1, if the _i_-th employee is the participant of the _j_-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to _n_ and the chats are numbered from 1 to _m_. Next _k_ lines contain the description of the log events. The _i_-th line contains two space-separated integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_;xa01u2009≤u2009_y__i_u2009≤u2009_m_) which mean that the employee number _x__i_ sent one message to chat number _y__i_. It is guaranteed that employee number _x__i_ is a participant of chat _y__i_. It is guaranteed that each chat contains at least two employees. Output Print in the single line _n_ space-separated integers, where the _i_-th integer shows the number of message notifications the _i_-th employee receives. Examples Input 3 4 5 1 1 1 1 1 0 1 1 1 1 0 0 1 1 3 1 1 3 2 4 3 2 Input 4 3 4 0 1 1 1 0 1 1 1 1 0 0 0 1 2 2 1 3 1 1 3 | 1,300 | false | false | true | false | false | false | false | false | false | false | 8,173 |
435B | Problem - 435B - 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 *1400 No tag edit access → Contest materials ") Editorial") . Output Print the maximum number that Pasha can get if he makes at most _k_ swaps. Examples Input 1990 1 Output 9190 Input 300 0 Output 300 Input 1034 2 Output 3104 Input 9090000078001234 6 Output 9907000008001234 | 1,400 | false | true | false | false | false | false | false | false | false | false | 8,101 |
1101B | An accordion is a string (yes, in the real world accordions are musical instruments, but let's forget about it for a while) which can be represented as a concatenation of: an opening bracket (ASCII code $$$091$$$), a colon (ASCII code $$$058$$$), some (possibly zero) vertical line characters (ASCII code $$$124$$$), another colon, and a closing bracket (ASCII code $$$093$$$). The length of the accordion is the number of characters in it. For example, [::], [::] and [::] are accordions having length $$$4$$$, $$$6$$$ and $$$7$$$. (::), {::}, [:], ]::[ are not accordions. You are given a string $$$s$$$. You want to transform it into an accordion by removing some (possibly zero) characters from it. Note that you may not insert new characters or reorder existing ones. Is it possible to obtain an accordion by removing characters from $$$s$$$, and if so, what is the maximum possible length of the result? Input The only line contains one string $$$s$$$ ($$$1 le s le 500000$$$). It consists of lowercase Latin letters and characters [, ], : and . Output If it is not possible to obtain an accordion by removing some characters from $$$s$$$, print $$$-1$$$. Otherwise print maximum possible length of the resulting accordion. | 1,300 | false | true | true | false | false | false | false | false | false | false | 5,199 |
1704E | Cirno has a DAG (Directed Acyclic Graph) with $$$n$$$ nodes and $$$m$$$ edges. The graph has exactly one node that has no out edges. The $$$i$$$-th node has an integer $$$a_i$$$ on it. Every second the following happens: Let $$$S$$$ be the set of nodes $$$x$$$ that have $$$a_x > 0$$$. For all $$$x in S$$$, $$$1$$$ is subtracted from $$$a_x$$$, and then for each node $$$y$$$, such that there is an edge from $$$x$$$ to $$$y$$$, $$$1$$$ is added to $$$a_y$$$. Find the first moment of time when all $$$a_i$$$ become $$$0$$$. Since the answer can be very large, output it modulo $$$998,244,353$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of test cases. Description of test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$1 leq n, m leq 1000$$$) — the number of vertices and edges in the graph. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 10^9$$$) — the integer on vertices. Each line of the following $$$m$$$ lines contains two integers $$$x, y$$$ ($$$1 leq x, y leq n$$$), represent a directed edge from $$$x$$$ to $$$y$$$. It is guaranteed that the graph is a DAG with no multi-edges, and there is exactly one node that has no out edges. It is guaranteed that both sum of $$$n$$$ and sum of $$$m$$$ over all test cases are less than or equal to $$$10,000$$$. Output For each test case, print an integer in a separate line — the first moment of time when all $$$a_i$$$ become $$$0$$$, modulo $$$998,244,353$$$. Example Input 5 3 2 1 1 1 1 2 2 3 5 5 1 0 0 0 0 1 2 2 3 3 4 4 5 1 5 10 11 998244353 0 0 0 998244353 0 0 0 0 0 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 1 3 7 9 5 6 1293 1145 9961 9961 1919 1 2 2 3 3 4 5 4 1 4 2 4 6 9 10 10 10 10 10 10 1 2 1 3 2 3 4 3 6 3 3 5 6 5 6 1 6 2 Note In the first test case: At time $$$0$$$, the values of the nodes are $$$[1, 1, 1]$$$. At time $$$1$$$, the values of the nodes are $$$[0, 1, 1]$$$. At time $$$2$$$, the values of the nodes are $$$[0, 0, 1]$$$. At time $$$3$$$, the values of the nodes are $$$[0, 0, 0]$$$. So the answer is $$$3$$$. In the second test case: At time $$$0$$$, the values of the nodes are $$$[1, 0, 0, 0, 0]$$$. At time $$$1$$$, the values of the nodes are $$$[0, 1, 0, 0, 1]$$$. At time $$$2$$$, the values of the nodes are $$$[0, 0, 1, 0, 0]$$$. At time $$$3$$$, the values of the nodes are $$$[0, 0, 0, 1, 0]$$$. At time $$$4$$$, the values of the nodes are $$$[0, 0, 0, 0, 1]$$$. At time $$$5$$$, the values of the nodes are $$$[0, 0, 0, 0, 0]$$$. So the answer is $$$5$$$. In the third test case: The first moment of time when all $$$a_i$$$ become $$$0$$$ is $$$6cdot 998244353 + 4$$$. | 2,200 | true | false | true | true | false | true | true | false | false | true | 2,051 |
681A | Codeforces user' handle color depends on his ratingxa0— it is red if his rating is greater or equal to 2400; it is orange if his rating is less than 2400 but greater or equal to 2200, etc. Each time participant takes part in a rated contest, his rating is changed depending on his performance. Anton wants the color of his handle to become red. He considers his performance in the rated contest to be good if he outscored some participant, whose handle was colored red before the contest and his rating has increased after it. Anton has written a program that analyses contest results and determines whether he performed good or not. Are you able to do the same? Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of participants Anton has outscored in this contest . The next _n_ lines describe participants results: the _i_-th of them consists of a participant handle _name__i_ and two integers _before__i_ and _after__i_ (u2009-u20094000u2009≤u2009_before__i_,u2009_after__i_u2009≤u20094000)xa0— participant's rating before and after the contest, respectively. Each handle is a non-empty string, consisting of no more than 10 characters, which might be lowercase and uppercase English letters, digits, characters «_» and «-» characters. It is guaranteed that all handles are distinct. Output Print «YES» (quotes for clarity), if Anton has performed good in the contest and «NO» (quotes for clarity) otherwise. Examples Input 3 Burunduk1 2526 2537 BudAlNik 2084 2214 subscriber 2833 2749 Input 3 Applejack 2400 2400 Fluttershy 2390 2431 Pinkie_Pie -2500 -2450 Note In the first sample, Anton has outscored user with handle Burunduk1, whose handle was colored red before the contest and his rating has increased after the contest. In the second sample, Applejack's rating has not increased after the contest, while both Fluttershy's and Pinkie_Pie's handles were not colored red before the contest. | 800 | false | false | true | false | false | false | false | false | false | false | 7,103 |
850D | Ivan is reading a book about tournaments. He knows that a tournament is an oriented graph with exactly one oriented edge between each pair of vertices. The score of a vertex is the number of edges going outside this vertex. Yesterday Ivan learned Landau's criterion: there is tournament with scores _d_1u2009≤u2009_d_2u2009≤u2009...u2009≤u2009_d__n_ if and only if for all 1u2009≤u2009_k_u2009<u2009_n_ and . Now, Ivan wanna solve following problem: given a set of numbers _S_u2009=u2009{_a_1,u2009_a_2,u2009...,u2009_a__m_}, is there a tournament with given set of scores? I.e. is there tournament with sequence of scores _d_1,u2009_d_2,u2009...,u2009_d__n_ such that if we remove duplicates in scores, we obtain the required set {_a_1,u2009_a_2,u2009...,u2009_a__m_}? Find a tournament with minimum possible number of vertices. Input The first line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u200931). The next line contains _m_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (0u2009≤u2009_a__i_u2009≤u200930)xa0— elements of the set _S_. It is guaranteed that all elements of the set are distinct. Output If there are no such tournaments, print string "=(" (without quotes). Otherwise, print an integer _n_xa0— the number of vertices in the tournament. Then print _n_ lines with _n_ charactersxa0— matrix of the tournament. The _j_-th element in the _i_-th row should be 1 if the edge between the _i_-th and the _j_-th vertices is oriented towards the _j_-th vertex, and 0 otherwise. The main diagonal should contain only zeros. Examples Output 6 000111 100011 110001 011001 001101 000000 | 2,800 | true | true | false | true | false | true | false | false | false | true | 6,348 |
981C | Ramesses knows a lot about problems involving trees (undirected connected graphs without cycles)! He created a new useful tree decomposition, but he does not know how to construct it, so he asked you for help! The decomposition is the splitting the edges of the tree in some simple paths in such a way that each two paths have at least one common vertex. Each edge of the tree should be in exactly one path. Help Remesses, find such a decomposition of the tree or derermine that there is no such decomposition. Input The first line contains a single integer $$$n$$$ ($$$2 leq n leq 10^{5}$$$) the number of nodes in the tree. Each of the next $$$nu2009-u20091$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n$$$, $$$a_i eq b_i$$$)xa0— the edges of the tree. It is guaranteed that the given edges form a tree. Output If there are no decompositions, print the only line containing "No". Otherwise in the first line print "Yes", and in the second line print the number of paths in the decomposition $$$m$$$. Each of the next $$$m$$$ lines should contain two integers $$$u_i$$$, $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$) denoting that one of the paths in the decomposition is the simple path between nodes $$$u_i$$$ and $$$v_i$$$. Each pair of paths in the decomposition should have at least one common vertex, and each edge of the tree should be presented in exactly one path. You can print the paths and the ends of each path in arbitrary order. If there are multiple decompositions, print any. Note The tree from the first example is shown on the picture below: The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions. The tree from the second example is shown on the picture below: We can show that there are no valid decompositions of this tree. The tree from the third example is shown on the picture below: The number next to each edge corresponds to the path number in the decomposition. It is easy to see that this decomposition suits the required conditions. | 1,400 | false | false | true | false | false | false | false | false | false | false | 5,798 |
1392I | As Kevin is in BigMan's house, suddenly a trap sends him onto a grid with $$$n$$$ rows and $$$m$$$ columns. BigMan's trap is configured by two arrays: an array $$$a_1,a_2,ldots,a_n$$$ and an array $$$b_1,b_2,ldots,b_m$$$. In the $$$i$$$-th row there is a heater which heats the row by $$$a_i$$$ degrees, and in the $$$j$$$-th column there is a heater which heats the column by $$$b_j$$$ degrees, so that the temperature of cell $$$(i,j)$$$ is $$$a_i+b_j$$$. Fortunately, Kevin has a suit with one parameter $$$x$$$ and two modes: heat resistance. In this mode suit can stand all temperatures greater or equal to $$$x$$$, but freezes as soon as reaches a cell with temperature less than $$$x$$$. cold resistance. In this mode suit can stand all temperatures less than $$$x$$$, but will burn as soon as reaches a cell with temperature at least $$$x$$$. Once Kevin lands on a cell the suit automatically turns to cold resistance mode if the cell has temperature less than $$$x$$$, or to heat resistance mode otherwise, and cannot change after that. We say that two cells are adjacent if they share an edge. Let a path be a sequence $$$c_1,c_2,ldots,c_k$$$ of cells such that $$$c_i$$$ and $$$c_{i+1}$$$ are adjacent for $$$1 leq i leq k-1$$$. We say that two cells are connected if there is a path between the two cells consisting only of cells that Kevin can step on. A connected component is a maximal set of pairwise connected cells. We say that a connected component is good if Kevin can escape the grid starting from it xa0— when it contains at least one border cell of the grid, and that it's bad otherwise. To evaluate the situation, Kevin gives a score of $$$1$$$ to each good component and a score of $$$2$$$ for each bad component. The final score will be the difference between the total score of components with temperatures bigger than or equal to $$$x$$$ and the score of components with temperatures smaller than $$$x$$$. There are $$$q$$$ possible values of $$$x$$$ that Kevin can use, and for each of them Kevin wants to know the final score. Help Kevin defeat BigMan! Input The first line contains three integers $$$n$$$,$$$m$$$,$$$q$$$ ($$$1 leq n,m,q leq 10^5$$$) xa0– the number of rows, columns, and the number of possible values for $$$x$$$ respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^5$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, dots, b_m$$$ ($$$1 leq b_i leq 10^5$$$). Each of the next $$$q$$$ lines contains one integer $$$x$$$ ($$$1 leq x leq 2 cdot 10^5$$$). | 3,300 | true | false | false | false | false | false | false | false | false | true | 3,708 |
1765H | There are $$$n$$$ people (numbered from $$$1$$$ to $$$n$$$) signed up for a doctor's appointment. The doctor has to choose in which order he will appoint these people. The $$$i$$$-th patient should be appointed among the first $$$p_i$$$ people. There are also $$$m$$$ restrictions of the following format: the $$$i$$$-th restriction is denoted by two integers $$$(a_i, b_i)$$$ and means that the patient with the index $$$a_i$$$ should be appointed earlier than the patient with the index $$$b_i$$$. For example, if $$$n = 4$$$, $$$p = [2, 3, 2, 4]$$$, $$$m = 1$$$, $$$a = [3]$$$ and $$$b = [1]$$$, then the only order of appointment of patients that does not violate the restrictions is $$$[3, 1, 2, 4]$$$. For $$$n =3$$$, $$$p = [3, 3, 3]$$$, $$$m = 0$$$, $$$a = []$$$ and $$$b = []$$$, any order of appointment is valid. For each patient, calculate the minimum position in the order that they can have among all possible orderings that don't violate the restrictions. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2000$$$; $$$0 le m le 2000$$$). The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$). Then $$$m$$$ lines follow. The $$$i$$$-th of them contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$; $$$a_i e b_i$$$). All pairs of $$$(a_i, b_i)$$$ are distinct (i.u2009e. if $$$i e j$$$, then either $$$a_i e a_j$$$, $$$b_i e b_j$$$, or both). Additional constraint on the input: there is at least one valid order of patients. Output Print $$$n$$$ integers, where $$$i$$$-th integer is equal to the minimum position of $$$i$$$-th patient in the order, among all valid orders. Positions in the order are numbered from $$$1$$$ to $$$n$$$. Examples Input 5 3 4 3 3 2 5 3 1 1 5 4 2 Note In the first example, $$$[3, 1, 2, 4]$$$ the only one valid order, so the minimum position of each patient is equal to their position in this order. In the second example, any order is valid, so any patient can be appointed first. In the third example, there are three valid orders: $$$[4, 2, 3, 1, 5]$$$, $$$[3, 4, 2, 1, 5]$$$ and $$$[4, 3, 2, 1, 5]$$$. | 2,200 | false | true | true | false | false | false | false | true | false | true | 1,710 |
793A | Oleg the bank client checks share prices every day. There are _n_ share prices he is interested in. Today he observed that each second exactly one of these prices decreases by _k_ rubles (note that each second exactly one price changes, but at different seconds different prices can change). Prices can become negative. Oleg found this process interesting, and he asked Igor the financial analyst, what is the minimum time needed for all _n_ prices to become equal, or it is impossible at all? Igor is busy right now, so he asked you to help Oleg. Can you answer this question? Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_k_u2009≤u2009109)xa0— the number of share prices, and the amount of rubles some price decreases each second. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the initial prices. Output Print the only line containing the minimum number of seconds needed for prices to become equal, of «-1» if it is impossible. Examples Input 4 1 1 1000000000 1000000000 1000000000 Note Consider the first example. Suppose the third price decreases in the first second and become equal 12 rubles, then the first price decreases and becomes equal 9 rubles, and in the third second the third price decreases again and becomes equal 9 rubles. In this case all prices become equal 9 rubles in 3 seconds. There could be other possibilities, but this minimizes the time needed for all prices to become equal. Thus the answer is 3. In the second example we can notice that parity of first and second price is different and never changes within described process. Thus prices never can become equal. In the third example following scenario can take place: firstly, the second price drops, then the third price, and then fourth price. It happens 999999999 times, and, since in one second only one price can drop, the whole process takes 999999999u2009*u20093u2009=u20092999999997 seconds. We can note that this is the minimum possible time. | 900 | true | false | true | false | false | false | false | false | false | false | 6,607 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.