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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
492A | Problem - 492A - 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 ") u2009+u2009_i_ cubes. Vanya wants to know what is the maximum height of the pyramid that he can make using the given cubes. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009104) — the number of cubes given to Vanya. Output Print the maximum possible height of the pyramid in the single line. Examples Input 1 Output 1 Input 25 Output 4 Note Illustration to the second sample: | 800 | false | false | true | false | false | false | false | false | false | false | 7,868 |
626G | Johnny is at a carnival which has _n_ raffles. Raffle _i_ has a prize with value _p__i_. Each participant can put tickets in whichever raffles they choose (they may have more than one ticket in a single raffle). At the end of the carnival, one ticket is selected at random from each raffle, and the owner of the ticket wins the associated prize. A single person can win multiple prizes from different raffles. However, county rules prevent any one participant from owning more than half the tickets in a single raffle, i.e. putting more tickets in the raffle than all the other participants combined. To help combat this (and possibly win some prizes), the organizers started by placing a single ticket in each raffle, which they will never remove. Johnny bought _t_ tickets and is wondering where to place them. Currently, there are a total of _l__i_ tickets in the _i_-th raffle. He watches as other participants place tickets and modify their decisions and, at every moment in time, wants to know how much he can possibly earn. Find the maximum possible expected value of Johnny's winnings at each moment if he distributes his tickets optimally. Johnny may redistribute all of his tickets arbitrarily between each update, but he may not place more than _t_ tickets total or have more tickets in a single raffle than all other participants combined. Input The first line contains two integers _n_, _t_, and _q_ (1u2009≤u2009_n_,u2009_t_,u2009_q_u2009≤u2009200 000) — the number of raffles, the number of tickets Johnny has, and the total number of updates, respectively. The second line contains _n_ space-separated integers _p__i_ (1u2009≤u2009_p__i_u2009≤u20091000) — the value of the _i_-th prize. The third line contains _n_ space-separated integers _l__i_ (1u2009≤u2009_l__i_u2009≤u20091000) — the number of tickets initially in the _i_-th raffle. The last _q_ lines contain the descriptions of the updates. Each description contains two integers _t__k_, _r__k_ (1u2009≤u2009_t__k_u2009≤u20092, 1u2009≤u2009_r__k_u2009≤u2009_n_) — the type of the update and the raffle number. An update of type 1 represents another participant adding a ticket to raffle _r__k_. An update of type 2 represents another participant removing a ticket from raffle _r__k_. It is guaranteed that, after each update, each raffle has at least 1 ticket (not including Johnny's) in it. Output Print _q_ lines, each containing a single real numberxa0— the maximum expected value of Johnny's winnings after the _k_-th update. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Examples Input 2 1 3 4 5 1 2 1 1 1 2 2 1 Output 1.666666667 1.333333333 2.000000000 Input 3 20 5 6 8 10 6 6 6 1 1 1 2 1 3 2 3 2 3 Output 12.000000000 12.000000000 11.769230769 12.000000000 12.000000000 Note In the first case, Johnny only has one ticket to distribute. The prizes are worth 4 and 5, and the raffles initially have 1 and 2 tickets, respectively. After the first update, each raffle has 2 tickets, so Johnny has expected value of winning by placing his ticket into the second raffle. The second update adds a ticket to the second raffle, so Johnny can win in the first raffle. After the final update, Johnny keeps his ticket in the first raffle and wins . In the second case, Johnny has more tickets than he is allowed to spend. In particular, after the first update, there are 7, 6, and 6 tickets in each raffle, respectively, so Johnny can only put in 19 tickets, winning each prize with probability . Also, note that after the last two updates, Johnny must remove a ticket from the last raffle in order to stay under the tickets in the third raffle. | 3,100 | true | true | false | true | true | false | false | false | false | false | 7,322 |
1431B | Polycarp has just finished writing down the lecture on elvish languages. The language this week was "VwV" (pronounced as "uwu"). The writing system of this language consists of only two lowercase Latin letters: 'v' and 'w'. Unfortunately, Polycarp has written all the lecture in cursive and without any spaces, so the notes look like a neverending sequence of squiggles. To be exact, Polycarp can't tell 'w' apart from 'vv' in his notes as both of them consist of the same two squiggles. Luckily, his brother Monocarp has better writing habits, so Polycarp managed to take his notes and now wants to make his own notes more readable. To do that he can follow the notes of Monocarp and underline some letters in his own notes in such a way that there is no more ambiguity. If he underlines a 'v', then it can't be mistaken for a part of some 'w', and if the underlines a 'w', then it can't be mistaken for two adjacent letters 'v'. What is the minimum number of letters Polycarp should underline to make his notes unambiguous? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of testcases. Each of the next $$$t$$$ lines contains a non-empty string in VwV language, which consists only of lowercase Latin letters 'v' and 'w'. The length of the string does not exceed $$$100$$$. Output For each testcase print a single integer: the minimum number of letters Polycarp should underline so that there is no ambiguity in his notes. Note In the first testcase it's enough to underline any of the two letters 'v'. In the second testcase the letter 'v' is not ambiguous by itself already, so you don't have to underline anything. In the third testcase you have to underline 'w', so that you don't mix it up with two letters 'v'. In the fourth testcase you can underline 'w' to avoid all the ambiguity. In the fifth testcase you can underline both letters 'w' and any of the two letters 'v' between them. | 1,400 | false | false | true | false | false | false | false | false | false | false | 3,508 |
1428G1 | This is the easy version of the problem. The only difference is that in this version $$$q=1$$$. You can make hacks only if all versions of the problem are solved. Zookeeper has been teaching his $$$q$$$ sheep how to write and how to add. The $$$i$$$-th sheep has to write exactly $$$k$$$ non-negative integers with the sum $$$n_i$$$. Strangely, sheep have superstitions about digits and believe that the digits $$$3$$$, $$$6$$$, and $$$9$$$ are lucky. To them, the fortune of a number depends on the decimal representation of the number; the fortune of a number is equal to the sum of fortunes of its digits, and the fortune of a digit depends on its value and position and can be described by the following table. For example, the number $$$319$$$ has fortune $$$F_{2} + 3F_{0}$$$. Each sheep wants to maximize the sum of fortune among all its $$$k$$$ written integers. Can you help them? Input The first line contains a single integer $$$k$$$ ($$$1 leq k leq 999999$$$): the number of numbers each sheep has to write. The next line contains six integers $$$F_0$$$, $$$F_1$$$, $$$F_2$$$, $$$F_3$$$, $$$F_4$$$, $$$F_5$$$ ($$$1 leq F_i leq 10^9$$$): the fortune assigned to each digit. The next line contains a single integer $$$q$$$ ($$$q = 1$$$): the number of sheep. Each of the next $$$q$$$ lines contains a single integer $$$n_i$$$ ($$$1 leq n_i leq 999999$$$): the sum of numbers that $$$i$$$-th sheep has to write. In this version, there is only one line. Output Print $$$q$$$ lines, where the $$$i$$$-th line contains the maximum sum of fortune of all numbers of the $$$i$$$-th sheep. In this version, you should print only one line. Note In the first test case, $$$57 = 9 + 9 + 39$$$. The three $$$9$$$'s contribute $$$1 cdot 3$$$ and $$$3$$$ at the tens position contributes $$$2 cdot 1$$$. Hence the sum of fortune is $$$11$$$. In the second test case, $$$63 = 35 + 19 + 9$$$. The sum of fortune is $$$8$$$. | 2,900 | false | true | false | true | false | false | false | false | false | false | 3,519 |
581D | Three companies decided to order a billboard with pictures of their logos. A billboard is a big square board. A logo of each company is a rectangle of a non-zero area. Advertisers will put up the ad only if it is possible to place all three logos on the billboard so that they do not overlap and the billboard has no empty space left. When you put a logo on the billboard, you should rotate it so that the sides were parallel to the sides of the billboard. Your task is to determine if it is possible to put the logos of all the three companies on some square billboard without breaking any of the described rules. Input The first line of the input contains six positive integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2,u2009_x_3,u2009_y_3 (1u2009≤u2009_x_1,u2009_y_1,u2009_x_2,u2009_y_2,u2009_x_3,u2009_y_3u2009≤u2009100), where _x__i_ and _y__i_ determine the length and width of the logo of the _i_-th company respectively. Output If it is impossible to place all the three logos on a square shield, print a single integer "-1" (without the quotes). If it is possible, print in the first line the length of a side of square _n_, where you can place all the three logos. Each of the next _n_ lines should contain _n_ uppercase English letters "A", "B" or "C". The sets of the same letters should form solid rectangles, provided that: the sizes of the rectangle composed from letters "A" should be equal to the sizes of the logo of the first company, the sizes of the rectangle composed from letters "B" should be equal to the sizes of the logo of the second company, the sizes of the rectangle composed from letters "C" should be equal to the sizes of the logo of the third company, Note that the logos of the companies can be rotated for printing on the billboard. The billboard mustn't have any empty space. If a square billboard can be filled with the logos in multiple ways, you are allowed to print any of them. See the samples to better understand the statement. Examples Output 5 AAAAA BBBBB BBBBB CCCCC CCCCC Output 6 BBBBBB BBBBBB AAAACC AAAACC AAAACC AAAACC | 1,700 | true | false | true | false | false | true | true | false | false | false | 7,520 |
490F | The "Road Accident" band is planning an unprecedented tour around Treeland. The RA fans are looking forward to the event and making bets on how many concerts their favorite group will have. Treeland consists of _n_ cities, some pairs of cities are connected by bidirectional roads. Overall the country has _n_u2009-u20091 roads. We know that it is possible to get to any city from any other one. The cities are numbered by integers from 1 to _n_. For every city we know its value _r__i_ — the number of people in it. We know that the band will travel along some path, having concerts in some cities along the path. The band's path will not pass one city twice, each time they move to the city that hasn't been previously visited. Thus, the musicians will travel along some path (without visiting any city twice) and in some (not necessarily all) cities along the way they will have concerts. The band plans to gather all the big stadiums and concert halls during the tour, so every time they will perform in a city which population is larger than the population of the previously visited with concert city. In other words, the sequence of population in the cities where the concerts will be held is strictly increasing. In a recent interview with the leader of the "road accident" band promised to the fans that the band will give concert in the largest possible number of cities! Thus the band will travel along some chain of cities of Treeland and have concerts in some of these cities, so that the population number will increase, and the number of concerts will be the largest possible. The fans of Treeland are frantically trying to figure out how many concerts the group will have in Treeland. Looks like they can't manage without some help from a real programmer! Help the fans find the sought number of concerts. Input The first line of the input contains integer _n_ (2u2009≤u2009_n_u2009≤u20096000) — the number of cities in Treeland. The next line contains _n_ integers _r_1,u2009_r_2,u2009...,u2009_r__n_ (1u2009≤u2009_r__i_u2009≤u2009106), where _r__i_ is the population of the _i_-th city. The next _n_u2009-u20091 lines contain the descriptions of the roads, one road per line. Each road is defined by a pair of integers _a__j_, _b__j_ (1u2009≤u2009_a__j_,u2009_b__j_u2009≤u2009_n_) — the pair of the numbers of the cities that are connected by the _j_-th road. All numbers in the lines are separated by spaces. | 2,200 | false | false | false | true | true | false | false | false | false | false | 7,872 |
1988F | Problem - 1988F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp fft math *3000 No tag edit access → Contest materials ") ") such that $$$u_i>u_{i-1}$$$. You are given three cost arrays: $$$ | 3,000 | true | false | false | true | false | false | false | false | false | false | 342 |
839E | There are _n_ castles in the Lannister's Kingdom and some walls connect two castles, no two castles are connected by more than one wall, no wall connects a castle to itself. Sir Jaime Lannister has discovered that Daenerys Targaryen is going to attack his kingdom soon. Therefore he wants to defend his kingdom. He has _k_ liters of a strange liquid. He wants to distribute that liquid among the castles, so each castle may contain some liquid (possibly zero or non-integer number of liters). After that the stability of a wall is defined as follows: if the wall connects two castles _a_ and _b_, and they contain _x_ and _y_ liters of that liquid, respectively, then the strength of that wall is _x_·_y_. Your task is to print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve. Input The first line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u200940, 1u2009≤u2009_k_u2009≤u20091000). Then _n_ lines follows. The _i_-th of these lines contains _n_ integers _a__i_,u20091,u2009_a__i_,u20092,u2009...,u2009_a__i_,u2009_n_ (). If castles _i_ and _j_ are connected by a wall, then _a__i_,u2009_j_u2009=u20091. Otherwise it is equal to 0. It is guaranteed that _a__i_,u2009_j_u2009=u2009_a__j_,u2009_i_ and _a__i_,u2009_i_u2009=u20090 for all 1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_. Output Print the maximum possible sum of stabilities of the walls that Sir Jaime Lannister can achieve. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Examples Input 4 4 0 1 0 1 1 0 1 0 0 1 0 1 1 0 1 0 Note In the first sample, we can assign 0.5,u20090.5,u20090 liters of liquid to castles 1,u20092,u20093, respectively, to get the maximum sum (0.25). In the second sample, we can assign 1.0,u20091.0,u20091.0,u20091.0 liters of liquid to castles 1,u20092,u20093,u20094, respectively, to get the maximum sum (4.0) | 2,700 | true | false | false | false | false | false | true | false | false | true | 6,404 |
1603A | YouKn0wWho has an integer sequence $$$a_1, a_2, ldots, a_n$$$. He will perform the following operation until the sequence becomes empty: select an index $$$i$$$ such that $$$1 le i le a$$$ and $$$a_i$$$ is not divisible by $$$(i + 1)$$$, and erase this element from the sequence. Here $$$a$$$ is the length of sequence $$$a$$$ at the moment of operation. Note that the sequence $$$a$$$ changes and the next operation is performed on this changed sequence. For example, if $$$a=[3,5,4,5]$$$, then he can select $$$i = 2$$$, because $$$a_2 = 5$$$ is not divisible by $$$i+1 = 3$$$. After this operation the sequence is $$$[3,4,5]$$$. Help YouKn0wWho determine if it is possible to erase the whole sequence using the aforementioned operation. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$) xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print "YES" (without quotes) if it is possible to erase the whole sequence using the aforementioned operation, print "NO" (without quotes) otherwise. You can print each letter in any register (upper or lower). Example Input 5 3 1 2 3 1 2 2 7 7 10 384836991 191890310 576823355 782177068 404011431 818008580 954291757 160449218 155374934 840594328 8 6 69 696 69696 696969 6969696 69696969 696969696 Note In the first test case, YouKn0wWho can perform the following operations (the erased elements are underlined): $$$[1, underline{2}, 3] ightarrow [underline{1}, 3] ightarrow [underline{3}] ightarrow [,].$$$ In the second test case, it is impossible to erase the sequence as $$$i$$$ can only be $$$1$$$, and when $$$i=1$$$, $$$a_1 = 2$$$ is divisible by $$$i + 1 = 2$$$. | 1,300 | true | false | false | false | false | true | false | false | false | false | 2,643 |
61C | It's now 260 AD. Shapur, being extremely smart, became the King of Persia. He is now called Shapur, His majesty King of kings of Iran and Aniran. Recently the Romans declared war on Persia. They dreamed to occupy Armenia. In the recent war, the Romans were badly defeated. Now their senior army general, Philip is captured by Shapur and Shapur is now going to capture Valerian, the Roman emperor. Being defeated, the cowardly Valerian hid in a room at the top of one of his castles. To capture him, Shapur has to open many doors. Fortunately Valerian was too scared to make impenetrable locks for the doors. Each door has 4 parts. The first part is an integer number _a_. The second part is either an integer number _b_ or some really odd sign which looks like R. The third one is an integer _c_ and the fourth part is empty! As if it was laid for writing something. Being extremely gifted, after opening the first few doors, Shapur found out the secret behind the locks. _c_ is an integer written in base _a_, to open the door we should write it in base _b_. The only bad news is that this R is some sort of special numbering system that is used only in Roman empire, so opening the doors is not just a piece of cake! Here's an explanation of this really weird number system that even doesn't have zero: Roman numerals are based on seven symbols: a stroke (identified with the letter I) for a unit, a chevron (identified with the letter V) for a five, a cross-stroke (identified with the letter X) for a ten, a C (identified as an abbreviation of Centum) for a hundred, etc.: I=1 V=5 X=10 L=50 C=100 D=500 M=1000 Symbols are iterated to produce multiples of the decimal (1, 10, 100, 1,u2009000) values, with V, L, D substituted for a multiple of five, and the iteration continuing: I 1, II 2, III 3, V 5, VI 6, VII 7, etc., and the same for other bases: X 10, XX 20, XXX 30, L 50, LXXX 80; CC 200, DCC 700, etc. At the fourth and ninth iteration, a subtractive principle must be employed, with the base placed before the higher base: IV 4, IX 9, XL 40, XC 90, CD 400, CM 900. Also in bases greater than 10 we use A for 10, B for 11, etc. Help Shapur capture Valerian and bring peace back to Persia, especially Armenia. | 2,000 | true | false | false | false | false | false | false | false | false | false | 9,668 |
785D | As you probably know, Anton goes to school. One of the school subjects that Anton studies is Bracketology. On the Bracketology lessons students usually learn different sequences that consist of round brackets (characters "(" and ")" (without quotes)). On the last lesson Anton learned about the regular simple bracket sequences (RSBS). A bracket sequence _s_ of length _n_ is an RSBS if the following conditions are met: For example, the sequence "((()))" is an RSBS but the sequences "((())" and "(()())" are not RSBS. Elena Ivanovna, Anton's teacher, gave him the following task as a homework. Given a bracket sequence _s_. Find the number of its distinct subsequences such that they are RSBS. Note that a subsequence of _s_ is a string that can be obtained from _s_ by deleting some of its elements. Two subsequences are considered distinct if distinct sets of positions are deleted. Because the answer can be very big and Anton's teacher doesn't like big numbers, she asks Anton to find the answer modulo 109u2009+u20097. Anton thought of this task for a very long time, but he still doesn't know how to solve it. Help Anton to solve this task and write a program that finds the answer for it! Input The only line of the input contains a string _s_xa0— the bracket sequence given in Anton's homework. The string consists only of characters "(" and ")" (without quotes). It's guaranteed that the string is not empty and its length doesn't exceed 200u2009000. Output Output one numberxa0— the answer for the task modulo 109u2009+u20097. Note In the first sample the following subsequences are possible: If we delete characters at the positions 1 and 5 (numbering starts with one), we will get the subsequence "(())". If we delete characters at the positions 1, 2, 3 and 4, we will get the subsequence "()". If we delete characters at the positions 1, 2, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 2, 5 and 6, we will get the subsequence "()". If we delete characters at the positions 1, 3, 4 and 5, we will get the subsequence "()". If we delete characters at the positions 1, 3, 5 and 6, we will get the subsequence "()". The rest of the subsequnces are not RSBS. So we got 6 distinct subsequences that are RSBS, so the answer is 6. | 2,300 | true | false | false | true | false | false | false | false | false | false | 6,630 |
820B | On one quiet day all of sudden Mister B decided to draw angle _a_ on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex _n_-gon (regular convex polygon with _n_ sides). That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices _v_1, _v_2, _v_3 such that the angle (where _v_2 is the vertex of the angle, and _v_1 and _v_3 lie on its sides) is as close as possible to _a_. In other words, the value should be minimum possible. If there are many optimal solutions, Mister B should be satisfied with any of them. Input First and only line contains two space-separated integers _n_ and _a_ (3u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_a_u2009≤u2009180)xa0— the number of vertices in the polygon and the needed angle, in degrees. Output Print three space-separated integers: the vertices _v_1, _v_2, _v_3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to _n_ in clockwise order. Note In first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct. Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since 45u2009-u200967u2009<u200990u2009-u200967. Other correct answers are: "3 1 2", "3 2 4", "4 2 3", "4 3 1", "1 3 4", "1 4 2", "2 4 1", "4 1 3", "3 1 4", "3 4 2", "2 4 3", "2 3 1", "1 3 2", "1 2 4", "4 2 1". In third sample test, on the contrary, the angle of 90 degrees was chosen, since 90u2009-u200968u2009<u200945u2009-u200968. Other correct answers are: "2 1 4", "3 2 1", "1 2 3", "4 3 2", "2 3 4", "1 4 3", "3 4 1". | 1,300 | true | false | false | false | false | true | false | false | false | false | 6,474 |
66D | Little Petya has a birthday soon. Due this wonderful event, Petya's friends decided to give him sweets. The total number of Petya's friends equals to _n_. Let us remind you the definition of the greatest common divisor: _GCD_(_a_1,u2009...,u2009_a__k_)u2009=u2009_d_, where _d_ represents such a maximal positive number that each _a__i_ (1u2009≤u2009_i_u2009≤u2009_k_) is evenly divisible by _d_. At that, we assume that all _a__i_'s are greater than zero. Knowing that Petya is keen on programming, his friends has agreed beforehand that the 1-st friend gives _a_1 sweets, the 2-nd one gives _a_2 sweets, ..., the _n_-th one gives _a__n_ sweets. At the same time, for any _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_) they want the _GCD_(_a__i_,u2009_a__j_) not to be equal to 1. However, they also want the following condition to be satisfied: _GCD_(_a_1,u2009_a_2,u2009...,u2009_a__n_)u2009=u20091. One more: all the _a__i_ should be distinct. Help the friends to choose the suitable numbers _a_1,u2009...,u2009_a__n_. Output If there is no answer, print "-1" without quotes. Otherwise print a set of _n_ distinct positive numbers _a_1,u2009_a_2,u2009...,u2009_a__n_. Each line must contain one number. Each number must consist of not more than 100 digits, and must not contain any leading zeros. If there are several solutions to that problem, print any of them. Do not forget, please, that all of the following conditions must be true: For every _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_): _GCD_(_a__i_,u2009_a__j_)u2009≠u20091 _GCD_(_a_1,u2009_a_2,u2009...,u2009_a__n_)u2009=u20091 For every _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_,u2009_i_u2009≠u2009_j_): _a__i_u2009≠u2009_a__j_ Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). | 1,700 | true | false | false | false | false | true | false | false | false | false | 9,638 |
1190E | Tokitsukaze and her friends are trying to infiltrate a secret base built by Claris. However, Claris has been aware of that and set a bomb which is going to explode in a minute. Although they try to escape, they have no place to go after they find that the door has been locked. At this very moment, CJB, Father of Tokitsukaze comes. With his magical power given by Ereshkigal, the goddess of the underworld, CJB is able to set $$$m$$$ barriers to protect them from the explosion. Formally, let's build a Cartesian coordinate system on the plane and assume the bomb is at $$$O(0, 0)$$$. There are $$$n$$$ persons in Tokitsukaze's crew, the $$$i$$$-th one of whom is at $$$P_i(X_i, Y_i)$$$. Every barrier can be considered as a line with infinity length and they can intersect each other. For every person from Tokitsukaze's crew, there must be at least one barrier separating the bomb and him, which means the line between the bomb and him intersects with at least one barrier. In this definition, if there exists a person standing at the position of the bomb, any line through $$$O(0, 0)$$$ will satisfy the requirement. Although CJB is very powerful, he still wants his barriers to be as far from the bomb as possible, in order to conserve his energy. Please help him calculate the maximum distance between the bomb and the closest barrier while all of Tokitsukaze's crew are safe. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n, m leq 10^5$$$), indicating the number of people and the number of barriers respectively. The $$$i$$$-th line of the next $$$n$$$ lines contains two integers $$$X_i$$$, $$$Y_i$$$ ($$$-10^5 leq X_i, Y_i leq 10^5$$$), indicating the $$$i$$$-th person's location $$$P_i(X_i, Y_i)$$$. Note that $$$P_i$$$ may have the same coordinates as $$$P_j$$$ ($$$j eq i$$$) or even $$$O$$$. Output Print a single real numberxa0— the maximum distance meeting the requirement. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$frac{a - b}{max(1, b)} leq 10^{-6}$$$. Examples Input 3 100000 3 2 -1 -3 2 -5 Note In the first two examples, CJB must set the barrier crossing $$$O(0, 0)$$$. In the last two examples, CJB can set each barrier crossing some $$$P_i$$$ such that the barrier is perpendicular to the line between $$$P_i$$$ and $$$O$$$. | 3,100 | false | true | false | false | false | false | false | true | false | false | 4,746 |
1902A | You are given a string $$$s$$$, consisting only of characters '0' and/or '1'. In one operation, you choose a position $$$i$$$ from $$$1$$$ to $$$s - 1$$$, where $$$s$$$ is the current length of string $$$s$$$. Then you insert a character between the $$$i$$$-th and the $$$(i+1)$$$-st characters of $$$s$$$. If $$$s_i = s_{i+1}$$$, you insert '1'. If $$$s_i eq s_{i+1}$$$, you insert '0'. Is it possible to make the number of zeroes in the string strictly greater than the number of ones, using any number of operations (possibly, none)? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of testcases. The first line of each testcase contains an integer $$$n$$$ ($$$1 le n le 100$$$). The second line contains a string $$$s$$$ of length exactly $$$n$$$, consisting only of characters '0' and/or '1'. Output For each testcase, print "YES" if it's possible to make the number of zeroes in $$$s$$$ strictly greater than the number of ones, using any number of operations (possibly, none). Otherwise, print "NO". Note In the first testcase, the number of zeroes is already greater than the number of ones. In the second testcase, it's impossible to insert any zeroes in the string. In the third testcase, you can choose $$$i = 1$$$ to insert a zero between the $$$1$$$-st and the $$$2$$$-nd characters. Since $$$s_1 eq s_2$$$, you insert a '0'. The resulting string is "100". It has two zeroes and only a single one, so the answer is "YES". | 800 | false | false | false | false | false | true | false | false | false | false | 903 |
1479B1 | The only difference between the two versions is that this version asks the maximal possible answer. Homer likes arrays a lot. Today he is painting an array $$$a_1, a_2, dots, a_n$$$ with two kinds of colors, white and black. A painting assignment for $$$a_1, a_2, dots, a_n$$$ is described by an array $$$b_1, b_2, dots, b_n$$$ that $$$b_i$$$ indicates the color of $$$a_i$$$ ($$$0$$$ for white and $$$1$$$ for black). According to a painting assignment $$$b_1, b_2, dots, b_n$$$, the array $$$a$$$ is split into two new arrays $$$a^{(0)}$$$ and $$$a^{(1)}$$$, where $$$a^{(0)}$$$ is the sub-sequence of all white elements in $$$a$$$ and $$$a^{(1)}$$$ is the sub-sequence of all black elements in $$$a$$$. For example, if $$$a = [1,2,3,4,5,6]$$$ and $$$b = [0,1,0,1,0,0]$$$, then $$$a^{(0)} = [1,3,5,6]$$$ and $$$a^{(1)} = [2,4]$$$. The number of segments in an array $$$c_1, c_2, dots, c_k$$$, denoted $$$mathit{seg}(c)$$$, is the number of elements if we merge all adjacent elements with the same value in $$$c$$$. For example, the number of segments in $$$[1,1,2,2,3,3,3,2]$$$ is $$$4$$$, because the array will become $$$[1,2,3,2]$$$ after merging adjacent elements with the same value. Especially, the number of segments in an empty array is $$$0$$$. Homer wants to find a painting assignment $$$b$$$, according to which the number of segments in both $$$a^{(0)}$$$ and $$$a^{(1)}$$$, i.e. $$$mathit{seg}(a^{(0)})+mathit{seg}(a^{(1)})$$$, is as large as possible. Find this number. Note In the first example, we can choose $$$a^{(0)} = [1,2,3,3]$$$, $$$a^{(1)} = [1,2,3]$$$ and $$$mathit{seg}(a^{(0)}) = mathit{seg}(a^{(1)}) = 3$$$. So the answer is $$$3+3 = 6$$$. In the second example, we can choose $$$a^{(0)} = [1,2,3,4,5,6,7]$$$ and $$$a^{(1)}$$$ is empty. We can see that $$$mathit{seg}(a^{(0)}) = 7$$$ and $$$mathit{seg}(a^{(1)}) = 0$$$. So the answer is $$$7+0 = 7$$$. | 1,900 | false | true | true | true | true | true | false | false | false | false | 3,280 |
405E | Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest. Chris is given a simple undirected connected graph with _n_ vertices (numbered from 1 to _n_) and _m_ edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partition all edges of the graph into pairs in such a way that the edges in a single pair are adjacent and each edge must be contained in exactly one pair. For example, the figure shows a way Chris can cut a graph. The first sample test contains the description of this graph. You are given a chance to compete with Chris. Find a way to cut the given graph or determine that it is impossible! Input The first line of input contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105), the number of vertices and the number of edges in the graph. The next _m_ lines contain the description of the graph's edges. The _i_-th line contains two space-separated integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_; _a__i_u2009≠u2009_b__i_), the numbers of the vertices connected by the _i_-th edge. It is guaranteed that the given graph is simple (without self-loops and multi-edges) and connected. Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++. Output If it is possible to cut the given graph into edge-distinct paths of length 2, output lines. In the _i_-th line print three space-separated integers _x__i_, _y__i_ and _z__i_, the description of the _i_-th path. The graph should contain this path, i.e., the graph should contain edges (_x__i_,u2009_y__i_) and (_y__i_,u2009_z__i_). Each edge should appear in exactly one path of length 2. If there are multiple solutions, output any of them. If it is impossible to cut the given graph, print "No solution" (without quotes). Examples Input 8 12 1 2 2 3 3 4 4 1 1 3 2 4 3 5 3 6 5 6 6 7 6 8 7 8 Output 1 2 4 1 3 2 1 4 3 5 3 6 5 6 8 6 7 8 | 2,300 | false | false | false | false | false | false | false | false | false | true | 8,201 |
1398F | Alice and Bob play a game. The game consists of several sets, and each set consists of several rounds. Each round is won either by Alice or by Bob, and the set ends when one of the players has won $$$x$$$ rounds in a row. For example, if Bob won five rounds in a row and $$$x = 2$$$, then two sets ends. You know that Alice and Bob have already played $$$n$$$ rounds, and you know the results of some rounds. For each $$$x$$$ from $$$1$$$ to $$$n$$$, calculate the maximum possible number of sets that could have already finished if each set lasts until one of the players wins $$$x$$$ rounds in a row. It is possible that the last set is still not finished — in that case, you should not count it in the answer. Input The first line contains one integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0— the number of rounds. The second line contains one string $$$s$$$ of length $$$n$$$ — the descriptions of rounds. If the $$$i$$$-th element of the string is 0, then Alice won the $$$i$$$-th round; if it is 1, then Bob won the $$$i$$$-th round, and if it is ?, then you don't know who won the $$$i$$$-th round. Output In the only line print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the maximum possible number of sets that could have already finished if each set lasts until one of the players wins $$$i$$$ rounds in a row. Examples Output 12 6 4 3 2 2 1 1 1 1 1 1 Note Let's consider the first test case: if $$$x = 1$$$ and $$$s = 110000$$$ or $$$s = 111000$$$ then there are six finished sets; if $$$x = 2$$$ and $$$s = 110000$$$ then there are three finished sets; if $$$x = 3$$$ and $$$s = 111000$$$ then there are two finished sets; if $$$x = 4$$$ and $$$s = 110000$$$ then there is one finished set; if $$$x = 5$$$ then there are no finished sets; if $$$x = 6$$$ then there are no finished sets. | 2,500 | false | true | false | true | true | false | false | true | false | false | 3,681 |
989A | When the curtains are opened, a canvas unfolds outside. Kanno marvels at all the blonde colours along the riversidexa0— not tangerines, but blossoms instead."What a pity it's already late spring," sighs Mino with regret, "one more drizzling night and they'd be gone." "But these blends are at their best, aren't they?" Absorbed in the landscape, Kanno remains optimistic. The landscape can be expressed as a row of consecutive cells, each of which either contains a flower of colour amber or buff or canary yellow, or is empty. When a flower withers, it disappears from the cell that it originally belonged to, and it spreads petals of its colour in its two neighbouring cells (or outside the field if the cell is on the side of the landscape). In case petals fall outside the given cells, they simply become invisible. You are to help Kanno determine whether it's possible that after some (possibly none or all) flowers shed their petals, at least one of the cells contains all three colours, considering both petals and flowers. Note that flowers can wither in arbitrary order. Input The first and only line of input contains a non-empty string $$$s$$$ consisting of uppercase English letters 'A', 'B', 'C' and characters '.' (dots) only ($$$lvert s vert leq 100$$$)xa0— denoting cells containing an amber flower, a buff one, a canary yellow one, and no flowers, respectively. Output Output "Yes" if it's possible that all three colours appear in some cell, and "No" otherwise. You can print each letter in any case (upper or lower). Note In the first example, the buff and canary yellow flowers can leave their petals in the central cell, blending all three colours in it. In the second example, it's impossible to satisfy the requirement because there is no way that amber and buff meet in any cell. | 900 | false | false | true | false | false | false | false | false | false | false | 5,757 |
1726G | There are $$$n$$$ people at a party. The $$$i$$$-th person has an amount of happiness $$$a_i$$$. Every person has a certain kind of personality which can be represented as a binary integer $$$b$$$. If $$$b = 0$$$, it means the happiness of the person will increase if he tells the story to someone strictly less happy than them. If $$$b = 1$$$, it means the happiness of the person will increase if he tells the story to someone strictly more happy than them. Let us define a speaking order as an ordering of the people from left to right. Now the following process occurs. We go from left to right. The current person tells the story to all people other than himself. Note that all happiness values stay constant while this happens. After the person is done, he counts the number of people who currently have strictly less/more happiness than him as per his kind of personality, and his happiness increases by that value. Note that only the current person's happiness value increases. As the organizer of the party, you don't want anyone to leave sad. Therefore, you want to count the number of speaking orders such that at the end of the process all $$$n$$$ people have equal happiness. Two speaking orders are considered different if there exists at least one person who does not have the same position in the two speaking orders. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2 cdot 10^5$$$)xa0— the number of people. The second line contains a sequence of $$$n$$$ integers $$$a_1,a_2,...,a_n$$$ ($$$1 leq a_i leq 2n$$$) xa0— the happiness values. The third line contains a sequence of $$$n$$$ binary numbers $$$b_1,b_2,...,b_n$$$ ($$$b_i in {0,1}$$$) xa0— the kinds of personality. Note Here is the explanation for the first example. One valid speaking order is $$$[2,1,4,3]$$$ (here, we have written the indices of each person). Each step shows the current happiness values and results. Step $$$1$$$: $$$[1,2,4,4]$$$ $$$ ightarrow$$$ Person $$$2$$$ tells the story to others. Since his kind of personality is $$$1$$$, his happiness increases by $$$2$$$ since persons $$$3$$$ and $$$4$$$ have strictly greater happiness. Step $$$2$$$: $$$[1,4,4,4]$$$ $$$ ightarrow$$$ Person $$$1$$$ tells the story to others. Since his kind of personality is $$$1$$$, his happiness increases by $$$3$$$ since persons $$$2$$$, $$$3$$$ and $$$4$$$ have strictly greater happiness. Step $$$3$$$: $$$[4,4,4,4]$$$ $$$ ightarrow$$$ Person $$$4$$$ tells the story to others. Since his kind of personality is $$$0$$$, his happiness increases by $$$0$$$ since no one has strictly lesser happiness. Step $$$4$$$: $$$[4,4,4,4]$$$ $$$ ightarrow$$$ Person $$$3$$$ tells the story to others. Since his kind of personality is $$$0$$$, his happiness increases by $$$0$$$ since no one has strictly lesser happiness. At the end, everyone has equal happiness. Note that $$$[2,1,3,4]$$$ is also a valid answer for this example. It can be shown that there is no valid ordering for the second example. | 3,300 | false | true | false | false | true | false | false | false | true | false | 1,927 |
363D | A group of _n_ schoolboys decided to ride bikes. As nobody of them has a bike, the boys need to rent them. The renting site offered them _m_ bikes. The renting price is different for different bikes, renting the _j_-th bike costs _p__j_ rubles. In total, the boys' shared budget is _a_ rubles. Besides, each of them has his own personal money, the _i_-th boy has _b__i_ personal rubles. The shared budget can be spent on any schoolchildren arbitrarily, but each boy's personal money can be spent on renting only this boy's bike. Each boy can rent at most one bike, one cannot give his bike to somebody else. What maximum number of schoolboys will be able to ride bikes? What minimum sum of personal money will they have to spend in total to let as many schoolchildren ride bikes as possible? Input The first line of the input contains three integers _n_, _m_ and _a_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105; 0u2009≤u2009_a_u2009≤u2009109). The second line contains the sequence of integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u2009104), where _b__i_ is the amount of the _i_-th boy's personal money. The third line contains the sequence of integers _p_1,u2009_p_2,u2009...,u2009_p__m_ (1u2009≤u2009_p__j_u2009≤u2009109), where _p__j_ is the price for renting the _j_-th bike. Output Print two integers _r_ and _s_, where _r_ is the maximum number of schoolboys that can rent a bike and _s_ is the minimum total personal money needed to rent _r_ bikes. If the schoolchildren cannot rent any bikes, then _r_u2009=u2009_s_u2009=u20090. Examples Input 4 5 2 8 1 1 2 6 3 7 5 2 Note In the first sample both schoolchildren can rent a bike. For instance, they can split the shared budget in half (5 rubles each). In this case one of them will have to pay 1 ruble from the personal money and the other one will have to pay 2 rubles from the personal money. In total, they spend 3 rubles of their personal money. This way of distribution of money minimizes the amount of spent personal money. | 1,800 | false | true | false | false | false | false | false | true | false | false | 8,379 |
2035D | Three r there are's in strawberry. You are given an array $$$b$$$ of length $$$m$$$. You can perform the following operation any number of times (possibly zero): Choose two distinct indices $$$i$$$ and $$$j$$$ where $$$bf{1le i < jle m}$$$ and $$$b_i$$$ is even, divide $$$b_i$$$ by $$$2$$$ and multiply $$$b_j$$$ by $$$2$$$. Your task is to maximize the sum of the array after performing any number of such operations. Since it could be large, output this sum modulo $$$10^9+7$$$. Since this problem is too easy, you are given an array $$$a$$$ of length $$$n$$$ and need to solve the problem for each prefix of $$$a$$$. In other words, denoting the maximum sum of $$$b$$$ after performing any number of such operations as $$$f(b)$$$, you need to output $$$f([a_1])$$$, $$$f([a_1,a_2])$$$, $$$ldots$$$, $$$f([a_1,a_2,ldots,a_n])$$$ modulo $$$10^9+7$$$ respectively. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the starting values of array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$n$$$ integers representing the answer for each prefix of $$$a$$$ modulo $$$10^9+7$$$. Example Input 3 10 1 2 3 4 5 6 7 8 9 10 11 1 6 9 4 7 4 4 10 3 2 3 4 527792568 502211460 850237282 374773208 Output 1 3 8 13 46 59 126 149 1174 1311 1 7 22 26 70 74 150 1303 1306 1308 1568 527792568 83665723 399119771 773892979 Note For each prefix in the first example, a possible array after operations is: $$$[1]$$$ and the sum is $$$1$$$; $$$[1, 2]$$$ and the sum is $$$3$$$; $$$[1, 1, 6]$$$ and the sum is $$$8$$$; $$$[1, 1, 3, 8]$$$ and the sum is $$$13$$$; $$$[1, 1, 3, 1, 40]$$$ and the sum is $$$46$$$; $$$[1, 1, 3, 1, 5, 48]$$$ and the sum is $$$59$$$; $$$[1, 1, 3, 1, 5, 3, 112]$$$ and the sum is $$$126$$$; $$$[1, 1, 3, 1, 5, 3, 7, 128]$$$ and the sum is $$$149$$$; $$$[1, 1, 3, 1, 5, 3, 7, 1, 1152]$$$ and the sum is $$$1174$$$; $$$[1, 1, 3, 1, 5, 3, 7, 1, 9, 1280]$$$ and the sum is $$$1311$$$. | 1,800 | true | true | true | false | true | false | false | true | false | false | 58 |
756F | Consider the following grammar: <expression> ::= <term> <expression> '+' <term> <term> ::= <number> <number> '-' <number> <number> '(' <expression> ')' <number> ::= <pos_digit> <number> <digit> <digit> ::= '0' <pos_digit> <pos_digit> ::= '1' '2' '3' '4' '5' '6' '7' '8' '9' This grammar describes a number in decimal system using the following rules: <number> describes itself, <number>-<number> (l-r, _l_u2009≤u2009_r_) describes integer which is concatenation of all integers from _l_ to _r_, written without leading zeros. For example, 8-11 describes 891011, <number>(<expression>) describes integer which is concatenation of <number> copies of integer described by <expression>, <expression>+<term> describes integer which is concatenation of integers described by <expression> and <term>. For example, 2(2-4+1)+2(2(17)) describes the integer 2341234117171717. You are given an expression in the given grammar. Print the integer described by it modulo 109u2009+u20097. Input The only line contains a non-empty string at most 105 characters long which is valid according to the given grammar. In particular, it means that in terms l-r _l_u2009≤u2009_r_ holds. Output Print single integerxa0— the number described by the expression modulo 109u2009+u20097. | 3,400 | true | false | false | false | false | false | false | false | false | false | 6,765 |
1267G | Esports is a form of competitive sports using video games. Dotaxa02 is one of the most popular competitive video games in Esports. Recently, a new video game Dotaxa03 was released. In Dotaxa03 a player can buy some relics for their hero. Relics are counters that track hero's actions and statistics in a game. Gloria likes to play Dotaxa03, so she wants to buy all $$$n$$$ available relics for her favorite hero. Relics can be bought using an in-game currency called shards. Each relic has its own pricexa0— $$$c_i$$$ shards for the $$$i$$$-th relic. A player can buy a relic using one of the following options: Pay $$$c_i$$$ shards to buy the $$$i$$$-th relic; Pay $$$x$$$ shards and randomly get one of all $$$n$$$ relics. The probability of getting a relic is the same for all $$$n$$$ relics. If a duplicate relic is received, then the relic is recycled and $$$frac{x}{2}$$$ shards are given back to the player. Gloria wants to buy all $$$n$$$ relics. Help her minimize the expected number of shards she spends to buy all the relics. Input The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 100$$$; $$$1 le x le 10,000$$$)xa0— the number of relics and the cost to receive a random relic. The second line consists of $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$x le c_i le 10,000$$$; $$$sum{c_i} le 10,000$$$)xa0— the prices of $$$n$$$xa0relics. Output Print a single real numberxa0— the minimum expected number of shards that Gloria must spend to buy all the relics. The absolute or relative error should not exceed $$$10^{-9}$$$. Examples Output 47.50000000000000000 Output 171.25000000000000000 Note In the first example, the optimal strategy is to randomly get one of the two relics paying $$$20$$$ shards. Then there are two scenarios. The first one happens if Gloria receives the first relic. Then she keeps getting random relics until she obtains the second relic. The expected number of shards to spend in this scenario is $$$20 + 30 = 50$$$. In the second scenario, Gloria initially gets the second relic. Then it is better to buy the first relic for $$$25$$$ shards, so the expected number of shards to spend in this scenario is $$$20 + 25 = 45$$$. Thus, the expected number of shards to spend is $$$frac{50 + 45}{2} = 47.5$$$. | 3,000 | true | false | false | false | false | false | false | false | false | false | 4,357 |
1400C | Consider the following process. You have a binary string (a string where each character is either 0 or 1) $$$w$$$ of length $$$n$$$ and an integer $$$x$$$. You build a new binary string $$$s$$$ consisting of $$$n$$$ characters. The $$$i$$$-th character of $$$s$$$ is chosen as follows: if the character $$$w_{i-x}$$$ exists and is equal to 1, then $$$s_i$$$ is 1 (formally, if $$$i > x$$$ and $$$w_{i-x} = $$$ 1, then $$$s_i = $$$ 1); if the character $$$w_{i+x}$$$ exists and is equal to 1, then $$$s_i$$$ is 1 (formally, if $$$i + x le n$$$ and $$$w_{i+x} = $$$ 1, then $$$s_i = $$$ 1); if both of the aforementioned conditions are false, then $$$s_i$$$ is 0. You are given the integer $$$x$$$ and the resulting string $$$s$$$. Reconstruct the original string $$$w$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Each test case consists of two lines. The first line contains the resulting string $$$s$$$ ($$$2 le s le 10^5$$$, each character of $$$s$$$ is either 0 or 1). The second line contains one integer $$$x$$$ ($$$1 le x le s - 1$$$). The total length of all strings $$$s$$$ in the input does not exceed $$$10^5$$$. Output For each test case, print the answer on a separate line as follows: if no string $$$w$$$ can produce the string $$$s$$$ at the end of the process, print $$$-1$$$; otherwise, print the binary string $$$w$$$ consisting of $$$s$$$ characters. If there are multiple answers, print any of them. Example Input 3 101110 2 01 1 110 1 | 1,500 | false | true | false | false | false | true | true | false | false | false | 3,670 |
737F | After one of celebrations there is a stack of dirty plates in Nikita's kitchen. Nikita has to wash them and put into a dryer. In dryer, the plates should be also placed in a stack also, and the plates sizes should increase down up. The sizes of all plates are distinct. Nikita has no so much free space, specifically, he has a place for only one more stack of plates. Therefore, he can perform only such two operations: Take any number of plates from 1 to _a_ from the top of the dirty stack, wash them and put them to the intermediate stack. Take any number of plates from 1 to _b_ from the top of the intermediate stack and put them to the stack in the dryer. Note that after performing each of the operations, the plates are put in the same order as they were before the operation. You are given the sizes of the plates _s_1,u2009_s_2,u2009...,u2009_s__n_ in the down up order in the dirty stack, and integers _a_ and _b_. All the sizes are distinct. Write a program that determines whether or not Nikita can put the plates in increasing down up order in the dryer. If he is able to do so, the program should find some sequence of operations (not necessary optimal) to achieve it. Input The first line contains three integers _n_, _a_ and _b_ (1u2009≤u2009_n_u2009≤u20092000, 1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_). The second line contains integers _s_1,u2009_s_2,u2009...,u2009_s__n_ (1u2009≤u2009_s__i_u2009≤u2009_n_)xa0— the sizes of the plates in down up order. All the sizes are distinct. Output In the first line print "YES" if there is a solution. In this case, in the second line print integer _k_xa0— the number of operations. Then in _k_ lines print the operations, one per line. Each operation is described by two integers _t__j_ and _c__j_, where _t__j_u2009=u20091, if the operation is to wash the top _c__j_ places from the dirty stack and put them onto the intermediate stack, and _t__j_u2009=u20092, if the operation is to move th top _c__j_ plates from the intermediate stack to the dryer. In case there is no solution, print single line "NO". If there are multiple solutions, print any of them. Note that it is not necessary to minimize the number of operations. Examples Output YES 8 1 2 1 1 2 1 1 2 1 1 2 1 2 1 2 3 Output YES 14 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 Note In the first example the initial order of plates was 2,u20093,u20096,u20094,u20091,u20095. Here is how the stacks look like after each of the operations: [1xa02]: Dirty stack: 6,u20094,u20091,u20095. Intermediary stack: 2,u20093. The dryer is empty. [1xa01]: Dirty stack: 4,u20091,u20095. Intermediary stack: 6,u20092,u20093. The dryer is empty. [2xa01]: Dirty stack: 4,u20091,u20095. Intermediary stack: 2,u20093. Dryer stack: 6. [1xa02]: Dirty stack: 5. Intermediary stack: 4,u20091,u20092,u20093. Dryer stack: 6. [1xa01]: There are no dirty plates. Intermediary stack: 5,u20094,u20091,u20092,u20093. Dryer stack: 6. [2xa01]: There are no dirty plates. Intermediary stack: 4,u20091,u20092,u20093. Dryer stack: 5,u20096. [2xa01]: There are no dirty plates. Intermediary stack: 1,u20092,u20093. Dryer stack: 4,u20095,u20096. [2xa03]: All the plates are in the dryer: 1,u20092,u20093,u20094,u20095,u20096. In the second example it is possible to wash all the plates in one operation, and then move them all to the dryer. This is not possible in the third example, because it is not permitted to move more than one plate at the same time. It is possible to wash plates one by one so that they are placed onto the intermediary stack in the reverse order, and then move plates one by one to the dryer. The final order is correct. | 3,300 | true | false | false | false | false | true | false | false | false | false | 6,844 |
1713A | You are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down). More formally, if you are standing at the point $$$(x, y)$$$, you can: go left, and move to $$$(x - 1, y)$$$, or go right, and move to $$$(x + 1, y)$$$, or go up, and move to $$$(x, y + 1)$$$, or go down, and move to $$$(x, y - 1)$$$. There are $$$n$$$ boxes on this plane. The $$$i$$$-th box has coordinates $$$(x_i,y_i)$$$. It is guaranteed that the boxes are either on the $$$x$$$-axis or the $$$y$$$-axis. That is, either $$$x_i=0$$$ or $$$y_i=0$$$. You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point $$$(0,0)$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$) — the number of boxes. The $$$i$$$-th line of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-100 le x_i, y_i le 100$$$) — the coordinate of the $$$i$$$-th box. It is guaranteed that either $$$x_i=0$$$ or $$$y_i=0$$$. Do note that the sum of $$$n$$$ over all test cases is not bounded. Output For each test case output a single integer — the minimum number of moves required. Example Input 3 4 0 -2 1 0 -1 0 0 2 3 0 2 -3 0 0 -1 1 0 0 Note In the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below. $$$$$$(0,0) o (1,0) o (1,1) o (1, 2) o (0,2) o (-1,2) o (-1,1) o (-1,0) o (-1,-1) o (-1,-2) o (0,-2) o (0,-1) o (0,0)$$$$$$ In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below. $$$$$$(0,0) o (0,1) o (0,2) o (-1, 2) o (-2,2) o (-3,2) o (-3,1) o (-3,0) o (-3,-1) o (-2,-1) o (-1,-1) o (0,-1) o (0,0)$$$$$$ In the third test case, we can collect all boxes without making any moves. | 800 | false | true | true | false | false | false | false | false | false | false | 2,006 |
1783F | You are given two permutations $$$a$$$ and $$$b$$$, both of size $$$n$$$. A permutation of size $$$n$$$ is an array of $$$n$$$ elements, where each integer from $$$1$$$ to $$$n$$$ appears exactly once. The elements in each permutation are indexed from $$$1$$$ to $$$n$$$. You can perform the following operation any number of times: choose an integer $$$i$$$ from $$$1$$$ to $$$n$$$; let $$$x$$$ be the integer such that $$$a_x = i$$$. Swap $$$a_i$$$ with $$$a_x$$$; let $$$y$$$ be the integer such that $$$b_y = i$$$. Swap $$$b_i$$$ with $$$b_y$$$. Your goal is to make both permutations sorted in ascending order (i.u2009e. the conditions $$$a_1 < a_2 < dots < a_n$$$ and $$$b_1 < b_2 < dots < b_n$$$ must be satisfied) using minimum number of operations. Note that both permutations must be sorted after you perform the sequence of operations you have chosen. Input The first line contains one integer $$$n$$$ ($$$2 le n le 3000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$; all $$$a_i$$$ are distinct). The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le n$$$; all $$$b_i$$$ are distinct). Output First, print one integer $$$k$$$ ($$$0 le k le 2n$$$) — the minimum number of operations required to sort both permutations. Note that it can be shown that $$$2n$$$ operations are always enough. Then, print $$$k$$$ integers $$$op_1, op_2, dots, op_k$$$ ($$$1 le op_j le n$$$), where $$$op_j$$$ is the value of $$$i$$$ you choose during the $$$j$$$-th operation. If there are multiple answers, print any of them. Examples Input 5 1 3 2 4 5 2 1 3 4 5 | 2,500 | false | false | false | false | false | false | false | false | false | true | 1,574 |
1090D | Vasya had an array of $$$n$$$ integers, each element of the array was from $$$1$$$ to $$$n$$$. He chose $$$m$$$ pairs of different positions and wrote them down to a sheet of paper. Then Vasya compared the elements at these positions, and wrote down the results of the comparisons to another sheet of paper. For each pair he wrote either "greater", "less", or "equal". After several years, he has found the first sheet of paper, but he couldn't find the second one. Also he doesn't remember the array he had. In particular, he doesn't remember if the array had equal elements. He has told this sad story to his informatics teacher Dr Helen. She told him that it could be the case that even if Vasya finds his second sheet, he would still not be able to find out whether the array had two equal elements. Now Vasya wants to find two arrays of integers, each of length $$$n$$$. All elements of the first array must be distinct, and there must be two equal elements in the second array. For each pair of positions Vasya wrote at the first sheet of paper, the result of the comparison must be the same for the corresponding elements of the first array, and the corresponding elements of the second array. Help Vasya find two such arrays of length $$$n$$$, or find out that there are no such arrays for his sets of pairs. Input The first line of input contains two integers $$$n$$$, $$$m$$$xa0— the number of elements in the array and number of comparisons made by Vasya ($$$1 le n le 100,000$$$, $$$0 le m le 100,000$$$). Each of the following $$$m$$$ lines contains two integers $$$a_i$$$, $$$b_i$$$ xa0— the positions of the $$$i$$$-th comparison ($$$1 le a_i, b_i le n$$$; $$$a_i e b_i$$$). It's guaranteed that any unordered pair is given in the input at most once. Output The first line of output must contain "YES" if there exist two arrays, such that the results of comparisons would be the same, and all numbers in the first one are distinct, and the second one contains two equal numbers. Otherwise it must contain "NO". If the arrays exist, the second line must contain the array of distinct integers, the third line must contain the array, that contains at least one pair of equal elements. Elements of the arrays must be integers from $$$1$$$ to $$$n$$$. | 1,800 | false | false | false | false | false | true | false | false | false | false | 5,269 |
1085A | Polycarp loves ciphers. He has invented his own cipher called Right-Left. Right-Left cipher is used for strings. To encrypt the string $$$s=s_{1}s_{2} dots s_{n}$$$ Polycarp uses the following algorithm: he writes down $$$s_1$$$, he appends the current word with $$$s_2$$$ (i.e. writes down $$$s_2$$$ to the right of the current result), he prepends the current word with $$$s_3$$$ (i.e. writes down $$$s_3$$$ to the left of the current result), he appends the current word with $$$s_4$$$ (i.e. writes down $$$s_4$$$ to the right of the current result), he prepends the current word with $$$s_5$$$ (i.e. writes down $$$s_5$$$ to the left of the current result), and so on for each position until the end of $$$s$$$. For example, if $$$s$$$="techno" the process is: "t" $$$ o$$$ "te" $$$ o$$$ "cte" $$$ o$$$ "cteh" $$$ o$$$ "ncteh" $$$ o$$$ "ncteho". So the encrypted $$$s$$$="techno" is "ncteho". Given string $$$t$$$ — the result of encryption of some string $$$s$$$. Your task is to decrypt it, i.e. find the string $$$s$$$. Input The only line of the input contains $$$t$$$ — the result of encryption of some string $$$s$$$. It contains only lowercase Latin letters. The length of $$$t$$$ is between $$$1$$$ and $$$50$$$, inclusive. Output Print such string $$$s$$$ that after encryption it equals $$$t$$$. | 800 | false | false | true | false | false | false | false | false | false | false | 5,299 |
319A | Problem - 319A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics math *1600 No tag edit access → Contest materials such that member _a_ from MDC dances with member _b_ from NFC. The complexity of a pairs' assignment is the number of pairs of dancing pairs (_a_,u2009_b_) and (_c_,u2009_d_) such that _a_u2009<u2009_c_ and _b_u2009>u2009_d_. You are given a binary number of length _n_ named _x_. We know that member _i_ from MDC dances with member from NFC. Your task is to calculate the complexity of this assignment modulo 1000000007 (109u2009+u20097). Expression denotes applying «XOR» to numbers _x_ and _y_. This operation exists in all modern programming languages, for example, in C++ and Java it denotes as «^», in Pascal — «xor». Input The first line of input contains a binary number _x_ of lenght _n_, (1u2009≤u2009_n_u2009≤u2009100). This number may contain leading zeros. Output Print the complexity of the given dance assignent modulo 1000000007 (109u2009+u20097). Examples Input 11 Output 6 Input 01 Output 2 Input 1 Output 1 | 1,600 | true | false | false | false | false | false | false | false | false | false | 8,558 |
258A | Problem - 258A - 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 math *1100 No tag edit access → Contest materials . The Little Elephant wants the number he is going to write on the paper to be as large as possible. Help him find the maximum number that he can obtain after deleting exactly one binary digit and print it in the binary notation. Input The single line contains integer _a_, written in the binary notation without leading zeroes. This number contains more than 1 and at most 105 digits. Output In the single line print the number that is written without leading zeroes in the binary notation — the answer to the problem. Examples Input 101 Output 11 Input 110010 Output 11010 Note In the first sample the best strategy is to delete the second digit. That results in number 112u2009=u2009310. In the second sample the best strategy is to delete the third or fourth digits — that results in number 110102u2009=u20092610. | 1,100 | true | true | false | false | false | false | false | false | false | false | 8,802 |
1249C2 | The only difference between easy and hard versions is the maximum value of $$$n$$$. You are given a positive integer number $$$n$$$. You really love good numbers so you want to find the smallest good number greater than or equal to $$$n$$$. The positive integer is called good if it can be represented as a sum of distinct powers of $$$3$$$ (i.e. no duplicates of powers of $$$3$$$ are allowed). For example: $$$30$$$ is a good number: $$$30 = 3^3 + 3^1$$$, $$$1$$$ is a good number: $$$1 = 3^0$$$, $$$12$$$ is a good number: $$$12 = 3^2 + 3^1$$$, but $$$2$$$ is not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ ($$$2 = 3^0 + 3^0$$$), $$$19$$$ is not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ (for example, the representations $$$19 = 3^2 + 3^2 + 3^0 = 3^2 + 3^1 + 3^1 + 3^1 + 3^0$$$ are invalid), $$$20$$$ is also not a good number: you can't represent it as a sum of distinct powers of $$$3$$$ (for example, the representation $$$20 = 3^2 + 3^2 + 3^0 + 3^0$$$ is invalid). Note, that there exist other representations of $$$19$$$ and $$$20$$$ as sums of powers of $$$3$$$ but none of them consists of distinct powers of $$$3$$$. For the given positive integer $$$n$$$ find such smallest $$$m$$$ ($$$n le m$$$) that $$$m$$$ is a good number. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 500$$$) — the number of queries. Then $$$q$$$ queries follow. The only line of the query contains one integer $$$n$$$ ($$$1 le n le 10^{18}$$$). Output For each query, print such smallest integer $$$m$$$ (where $$$n le m$$$) that $$$m$$$ is a good number. Example Input 8 1 2 6 13 14 3620 10000 1000000000000000000 Output 1 3 9 13 27 6561 19683 1350851717672992089 | 1,500 | true | true | false | false | false | false | false | true | false | false | 4,460 |
535A | Problem - 535A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force implementation *1000 No tag edit access → Contest materials , Tavas's score. Output In the first and only line of output, print a single string consisting only from English lowercase letters and hyphens ('-'). Do not use spaces. Examples Input 6 Output six Input 99 Output ninety-nine Input 20 Output twenty Note You can find all you need to know about English numerals in | 1,000 | false | false | true | false | false | false | true | false | false | false | 7,695 |
1425B | It's our faculty's 34th anniversary! To celebrate this great event, the Faculty of Computer Science, University of Indonesia (Fasilkom), held CPC - Coloring Pavements Competition. The gist of CPC is two players color the predetermined routes of Fasilkom in Blue and Red. There are $$$N$$$ Checkpoints and $$$M$$$ undirected predetermined routes. Routes $$$i$$$ connects checkpoint $$$U_i$$$ and $$$V_i$$$, for $$$(1 le i le M)$$$. It is guaranteed that any pair of checkpoints are connected by using one or more routes. The rules of CPC is as follows: Two players play in each round. One player plays as blue, the other plays as red. For simplicity, let's call these players $$$Blue$$$ and $$$Red$$$. $$$Blue$$$ will color every route in he walks on blue, $$$Red$$$ will color the route he walks on red. Both players start at checkpoint number $$$1$$$. Initially, all routes are gray. Each phase, from their current checkpoint, $$$Blue$$$ and $$$Red$$$ select a different gray route and moves to the checkpoint on the other end of the route simultaneously. The game ends when $$$Blue$$$ or $$$Red$$$ can no longer move. That is, there is no two distinct gray routes they can choose to continue moving. Chaneka is interested in participating. However, she does not want to waste much energy. So, She is only interested in the number of final configurations of the routes after each round. Turns out, counting this is also exhausting, so Chaneka asks you to figure this out! Two final configurations are considered different if there is a route $$$U$$$ in a different color in the two configurations. Input The first line contains two integers $$$N$$$ and $$$M$$$. $$$N$$$ $$$(2 le N le 2 cdot 10^3)$$$ denotes the number of checkpoints, $$$M$$$ $$$(1 le M le 2 cdot N)$$$ denotes the number of routes. It is guaranteed that every checkpoint except checkpoint $$$1$$$ has exactly two routes connecting it. The next $$$M$$$ lines each contains two integers $$$U_i$$$ and $$$V_i$$$ $$$(1 le U_i, V_i le N, U_i e V_i)$$$, which denotes the checkpoint that route $$$i$$$ connects. It is guaranteed that for every pair of checkpoints, there exists a path connecting them directly or indirectly using the routes. Output Output a single integer which denotes the number of final configurations after each round of CPC modulo $$$10^9 + 7$$$ Example Input 5 6 1 2 2 3 3 4 4 1 1 5 5 1 Note Every possible final configuration for the example is listed below: The blue-colored numbers give the series of moves $$$Blue$$$ took, and the red-colored numbers give the series of moves $$$Red$$$ took. | 2,600 | false | false | false | true | false | false | false | false | false | false | 3,546 |
805A | Problem - 805A - 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 math *1000 No tag edit access → Contest materials . Output Print single integer, the integer that appears maximum number of times in the divisors. If there are multiple answers, print any of them. Examples Input 19 29 Output 2 Input 3 6 Output 3 Note Definition of a divisor: | 1,000 | true | true | false | false | false | false | false | false | false | false | 6,539 |
318B | Volodya likes listening to heavy metal and (occasionally) reading. No wonder Volodya is especially interested in texts concerning his favourite music style. Volodya calls a string powerful if it starts with "heavy" and ends with "metal". Finding all powerful substrings (by substring Volodya means a subsequence of consecutive characters in a string) in a given text makes our hero especially joyful. Recently he felt an enormous fit of energy while reading a certain text. So Volodya decided to count all powerful substrings in this text and brag about it all day long. Help him in this difficult task. Two substrings are considered different if they appear at the different positions in the text. For simplicity, let us assume that Volodya's text can be represented as a single string. Input Input contains a single non-empty string consisting of the lowercase Latin alphabet letters. Length of this string will not be greater than 106 characters. Output Print exactly one number — the number of powerful substrings of the given string. 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. Examples Input heavymetalisheavymetal Input trueheavymetalissotruewellitisalsosoheavythatyoucanalmostfeeltheweightofmetalonyou Note In the first sample the string "heavymetalisheavymetal" contains powerful substring "heavymetal" twice, also the whole string "heavymetalisheavymetal" is certainly powerful. In the second sample the string "heavymetalismetal" contains two powerful substrings: "heavymetal" and "heavymetalismetal". | 1,300 | false | false | true | false | false | false | false | false | false | false | 8,559 |
1077F1 | The only difference between easy and hard versions is the constraints. Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of $$$n$$$ consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the $$$i$$$-th picture has beauty $$$a_i$$$. Vova wants to repost exactly $$$x$$$ pictures in such a way that: each segment of the news feed of at least $$$k$$$ consecutive pictures has at least one picture reposted by Vova; the sum of beauty values of reposted pictures is maximum possible. For example, if $$$k=1$$$ then Vova has to repost all the pictures in the news feed. If $$$k=2$$$ then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them. Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions. Input The first line of the input contains three integers $$$n, k$$$ and $$$x$$$ ($$$1 le k, x le n le 200$$$) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the beauty of the $$$i$$$-th picture. Output Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement. Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement. Examples Input 6 1 5 10 30 30 70 10 10 | 1,900 | false | false | false | true | false | false | false | false | false | false | 5,332 |
1877A | There are $$$n$$$ teams in a football tournament. Each pair of teams match up once. After every match, Pak Chanek receives two integers as the result of the match, the number of goals the two teams score during the match. The efficiency of a team is equal to the total number of goals the team scores in each of its matches minus the total number of goals scored by the opponent in each of its matches. After the tournament ends, Pak Dengklek counts the efficiency of every team. Turns out that he forgot about the efficiency of one of the teams. Given the efficiency of $$$n-1$$$ teams $$$a_1,a_2,a_3,ldots,a_{n-1}$$$. What is the efficiency of the missing team? It can be shown that the efficiency of the missing team can be uniquely determined. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 500$$$) — the number of test cases. The following lines contain the description of each test case. The first line contains a single integer $$$n$$$ ($$$2 leq n leq 100$$$) — the number of teams. The second line contains $$$n-1$$$ integers $$$a_1,a_2,a_3,ldots,a_{n-1}$$$ ($$$-100leq a_ileq100$$$) — the efficiency of $$$n-1$$$ teams. Output For each test case, output a line containing an integer representing the efficiency of the missing team. Example Input 2 4 3 -4 5 11 -30 12 -57 7 0 -81 -68 41 -89 0 Note In the first test case, below is a possible tournament result: Team $$$1$$$ vs. Team $$$2$$$: $$$1-2$$$ Team $$$1$$$ vs. Team $$$3$$$: $$$3-0$$$ Team $$$1$$$ vs. Team $$$4$$$: $$$3-2$$$ Team $$$2$$$ vs. Team $$$3$$$: $$$1-4$$$ Team $$$2$$$ vs. Team $$$4$$$: $$$1-3$$$ Team $$$3$$$ vs. Team $$$4$$$: $$$5-0$$$ The efficiency of each team is: 1. Team $$$1$$$: $$$(1+3+3)-(2+0+2)=7-4=3$$$ 2. Team $$$2$$$: $$$(2+1+1)-(1+4+3)=4-8=-4$$$ 3. Team $$$3$$$: $$$(0+4+5)-(3+1+0)=9-4=5$$$ 4. Team $$$4$$$: $$$(2+3+0)-(3+1+5)=5-9=-4$$$ Therefore, the efficiency of the missing team (team $$$4$$$) is $$$-4$$$. It can be shown that any possible tournament of $$$4$$$ teams that has the efficiency of $$$3$$$ teams be $$$3$$$, $$$-4$$$, and $$$5$$$ will always have the efficiency of the $$$4$$$-th team be $$$-4$$$. | 800 | true | false | false | false | false | false | false | false | false | false | 1,019 |
858D | There are _n_ phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: if he enters 00 two numbers will show up: 100000000 and 100123456, if he enters 123 two numbers will show up 123456789 and 100123456, if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u200970000) — the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. Output Print exactly _n_ lines: the _i_-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the _i_-th number from the contacts. If there are several such sequences, print any of them. Examples Input 3 123456789 100000000 100123456 Input 4 123456789 193456789 134567819 934567891 | 1,600 | false | false | true | false | true | false | false | false | true | false | 6,311 |
1433A | There is a building consisting of $$$10~000$$$ apartments numbered from $$$1$$$ to $$$10~000$$$, inclusive. Call an apartment boring, if its number consists of the same digit. Examples of boring apartments are $$$11, 2, 777, 9999$$$ and so on. Our character is a troublemaker, and he calls the intercoms of all boring apartments, till someone answers the call, in the following order: First he calls all apartments consisting of digit $$$1$$$, in increasing order ($$$1, 11, 111, 1111$$$). Next he calls all apartments consisting of digit $$$2$$$, in increasing order ($$$2, 22, 222, 2222$$$) And so on. The resident of the boring apartment $$$x$$$ answers the call, and our character stops calling anyone further. Our character wants to know how many digits he pressed in total and your task is to help him to count the total number of keypresses. For example, if the resident of boring apartment $$$22$$$ answered, then our character called apartments with numbers $$$1, 11, 111, 1111, 2, 22$$$ and the total number of digits he pressed is $$$1 + 2 + 3 + 4 + 1 + 2 = 13$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 36$$$) — the number of test cases. The only line of the test case contains one integer $$$x$$$ ($$$1 le x le 9999$$$) — the apartment number of the resident who answered the call. It is guaranteed that $$$x$$$ consists of the same digit. | 800 | true | false | true | false | false | false | false | false | false | false | 3,499 |
1667F | You are given a rectangular grid with $$$n$$$ rows and $$$m$$$ columns. $$$n$$$ and $$$m$$$ are divisible by $$$4$$$. Some of the cells are already colored black or white. It is guaranteed that no two colored cells share a corner or an edge. Color the remaining cells in a way that both the black and the white cells becomes orthogonally connected or determine that it is impossible. Consider a graph, where the black cells are the nodes. Two nodes are adjacent if the corresponding cells share an edge. If the described graph is connected, the black cells are orthogonally connected. Same for white cells. Input The input consists of multiple test cases. The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 4000$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$8 le n, m le 500$$$, $$$n$$$ and $$$m$$$ are divisible by $$$4$$$) — the number of rows and columns. Each of the next $$$n$$$ lines contains $$$m$$$ characters. Each character is either 'B', 'W' or '.', representing black, white or empty cell respectively. Two colored (black or white) cell does not share a corner or an edge. It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$250,000$$$. Output For each testcase print "NO" if there is no solution, otherwise print "YES" and a grid with the same format. If there are multiple solutions, you can print any. Example Input 4 8 8 .W.W.... .....B.W .W.W.... .....W.W B.B..... ....B.B. B.W..... ....B.B. 8 8 B.W..B.W ........ W.B..W.B ........ ........ B.W..B.W ........ W.B..W.B 8 12 W.B......... ....B...B.W. B.B......... ....B...B.B. .B.......... ........B... .W..B.B...W. ............ 16 16 .W............W. ...W..W..W.W.... .B...........B.W ....W....W...... W......B....W.W. ..W.......B..... ....W...W....B.W .W....W....W.... ...B...........W W.....W...W..B.. ..W.W...W......B ............W... .W.B...B.B....B. .....W.....W.... ..W......W...W.. W...W..W...W...W Output YES BWWWWWWW BWBBBBBW BWBWWWBW BWBWBWBW BWBWBWBW BWBBBWBW BWWWWWBW BBBBBBBW NO YES WWBBBBBBBBBB BWWWBBBBBBWB BBBWBBBWWWWB BBBWBBBWBBBB BBBWBBBWBBBB BBBWWWWWBBBB BWWWBBBWWWWB BBBBBBBBBBBB YES WWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWW WBBBBBBBBBBBBBWW WBBBWBWWWWBWWBWW WBBWWBBBWWBWWBWW WBWWWBWWWWBWWBWW WBBWWBBBWWBWWBWW WWBWWWWWWWWWWWWW WWBBBBBBBBBBBBWW WBBBWWWBWWWBWBWW WWWBWBBBWBBBWBBB WWWBWBWWWWWBWWBW WWWBWBBBWBBBWWBW WWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWW WWWWWWWWWWWWWWWW Note Solution for test case 1: Test case 2: one can see that the black and the white part can't be connected in the same time. So the answer is "NO". | 3,500 | false | false | true | false | false | false | false | false | false | false | 2,268 |
36B | Ever since Kalevitch, a famous Berland abstractionist, heard of fractals, he made them the main topic of his canvases. Every morning the artist takes a piece of graph paper and starts with making a model of his future canvas. He takes a square as big as _n_u2009×u2009_n_ squares and paints some of them black. Then he takes a clean square piece of paper and paints the fractal using the following algorithm: Step 1. The paper is divided into _n_2 identical squares and some of them are painted black according to the model. Step 2. Every square that remains white is divided into _n_2 smaller squares and some of them are painted black according to the model. Every following step repeats step 2. Unfortunately, this tiresome work demands too much time from the painting genius. Kalevitch has been dreaming of making the process automatic to move to making 3D or even 4D fractals. Input The first line contains integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u20093, 1u2009≤u2009_k_u2009≤u20095), where _k_ is the amount of steps of the algorithm. Each of the following _n_ lines contains _n_ symbols that determine the model. Symbol «.» stands for a white square, whereas «*» stands for a black one. It is guaranteed that the model has at least one white square. Output Output a matrix _n__k_u2009×u2009_n__k_ which is what a picture should look like after _k_ steps of the algorithm. Examples Output .****** ..***** .*.**** ....*** .***.** ..**..* .*.*.*. ........ Output .*.***.*. ******** .*.***.*. ******** ******** ******** .*.***.*. ******** .*.***.*. | 1,600 | false | false | true | false | false | false | false | false | false | false | 9,817 |
1039A | There are two bus stops denoted A and B, and there $$$n$$$ buses that go from A to B every day. The shortest path from A to B takes $$$t$$$ units of time but some buses might take longer paths. Moreover, buses are allowed to overtake each other during the route. At each station one can find a sorted list of moments of time when a bus is at this station. We denote this list as $$$a_1 < a_2 < ldots < a_n$$$ for stop A and as $$$b_1 < b_2 < ldots < b_n$$$ for stop B. The buses always depart from A and arrive to B according to the timetable, but the order in which the buses arrive may differ. Let's call an order of arrivals valid if each bus arrives at least $$$t$$$ units of time later than departs. It is known that for an order to be valid the latest possible arrival for the bus that departs at $$$a_i$$$ is $$$b_{x_i}$$$, i.e. $$$x_i$$$-th in the timetable. In other words, for each $$$i$$$ there exists such a valid order of arrivals that the bus departed $$$i$$$-th arrives $$$x_i$$$-th (and all other buses can arrive arbitrary), but there is no valid order of arrivals in which the $$$i$$$-th departed bus arrives $$$(x_i + 1)$$$-th. Formally, let's call a permutation $$$p_1, p_2, ldots, p_n$$$ valid, if $$$b_{p_i} ge a_i + t$$$ for all $$$i$$$. Then $$$x_i$$$ is the maximum value of $$$p_i$$$ among all valid permutations. You are given the sequences $$$a_1, a_2, ldots, a_n$$$ and $$$x_1, x_2, ldots, x_n$$$, but not the arrival timetable. Find out any suitable timetable for stop B $$$b_1, b_2, ldots, b_n$$$ or determine that there is no such timetable. Input The first line of the input contains two integers $$$n$$$ and $$$t$$$ ($$$1 leq n leq 200,000$$$, $$$1 leq t leq 10^{18}$$$)xa0— the number of buses in timetable for and the minimum possible travel time from stop A to stop B. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_1 < a_2 < ldots < a_n leq 10^{18}$$$), defining the moments of time when the buses leave stop A. The third line contains $$$n$$$ integers $$$x_1, x_2, ldots, x_n$$$ ($$$1 leq x_i leq n$$$), the $$$i$$$-th of them stands for the maximum possible timetable position, at which the $$$i$$$-th bus leaving stop A can arrive at stop B. Output If a solution exists, print "Yes" (without quotes) in the first line of the output. In the second line print $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_1 < b_2 < ldots < b_n leq 3 cdot 10^{18}$$$). We can show that if there exists any solution, there exists a solution that satisfies such constraints on $$$b_i$$$. If there are multiple valid answers you can print any of them. If there is no valid timetable, print "No" (without quotes) in the only line of the output. Note Consider the first example and the timetable $$$b_1, b_2, ldots, b_n$$$ from the output. To get $$$x_1 = 2$$$ the buses can arrive in the order $$$(2, 1, 3)$$$. To get $$$x_2 = 2$$$ and $$$x_3 = 3$$$ the buses can arrive in the order $$$(1, 2, 3)$$$. $$$x_1$$$ is not $$$3$$$, because the permutations $$$(3, 1, 2)$$$ and $$$(3, 2, 1)$$$ (all in which the $$$1$$$-st bus arrives $$$3$$$-rd) are not valid (sube buses arrive too early), $$$x_2$$$ is not $$$3$$$ because of similar reasons. | 2,300 | true | true | false | false | true | true | false | false | false | false | 5,502 |
644B | In this problem you have to simulate the workflow of one-thread server. There are _n_ queries to process, the _i_-th will be received at moment _t__i_ and needs to be processed for _d__i_ units of time. All _t__i_ are guaranteed to be distinct. When a query appears server may react in three possible ways: 1. If server is free and query queue is empty, then server immediately starts to process this query. 2. If server is busy and there are less than _b_ queries in the queue, then new query is added to the end of the queue. 3. If server is busy and there are already _b_ queries pending in the queue, then new query is just rejected and will never be processed. As soon as server finished to process some query, it picks new one from the queue (if it's not empty, of course). If a new query comes at some moment _x_, and the server finishes to process another query at exactly the same moment, we consider that first query is picked from the queue and only then new query appears. For each query find the moment when the server will finish to process it or print -1 if this query will be rejected. Input The first line of the input contains two integers _n_ and _b_ (1u2009≤u2009_n_,u2009_b_u2009≤u2009200u2009000)xa0— the number of queries and the maximum possible size of the query queue. Then follow _n_ lines with queries descriptions (in chronological order). Each description consists of two integers _t__i_ and _d__i_ (1u2009≤u2009_t__i_,u2009_d__i_u2009≤u2009109), where _t__i_ is the moment of time when the _i_-th query appears and _d__i_ is the time server needs to process it. It is guaranteed that _t__i_u2009-u20091u2009<u2009_t__i_ for all _i_u2009>u20091. Output Print the sequence of _n_ integers _e_1,u2009_e_2,u2009...,u2009_e__n_, where _e__i_ is the moment the server will finish to process the _i_-th query (queries are numbered in the order they appear in the input) or u2009-u20091 if the corresponding query will be rejected. Examples Input 5 1 2 9 4 8 10 9 15 2 19 1 Note Consider the first sample. 1. The server will start to process first query at the moment 2 and will finish to process it at the moment 11. 2. At the moment 4 second query appears and proceeds to the queue. 3. At the moment 10 third query appears. However, the server is still busy with query 1, _b_u2009=u20091 and there is already query 2 pending in the queue, so third query is just rejected. 4. At the moment 11 server will finish to process first query and will take the second query from the queue. 5. At the moment 15 fourth query appears. As the server is currently busy it proceeds to the queue. 6. At the moment 19 two events occur simultaneously: server finishes to proceed the second query and the fifth query appears. As was said in the statement above, first server will finish to process the second query, then it will pick the fourth query from the queue and only then will the fifth query appear. As the queue is empty fifth query is proceed there. 7. Server finishes to process query number 4 at the moment 21. Query number 5 is picked from the queue. 8. Server finishes to process query number 5 at the moment 22. | 1,700 | false | false | false | false | true | true | false | false | false | false | 7,236 |
187A | Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot. One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through _n_ and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation. Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the quantity of the numbers in the both given permutations. Next line contains _n_ space-separated integers — the first permutation. Each number between 1 to _n_ will appear in the permutation exactly once. Next line describe the second permutation in the same format. Output Print a single integer denoting the minimum number of moves required to convert the first permutation to the second. Note In the first sample, he removes number 1 from end of the list and places it at the beginning. After that he takes number 2 and places it between 1 and 3. In the second sample, he removes number 5 and inserts it after 1. In the third sample, the sequence of changes are like this: 1 5 2 3 4 1 4 5 2 3 1 3 4 5 2 1 2 3 4 5 So he needs three moves. | 1,500 | false | true | false | false | false | false | false | false | false | false | 9,097 |
1485B | Given a positive integer $$$k$$$, two arrays are called $$$k$$$-similar if: they are strictly increasing; they have the same length; all their elements are positive integers between $$$1$$$ and $$$k$$$ (inclusive); they differ in exactly one position. You are given an integer $$$k$$$, a strictly increasing array $$$a$$$ and $$$q$$$ queries. For each query, you are given two integers $$$l_i leq r_i$$$. Your task is to find how many arrays $$$b$$$ exist, such that $$$b$$$ is $$$k$$$-similar to array $$$[a_{l_i},a_{l_i+1}ldots,a_{r_i}]$$$. Input The first line contains three integers $$$n$$$, $$$q$$$ and $$$k$$$ ($$$1leq n, q leq 10^5$$$, $$$nleq k leq 10^9$$$)xa0— the length of array $$$a$$$, the number of queries and number $$$k$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots,a_n$$$ ($$$1 leq a_i leq k$$$). This array is strictly increasing xa0— $$$a_1 < a_2 < ldots < a_n$$$. Each of the following $$$q$$$ lines contains two integers $$$l_i$$$, $$$r_i$$$ ($$$1 leq l_i leq r_i leq n$$$). Output Print $$$q$$$ lines. The $$$i$$$-th of them should contain the answer to the $$$i$$$-th query. Examples Input 4 2 5 1 2 4 5 2 3 3 4 Input 6 5 10 2 4 6 7 8 9 1 4 1 2 3 5 1 6 5 5 Note In the first example: In the first query there are $$$4$$$ arrays that are $$$5$$$-similar to $$$[2,4]$$$: $$$[1,4],[3,4],[2,3],[2,5]$$$. In the second query there are $$$3$$$ arrays that are $$$5$$$-similar to $$$[4,5]$$$: $$$[1,5],[2,5],[3,5]$$$. | 1,200 | true | false | true | true | false | false | false | false | false | false | 3,258 |
459A | Problem - 459A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *1200 No tag edit access → Contest materials integers, where _x_1 and _y_1 are coordinates of the first tree and _x_2 and _y_2 are coordinates of the second tree. It's guaranteed that the given points are distinct. Output If there is no solution to the problem, print -1. Otherwise print four space-separated integers _x_3,u2009_y_3,u2009_x_4,u2009_y_4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them. Note that _x_3,u2009_y_3,u2009_x_4,u2009_y_4 must be in the range (u2009-u20091000u2009≤u2009_x_3,u2009_y_3,u2009_x_4,u2009_y_4u2009≤u20091000). Examples Input 0 0 0 1 Output 1 0 1 1 Input 0 0 1 1 Output 0 1 1 0 Input 0 0 1 2 Output -1 | 1,200 | false | false | true | false | false | false | false | false | false | false | 8,006 |
933B | In order to put away old things and welcome a fresh new year, a thorough cleaning of the house is a must. Little Tommy finds an old polynomial and cleaned it up by taking it modulo another. But now he regrets doing this... Given two integers _p_ and _k_, find a polynomial _f_(_x_) with non-negative integer coefficients strictly less than _k_, whose remainder is _p_ when divided by (_x_u2009+u2009_k_). That is, _f_(_x_)u2009=u2009_q_(_x_)·(_x_u2009+u2009_k_)u2009+u2009_p_, where _q_(_x_) is a polynomial (not necessarily with integer coefficients). Input The only line of input contains two space-separated integers _p_ and _k_ (1u2009≤u2009_p_u2009≤u20091018, 2u2009≤u2009_k_u2009≤u20092u2009000). Output If the polynomial does not exist, print a single integer -1, or output two lines otherwise. In the first line print a non-negative integer _d_ — the number of coefficients in the polynomial. In the second line print _d_ space-separated integers _a_0,u2009_a_1,u2009...,u2009_a__d_u2009-u20091, describing a polynomial fulfilling the given requirements. Your output should satisfy 0u2009≤u2009_a__i_u2009<u2009_k_ for all 0u2009≤u2009_i_u2009≤u2009_d_u2009-u20091, and _a__d_u2009-u20091u2009≠u20090. If there are many possible solutions, print any of them. Note In the first example, _f_(_x_)u2009=u2009_x_6u2009+u2009_x_5u2009+u2009_x_4u2009+u2009_x_u2009=u2009(_x_5u2009-u2009_x_4u2009+u20093_x_3u2009-u20096_x_2u2009+u200912_x_u2009-u200923)·(_x_u2009+u20092)u2009+u200946. In the second example, _f_(_x_)u2009=u2009_x_2u2009+u2009205_x_u2009+u200992u2009=u2009(_x_u2009-u20099)·(_x_u2009+u2009214)u2009+u20092018. | 2,000 | true | false | false | false | false | false | false | false | false | false | 5,970 |
625A | Kolya Gerasimov loves kefir very much. He lives in year 1984 and knows all the details of buying this delicious drink. One day, as you probably know, he found himself in year 2084, and buying kefir there is much more complicated. Kolya is hungry, so he went to the nearest milk shop. In 2084 you may buy kefir in a plastic liter bottle, that costs _a_ rubles, or in glass liter bottle, that costs _b_ rubles. Also, you may return empty glass bottle and get _c_ (_c_u2009<u2009_b_) rubles back, but you cannot return plastic bottles. Kolya has _n_ rubles and he is really hungry, so he wants to drink as much kefir as possible. There were no plastic bottles in his 1984, so Kolya doesn't know how to act optimally and asks for your help. Input First line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091018)xa0— the number of rubles Kolya has at the beginning. Then follow three lines containing integers _a_, _b_ and _c_ (1u2009≤u2009_a_u2009≤u20091018, 1u2009≤u2009_c_u2009<u2009_b_u2009≤u20091018)xa0— the cost of one plastic liter bottle, the cost of one glass liter bottle and the money one can get back by returning an empty glass bottle, respectively. Output Print the only integerxa0— maximum number of liters of kefir, that Kolya can drink. Note In the first sample, Kolya can buy one glass bottle, then return it and buy one more glass bottle. Thus he will drink 2 liters of kefir. In the second sample, Kolya can buy two plastic bottle and get two liters of kefir, or he can buy one liter glass bottle, then return it and buy one plastic bottle. In both cases he will drink two liters of kefir. | 1,700 | true | false | true | false | false | false | false | false | false | false | 7,333 |
1912E | Problem - 1912E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms implementation math *2200 No tag edit access → Contest materials ") ") ") . Output Print the expression without spaces or line breaks. It can only contain digits 0 through 9, '+', '-', and '*' characters. The expression must contain at most $$$1000$$$ characters. Leading zeros in numbers are not allowed (the only exception is the notation '0' representing the number $$$0$$$) in both the expression and its reverse. Use of unary '+' or '-' is not allowed. The expression must be well-formed in both directions. The calculation uses the standard operator precedence. Examples Input 1998 -3192 Output 2023-12-13 Input 413 908 Output 12*34+5 | 2,200 | true | false | true | false | false | true | false | false | false | false | 833 |
1684B | You are given three positive integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$a < b < c$$$). You have to find three positive integers $$$x$$$, $$$y$$$, $$$z$$$ such that: $$$$$$x bmod y = a,$$$$$$ $$$$$$y bmod z = b,$$$$$$ $$$$$$z bmod x = c.$$$$$$ Here $$$p bmod q$$$ denotes the remainder from dividing $$$p$$$ by $$$q$$$. It is possible to show that for such constraints the answer always exists. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0— the number of test cases. Description of the test cases follows. Each test case contains a single line with three integers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 le a < b < c le 10^8$$$). Output For each test case output three positive integers $$$x$$$, $$$y$$$, $$$z$$$ ($$$1 le x, y, z le 10^{18}$$$) such that $$$x bmod y = a$$$, $$$y bmod z = b$$$, $$$z bmod x = c$$$. You can output any correct answer. Example Input 4 1 3 4 127 234 421 2 7 8 59 94 388 Output 12 11 4 1063 234 1484 25 23 8 2221 94 2609 Note In the first test case: $$$$$$x bmod y = 12 bmod 11 = 1;$$$$$$ $$$$$$y bmod z = 11 bmod 4 = 3;$$$$$$ $$$$$$z bmod x = 4 bmod 12 = 4.$$$$$$ | 800 | true | false | false | false | false | true | false | false | false | false | 2,172 |
101A | Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of _n_ small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than _k_ characters, it will be very suspicious. Find the least number of distinct characters that can remain in the string after no more than _k_ characters are deleted. You also have to find any possible way to delete the characters. Input The first input data line contains a string whose length is equal to _n_ (1u2009≤u2009_n_u2009≤u2009105). The string consists of lowercase Latin letters. The second line contains the number _k_ (0u2009≤u2009_k_u2009≤u2009105). Output Print on the first line the only number _m_ — the least possible number of different characters that could remain in the given string after it loses no more than _k_ characters. Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly _m_ distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly _m_ distinct characters, print any of them. Note In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length. In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string. In the third sample you are given a line whose length is equal to 8, and _k_u2009=u200910, so that the whole line can be deleted. The correct answer is 0 and an empty string. | 1,200 | false | true | false | false | false | false | false | false | false | false | 9,479 |
1392B | Being stuck at home, Ray became extremely bored. To pass time, he asks Lord Omkar to use his time bending power: Infinity Clock! However, Lord Omkar will only listen to mortals who can solve the following problem: You are given an array $$$a$$$ of $$$n$$$ integers. You are also given an integer $$$k$$$. Lord Omkar wants you to do $$$k$$$ operations with this array. Define one operation as the following: 1. Set $$$d$$$ to be the maximum value of your array. 2. For every $$$i$$$ from $$$1$$$ to $$$n$$$, replace $$$a_{i}$$$ with $$$d-a_{i}$$$. The goal is to predict the contents in the array after $$$k$$$ operations. Please help Ray determine what the final sequence will look like! Input Each test contains multiple test cases. The first line contains the number of cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 2 cdot 10^5, 1 leq k leq 10^{18}$$$) – the length of your array and the number of operations to perform. The second line of each test case contains $$$n$$$ integers $$$a_{1},a_{2},...,a_{n}$$$ $$$(-10^9 leq a_{i} leq 10^9)$$$ – the initial contents of your array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each case, print the final version of array $$$a$$$ after $$$k$$$ operations described above. Example Input 3 2 1 -199 192 5 19 5 -1 4 2 0 1 2 69 Note In the first test case the array changes as follows: Initially, the array is $$$[-199, 192]$$$. $$$d = 192$$$. After the operation, the array becomes $$$[d-(-199), d-192] = [391, 0]$$$. | 800 | true | false | true | false | false | false | false | false | false | false | 3,715 |
466C | Problem - 466C - 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 brute force data structures dp two pointers *1700 No tag edit access → Contest materials ") ") , that . Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20095·105), showing how many numbers are in the array. The second line contains _n_ integers _a_ | 1,700 | false | false | false | true | true | false | true | true | false | false | 7,975 |
1994F | Pelican Town represents $$$n$$$ houses connected by $$$m$$$ bidirectional roads. Some roads have NPCs standing on them. Farmer Buba needs to walk on each road with an NPC and talk to them. Help the farmer find a route satisfying the following properties: The route starts at some house, follows the roads, and ends at the same house. The route does not follow any road more than once (in both directions together). The route follows each road with an NPC exactly once. Note that the route can follow roads without NPCs, and you do not need to minimize the length of the route. It is guaranteed that you can reach any house from any other by walking on the roads with NPCs only. Input Each test consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^{4}$$$)xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 5 cdot 10^5, 1 leq m leq 5 cdot 10^5$$$)xa0— the number of houses and roads in Pelican Town respectively. In each of the next $$$m$$$ lines, three integers $$$u$$$, $$$v$$$, and $$$c$$$ ($$$1 leq u, v leq n, c = 0/1$$$) are givenxa0— the ends of the road and whether an NPC is on this road. If $$$c = 1$$$, then the road has an NPC. If $$$c = 0$$$, then the road has no NPC. The graph may contain multiple edges and loops, and if there are multiple edges with NPCs standing on them, the route must follow each of these roads. It is guaranteed that you can reach any house from any other by walking on the roads with NPCs only. It is guaranteed that the sum of $$$n$$$ and $$$m$$$ for all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, if there is no solution, then output "No" (without quotes). Otherwise, output "Yes" (without quotes), and then output $$$k$$$xa0— the number of roads in the route. In the next line, output $$$k + 1$$$ numbersxa0— the houses of the route in the order of traversal. Note that the first house should match the last one, as the route is cyclic. If there are multiple answers, you can print any of them. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 3 3 2 1 2 1 2 3 1 3 3 1 2 1 1 3 1 2 3 0 5 9 1 2 0 5 2 1 5 4 1 5 1 1 2 3 1 5 2 1 4 1 0 4 3 0 5 2 0 Output NO YES 3 1 2 3 1 YES 7 1 2 5 4 3 2 5 1 Note Note that in the third test case, there are multiple edges $$$(5, 2)$$$. You must walk on two of them. | 2,500 | false | false | false | false | false | true | false | false | false | true | 300 |
1779H | Iron and Werewolf are participating in a chess Olympiad, so they want to practice team building. They gathered $$$n$$$ players, where $$$n$$$ is a power of $$$2$$$, and they will play sports. Iron and Werewolf are among those $$$n$$$ people. One of the sports is tug of war. For each $$$1leq i leq n$$$, the $$$i$$$-th player has strength $$$s_i$$$. Elimination rounds will be held until only one player remainsxa0— we call that player the absolute winner. In each round: Assume that $$$m>1$$$ players are still in the game, where $$$m$$$ is a power of $$$2$$$. The $$$m$$$ players are split into two teams of equal sizes (i.xa0e., with $$$m/2$$$ players in each team). The strength of a team is the sum of the strengths of its players. If the teams have equal strengths, Iron chooses who wins; otherwise, the stronger team wins. Every player in the losing team is eliminated, so $$$m/2$$$ players remain. Iron already knows each player's strength and is wondering who can become the absolute winner and who can't if he may choose how the teams will be formed in each round, as well as the winning team in case of equal strengths. Input The first line contains a single integer $$$n$$$ ($$$4 leq n leq 32$$$)xa0— the number of players participating in tug of war. It is guaranteed that $$$n$$$ is a power of $$$2$$$. The second line consists of a sequence $$$s_1,s_2, ldots, s_n$$$ of integers ($$$1 leq s_i leq 10^{15}$$$)xa0— the strengths of the players. Output In a single line output a binary string $$$s$$$ of length $$$n$$$xa0— the $$$i$$$-th character of $$$s$$$ should be $$$1$$$ if the $$$i$$$-th player can become the absolute winner and it should be $$$0$$$ otherwise. Examples Input 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Output 00000000000000001111111111111111 Input 16 1 92875987325987 1 1 92875987325986 92875987325985 1 92875987325988 92875987325990 92875987325989 1 1 92875987325984 92875987325983 1 1 Note In the first example, players $$$1$$$ and $$$4$$$ with their respective strengths of $$$60$$$ and $$$87$$$ can become the absolute winners. Let's describe the process for player $$$1$$$. Firstly, we divide the players into teams $$$[1,3]$$$ and $$$[2,4]$$$. Strengths of those two teams are $$$60+59=119$$$ and $$$32+87=119$$$. They they are equal, Iron can choose to disqualify any of the two teams. Let his choice be the second team. We are left with players $$$1$$$ and $$$3$$$. Since $$$1$$$ has greater strength ($$$60>59$$$) they win and are declared the absolute winner as they are the last remaining player. In the third example, the strengths of the remaining players may look like $$$[8,8,8,8,4,4,4,4] ightarrow [8,8,4,4] ightarrow [8,4] ightarrow [8]$$$. Each person with strength $$$8$$$ can become the absolute winner and it can be proved that others can't. | 3,500 | false | false | false | false | false | false | true | false | false | false | 1,595 |
1167F | You are given an array $$$a_1, a_2, dots, a_n$$$. All $$$a_i$$$ are pairwise distinct. Let's define function $$$f(l, r)$$$ as follows: let's define array $$$b_1, b_2, dots, b_{r - l + 1}$$$, where $$$b_i = a_{l - 1 + i}$$$; sort array $$$b$$$ in increasing order; result of the function $$$f(l, r)$$$ is $$$sumlimits_{i = 1}^{r - l + 1}{b_i cdot i}$$$. Calculate $$$left(sumlimits_{1 le l le r le n}{f(l, r)} ight) mod (10^9+7)$$$, i.e. total sum of $$$f$$$ for all subsegments of $$$a$$$ modulo $$$10^9+7$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$) — the length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$, $$$a_i eq a_j$$$ for $$$i eq j$$$) — array $$$a$$$. Output Print one integer — the total sum of $$$f$$$ for all subsegments of $$$a$$$ modulo $$$10^9+7$$$ Examples Input 3 123456789 214365879 987654321 Note Description of the first example: $$$f(1, 1) = 5 cdot 1 = 5$$$; $$$f(1, 2) = 2 cdot 1 + 5 cdot 2 = 12$$$; $$$f(1, 3) = 2 cdot 1 + 4 cdot 2 + 5 cdot 3 = 25$$$; $$$f(1, 4) = 2 cdot 1 + 4 cdot 2 + 5 cdot 3 + 7 cdot 4 = 53$$$; $$$f(2, 2) = 2 cdot 1 = 2$$$; $$$f(2, 3) = 2 cdot 1 + 4 cdot 2 = 10$$$; $$$f(2, 4) = 2 cdot 1 + 4 cdot 2 + 7 cdot 3 = 31$$$; $$$f(3, 3) = 4 cdot 1 = 4$$$; $$$f(3, 4) = 4 cdot 1 + 7 cdot 2 = 18$$$; $$$f(4, 4) = 7 cdot 1 = 7$$$; | 2,300 | true | false | false | false | true | false | false | false | true | false | 4,878 |
123C | A two dimensional array is called a bracket array if each grid contains one of the two possible brackets — "(" or ")". A path through the two dimensional array cells is called monotonous if any two consecutive cells in the path are side-adjacent and each cell of the path is located below or to the right from the previous one. A two dimensional array whose size equals _n_u2009×u2009_m_ is called a correct bracket array, if any string formed by writing out the brackets on some monotonous way from cell (1,u20091) to cell (_n_,u2009_m_) forms a correct bracket sequence. Let's define the operation of comparing two correct bracket arrays of equal size (_a_ and _b_) like that. Let's consider a given two dimensional array of priorities (_c_) — a two dimensional array of same size, containing different integers from 1 to _nm_. Let's find such position (_i_,u2009_j_) in the two dimensional array, that _a__i_,u2009_j_u2009≠u2009_b__i_,u2009_j_. If there are several such positions, let's choose the one where number _c__i_,u2009_j_ is minimum. If _a__i_,u2009_j_u2009=u2009"(", then _a_u2009<u2009_b_, otherwise _a_u2009>u2009_b_. If the position (_i_,u2009_j_) is not found, then the arrays are considered equal. Your task is to find a _k_-th two dimensional correct bracket array. It is guaranteed that for the given sizes of _n_ and _m_ there will be no less than _k_ two dimensional correct bracket arrays. Input The first line contains integers _n_, _m_ and _k_ — the sizes of the array and the number of the sought correct bracket array (1u2009≤u2009_n_,u2009_m_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u20091018). Then an array of priorities is given, _n_ lines each containing _m_ numbers, number _p__i_,u2009_j_ shows the priority of character _j_ in line _i_ (1u2009≤u2009_p__i_,u2009_j_u2009≤u2009_nm_, all _p__i_,u2009_j_ are different). Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specificator. Note In the first sample exists only one correct two-dimensional bracket array. In the second and in the third samples two arrays exist. A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters «+» and «1» into this sequence. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not. | 2,300 | false | true | false | true | false | false | false | false | false | false | 9,387 |
1627B | Rahul and Tina are looking forward to starting their new year at college. As they enter their new classroom, they observe the seats of students are arranged in a $$$n imes m$$$ grid. The seat in row $$$r$$$ and column $$$c$$$ is denoted by $$$(r, c)$$$, and the distance between two seats $$$(a,b)$$$ and $$$(c,d)$$$ is $$$a-c + b-d$$$. As the class president, Tina has access to exactly $$$k$$$ buckets of pink paint. The following process occurs. First, Tina chooses exactly $$$k$$$ seats in the classroom to paint with pink paint. One bucket of paint can paint exactly one seat. After Tina has painted $$$k$$$ seats in the previous step, Rahul chooses where he sits. He will not choose a seat that has been painted pink due to his hatred of the colour pink. After Rahul has chosen his seat, Tina chooses a seat for herself. She can choose any of the seats, painted or not, other than the one chosen by Rahul. Rahul wants to choose a seat such that he sits as close to Tina as possible. However, Tina wants to sit as far away from Rahul as possible due to some complicated relationship history that we couldn't fit into the statement! Now, Rahul wonders for $$$k = 0, 1, dots, n cdot m - 1$$$, if Tina has $$$k$$$ buckets of paint, how close can Rahul sit to Tina, if both Rahul and Tina are aware of each other's intentions and they both act as strategically as possible? Please help satisfy Rahul's curiosity! Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 5 cdot 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 leq n cdot m leq 10^5$$$)xa0— the number of rows and columns of seats in the classroom. The sum of $$$n cdot m$$$ across all test cases does not exceed $$$10^5$$$. Output For each test case, output $$$n cdot m$$$ ordered integersxa0— the distance between Rahul and Tina if both of them act optimally for every $$$k in [0, n cdot m - 1]$$$. Example Output 3 3 4 4 4 4 4 4 5 5 5 5 1 1 Note One possible sequence of choices for the first testcase where Tina has $$$k=3$$$ buckets of paints is as follows. Tina paints the seats at positions $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ with pink paint. Rahul chooses the seat at $$$(3, 1)$$$ after which Tina chooses to sit at $$$(1, 3)$$$. Therefore, the distance between Tina and Rahul is $$$3-1 + 1-3 = 4$$$, and we can prove that this is indeed the minimum possible distance under the given constraints. There may be other choices of seats which lead to the same answer as well. For $$$k=0$$$ in the first test case, Rahul can decide to sit at $$$(2, 2)$$$ and Tina can decide to sit at $$$(4, 3)$$$ so the distance between them would be $$$2 - 4 + 2 - 3 = 3$$$. Below are pictorial representations of the $$$k=3$$$ and $$$k=0$$$ cases for the first test case. A possible seating arrangement for $$$k=3$$$. A possible seating arrangement for $$$k=0$$$. | 1,300 | false | true | false | false | false | false | false | false | true | false | 2,480 |
1552A | A string $$$s$$$ of length $$$n$$$, consisting of lowercase letters of the English alphabet, is given. You must choose some number $$$k$$$ between $$$0$$$ and $$$n$$$. Then, you select $$$k$$$ characters of $$$s$$$ and permute them however you want. In this process, the positions of the other $$$n-k$$$ characters remain unchanged. You have to perform this operation exactly once. For example, if $$$s= exttt{"andrea"}$$$, you can choose the $$$k=4$$$ characters $$$ exttt{"a_d_ea"}$$$ and permute them into $$$ exttt{"d_e_aa"}$$$ so that after the operation the string becomes $$$ exttt{"dneraa"}$$$. Determine the minimum $$$k$$$ so that it is possible to sort $$$s$$$ alphabetically (that is, after the operation its characters appear in alphabetical order). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 40$$$) — the length of the string. The second line of each test case contains the string $$$s$$$. It is guaranteed that $$$s$$$ contains only lowercase letters of the English alphabet. Output For each test case, output the minimum $$$k$$$ that allows you to obtain a string sorted alphabetically, through the operation described above. Example Input 4 3 lol 10 codeforces 5 aaaaa 4 dcba Note In the first test case, we can choose the $$$k=2$$$ characters $$$ exttt{"_ol"}$$$ and rearrange them as $$$ exttt{"_lo"}$$$ (so the resulting string is $$$ exttt{"llo"}$$$). It is not possible to sort the string choosing strictly less than $$$2$$$ characters. In the second test case, one possible way to sort $$$s$$$ is to consider the $$$k=6$$$ characters $$$ exttt{"_o__force_"}$$$ and rearrange them as $$$ exttt{"_c__efoor_"}$$$ (so the resulting string is $$$ exttt{"ccdeefoors"}$$$). One can show that it is not possible to sort the string choosing strictly less than $$$6$$$ characters. In the third test case, string $$$s$$$ is already sorted (so we can choose $$$k=0$$$ characters). In the fourth test case, we can choose all $$$k=4$$$ characters $$$ exttt{"dcba"}$$$ and reverse the whole string (so the resulting string is $$$ exttt{"abcd"}$$$). | 800 | false | false | false | false | false | false | false | false | true | false | 2,884 |
1680F | You are given a simple connected undirected graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. The vertices are numbered from $$$1$$$ to $$$n$$$. A vertex cover of a graph is a set of vertices such that each edge has at least one of its endpoints in the set. Let's call a lenient vertex cover such a vertex cover that at most one edge in it has both endpoints in the set. Find a lenient vertex cover of a graph or report that there is none. If there are multiple answers, then print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 10^6$$$; $$$n - 1 le m le min(10^6, frac{n cdot (n - 1)}{2})$$$)xa0— the number of vertices and the number of edges of the graph. Each of the next $$$m$$$ lines contains two integers $$$v$$$ and $$$u$$$ ($$$1 le v, u le n$$$; $$$v eq u$$$)xa0— the descriptions of the edges. For each testcase, the graph is connected and doesn't have multiple edges. The sum of $$$n$$$ over all testcases doesn't exceed $$$10^6$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$10^6$$$. Output For each testcase, the first line should contain YES if a lenient vertex cover exists, and NO otherwise. If it exists, the second line should contain a binary string $$$s$$$ of length $$$n$$$, where $$$s_i = 1$$$ means that vertex $$$i$$$ is in the vertex cover, and $$$s_i = 0$$$ means that vertex $$$i$$$ isn't. If there are multiple answers, then print any of them. Examples Input 4 6 5 1 3 2 4 3 4 3 5 4 6 4 6 1 2 2 3 3 4 1 4 1 3 2 4 8 11 1 3 2 4 3 5 4 6 5 7 6 8 1 2 3 4 5 6 7 8 7 2 4 5 1 2 2 3 3 4 1 3 2 4 Output YES 001100 NO YES 01100110 YES 0110 Input 1 10 15 9 4 3 4 6 4 1 2 8 2 8 3 7 2 9 5 7 8 5 10 1 4 2 10 5 3 5 7 2 9 Input 1 10 19 7 9 5 3 3 4 1 6 9 4 1 4 10 5 7 1 9 2 8 3 7 3 10 9 2 10 9 8 3 2 1 5 10 7 9 5 1 2 Note Here are the graphs from the first example. The vertices in the lenient vertex covers are marked red. | 2,600 | false | false | false | false | false | false | false | false | false | true | 2,186 |
1415B | There is a street with $$$n$$$ houses in a line, numbered from $$$1$$$ to $$$n$$$. The house $$$i$$$ is initially painted in color $$$c_i$$$. The street is considered beautiful if all houses are painted in the same color. Tom, the painter, is in charge of making the street beautiful. Tom's painting capacity is defined by an integer, let's call it $$$k$$$. On one day, Tom can do the following repainting process that consists of two steps: 1. He chooses two integers $$$l$$$ and $$$r$$$ such that $$$ 1 le l le r le n $$$ and $$$ r - l + 1 = k $$$. 2. For each house $$$i$$$ such that $$$l le i le r$$$, he can either repaint it with any color he wants, or ignore it and let it keep its current color. Note that in the same day Tom can use different colors to repaint different houses. Tom wants to know the minimum number of days needed to repaint the street so that it becomes beautiful. Input The first line of input contains a single integer $$$t$$$ ($$$ 1 le t le 10^4$$$), the number of test cases. Description of test cases follows. In the first line of a test case description there are two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^5$$$). The second line contains $$$n$$$ space-separated integers. The $$$i$$$-th of these integers represents $$$c_i$$$ ($$$1 le c_i le 100$$$), the color which house $$$i$$$ is initially painted in. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output Print $$$t$$$ lines, each with one integer: the minimum number of days Tom needs to make the street beautiful for each test case. Example Input 3 10 2 1 1 2 2 1 1 2 2 2 1 7 1 1 2 3 4 5 6 7 10 3 1 3 3 3 3 1 2 1 3 3 Note In the first test case Tom should paint houses 1 and 2 in the first day in color 2, houses 5 and 6 in the second day in color 2, and the last house in color 2 on the third day. In the second test case Tom can, for example, spend 6 days to paint houses 1, 2, 4, 5, 6, 7 in color 3. In the third test case Tom can paint the first house in the first day and houses 6, 7, and 8 in the second day in color 3. | 1,100 | false | true | false | false | false | false | true | false | false | false | 3,607 |
1918E | This is an interactive problem! In the new round, there were $$$n$$$ tasks with difficulties from $$$1$$$ to $$$n$$$. The coordinator, who decided to have the first round with tasks in unsorted order of difficulty, rearranged the tasks, resulting in a permutation of difficulties from $$$1$$$ to $$$n$$$. After that, the coordinator challenged ace5 to guess the permutation in the following way. Initially, the coordinator chooses a number $$$x$$$ from $$$1$$$ to $$$n$$$. ace5 can make queries of the form: $$$? i$$$. The answer will be: $$$>$$$, if $$$a_i > x$$$, after which $$$x$$$ increases by $$$1$$$. $$$<$$$, if $$$a_i < x$$$, after which $$$x$$$ decreases by $$$1$$$. $$$=$$$, if $$$a_i = x$$$, after which $$$x$$$ remains unchanged. The task for ace5 is to guess the permutation in no more than $$$40n$$$ queries. Since ace5 is too busy writing the announcement, he has entrusted this task to you. Interaction The interaction between your program and the jury's program begins with reading a positive integer $$$n$$$ ($$$1 leq n leq 2000$$$) — the length of the hidden permutation. To make a query, output a line in the format "? i", where $$$1 leq i leq n$$$. As an answer, you will receive: ">", if $$$a_i$$$ > $$$x$$$, after which $$$x$$$ will increase by $$$1$$$. "<", if $$$a_i$$$ < $$$x$$$, after which $$$x$$$ will decrease by $$$1$$$. "=", if $$$a_i$$$ = $$$x$$$, after which $$$x$$$ will remain unchanged. You can make no more than $$$40n$$$ queries. To output the answer, you need to print "! a_1 a_2 ... a_n", where $$$1 leq a_i leq n$$$, and all of them are distinct. Outputting the answer does not count as a query. If your program makes more than $$$40n$$$ queries for one test case, or makes an invalid query, then the response to the query will be -1. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, it may receive any other verdict. After outputting a query, do not forget to print a newline and flush the output buffer. Otherwise, you will receive the verdict Presentation Error. To flush the buffer, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. The interactor in this problem is not adaptive. Hacks: To make a hack, use the following format: The first line contains a single integer $$$t$$$ — the number of test cases. The description of each test case should consist of two lines. The first line contains the numbers $$$n$$$ and $$$x$$$ ($$$1 leq x leq n leq 2000$$$) — the length of the hidden permutation and the initial value of the number $$$x$$$. The second line contains $$$n$$$ distinct numbers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq n$$$) — the permutation that the jury should choose in this test case. Sum of $$$n$$$ over all test cases should not exceed $$$2000$$$. Note In the first test, the hidden permutation is $$$a$$$ = [$$$2,4,1,5,3$$$], and the initial value of $$$x$$$ is $$$3$$$. In the second test, the hidden permutation is $$$a$$$ = [$$$2,1$$$], and the initial value of $$$x$$$ is $$$1$$$. | 2,200 | false | false | true | false | false | true | false | false | true | false | 784 |
1876C | Chaneka writes down an array $$$a$$$ of $$$n$$$ positive integer elements. Initially, all elements are not circled. In one operation, Chaneka can circle an element. It is possible to circle the same element more than once. After doing all operations, Chaneka makes a sequence $$$r$$$ consisting of all uncircled elements of $$$a$$$ following the order of their indices. Chaneka also makes another sequence $$$p$$$ such that its length is equal to the number of operations performed and $$$p_i$$$ is the index of the element that is circled in the $$$i$$$-th operation. Chaneka wants to do several operations such that sequence $$$r$$$ is equal to sequence $$$p$$$. Help her achieve this, or report if it is impossible! Note that if there are multiple solutions, you can print any of them. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 2cdot10^5$$$) — the size of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,a_3,ldots,a_n$$$ ($$$1leq a_ileq n$$$). Output A single line containing $$$-1$$$ if it is impossible. Otherwise, the output consists of two lines. The first line contains an integer $$$z$$$ representing the number of operations performed. The second line contains $$$z$$$ integers, the $$$i$$$-th integer represents the index of the element circled in the $$$i$$$-th operation. If there are multiple solutions, you can print any of them. Note In the first example, doing the operations like the example output gives the following results: Element $$$a_2$$$ gets circled $$$1$$$ time. Element $$$a_3$$$ gets circled $$$2$$$ times. The uncircled elements (ordered by their indices) are $$$a_1$$$, $$$a_4$$$, and $$$a_5$$$. So $$$r=[3,2,3]$$$. $$$p=[3,2,3]$$$ Therefore, $$$r=p$$$. In the second example, it is impossible. | 2,100 | false | true | false | false | false | true | false | false | true | true | 1,024 |
253C | Vasya is pressing the keys on the keyboard reluctantly, squeezing out his ideas on the classical epos depicted in Homer's Odysseus... How can he explain to his literature teacher that he isn't going to become a writer? In fact, he is going to become a programmer. So, he would take great pleasure in writing a program, but none — in writing a composition. As Vasya was fishing for a sentence in the dark pond of his imagination, he suddenly wondered: what is the least number of times he should push a key to shift the cursor from one position to another one? Let's describe his question more formally: to type a text, Vasya is using the text editor. He has already written _n_ lines, the _i_-th line contains _a__i_ characters (including spaces). If some line contains _k_ characters, then this line overall contains (_k_u2009+u20091) positions where the cursor can stand: before some character or after all characters (at the end of the line). Thus, the cursor's position is determined by a pair of integers (_r_,u2009_c_), where _r_ is the number of the line and _c_ is the cursor's position in the line (the positions are indexed starting from one from the beginning of the line). Vasya doesn't use the mouse to move the cursor. He uses keys "Up", "Down", "Right" and "Left". When he pushes each of these keys, the cursor shifts in the needed direction. Let's assume that before the corresponding key is pressed, the cursor was located in the position (_r_,u2009_c_), then Vasya pushed key: "Up": if the cursor was located in the first line (_r_u2009=u20091), then it does not move. Otherwise, it moves to the previous line (with number _r_u2009-u20091), to the same position. At that, if the previous line was short, that is, the cursor couldn't occupy position _c_ there, the cursor moves to the last position of the line with number _r_u2009-u20091; "Down": if the cursor was located in the last line (_r_u2009=u2009_n_), then it does not move. Otherwise, it moves to the next line (with number _r_u2009+u20091), to the same position. At that, if the next line was short, that is, the cursor couldn't occupy position _c_ there, the cursor moves to the last position of the line with number _r_u2009+u20091; "Right": if the cursor can move to the right in this line (_c_u2009<u2009_a__r_u2009+u20091), then it moves to the right (to position _c_u2009+u20091). Otherwise, it is located at the end of the line and doesn't move anywhere when Vasya presses the "Right" key; "Left": if the cursor can move to the left in this line (_c_u2009>u20091), then it moves to the left (to position _c_u2009-u20091). Otherwise, it is located at the beginning of the line and doesn't move anywhere when Vasya presses the "Left" key. You've got the number of lines in the text file and the number of characters, written in each line of this file. Find the least number of times Vasya should push the keys, described above, to shift the cursor from position (_r_1,u2009_c_1) to position (_r_2,u2009_c_2). Input The first line of the input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of lines in the file. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009105), separated by single spaces. The third line contains four integers _r_1,u2009_c_1,u2009_r_2,u2009_c_2 (1u2009≤u2009_r_1,u2009_r_2u2009≤u2009_n_,u20091u2009≤u2009_c_1u2009≤u2009_a__r_1u2009+u20091,u20091u2009≤u2009_c_2u2009≤u2009_a__r_2u2009+u20091). Output Print a single integer — the minimum number of times Vasya should push a key to move the cursor from position (_r_1,u2009_c_1) to position (_r_2,u2009_c_2). Note In the first sample the editor contains four lines. Let's represent the cursor's possible positions in the line as numbers. Letter _s_ represents the cursor's initial position, letter _t_ represents the last one. Then all possible positions of the cursor in the text editor are described by the following table. 123 12 123s567 1t345 One of the possible answers in the given sample is: "Left", "Down", "Left". | 1,600 | false | true | false | false | true | false | false | false | false | true | 8,822 |
1605B | Ashish has a binary string $$$s$$$ of length $$$n$$$ that he wants to sort in non-decreasing order. He can perform the following operation: 1. Choose a subsequence of any length such that its elements are in non-increasing order. Formally, choose any $$$k$$$ such that $$$1 leq k leq n$$$ and any sequence of $$$k$$$ indices $$$1 le i_1 lt i_2 lt ldots lt i_k le n$$$ such that $$$s_{i_1} ge s_{i_2} ge ldots ge s_{i_k}$$$. 2. Reverse this subsequence in-place. Formally, swap $$$s_{i_1}$$$ with $$$s_{i_k}$$$, swap $$$s_{i_2}$$$ with $$$s_{i_{k-1}}$$$, $$$ldots$$$ and swap $$$s_{i_{lfloor k/2 floor}}$$$ with $$$s_{i_{lceil k/2 ceil + 1}}$$$ (Here $$$lfloor x floor$$$ denotes the largest integer not exceeding $$$x$$$, and $$$lceil x ceil$$$ denotes the smallest integer not less than $$$x$$$) Find the minimum number of operations required to sort the string in non-decreasing order. It can be proven that it is always possible to sort the given binary string in at most $$$n$$$ operations. Input The first line contains a single integer $$$t$$$ $$$(1 le t le 1000)$$$ xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ $$$(1 le n le 1000)$$$ xa0— the length of the binary string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ containing only $$$0$$$s and $$$1$$$s. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Output For each test case output the following: The minimum number of operations $$$m$$$ in the first line ($$$0 le m le n$$$). Each of the following $$$m$$$ lines should be of the form: $$$k$$$ $$$i_1$$$ $$$i_2$$$ ... $$$i_{k}$$$, where $$$k$$$ is the length and $$$i_1 lt i_2 lt ... lt i_{k}$$$ are the indices of the chosen subsequence. For them the conditions from the statement must hold. Example Input 3 7 0011111 5 10100 6 001000 Output 0 1 4 1 3 4 5 1 3 3 5 6 Note In the first test case, the binary string is already sorted in non-decreasing order. In the second test case, we can perform the following operation: $$$k = 4:$$$ choose the indices $$${1, 3, 4, 5}$$$$$$underline{1}$$$ $$$0$$$ $$$underline{1}$$$ $$$underline{0}$$$ $$$underline{0}$$$ $$$ ightarrow $$$ $$$underline{0}$$$ $$$0$$$ $$$underline{0}$$$ $$$underline{1}$$$ $$$underline{1}$$$ In the third test case, we can perform the following operation: $$$k = 3:$$$ choose the indices $$${3, 5, 6}$$$$$$0$$$ $$$0$$$ $$$underline{1}$$$ $$$0$$$ $$$underline{0}$$$ $$$underline{0}$$$ $$$ ightarrow $$$ $$$0$$$ $$$0$$$ $$$underline{0}$$$ $$$0$$$ $$$underline{0}$$$ $$$underline{1}$$$ | 1,000 | false | true | false | false | false | false | false | false | true | false | 2,634 |
1644E | Consider a grid of size $$$n imes n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$. The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: Dxa0— move one cell down; Rxa0— move one cell right. The robot is not allowed to move outside the grid. You are given a sequence of moves $$$s$$$xa0— the initial path of the robot. This path doesn't lead the robot outside the grid. You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR. Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 le n le 10^8$$$)xa0— the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R,xa0— the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Note In the first testcase, it's enough to consider the following modified paths: RD $$$ ightarrow$$$ RRD $$$ ightarrow$$$ RRRD $$$ ightarrow$$$ RRRDD $$$ ightarrow$$$ RRRDDDxa0— this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$ ightarrow$$$ RRD $$$ ightarrow$$$ RRDD $$$ ightarrow$$$ RRDDDxa0— this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$ ightarrow$$$ RDD $$$ ightarrow$$$ RDDDxa0— this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$. In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR. In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$. Here are the cells for all testcases: | 1,900 | true | false | true | false | true | false | true | false | false | false | 2,397 |
1868E | Tom is waiting for his results of Zhongkao examination. To ease the tense atmosphere, his friend, Daniel, decided to play a game with him. This game is called "Divide the Array". The game is about the array a consisting of n integers. Denote [l,r] as the subsegment consisting of integers al,al+1,…,ar. Tom will divide the array into contiguous subsegments [l1,r1],[l2,r2],…,[lm,rm], such that each integer is in exactly one subsegment. More formally: For all 1≤i≤m, 1≤li≤ri≤n; l1=1, rm=n; For all 1<i≤m, li=ri−1+1. Denote si=∑k=liriak, that is, si is the sum of integers in the i-th subsegment. For all 1≤i≤j≤m, the following condition must hold: mini≤k≤jsk≤∑k=ijsk≤maxi≤k≤jsk. Tom believes that the more subsegments the array a is divided into, the better results he will get. So he asks Daniel to find the maximum number of subsegments among all possible ways to divide the array a. You have to help him find it. Input The first line of input contains a single integer t (1≤t≤50) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer n (1≤n≤300) — the length of the array a. The second line of each test case contains n integers a1,a2,…,an (−109≤ai≤109) — the elements of the array a. It is guaranteed that the sum of n over all test cases does not exceed 1000. Output For each test case, output a single integer — the maximum number of subsegments among all possible ways to divide the array a. Example 8 3 -1 5 4 2 2023 2043 6 1 4 7 -1 5 -4 5 -4 0 3 -18 10 1 998244853 10 -4 2 5 -10 4 8 2 9 -15 7 7 -7 3 8 -9 -2 2 4 4 -5 5 -2 -5 Note In the first test case, Daniel can divide the array into [−1] and [5,4], and s=[−1,9]. It can be shown that for any i=j, the condition in the statement is already satisfied, and for i=1,j=2, we have min(−1,9)≤(−1)+9≤max(−1,9). In the second test case, if Daniel divides the array into [2023] and [2043], then for i=1,j=2 we have 2023+2043>max(2023,2043), so the maximum number of subsegments is 1. In the third test case, the optimal way to divide the array is [1,4,7],[−1],[5,−4]. In the fourth test case, the optimal to divide the array way is [−4,0,3,−18],[10]. In the fifth test case, Daniel can only get one subsegment. | 3,500 | false | true | false | true | false | true | false | false | false | false | 1,063 |
1452G | Alice and Bob are playing a game. They have a tree consisting of $$$n$$$ vertices. Initially, Bob has $$$k$$$ chips, the $$$i$$$-th chip is located in the vertex $$$a_i$$$ (all these vertices are unique). Before the game starts, Alice will place a chip into one of the vertices of the tree. The game consists of turns. Each turn, the following events happen (sequentially, exactly in the following order): 1. Alice either moves her chip to an adjacent vertex or doesn't move it; 2. for each Bob's chip, he either moves it to an adjacent vertex or doesn't move it. Note that this choice is done independently for each chip. The game ends when Alice's chip shares the same vertex with one (or multiple) of Bob's chips. Note that Bob's chips may share the same vertex, even though they are in different vertices at the beginning of the game. Alice wants to maximize the number of turns, Bob wants to minimize it. If the game ends in the middle of some turn (Alice moves her chip to a vertex that contains one or multiple Bob's chips), this turn is counted. For each vertex, calculate the number of turns the game will last if Alice places her chip in that vertex. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of vertices in the tree. Then $$$n - 1$$$ lines follow, each line contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i e v_i$$$) that denote the endpoints of an edge. These edges form a tree. The next line contains one integer $$$k$$$ ($$$1 le k le n - 1$$$) — the number of Bob's chips. The last line contains $$$k$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_k$$$ ($$$1 le a_i le n$$$; $$$a_i e a_j$$$ if $$$i e j$$$) — the vertices where the Bob's chips are initially placed. Output Print $$$n$$$ integers. The $$$i$$$-th of them should be equal to the number of turns the game will last if Alice initially places her chip in the vertex $$$i$$$. If one of Bob's chips is already placed in vertex $$$i$$$, then the answer for vertex $$$i$$$ is $$$0$$$. Examples Input 5 2 4 3 1 3 4 3 5 2 4 5 Input 8 4 1 8 4 4 5 6 4 2 5 4 3 1 7 3 2 8 3 Input 10 2 5 4 3 7 3 7 2 5 8 3 6 8 10 7 9 7 1 4 10 6 9 1 Output 0 2 2 2 2 0 2 2 0 0 | 2,700 | false | true | false | false | true | false | false | false | false | false | 3,415 |
1737G | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Dytechlab Cup 2022 Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search data structures *3500 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST G. Ela Takes Dancing Class time limit per test4 seconds memory limit per test256 megabytes DTL engineers love partying in the weekend. Ela does, too! Unfortunately, she didn't know how to dance yet. Therefore, she decided to take a dancing class. There are $$$n$$$ students in the dancing class, including Ela. In the final project, $$$n$$$ students will participate in a choreography described below. $$$n$$$ students are positioned on the positive side of the $$$Ox$$$-axis. The $$$i$$$-th dancer is located at $$$a_i > 0$$$. Some dancers will change positions during the dance (we'll call them movable dancers), and others will stay in the same place during a choreography (we'll call them immovable dancers). We distinguish the dancers using a binary string $$$s$$$ of length $$$n$$$: if $$$s_i$$$ equals '1', then the $$$i$$$-th dancer is movable, otherwise the $$$i$$$-th dancer is immovable. Let's call the "positive energy value" of the choreography $$$d > 0$$$. The dancers will perform "movements" based on this value. Each minute after the dance begins, the movable dancer with the smallest $$$x$$$-coordinate will start moving to the right and initiate a "movement". At the beginning of the movement, the dancer's energy level will be initiated equally to the positive energy value of the choreography, which is $$$d$$$. Each time they move from some $$$y$$$ to $$$y+1$$$, the energy level will be decreased by $$$1$$$. At some point, the dancer might meet other fellow dancers in the same coordinates. If it happens, then the energy level of the dancer will be increased by $$$1$$$. A dancer will stop moving to the right when his energy level reaches $$$0$$$, and he doesn't share a position with another dancer. The dancers are very well-trained, and each "movement" will end before the next minute begins. To show her understanding of this choreography, Ela has to answer $$$q$$$ queries, each consisting of two integers $$$k$$$ and $$$m$$$. The answer to this query is the coordinate of the $$$m$$$-th dancer of both types from the left at $$$k$$$-th minute after the choreography begins. In other words, denote $$$x_{k, 1}, x_{k, 2}, dots, x_{k, n}$$$ as the sorted coordinates of the dancers at $$$k$$$-th minute from the beginning, you need to print $$$x_{k, m}$$$. Input The first line contains three integers $$$n$$$, $$$d$$$ and $$$q$$$ ($$$1 le n le 10^5$$$; $$$1 le d le 10^9$$$; $$$1 le q le 10^5$$$) — the number of dancers, the positive energy value of the choreography, and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_1 < a_2 < dots < a_n le 10^9$$$) — the coordinates of each dancer. The third line contains a binary string $$$s$$$ of length $$$n$$$ — the movability of each dancer. Each character is either '0' or '1'. It is guaranteed that $$$s$$$ contains at least one character '1'. Then $$$q$$$ lines follow, the $$$i$$$-th of them contains two integers $$$k_i$$$ and $$$m_i$$$ ($$$1 le k_i le 10^9$$$, $$$1 le m_i le n$$$) — the content of each query. Output Output $$$q$$$ lines, each contains a single integer — the answer for the corresponding query. Examples input 4 3 8 1 3 6 7 1011 1 1 1 2 1 3 1 4 2 1 2 2 2 3 2 4 output 3 5 6 7 3 6 7 10 input 1 1 5 2 1 1 1 2 1 3 1 4 1 5 1 output 3 4 5 6 7 Note Let's consider the first example test case. In the first minute, $$$1$$$ is the lowest coordinate between movable dancers. The energy level is initiated with $$$3$$$. Then the following happens: The dancer moves from $$$1$$$ to $$$2$$$. The energy level decreased to $$$2$$$. The dancer moves from $$$2$$$ to $$$3$$$. The energy level decreased to $$$1$$$, then increased to $$$2$$$ when she met another dancer at $$$3$$$. The dancer moves from $$$3$$$ to $$$4$$$. The energy level decreased to $$$1$$$. The dancer moves from $$$4$$$ to $$$5$$$. The energy level decreased to $$$0$$$. At the end of the first minute, the sorted coordinates of the dancers become $$$[3, 5, 6, 7]$$$, and their respective movability is '0111'. In the second minute, $$$5$$$ is the lowest coordinate between movable dancers. The energy level is initiated with $$$3$$$. Then the following happens: The dancer moves from $$$5$$$ to $$$6$$$. The energy level decreased to $$$2$$$, then increased to $$$3$$$ when she met another dancer at $$$6$$$. The dancer moves from $$$6$$$ to $$$7$$$. The energy level decreased to $$$2$$$, then increased to $$$3$$$ when she met another dancer at $$$7$$$. The dancer moves from $$$7$$$ to $$$8$$$. The energy level decreased to $$$2$$$. The dancer moves from $$$8$$$ to $$$9$$$. The energy level decreased to $$$1$$$. The dancer moves from $$$9$$$ to $$$10$$$. The energy level decreased to $$$0$$$. At the end of the second minute, the sorted coordinates of the dancers become $$$[3, 6, 7, 10]$$$, and their respective movability is '0111'. Codeforces (c) | 3,500 | false | false | false | false | true | false | false | true | false | false | 1,862 |
225D | Let us remind you the rules of a very popular game called "Snake" (or sometimes "Boa", "Python" or "Worm"). The game field is represented by an _n_u2009×u2009_m_ rectangular table. Some squares of the field are considered impassable (walls), all other squares of the fields are passable. You control a snake, the snake consists of segments. Each segment takes up exactly one passable square of the field, but any passable square contains at most one segment. All segments are indexed by integers from 1 to _k_, where _k_ is the snake's length. The 1-th segment is the head and the _k_-th segment is the tail. For any _i_ (1u2009≤u2009_i_u2009<u2009_k_), segments with indexes _i_ and _i_u2009+u20091 are located in the adjacent squares of the field, that is, these squares share a common side. One of the passable field squares contains an apple. The snake's aim is to reach the apple and eat it (that is, to position its head in the square with the apple). The snake moves throughout the game. During one move the snake can move its head to an adjacent field square. All other segments follow the head. That is, each segment number _i_ (1u2009<u2009_i_u2009≤u2009_k_) moves to the square that has just had segment number _i_u2009-u20091. Consider that all segments including the head move simultaneously (see the second test sample). If the snake's head moves to an unpassable square or to the square, occupied by its other segment, the snake dies. That's why we will consider such moves unvalid. Your task is to determine the minimum number of valid moves that the snake needs to reach the apple. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u200915) — the number of rows and columns of the game field. Next _n_ lines describe the game field. Each of these lines contains _m_ characters. Character "#" represents a wall, "." is a passable square, "@" is an apple. The snake's first segment is represented by character "1", the second one segment — by character "2" and so on. The game field description doesn't contain any characters besides "#', ".", "@" and digits (except 0). It is guaranteed that the described field is correct. It is guaranteed that the described field contains exactly one apple and exactly one snake, the snake's length is at least 3 and at most 9. | 2,200 | false | false | true | false | false | false | false | false | false | true | 8,937 |
1187C | Vasya has an array $$$a_1, a_2, dots, a_n$$$. You don't know this array, but he told you $$$m$$$ facts about this array. The $$$i$$$-th fact is a triple of numbers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 le t_i le 1, 1 le l_i < r_i le n$$$) and it means: if $$$t_i=1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, dots, a_{r_i}$$$ is sorted in non-decreasing order; if $$$t_i=0$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, dots, a_{r_i}$$$ is not sorted in non-decreasing order. A subarray is not sorted if there is at least one pair of consecutive elements in this subarray such that the former is greater than the latter. For example if $$$a = [2, 1, 1, 3, 2]$$$ then he could give you three facts: $$$t_1=1, l_1=2, r_1=4$$$ (the subarray $$$[a_2, a_3, a_4] = [1, 1, 3]$$$ is sorted), $$$t_2=0, l_2=4, r_2=5$$$ (the subarray $$$[a_4, a_5] = [3, 2]$$$ is not sorted), and $$$t_3=0, l_3=3, r_3=5$$$ (the subarray $$$[a_3, a_5] = [1, 3, 2]$$$ is not sorted). You don't know the array $$$a$$$. Find any array which satisfies all the given facts. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 1000, 1 le m le 1000$$$). Each of the next $$$m$$$ lines contains three integers $$$t_i$$$, $$$l_i$$$ and $$$r_i$$$ ($$$0 le t_i le 1, 1 le l_i < r_i le n$$$). If $$$t_i = 1$$$ then subbarray $$$a_{l_i}, a_{l_i + 1}, dots , a_{r_i}$$$ is sorted. Otherwise (if $$$t_i = 0$$$) subbarray $$$a_{l_i}, a_{l_i + 1}, dots , a_{r_i}$$$ is not sorted. Output If there is no array that satisfies these facts in only line print NO (in any letter case). If there is a solution, print YES (in any letter case). In second line print $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) — the array $$$a$$$, satisfying all the given facts. If there are multiple satisfying arrays you can print any of them. | 1,800 | false | true | true | false | false | true | false | false | false | false | 4,764 |
630C | Problem - 630C - Codeforces =============== xa0 ]( "Experimental Educational Round: VolBIT Formulas Blitz") — the maximum length of a number that a door-plate can hold. Output Output one integer — the maximum number of offices, than can have unique lucky numbers not longer than _n_ digits. Examples Input 2 Output 6 | 1,100 | true | false | false | false | false | false | false | false | false | false | 7,302 |
936A | Julia is going to cook a chicken in the kitchen of her dormitory. To save energy, the stove in the kitchen automatically turns off after _k_ minutes after turning on. During cooking, Julia goes to the kitchen every _d_ minutes and turns on the stove if it is turned off. While the cooker is turned off, it stays warm. The stove switches on and off instantly. It is known that the chicken needs _t_ minutes to be cooked on the stove, if it is turned on, and 2_t_ minutes, if it is turned off. You need to find out, how much time will Julia have to cook the chicken, if it is considered that the chicken is cooked evenly, with constant speed when the stove is turned on and at a constant speed when it is turned off. Input The single line contains three integers _k_, _d_ and _t_ (1u2009≤u2009_k_,u2009_d_,u2009_t_u2009≤u20091018). Output Print a single number, the total time of cooking in minutes. The relative or absolute error must not exceed 10u2009-u20099. Namely, let's assume that your answer is _x_ and the answer of the jury is _y_. The checker program will consider your answer correct if . Note In the first example, the chicken will be cooked for 3 minutes on the turned on stove, after this it will be cooked for . Then the chicken will be cooked for one minute on a turned off stove, it will be cooked for . Thus, after four minutes the chicken will be cooked for . Before the fifth minute Julia will turn on the stove and after 2.5 minutes the chicken will be ready . In the second example, when the stove is turned off, Julia will immediately turn it on, so the stove will always be turned on and the chicken will be cooked in 20 minutes. | 1,700 | true | false | true | false | false | false | false | true | false | false | 5,958 |
1742E | Timur has a stairway with $$$n$$$ steps. The $$$i$$$-th step is $$$a_i$$$ meters higher than its predecessor. The first step is $$$a_1$$$ meters higher than the ground, and the ground starts at $$$0$$$ meters. The stairs for the first test case. Timur has $$$q$$$ questions, each denoted by an integer $$$k_1, dots, k_q$$$. For each question $$$k_i$$$, you have to print the maximum possible height Timur can achieve by climbing the steps if his legs are of length $$$k_i$$$. Timur can only climb the $$$j$$$-th step if his legs are of length at least $$$a_j$$$. In other words, $$$k_i geq a_j$$$ for each step $$$j$$$ climbed. Note that you should answer each question independently. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n, q$$$ ($$$1 leq n, q leq 2cdot10^5$$$)xa0— the number of steps and the number of questions, respectively. The second line of each test case contains $$$n$$$ integers ($$$1 leq a_i leq 10^9$$$)xa0— the height of the steps. The third line of each test case contains $$$q$$$ integers ($$$0 leq k_i leq 10^9$$$)xa0— the numbers for each question. It is guaranteed that the sum of $$$n$$$ does not exceed $$$2cdot10^5$$$, and the sum of $$$q$$$ does not exceed $$$2cdot10^5$$$. Output For each test case, output a single line containing $$$q$$$ integers, the answer for each question. Please note, that the answer for some questions won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). Example Input 3 4 5 1 2 1 5 1 2 4 9 10 2 2 1 1 0 1 3 1 1000000000 1000000000 1000000000 1000000000 Output 1 4 4 9 9 0 2 3000000000 Note Consider the first test case, pictured in the statement. If Timur's legs have length $$$1$$$, then he can only climb stair $$$1$$$, so the highest he can reach is $$$1$$$ meter. If Timur's legs have length $$$2$$$ or $$$4$$$, then he can only climb stairs $$$1$$$, $$$2$$$, and $$$3$$$, so the highest he can reach is $$$1+2+1=4$$$ meters. If Timur's legs have length $$$9$$$ or $$$10$$$, then he can climb the whole staircase, so the highest he can reach is $$$1+2+1+5=9$$$ meters. In the first question of the second test case, Timur has no legs, so he cannot go up even a single step. :( | 1,200 | true | true | false | false | false | false | false | true | false | false | 1,827 |
1015E2 | A star is a figure of the following type: an asterisk character '*' in the center of the figure and four rays (to the left, right, top, bottom) of the same positive length. The size of a star is the length of its rays. The size of a star must be a positive number (i.e. rays of length $$$0$$$ are not allowed). Let's consider empty cells are denoted by '.', then the following figures are stars: The leftmost figure is a star of size $$$1$$$, the middle figure is a star of size $$$2$$$ and the rightmost figure is a star of size $$$3$$$. You are given a rectangular grid of size $$$n imes m$$$ consisting only of asterisks '*' and periods (dots) '.'. Rows are numbered from $$$1$$$ to $$$n$$$, columns are numbered from $$$1$$$ to $$$m$$$. Your task is to draw this grid using any number of stars or find out that it is impossible. Stars can intersect, overlap or even coincide with each other. The number of stars in the output can't exceed $$$n cdot m$$$. Each star should be completely inside the grid. You can use stars of same and arbitrary sizes. In this problem, you do not need to minimize the number of stars. Just find any way to draw the given grid with at most $$$n cdot m$$$ stars. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n, m le 1000$$$) — the sizes of the given grid. The next $$$n$$$ lines contains $$$m$$$ characters each, the $$$i$$$-th line describes the $$$i$$$-th row of the grid. It is guaranteed that grid consists of characters '*' and '.' only. Output If it is impossible to draw the given grid using stars only, print "-1". Otherwise in the first line print one integer $$$k$$$ ($$$0 le k le n cdot m$$$) — the number of stars needed to draw the given grid. The next $$$k$$$ lines should contain three integers each — $$$x_j$$$, $$$y_j$$$ and $$$s_j$$$, where $$$x_j$$$ is the row index of the central star character, $$$y_j$$$ is the column index of the central star character and $$$s_j$$$ is the size of the star. Each star should be completely inside the grid. Examples Input 6 8 ....*... ...**... ..*****. ...**... ....*... ........ Input 5 5 .*... ****. .*** ..**. ..... Input 5 5 .*... ***.. .*... .*... ..... Note In the first example the output 2 3 4 1 3 5 2 is also correct. | 1,900 | false | true | false | true | false | false | false | true | false | false | 5,614 |
119A | Simon and Antisimon play a game. Initially each player receives one fixed positive integer that doesn't change throughout the game. Simon receives number _a_ and Antisimon receives number _b_. They also have a heap of _n_ stones. The players take turns to make a move and Simon starts. During a move a player should take from the heap the number of stones equal to the greatest common divisor of the fixed number he has received and the number of stones left in the heap. A player loses when he cannot take the required number of stones (i. e. the heap has strictly less stones left than one needs to take). Your task is to determine by the given _a_, _b_ and _n_ who wins the game. Input The only string contains space-separated integers _a_, _b_ and _n_ (1u2009≤u2009_a_,u2009_b_,u2009_n_u2009≤u2009100) — the fixed numbers Simon and Antisimon have received correspondingly and the initial number of stones in the pile. Output If Simon wins, print "0" (without the quotes), otherwise print "1" (without the quotes). Note The greatest common divisor of two non-negative integers _a_ and _b_ is such maximum positive integer _k_, that _a_ is divisible by _k_ without remainder and similarly, _b_ is divisible by _k_ without remainder. Let _gcd_(_a_,u2009_b_) represent the operation of calculating the greatest common divisor of numbers _a_ and _b_. Specifically, _gcd_(_x_,u20090)u2009=u2009_gcd_(0,u2009_x_)u2009=u2009_x_. In the first sample the game will go like that: Simon should take _gcd_(3,u20099)u2009=u20093 stones from the heap. After his move the heap has 6 stones left. Antisimon should take _gcd_(5,u20096)u2009=u20091 stone from the heap. After his move the heap has 5 stones left. Simon should take _gcd_(3,u20095)u2009=u20091 stone from the heap. After his move the heap has 4 stones left. Antisimon should take _gcd_(5,u20094)u2009=u20091 stone from the heap. After his move the heap has 3 stones left. Simon should take _gcd_(3,u20093)u2009=u20093 stones from the heap. After his move the heap has 0 stones left. Antisimon should take _gcd_(5,u20090)u2009=u20095 stones from the heap. As 0u2009<u20095, it is impossible and Antisimon loses. In the second sample each player during each move takes one stone from the heap. As _n_ is even, Antisimon takes the last stone and Simon can't make a move after that. | 800 | false | false | true | false | false | false | false | false | false | false | 9,411 |
1987A | Oh no, the ForceCodes servers are running out of memory! Luckily, you can help them out by uploading some of your RAM! You want to upload $$$n$$$ GBs of RAM. Every second, you will upload either $$$0$$$ or $$$1$$$ GB of RAM. However, there is a restriction on your network speed: in any $$$k$$$ consecutive seconds, you can upload only at most $$$1$$$ GB of RAM in total. Find the minimum number of seconds needed to upload $$$n$$$ GBs of RAM! Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 100$$$)xa0— the number of GBs that you want to upload and the length of the time window respectively. Output For each test case, output a single integerxa0— the minimum number of seconds needed to upload $$$n$$$ GBs of RAM. Example Input 6 5 1 2 2 2 3 1 7 11 5 100 100 Note In the first test case, you can upload $$$1$$$ GB of RAM per second, so to upload $$$5$$$ GBs, you need $$$5$$$ seconds. In the second test case, you can upload $$$1$$$ GB in the first second, $$$0$$$ GBs in the second second, and $$$1$$$ GB in the third second, which in total adds up to exactly $$$2$$$ GBs of uploaded RAM. In the third test case, you can upload $$$1$$$ GB in the first second, $$$0$$$ GBs in the second second, $$$0$$$ GBs in the third second, and $$$1$$$ GB in the fourth second, which in total adds up to exactly $$$2$$$ GBs of uploaded RAM. | 800 | true | true | false | false | false | false | false | false | false | false | 357 |
1351C | Problem - 1351C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures implementation *1400 No tag edit access → Contest materials ") . It is known that if he moves along a previously unvisited segment of a path (i.e. this segment of the path is visited the first time), then the time of such movement is $$$5$$$ seconds. If he rolls along previously visited segment of a path (i.e., this segment of the path has been covered by his path before), then it takes $$$1$$$ second. Find the skier's time to roll all the path. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each set is given by one nonempty string of the characters 'S', 'N', 'W', 'E'. The length of the string does not exceed $$$10^5$$$ characters. The sum of the lengths of $$$t$$$ given lines over all test cases in the input does not exceed $$$10^5$$$. Output For each test case, print the desired path time in seconds. Example Input 5 NNN NS WWEN WWEE NWNWS Output 15 6 16 12 25 | 1,400 | false | false | true | false | true | false | false | false | false | false | 3,953 |
1220F | Gardener Alex loves to grow trees. We remind that tree is a connected acyclic graph on $$$n$$$ vertices. Today he decided to grow a rooted binary tree. A binary tree is a tree where any vertex has no more than two sons. Luckily, Alex has a permutation of numbers from $$$1$$$ to $$$n$$$ which he was presented at his last birthday, so he decided to grow a tree according to this permutation. To do so he does the following process: he finds a minimum element and makes it a root of the tree. After that permutation is divided into two parts: everything that is to the left of the minimum element, and everything that is to the right. The minimum element on the left part becomes the left son of the root, and the minimum element on the right part becomes the right son of the root. After that, this process is repeated recursively on both parts. Now Alex wants to grow a forest of trees: one tree for each cyclic shift of the permutation. He is interested in what cyclic shift gives the tree of minimum depth. Unfortunately, growing a forest is a hard and long process, but Alex wants the answer right now. Will you help him? We remind that cyclic shift of permutation $$$a_1, a_2, ldots, a_k, ldots, a_n$$$ for $$$k$$$ elements to the left is the permutation $$$a_{k + 1}, a_{k + 2}, ldots, a_n, a_1, a_2, ldots, a_k$$$. Input First line contains an integer number $$$n ~ (1 leqslant n leqslant 200,000)$$$ — length of the permutation. Second line contains $$$n$$$ integer numbers $$$a_1, a_2, ldots, a_n ~ (1 leqslant a_i leqslant n)$$$, and it is guaranteed that all numbers occur exactly one time. Output Print two numbers separated with space: minimum possible depth of a tree and how many elements we need to shift left to achieve this depth. The number of elements should be a number from $$$0$$$ to $$$n - 1$$$. If there are several possible answers, print any of them. | 2,700 | false | false | false | false | true | false | false | true | false | false | 4,568 |
1923D | There are $$$n$$$ slimes placed in a line. The slimes are numbered from $$$1$$$ to $$$n$$$ in order from left to right. The size of the $$$i$$$-th slime is $$$a_i$$$. Every second, the following happens: exactly one slime eats one of its neighbors and increases its size by the eaten neighbor's size. A slime can eat its neighbor only if it is strictly bigger than this neighbor. If there is no slime which is strictly bigger than one of its neighbors, the process ends. For example, suppose $$$n = 5$$$, $$$a = [2, 2, 3, 1, 4]$$$. The process can go as follows: first, the $$$3$$$-rd slime eats the $$$2$$$-nd slime. The size of the $$$3$$$-rd slime becomes $$$5$$$, the $$$2$$$-nd slime is eaten. then, the $$$3$$$-rd slime eats the $$$1$$$-st slime (they are neighbors since the $$$2$$$-nd slime is already eaten). The size of the $$$3$$$-rd slime becomes $$$7$$$, the $$$1$$$-st slime is eaten. then, the $$$5$$$-th slime eats the $$$4$$$-th slime. The size of the $$$5$$$-th slime becomes $$$5$$$, the $$$4$$$-th slime is eaten. then, the $$$3$$$-rd slime eats the $$$5$$$-th slime (they are neighbors since the $$$4$$$-th slime is already eaten). The size of the $$$3$$$-rd slime becomes $$$12$$$, the $$$5$$$-th slime is eaten. For each slime, calculate the minimum number of seconds it takes for this slime to be eaten by another slime (among all possible ways the process can go), or report that it is impossible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the number of slimes. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). The sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers. The $$$i$$$-th integer should be equal to the minimum number of seconds it takes for the $$$i$$$-th slime to be eaten by another slime or -1 if it is impossible. Example Input 4 4 3 2 4 2 3 1 2 3 5 2 2 3 1 1 7 4 2 3 6 1 1 8 Output 2 1 2 1 1 1 -1 2 1 -1 1 2 2 1 1 3 1 1 4 | 1,800 | false | true | false | false | true | true | false | true | false | false | 749 |
1404C | Let $$$a_1, ldots, a_n$$$ be an array of $$$n$$$ positive integers. In one operation, you can choose an index $$$i$$$ such that $$$a_i = i$$$, and remove $$$a_i$$$ from the array (after the removal, the remaining parts are concatenated). The weight of $$$a$$$ is defined as the maximum number of elements you can remove. You must answer $$$q$$$ independent queries $$$(x, y)$$$: after replacing the $$$x$$$ first elements of $$$a$$$ and the $$$y$$$ last elements of $$$a$$$ by $$$n+1$$$ (making them impossible to remove), what would be the weight of $$$a$$$? Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 3 cdot 10^5$$$) xa0— the length of the array and the number of queries. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 leq a_i leq n$$$)xa0— elements of the array. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$x, y ge 0$$$ and $$$x+y < n$$$). Output Print $$$q$$$ lines, $$$i$$$-th line should contain a single integer xa0— the answer to the $$$i$$$-th query. Examples Input 13 5 2 2 3 9 5 4 6 5 7 8 3 11 13 3 1 0 0 2 4 5 0 0 12 Input 5 2 1 4 1 2 4 0 0 1 0 Note Explanation of the first query: After making first $$$x = 3$$$ and last $$$y = 1$$$ elements impossible to remove, $$$a$$$ becomes $$$[ imes, imes, imes, 9, 5, 4, 6, 5, 7, 8, 3, 11, imes]$$$ (we represent $$$14$$$ as $$$ imes$$$ for clarity). Here is a strategy that removes $$$5$$$ elements (the element removed is colored in red): $$$[ imes, imes, imes, 9, color{red}{5}, 4, 6, 5, 7, 8, 3, 11, imes]$$$ $$$[ imes, imes, imes, 9, 4, 6, 5, 7, 8, 3, color{red}{11}, imes]$$$ $$$[ imes, imes, imes, 9, 4, color{red}{6}, 5, 7, 8, 3, imes]$$$ $$$[ imes, imes, imes, 9, 4, 5, 7, color{red}{8}, 3, imes]$$$ $$$[ imes, imes, imes, 9, 4, 5, color{red}{7}, 3, imes]$$$ $$$[ imes, imes, imes, 9, 4, 5, 3, imes]$$$ (final state) It is impossible to remove more than $$$5$$$ elements, hence the weight is $$$5$$$. | 2,300 | false | true | false | false | true | true | false | true | false | false | 3,651 |
1009G | Polycarp has just launched his new startup idea. The niche is pretty free and the key vector of development sounds really promising, so he easily found himself some investors ready to sponsor the company. However, he is yet to name the startup! Actually, Polycarp has already came up with the name but some improvement to it will never hurt. So now he wants to swap letters at some positions in it to obtain the better name. It isn't necessary for letters to be adjacent. In addition, each of the investors has chosen some index in the name and selected a set of letters that can go there. Indices chosen by different investors are pairwise distinct. If some indices aren't chosen by any investor then any letter can go there. Finally, Polycarp is sure that the smallest lexicographically name is the best. (Like why do you think Google decided to become Alphabet?) More formally, you are given a string consisting of lowercase Latin letters from "a" to "f". You can swap letters at any positions arbitrary number of times (zero swaps is also possible). What is the smallest lexicographically name you can obtain such that the letter at every position is among the allowed letters? If Polycarp can't produce any valid name then print "Impossible". Input The first line is the string $$$s$$$ ($$$1 le s le 10^5$$$) — the name Polycarp has came up with. The string consists only of lowercase Latin letters from "a" to "f". The second line contains a single integer $$$m$$$ ($$$0 le m le s$$$) — the number of investors. The $$$i$$$-th of the next $$$m$$$ lines contain an integer number $$$pos_i$$$ and a non-empty string of allowed characters for $$$pos_i$$$ ($$$1 le pos_i le s$$$). Each string contains pairwise distinct letters from "a" to "f". $$$pos_1, pos_2, dots, pos_m$$$ are pairwise distinct. If any position of the string doesn't appear in the investors demands then any letter can go in this position. Output If Polycarp can't produce any valid name then print "Impossible". Otherwise print the smallest lexicographically name Polycarp can obtain by swapping letters in string $$$s$$$ such that the letter at every position is among the allowed ones. | 2,400 | false | true | false | false | false | false | false | false | false | true | 5,636 |
1073C | Vasya has got a robot which is situated on an infinite Cartesian plane, initially in the cell $$$(0, 0)$$$. Robot can perform the following four kinds of operations: U — move from $$$(x, y)$$$ to $$$(x, y + 1)$$$; D — move from $$$(x, y)$$$ to $$$(x, y - 1)$$$; L — move from $$$(x, y)$$$ to $$$(x - 1, y)$$$; R — move from $$$(x, y)$$$ to $$$(x + 1, y)$$$. Vasya also has got a sequence of $$$n$$$ operations. Vasya wants to modify this sequence so after performing it the robot will end up in $$$(x, y)$$$. Vasya wants to change the sequence so the length of changed subsegment is minimum possible. This length can be calculated as follows: $$$maxID - minID + 1$$$, where $$$maxID$$$ is the maximum index of a changed operation, and $$$minID$$$ is the minimum index of a changed operation. For example, if Vasya changes RRRRRRR to RLRRLRL, then the operations with indices $$$2$$$, $$$5$$$ and $$$7$$$ are changed, so the length of changed subsegment is $$$7 - 2 + 1 = 6$$$. Another example: if Vasya changes DDDD to DDRD, then the length of changed subsegment is $$$1$$$. If there are no changes, then the length of changed subsegment is $$$0$$$. Changing an operation means replacing it with some operation (possibly the same); Vasya can't insert new operations into the sequence or remove them. Help Vasya! Tell him the minimum length of subsegment that he needs to change so that the robot will go from $$$(0, 0)$$$ to $$$(x, y)$$$, or tell him that it's impossible. Input The first line contains one integer number $$$n~(1 le n le 2 cdot 10^5)$$$ — the number of operations. The second line contains the sequence of operations — a string of $$$n$$$ characters. Each character is either U, D, L or R. The third line contains two integers $$$x, y~(-10^9 le x, y le 10^9)$$$ — the coordinates of the cell where the robot should end its path. Output Print one integer — the minimum possible length of subsegment that can be changed so the resulting sequence of operations moves the robot from $$$(0, 0)$$$ to $$$(x, y)$$$. If this change is impossible, print $$$-1$$$. Note In the first example the sequence can be changed to LULUU. So the length of the changed subsegment is $$$3 - 1 + 1 = 3$$$. In the second example the given sequence already leads the robot to $$$(x, y)$$$, so the length of the changed subsegment is $$$0$$$. In the third example the robot can't end his path in the cell $$$(x, y)$$$. | 1,800 | false | false | false | false | false | false | false | true | false | false | 5,351 |
1873E | You love fish, that's why you have decided to build an aquarium. You have a piece of coral made of $$$n$$$ columns, the $$$i$$$-th of which is $$$a_i$$$ units tall. Afterwards, you will build a tank around the coral as follows: Pick an integer $$$h geq 1$$$xa0— the height of the tank. Build walls of height $$$h$$$ on either side of the tank. Then, fill the tank up with water so that the height of each column is $$$h$$$, unless the coral is taller than $$$h$$$; then no water should be added to this column. For example, with $$$a=[3,1,2,4,6,2,5]$$$ and a height of $$$h=4$$$, you will end up using a total of $$$w=8$$$ units of water, as shown. You can use at most $$$x$$$ units of water to fill up the tank, but you want to build the biggest tank possible. What is the largest value of $$$h$$$ you can select? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains two positive integers $$$n$$$ and $$$x$$$ ($$$1 leq n leq 2 cdot 10^5$$$; $$$1 leq x leq 10^9$$$)xa0— the number of columns of the coral and the maximum amount of water you can use. The second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the heights of the coral. The sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output a single positive integer $$$h$$$ ($$$h geq 1$$$)xa0— the maximum height the tank can have, so you need at most $$$x$$$ units of water to fill up the tank. We have a proof that under these constraints, such a value of $$$h$$$ always exists. Example Input 5 7 9 3 1 2 4 6 2 5 3 10 1 1 1 4 1 1 4 3 4 6 1984 2 6 5 9 1 8 1 1000000000 1 Output 4 4 2 335 1000000001 Note The first test case is pictured in the statement. With $$$h=4$$$ we need $$$8$$$ units of water, but if $$$h$$$ is increased to $$$5$$$ we need $$$13$$$ units of water, which is more than $$$x=9$$$. So $$$h=4$$$ is optimal. In the second test case, we can pick $$$h=4$$$ and add $$$3$$$ units to each column, using a total of $$$9$$$ units of water. It can be shown that this is optimal. In the third test case, we can pick $$$h=2$$$ and use all of our water, so it is optimal. | 1,100 | false | false | false | false | false | false | false | true | true | false | 1,040 |
1472D | During their New Year holidays, Alice and Bob play the following game using an array $$$a$$$ of $$$n$$$ integers: Players take turns, Alice moves first. Each turn a player chooses any element and removes it from the array. If Alice chooses even value, then she adds it to her score. If the chosen value is odd, Alice's score does not change. Similarly, if Bob chooses odd value, then he adds it to his score. If the chosen value is even, then Bob's score does not change. If there are no numbers left in the array, then the game ends. The player with the highest score wins. If the scores of the players are equal, then a draw is declared. For example, if $$$n = 4$$$ and $$$a = [5, 2, 7, 3]$$$, then the game could go as follows (there are other options): On the first move, Alice chooses $$$2$$$ and get two points. Her score is now $$$2$$$. The array $$$a$$$ is now $$$[5, 7, 3]$$$. On the second move, Bob chooses $$$5$$$ and get five points. His score is now $$$5$$$. The array $$$a$$$ is now $$$[7, 3]$$$. On the third move, Alice chooses $$$7$$$ and get no points. Her score is now $$$2$$$. The array $$$a$$$ is now $$$[3]$$$. On the last move, Bob chooses $$$3$$$ and get three points. His score is now $$$8$$$. The array $$$a$$$ is empty now. Since Bob has more points at the end of the game, he is the winner. You want to find out who will win if both players play optimally. Note that there may be duplicate numbers in the array. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of elements in the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the array $$$a$$$ used to play the game. 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 on a separate line: "Alice" if Alice wins with the optimal play; "Bob" if Bob wins with the optimal play; "Tie", if a tie is declared during the optimal play. Example Input 4 4 5 2 7 3 3 3 2 1 4 2 2 2 2 2 7 8 Output Bob Tie Alice Alice | 1,200 | false | true | false | true | false | false | false | false | true | false | 3,321 |
105E | You are given a straight half-line divided into segments of unit length, which we will call positions. The positions are numbered by positive integers that start with 1 from the end of half-line, i. e. 1, 2, 3 and so on. The distance between the positions is the absolute difference between the respective numbers. Laharl, Etna and Flonne occupy some positions on the half-line and they want to get to the position with the largest possible number. They are originally placed in different positions. Each of the characters can perform each of the following actions no more than once: Move a certain distance. Grab another character and lift him above the head. Throw the lifted character a certain distance. Each character has a movement range parameter. They can only move to free positions, assuming that distance between those positions doesn't exceed the movement range. One character can lift another character if the distance between the two characters equals 1, and no one already holds that another character. We can assume that the lifted character moves to the same position as the person who has lifted him, and the position in which he stood before becomes free. A lifted character cannot perform any actions and the character that holds him cannot walk. Also, each character has a throwing range parameter. It is the distance at which this character can throw the one lifted above his head. He can only throw a character to a free position, and only when there is a lifted character. We accept the situation when one person grabs another one who in his turn has the third character in his hands. This forms a "column" of three characters. For example, Laharl can hold Etna while Etna holds Flonne. In this case, Etna and the Flonne cannot perform any actions, and Laharl can only throw Etna (together with Flonne) at some distance. Laharl, Etna and Flonne perform actions in any order. They perform actions in turns, that is no two of them can do actions at the same time. Determine the maximum number of position at least one of the characters can reach. That is, such maximal number _x_ so that one of the characters can reach position _x_. Input The first line contains three integers: Laharl's position, his movement range and throwing range. The second and the third lines describe Etna's and Flonne's parameters correspondingly in the similar form. It is guaranteed that the three characters occupy distinct positions. All numbers in the input are between 1 and 10, inclusive. Note Let us explain how to reach position 15 in the sample. Initially Laharl occupies position 9, Etna — position 4 and Flonne — position 2. First Laharl moves to position 6. Then Flonne moves to position 5 and grabs Etna. Laharl grabs Flonne and throws to position 9. Flonne throws Etna to position 12. Etna moves to position 15. | 2,500 | false | false | false | false | false | false | true | false | false | false | 9,462 |
255B | Little Vitaly loves different algorithms. Today he has invented a new algorithm just for you. Vitaly's algorithm works with string _s_, consisting of characters "x" and "y", and uses two following operations at runtime: 1. Find two consecutive characters in the string, such that the first of them equals "y", and the second one equals "x" and swap them. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string. 2. Find in the string two consecutive characters, such that the first of them equals "x" and the second one equals "y". Remove these characters from the string. If there are several suitable pairs of characters, we choose the pair of characters that is located closer to the beginning of the string. The input for the new algorithm is string _s_, and the algorithm works as follows: 1. If you can apply at least one of the described operations to the string, go to step 2 of the algorithm. Otherwise, stop executing the algorithm and print the current string. 2. If you can apply operation 1, then apply it. Otherwise, apply operation 2. After you apply the operation, go to step 1 of the algorithm. Now Vitaly wonders, what is going to be printed as the result of the algorithm's work, if the input receives string _s_. Input The first line contains a non-empty string _s_. It is guaranteed that the string only consists of characters "x" and "y". It is guaranteed that the string consists of at most 106 characters. It is guaranteed that as the result of the algorithm's execution won't be an empty string. Output In the only line print the string that is printed as the result of the algorithm's work, if the input of the algorithm input receives string _s_. Note In the first test the algorithm will end after the first step of the algorithm, as it is impossible to apply any operation. Thus, the string won't change. In the second test the transformation will be like this: 1. string "yxyxy" transforms into string "xyyxy"; 2. string "xyyxy" transforms into string "xyxyy"; 3. string "xyxyy" transforms into string "xxyyy"; 4. string "xxyyy" transforms into string "xyy"; 5. string "xyy" transforms into string "y". As a result, we've got string "y". In the third test case only one transformation will take place: string "xxxxxy" transforms into string "xxxx". Thus, the answer will be string "xxxx". | 1,200 | false | false | true | false | false | false | false | false | false | false | 8,813 |
1658C | Shinju loves permutations very much! Today, she has borrowed a permutation $$$p$$$ from Juju to play with. The $$$i$$$-th cyclic shift of a permutation $$$p$$$ is a transformation on the permutation such that $$$p = [p_1, p_2, ldots, p_n] $$$ will now become $$$ p = [p_{n-i+1}, ldots, p_n, p_1,p_2, ldots, p_{n-i}]$$$. Let's define the power of permutation $$$p$$$ as the number of distinct elements in the prefix maximums array $$$b$$$ of the permutation. The prefix maximums array $$$b$$$ is the array of length $$$n$$$ such that $$$b_i = max(p_1, p_2, ldots, p_i)$$$. For example, the power of $$$[1, 2, 5, 4, 6, 3]$$$ is $$$4$$$ since $$$b=[1,2,5,5,6,6]$$$ and there are $$$4$$$ distinct elements in $$$b$$$. Unfortunately, Shinju has lost the permutation $$$p$$$! The only information she remembers is an array $$$c$$$, where $$$c_i$$$ is the power of the $$$(i-1)$$$-th cyclic shift of the permutation $$$p$$$. She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough. Given the array $$$c$$$, determine if there exists a permutation $$$p$$$ that is consistent with $$$c$$$. You do not have to construct the permutation $$$p$$$. 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). Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5 cdot 10^3$$$) — the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$c_1,c_2,ldots,c_n$$$ ($$$1 leq c_i leq n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print "YES" if there is a permutation $$$p$$$ exists that satisfies the array $$$c$$$, and "NO" otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response). Note In the first test case, the permutation $$$[1]$$$ satisfies the array $$$c$$$. In the second test case, the permutation $$$[2,1]$$$ satisfies the array $$$c$$$. In the fifth test case, the permutation $$$[5, 1, 2, 4, 6, 3]$$$ satisfies the array $$$c$$$. Let's see why this is true. The zeroth cyclic shift of $$$p$$$ is $$$[5, 1, 2, 4, 6, 3]$$$. Its power is $$$2$$$ since $$$b = [5, 5, 5, 5, 6, 6]$$$ and there are $$$2$$$ distinct elements — $$$5$$$ and $$$6$$$. The first cyclic shift of $$$p$$$ is $$$[3, 5, 1, 2, 4, 6]$$$. Its power is $$$3$$$ since $$$b=[3,5,5,5,5,6]$$$. The second cyclic shift of $$$p$$$ is $$$[6, 3, 5, 1, 2, 4]$$$. Its power is $$$1$$$ since $$$b=[6,6,6,6,6,6]$$$. The third cyclic shift of $$$p$$$ is $$$[4, 6, 3, 5, 1, 2]$$$. Its power is $$$2$$$ since $$$b=[4,6,6,6,6,6]$$$. The fourth cyclic shift of $$$p$$$ is $$$[2, 4, 6, 3, 5, 1]$$$. Its power is $$$3$$$ since $$$b = [2, 4, 6, 6, 6, 6]$$$. The fifth cyclic shift of $$$p$$$ is $$$[1, 2, 4, 6, 3, 5]$$$. Its power is $$$4$$$ since $$$b = [1, 2, 4, 6, 6, 6]$$$. Therefore, $$$c = [2, 3, 1, 2, 3, 4]$$$. In the third, fourth, and sixth testcases, we can show that there is no permutation that satisfies array $$$c$$$. | 1,700 | true | false | false | false | false | true | false | false | false | false | 2,337 |
1552E | The numbers $$$1, , 2, , dots, , n cdot k$$$ are colored with $$$n$$$ colors. These colors are indexed by $$$1, , 2, , dots, , n$$$. For each $$$1 le i le n$$$, there are exactly $$$k$$$ numbers colored with color $$$i$$$. Let $$$[a, , b]$$$ denote the interval of integers between $$$a$$$ and $$$b$$$ inclusive, that is, the set $$${a, , a + 1, , dots, , b}$$$. You must choose $$$n$$$ intervals $$$[a_1, , b_1], , [a_2, , b_2], , dots, [a_n, , b_n]$$$ such that: for each $$$1 le i le n$$$, it holds $$$1 le a_i < b_i le n cdot k$$$; for each $$$1 le i le n$$$, the numbers $$$a_i$$$ and $$$b_i$$$ are colored with color $$$i$$$; each number $$$1 le x le n cdot k$$$ belongs to at most $$$leftlceil frac{n}{k - 1} ight ceil$$$ intervals. One can show that such a family of intervals always exists under the given constraints. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100$$$, $$$2 le k le 100$$$) — the number of colors and the number of occurrences of each color. The second line contains $$$n cdot k$$$ integers $$$c_1, , c_2, , dots, , c_{nk}$$$ ($$$1 le c_j le n$$$), where $$$c_j$$$ is the color of number $$$j$$$. It is guaranteed that, for each $$$1 le i le n$$$, it holds $$$c_j = i$$$ for exactly $$$k$$$ distinct indices $$$j$$$. Output Output $$$n$$$ lines. The $$$i$$$-th line should contain the two integers $$$a_i$$$ and $$$b_i$$$. If there are multiple valid choices of the intervals, output any. Note In the first sample, each number can be contained in at most $$$leftlceil frac{4}{3 - 1} ight ceil = 2$$$ intervals. The output is described by the following picture: In the second sample, the only interval to be chosen is forced to be $$$[1, , 2]$$$, and each number is indeed contained in at most $$$leftlceil frac{1}{2 - 1} ight ceil = 1$$$ interval. In the third sample, each number can be contained in at most $$$leftlceil frac{3}{3 - 1} ight ceil = 2$$$ intervals. The output is described by the following picture: | 2,300 | false | true | false | false | true | true | false | false | true | false | 2,880 |
1110E | Grigory has $$$n$$$ magic stones, conveniently numbered from $$$1$$$ to $$$n$$$. The charge of the $$$i$$$-th stone is equal to $$$c_i$$$. Sometimes Grigory gets bored and selects some inner stone (that is, some stone with index $$$i$$$, where $$$2 le i le n - 1$$$), and after that synchronizes it with neighboring stones. After that, the chosen stone loses its own charge, but acquires the charges from neighboring stones. In other words, its charge $$$c_i$$$ changes to $$$c_i' = c_{i + 1} + c_{i - 1} - c_i$$$. Andrew, Grigory's friend, also has $$$n$$$ stones with charges $$$t_i$$$. Grigory is curious, whether there exists a sequence of zero or more synchronization operations, which transforms charges of Grigory's stones into charges of corresponding Andrew's stones, that is, changes $$$c_i$$$ into $$$t_i$$$ for all $$$i$$$? Input The first line contains one integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of magic stones. The second line contains integers $$$c_1, c_2, ldots, c_n$$$ ($$$0 le c_i le 2 cdot 10^9$$$)xa0— the charges of Grigory's stones. The second line contains integers $$$t_1, t_2, ldots, t_n$$$ ($$$0 le t_i le 2 cdot 10^9$$$)xa0— the charges of Andrew's stones. Output If there exists a (possibly empty) sequence of synchronization operations, which changes all charges to the required ones, print "Yes". Otherwise, print "No". Examples Input 4 7 2 4 12 7 15 10 12 Note In the first example, we can perform the following synchronizations ($$$1$$$-indexed): First, synchronize the third stone $$$[7, 2, mathbf{4}, 12] ightarrow [7, 2, mathbf{10}, 12]$$$. Then synchronize the second stone: $$$[7, mathbf{2}, 10, 12] ightarrow [7, mathbf{15}, 10, 12]$$$. In the second example, any operation with the second stone will not change its charge. | 2,200 | true | false | false | false | false | true | false | false | true | false | 5,145 |
1036F | Problem - 1036F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics math number theory *2400 No tag edit access → Contest materials , $$$2500 = 2^2 cdot 5^4$$$ ($$$GCD = 2$$$) are not. Count the number of elegant integers from $$$2$$$ to $$$n$$$. Each testcase contains several values of $$$n$$$, for each of them you are required to solve the problem separately. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 10^5$$$) — the number of values of $$$n$$$ in the testcase. Each of the next $$$T$$$ lines contains a single integer $$$n_i$$$ ($$$2 le n_i le 10^{18}$$$). Output Print $$$T$$$ lines — the $$$i$$$-th line should contain the number of elegant numbers from $$$2$$$ to $$$n_i$$$. Example Input 4 4 2 72 10 Output 2 1 61 6 Note Here is the list of non-elegant numbers up to $$$10$$$: $$$4 = 2^2, GCD = 2$$$; $$$8 = 2^3, GCD = 3$$$; $$$9 = 3^2, GCD = 2$$$. The rest have $$$GCD = 1$$$. | 2,400 | true | false | false | false | false | false | false | false | false | false | 5,518 |
137B | Hey, it's homework time" — thought Polycarpus and of course he started with his favourite subject, IT. Polycarpus managed to solve all tasks but for the last one in 20 minutes. However, as he failed to solve the last task after some considerable time, the boy asked you to help him. The sequence of _n_ integers is called a permutation if it contains all integers from 1 to _n_ exactly once. You are given an arbitrary sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ containing _n_ integers. Each integer is not less than 1 and not greater than 5000. Determine what minimum number of elements Polycarpus needs to change to get a permutation (he should not delete or add numbers). In a single change he can modify any single sequence element (i. e. replace it with another integer). Input The first line of the input data contains an integer _n_ (1u2009≤u2009_n_u2009≤u20095000) which represents how many numbers are in the sequence. The second line contains a sequence of integers _a__i_ (1u2009≤u2009_a__i_u2009≤u20095000,u20091u2009≤u2009_i_u2009≤u2009_n_). Output Print the only number — the minimum number of changes needed to get the permutation. Note The first sample contains the permutation, which is why no replacements are required. In the second sample it is enough to replace the first element with the number 1 and that will make the sequence the needed permutation. In the third sample we can replace the second element with number 4 and the fourth element with number 2. | 1,000 | false | true | false | false | false | false | false | false | false | false | 9,329 |
90B | Problem - 90B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation strings *1100 No tag edit access → Contest materials contains some encrypted word that needs to be decoded. To solve the crossword you should cross out all repeated letters in rows and columns. In other words, a letter should only be crossed out if and only if the corresponding column or row contains at least one more letter that is exactly the same. Besides, all such letters are crossed out simultaneously. When all repeated letters have been crossed out, we should write the remaining letters in a string. The letters that occupy a higher position follow before the letters that occupy a lower position. If the letters are located in one row, then the letter to the left goes first. The resulting word is the answer to the problem. You are suggested to solve an African crossword and print the word encrypted there. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100). Next _n_ lines contain _m_ lowercase Latin letters each. That is the crossword grid. Output Print the encrypted word on a single line. It is guaranteed that the answer consists of at least one letter. Examples Input 3 3 cba bcd cbc Output abcd Input 5 5 fcofd ooedo afaoa rdcdf eofsf Output codeforces | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,523 |
733F | In one kingdom there are _n_ cities and _m_ two-way roads. Each road connects a pair of cities, and for each road we know the level of drivers dissatisfactionxa0— the value _w__i_. For each road we know the value _c__i_xa0— how many lamziks we should spend to reduce the level of dissatisfaction with this road by one. Thus, to reduce the dissatisfaction with the _i_-th road by _k_, we should spend _k_·_c__i_ lamziks. And it is allowed for the dissatisfaction to become zero or even negative. In accordance with the king's order, we need to choose _n_u2009-u20091 roads and make them the main roads. An important condition must hold: it should be possible to travel from any city to any other by the main roads. The road ministry has a budget of _S_ lamziks for the reform. The ministry is going to spend this budget for repair of some roads (to reduce the dissatisfaction with them), and then to choose the _n_u2009-u20091 main roads. Help to spend the budget in such a way and then to choose the main roads so that the total dissatisfaction with the main roads will be as small as possible. The dissatisfaction with some roads can become negative. It is not necessary to spend whole budget _S_. It is guaranteed that it is possible to travel from any city to any other using existing roads. Each road in the kingdom is a two-way road. Input The first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20092·105, _n_u2009-u20091u2009≤u2009_m_u2009≤u20092·105)xa0— the number of cities and the number of roads in the kingdom, respectively. The second line contains _m_ integers _w_1,u2009_w_2,u2009...,u2009_w__m_ (1u2009≤u2009_w__i_u2009≤u2009109), where _w__i_ is the drivers dissatisfaction with the _i_-th road. The third line contains _m_ integers _c_1,u2009_c_2,u2009...,u2009_c__m_ (1u2009≤u2009_c__i_u2009≤u2009109), where _c__i_ is the cost (in lamziks) of reducing the dissatisfaction with the _i_-th road by one. The next _m_ lines contain the description of the roads. The _i_-th of this lines contain a pair of integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_) which mean that the _i_-th road connects cities _a__i_ and _b__i_. All roads are two-way oriented so it is possible to move by the _i_-th road from _a__i_ to _b__i_, and vice versa. It is allowed that a pair of cities is connected by more than one road. The last line contains one integer _S_ (0u2009≤u2009_S_u2009≤u2009109)xa0— the number of lamziks which we can spend for reforms. Output In the first line print _K_xa0— the minimum possible total dissatisfaction with main roads. In each of the next _n_u2009-u20091 lines print two integers _x_,u2009_v__x_, which mean that the road _x_ is among main roads and the road _x_, after the reform, has the level of dissatisfaction _v__x_. Consider that roads are numbered from 1 to _m_ in the order as they are given in the input data. The edges can be printed in arbitrary order. If there are several answers, print any of them. Examples Input 6 9 1 3 1 1 3 1 2 2 2 4 1 4 2 2 5 3 1 6 1 2 1 3 2 3 2 4 2 5 3 5 3 6 4 5 5 6 7 Input 3 3 9 5 1 7 7 2 2 1 3 1 3 2 2 | 2,200 | false | false | false | false | true | false | false | false | false | true | 6,859 |
992A | Problem - 992A - 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 sortings *800 No tag edit access → Contest materials ") integer to all elements of the array that are not equal to zero. When all elements of the array become equal to zero, the array explodes. Nastya is always busy, so she wants to explode the array as fast as possible. Compute the minimum time in which the array can be exploded. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the size of the array. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009105u2009≤u2009_a__i_u2009≤u2009105) — the elements of the array. Output Print a single integer — the minimum number of seconds needed to make all elements of the array equal to zero. Examples Input 5 1 1 1 1 1 Output 1 Input 3 2 0 -1 Output 2 Input 4 5 -6 -5 1 Output 4 Note In the first example you can add u2009-u20091 to all non-zero elements in one second and make them equal to zero. In the second example you can add u2009-u20092 on the first second, then the array becomes equal to | 800 | false | false | true | false | false | false | false | false | true | false | 5,739 |
815D | Karen just got home from the supermarket, and is getting ready to go to sleep. After taking a shower and changing into her pajamas, she looked at her shelf and saw an album. Curious, she opened it and saw a trading card collection. She recalled that she used to play with those cards as a child, and, although she is now grown-up, she still wonders a few things about it. Each card has three characteristics: strength, defense and speed. The values of all characteristics of all cards are positive integers. The maximum possible strength any card can have is _p_, the maximum possible defense is _q_ and the maximum possible speed is _r_. There are _n_ cards in her collection. The _i_-th card has a strength _a__i_, defense _b__i_ and speed _c__i_, respectively. A card beats another card if at least two of its characteristics are strictly greater than the corresponding characteristics of the other card. She now wonders how many different cards can beat all the cards in her collection. Two cards are considered different if at least one of their characteristics have different values. Input The first line of input contains four integers, _n_, _p_, _q_ and _r_ (1u2009≤u2009_n_,u2009_p_,u2009_q_,u2009_r_u2009≤u2009500000), the number of cards in the collection, the maximum possible strength, the maximum possible defense, and the maximum possible speed, respectively. The next _n_ lines each contain three integers. In particular, the _i_-th line contains _a__i_, _b__i_ and _c__i_ (1u2009≤u2009_a__i_u2009≤u2009_p_, 1u2009≤u2009_b__i_u2009≤u2009_q_, 1u2009≤u2009_c__i_u2009≤u2009_r_), the strength, defense and speed of the _i_-th collection card, respectively. Output Output a single integer on a line by itself, the number of different cards that can beat all the cards in her collection. Examples Input 3 4 4 5 2 2 5 1 3 4 4 1 1 Input 5 10 10 10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Note In the first test case, the maximum possible strength is 4, the maximum possible defense is 4 and the maximum possible speed is 5. Karen has three cards: The first card has strength 2, defense 2 and speed 5. The second card has strength 1, defense 3 and speed 4. The third card has strength 4, defense 1 and speed 1. There are 10 cards that beat all the cards here: 1. The card with strength 3, defense 3 and speed 5. 2. The card with strength 3, defense 4 and speed 2. 3. The card with strength 3, defense 4 and speed 3. 4. The card with strength 3, defense 4 and speed 4. 5. The card with strength 3, defense 4 and speed 5. 6. The card with strength 4, defense 3 and speed 5. 7. The card with strength 4, defense 4 and speed 2. 8. The card with strength 4, defense 4 and speed 3. 9. The card with strength 4, defense 4 and speed 4. 10. The card with strength 4, defense 4 and speed 5. In the second test case, the maximum possible strength is 10, the maximum possible defense is 10 and the maximum possible speed is 10. Karen has five cards, all with strength 1, defense 1 and speed 1. Any of the 972 cards which have at least two characteristics greater than 1 can beat all of the cards in her collection. | 2,800 | false | false | false | false | true | false | false | true | false | false | 6,497 |
1374B | You are given an integer $$$n$$$. In one move, you can either multiply $$$n$$$ by two or divide $$$n$$$ by $$$6$$$ (if it is divisible by $$$6$$$ without the remainder). Your task is to find the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ or determine if it's impossible to do that. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$1 le n le 10^9$$$). Output For each test case, print the answer — the minimum number of moves needed to obtain $$$1$$$ from $$$n$$$ if it's possible to do that or -1 if it's impossible to obtain $$$1$$$ from $$$n$$$. Example Input 7 1 2 3 12 12345 15116544 387420489 Note Consider the sixth test case of the example. The answer can be obtained by the following sequence of moves from the given integer $$$15116544$$$: 1. Divide by $$$6$$$ and get $$$2519424$$$; 2. divide by $$$6$$$ and get $$$419904$$$; 3. divide by $$$6$$$ and get $$$69984$$$; 4. divide by $$$6$$$ and get $$$11664$$$; 5. multiply by $$$2$$$ and get $$$23328$$$; 6. divide by $$$6$$$ and get $$$3888$$$; 7. divide by $$$6$$$ and get $$$648$$$; 8. divide by $$$6$$$ and get $$$108$$$; 9. multiply by $$$2$$$ and get $$$216$$$; 10. divide by $$$6$$$ and get $$$36$$$; 11. divide by $$$6$$$ and get $$$6$$$; 12. divide by $$$6$$$ and get $$$1$$$. | 900 | true | false | false | false | false | false | false | false | false | false | 3,793 |
1101G | You are given an array $$$a_1, a_2, dots, a_n$$$ of integer numbers. Your task is to divide the array into the maximum number of segments in such a way that: each element is contained in exactly one segment; each segment contains at least one element; there doesn't exist a non-empty subset of segments such that bitwise XOR of the numbers from them is equal to $$$0$$$. Print the maximum number of segments the array can be divided into. Print -1 if no suitable division exists. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the size of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$). Note In the first example $$$2$$$ is the maximum number. If you divide the array into $$${[5], [5, 7, 2]}$$$, the XOR value of the subset of only the second segment is $$$5 oplus 7 oplus 2 = 0$$$. $$${[5, 5], [7, 2]}$$$ has the value of the subset of only the first segment being $$$5 oplus 5 = 0$$$. However, $$${[5, 5, 7], [2]}$$$ will lead to subsets $$${[5, 5, 7]}$$$ of XOR $$$7$$$, $$${[2]}$$$ of XOR $$$2$$$ and $$${[5, 5, 7], [2]}$$$ of XOR $$$5 oplus 5 oplus 7 oplus 2 = 5$$$. Let's take a look at some division on $$$3$$$ segments — $$${[5], [5, 7], [2]}$$$. It will produce subsets: $$${[5]}$$$, XOR $$$5$$$; $$${[5, 7]}$$$, XOR $$$2$$$; $$${[5], [5, 7]}$$$, XOR $$$7$$$; $$${[2]}$$$, XOR $$$2$$$; $$${[5], [2]}$$$, XOR $$$7$$$; $$${[5, 7], [2]}$$$, XOR $$$0$$$; $$${[5], [5, 7], [2]}$$$, XOR $$$5$$$; As you can see, subset $$${[5, 7], [2]}$$$ has its XOR equal to $$$0$$$, which is unacceptable. You can check that for other divisions of size $$$3$$$ or $$$4$$$, non-empty subset with $$$0$$$ XOR always exists. The second example has no suitable divisions. The third example array can be divided into $$${[3], [1], [10]}$$$. No subset of these segments has its XOR equal to $$$0$$$. | 2,300 | true | false | false | false | false | false | false | false | false | false | 5,194 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.