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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1578E | Eonathan Eostar decided to learn the magic of multiprocessor systems. He has a full binary tree of tasks with height $$$h$$$. In the beginning, there is only one ready task in the treexa0— the task in the root. At each moment of time, $$$p$$$ processes choose at most $$$p$$$ ready tasks and perform them. After that, tasks whose parents were performed become ready for the next moment of time. Once the task becomes ready, it stays ready until it is performed. You shall calculate the smallest number of time moments the system needs to perform all the tasks. Input The first line of the input contains the number of tests $$$t$$$ ($$$1 leq t leq 5cdot 10^5$$$). Each of the next $$$t$$$ lines contains the description of a test. A test is described by two integers $$$h$$$ ($$$1 leq h leq 50$$$) and $$$p$$$ ($$$1 leq p leq 10^4$$$)xa0— the height of the full binary tree and the number of processes. It is guaranteed that all the tests are different. Output For each test output one integer on a separate linexa0— the smallest number of time moments the system needs to perform all the tasks. Note Let us consider the second test from the sample input. There is a full binary tree of height $$$3$$$ and there are two processes. At the first moment of time, there is only one ready task, $$$1$$$, and $$$p_1$$$ performs it. At the second moment of time, there are two ready tasks, $$$2$$$ and $$$3$$$, and the processes perform them. At the third moment of time, there are four ready tasks, $$$4$$$, $$$5$$$, $$$6$$$, and $$$7$$$, and $$$p_1$$$ performs $$$6$$$ and $$$p_2$$$ performs $$$5$$$. At the fourth moment of time, there are two ready tasks, $$$4$$$ and $$$7$$$, and the processes perform them. Thus, the system spends $$$4$$$ moments of time to perform all the tasks. | 1,200 | true | false | true | false | false | false | false | false | false | false | 2,750 |
1343F | We guessed a permutation $$$p$$$ consisting of $$$n$$$ integers. The permutation of length $$$n$$$ is the array of length $$$n$$$ where each element from $$$1$$$ to $$$n$$$ appears exactly once. This permutation is a secret for you. For each position $$$r$$$ from $$$2$$$ to $$$n$$$ we chose some other index $$$l$$$ ($$$l < r$$$) and gave you the segment $$$p_l, p_{l + 1}, dots, p_r$$$ in sorted order (i.e. we rearranged the elements of this segment in a way that the elements of this segment are sorted). Thus, you are given exactly $$$n-1$$$ segments of the initial permutation but elements inside each segment are sorted. The segments are given to you in random order. For example, if the secret permutation is $$$p=[3, 1, 4, 6, 2, 5]$$$ then the possible given set of segments can be: $$$[2, 5, 6]$$$ $$$[4, 6]$$$ $$$[1, 3, 4]$$$ $$$[1, 3]$$$ $$$[1, 2, 4, 6]$$$ Your task is to find any suitable permutation (i.e. any permutation corresponding to the given input data). It is guaranteed that the input data corresponds to some permutation (i.e. such permutation exists). You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 le n le 200$$$) — the length of the permutation. The next $$$n-1$$$ lines describe given segments. The $$$i$$$-th line contains the description of the $$$i$$$-th segment. The line starts with the integer $$$k_i$$$ ($$$2 le k_i le n$$$) — the length of the $$$i$$$-th segment. Then $$$k_i$$$ integers follow. All integers in a line are distinct, sorted in ascending order, between $$$1$$$ and $$$n$$$, inclusive. It is guaranteed that the required $$$p$$$ exists for each test case. It is also guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200$$$ ($$$sum n le 200$$$). Output For each test case, print the answer: $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$, all $$$p_i$$$ should be distinct) — any suitable permutation (i.e. any permutation corresponding to the test case input). Example Input 5 6 3 2 5 6 2 4 6 3 1 3 4 2 1 3 4 1 2 4 6 5 2 2 3 2 1 2 2 1 4 2 4 5 7 3 1 2 6 4 1 3 5 6 2 1 2 3 4 5 7 6 1 2 3 4 5 6 3 1 3 6 2 2 1 2 5 2 2 5 3 2 3 5 4 2 3 4 5 5 1 2 3 4 5 Output 3 1 4 6 2 5 3 2 1 4 5 2 1 6 3 5 4 7 1 2 2 5 3 4 1 | 2,400 | false | true | true | false | true | true | true | false | false | false | 3,988 |
1386C | Joker returns to Gotham City to execute another evil plan. In Gotham City, there are $$$N$$$ street junctions (numbered from $$$1$$$ to $$$N$$$) and $$$M$$$ streets (numbered from $$$1$$$ to $$$M$$$). Each street connects two distinct junctions, and two junctions are connected by at most one street. For his evil plan, Joker needs to use an odd number of streets that together form a cycle. That is, for a junction $$$S$$$ and an even positive integer $$$k$$$, there is a sequence of junctions $$$S, s_1, ldots, s_k, S$$$ such that there are streets connecting (a) $$$S$$$ and $$$s_1$$$, (b) $$$s_k$$$ and $$$S$$$, and (c) $$$s_{i-1}$$$ and $$$s_i$$$ for each $$$i = 2, ldots, k$$$. However, the police are controlling the streets of Gotham City. On each day $$$i$$$, they monitor a different subset of all streets with consecutive numbers $$$j$$$: $$$l_i leq j leq r_i$$$. These monitored streets cannot be a part of Joker's plan, of course. Unfortunately for the police, Joker has spies within the Gotham City Police Department; they tell him which streets are monitored on which day. Now Joker wants to find out, for some given number of days, whether he can execute his evil plan. On such a day there must be a cycle of streets, consisting of an odd number of streets which are not monitored on that day. Input The first line of the input contains three integers $$$N$$$, $$$M$$$, and $$$Q$$$ ($$$1 leq N, M, Q leq 200,000$$$): the number of junctions, the number of streets, and the number of days to be investigated. The following $$$M$$$ lines describe the streets. The $$$j$$$-th of these lines ($$$1 le j le M$$$) contains two junction numbers $$$u$$$ and $$$v$$$ ($$$u eq v$$$), saying that street $$$j$$$ connects these two junctions. It is guaranteed that any two junctions are connected by at most one street. The following $$$Q$$$ lines contain two integers $$$l_i$$$ and $$$r_i$$$, saying that all streets $$$j$$$ with $$$l_i leq j leq r_i$$$ are checked by the police on day $$$i$$$ ($$$1 leq i leq Q$$$). | 2,800 | false | false | false | false | true | false | false | false | false | false | 3,738 |
555E | Andrewid the Android is a galaxy-known detective. Now he is preparing a defense against a possible attack by hackers on a major computer network. In this network are _n_ vertices, some pairs of vertices are connected by _m_ undirected channels. It is planned to transfer _q_ important messages via this network, the _i_-th of which must be sent from vertex _s__i_ to vertex _d__i_ via one or more channels, perhaps through some intermediate vertices. To protect against attacks a special algorithm was developed. Unfortunately it can be applied only to the network containing directed channels. Therefore, as new channels can't be created, it was decided for each of the existing undirected channels to enable them to transmit data only in one of the two directions. Your task is to determine whether it is possible so to choose the direction for each channel so that each of the _q_ messages could be successfully transmitted. Input The first line contains three integers _n_, _m_ and _q_ (1u2009≤u2009_n_,u2009_m_,u2009_q_u2009≤u20092·105) — the number of nodes, channels and important messages. Next _m_ lines contain two integers each, _v__i_ and _u__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_, _v__i_u2009≠u2009_u__i_), that means that between nodes _v__i_ and _u__i_ is a channel. Between a pair of nodes can exist more than one channel. Next _q_ lines contain two integers _s__i_ and _d__i_ (1u2009≤u2009_s__i_,u2009_d__i_u2009≤u2009_n_, _s__i_u2009≠u2009_d__i_) — the numbers of the nodes of the source and destination of the corresponding message. It is not guaranteed that in it initially possible to transmit all the messages. Output If a solution exists, print on a single line "Yes" (without the quotes). Otherwise, print "No" (without the quotes). Examples Input 4 4 2 1 2 1 3 2 3 3 4 1 3 4 2 Input 3 3 2 1 2 1 2 3 2 1 3 2 1 Note In the first sample test you can assign directions, for example, as follows: 1u2009→u20092, 1u2009→u20093, 3u2009→u20092, 4u2009→u20093. Then the path for for the first message will be 1u2009→u20093, and for the second one — 4u2009→u20093u2009→u20092. In the third sample test you can assign directions, for example, as follows: 1u2009→u20092, 2u2009→u20091, 2u2009→u20093. Then the path for the first message will be 1u2009→u20092u2009→u20093, and for the second one — 2u2009→u20091. | 2,800 | false | false | false | false | false | false | false | false | false | true | 7,611 |
617C | A flowerbed has many flowers and two fountains. You can adjust the water pressure and set any values _r_1(_r_1u2009≥u20090) and _r_2(_r_2u2009≥u20090), giving the distances at which the water is spread from the first and second fountain respectively. You have to set such _r_1 and _r_2 that all the flowers are watered, that is, for each flower, the distance between the flower and the first fountain doesn't exceed _r_1, or the distance to the second fountain doesn't exceed _r_2. It's OK if some flowers are watered by both fountains. You need to decrease the amount of water you need, that is set such _r_1 and _r_2 that all the flowers are watered and the _r_12u2009+u2009_r_22 is minimum possible. Find this minimum value. Input The first line of the input contains integers _n_, _x_1, _y_1, _x_2, _y_2 (1u2009≤u2009_n_u2009≤u20092000, u2009-u2009107u2009≤u2009_x_1,u2009_y_1,u2009_x_2,u2009_y_2u2009≤u2009107)xa0— the number of flowers, the coordinates of the first and the second fountain. Next follow _n_ lines. The _i_-th of these lines contains integers _x__i_ and _y__i_ (u2009-u2009107u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009107)xa0— the coordinates of the _i_-th flower. It is guaranteed that all _n_u2009+u20092 points in the input are distinct. Output Print the minimum possible value _r_12u2009+u2009_r_22. Note, that in this problem optimal answer is always integer. Examples Input 4 0 0 5 0 9 4 8 3 -1 0 1 4 Note The first sample is (_r_12u2009=u20095, _r_22u2009=u20091): The second sample is (_r_12u2009=u20091, _r_22u2009=u200932): | 1,600 | false | false | true | false | false | false | false | false | false | false | 7,367 |
906A | Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly. Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of actions Valentin did. The next _n_ lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types: 1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and _w_ is the word that Valentin said. 2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and _w_ is the word that Valentin said. 3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and _s_ is the guessxa0— a lowercase English letter. All words consist only of lowercase English letters. The total length of all words does not exceed 105. It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter. Output Output a single integerxa0— the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined. Examples Input 5 ! abc . ad . b ! cd ? c Input 8 ! hello ! codeforces ? c . o ? d ? h . l ? e Input 7 ! ababahalamaha ? a ? b ? a ? b ? a ? h Note In the first test case after the first action it becomes clear that the selected letter is one of the following: _a_,u2009_b_,u2009_c_. After the second action we can note that the selected letter is not _a_. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is _c_, but Valentin pronounces the word _cd_ and gets an excessive electric shock. In the second test case after the first two electric shocks we understand that the selected letter is _e_ or _o_. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is _e_, but Valentin makes 3 more actions before he makes a correct hypothesis. In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks. | 1,600 | false | false | true | false | false | false | false | false | false | false | 6,127 |
1765M | You are given an integer $$$n$$$. Your task is to find two positive (greater than $$$0$$$) integers $$$a$$$ and $$$b$$$ such that $$$a+b=n$$$ and the least common multiple (LCM) of $$$a$$$ and $$$b$$$ is the minimum among all possible values of $$$a$$$ and $$$b$$$. If there are multiple answers, you can print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^9$$$). Output For each test case, print two positive integers $$$a$$$ and $$$b$$$xa0— the answer to the problem. If there are multiple answers, you can print any of them. Note In the second example, there are $$$8$$$ possible pairs of $$$a$$$ and $$$b$$$: $$$a = 1$$$, $$$b = 8$$$, $$$LCM(1, 8) = 8$$$; $$$a = 2$$$, $$$b = 7$$$, $$$LCM(2, 7) = 14$$$; $$$a = 3$$$, $$$b = 6$$$, $$$LCM(3, 6) = 6$$$; $$$a = 4$$$, $$$b = 5$$$, $$$LCM(4, 5) = 20$$$; $$$a = 5$$$, $$$b = 4$$$, $$$LCM(5, 4) = 20$$$; $$$a = 6$$$, $$$b = 3$$$, $$$LCM(6, 3) = 6$$$; $$$a = 7$$$, $$$b = 2$$$, $$$LCM(7, 2) = 14$$$; $$$a = 8$$$, $$$b = 1$$$, $$$LCM(8, 1) = 8$$$. In the third example, there are $$$5$$$ possible pairs of $$$a$$$ and $$$b$$$: $$$a = 1$$$, $$$b = 4$$$, $$$LCM(1, 4) = 4$$$; $$$a = 2$$$, $$$b = 3$$$, $$$LCM(2, 3) = 6$$$; $$$a = 3$$$, $$$b = 2$$$, $$$LCM(3, 2) = 6$$$; $$$a = 4$$$, $$$b = 1$$$, $$$LCM(4, 1) = 4$$$. | 1,000 | true | false | false | false | false | false | false | false | false | false | 1,705 |
1741C | You are given a sequence $$$a=[a_1,a_2,dots,a_n]$$$ consisting of $$$n$$$ positive integers. Let's call a group of consecutive elements a segment. Each segment is characterized by two indices: the index of its left end and the index of its right end. Denote by $$$a[l,r]$$$ a segment of the sequence $$$a$$$ with the left end in $$$l$$$ and the right end in $$$r$$$, i.e. $$$a[l,r]=[a_l, a_{l+1}, dots, a_r]$$$. For example, if $$$a=[31,4,15,92,6,5]$$$, then $$$a[2,5]=[4,15,92,6]$$$, $$$a[5,5]=[6]$$$, $$$a[1,6]=[31,4,15,92,6,5]$$$ are segments. We split the given sequence $$$a$$$ into segments so that: each element is in exactly one segment; the sums of elements for all segments are equal. For example, if $$$a$$$ = [$$$55,45,30,30,40,100$$$], then such a sequence can be split into three segments: $$$a[1,2]=[55,45]$$$, $$$a[3,5]=[30, 30, 40]$$$, $$$a[6,6]=[100]$$$. Each element belongs to exactly segment, the sum of the elements of each segment is $$$100$$$. Let's define thickness of split as the length of the longest segment. For example, the thickness of the split from the example above is $$$3$$$. Find the minimum thickness among all possible splits of the given sequence of $$$a$$$ into segments in the required way. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Each test case is described by two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2000$$$) — the length of the sequence $$$a$$$. The second line of each test case contains exactly $$$n$$$ integers: $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^6$$$) — elements of the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2000$$$. Note The split in the first test case is explained in the statement, it can be shown that it is optimal. In the second test case, it is possible to split into segments only by leaving a single segment. Then the thickness of this split is equal to the length of the entire sequence, that is, $$$4$$$. In the third test case, the optimal split will be $$$[10, 55], [35, 30], [65]$$$. The thickness of the split equals to $$$2$$$. In the fourth test case possible splits are: $$$[4] + [1, 1, 1, 1] + [4]$$$; $$$[4, 1, 1] + [1, 1, 4]$$$. | 1,100 | true | true | false | false | false | false | true | false | false | false | 1,836 |
140C | Problem - 140C - Codeforces =============== xa0 — the number of snowballs. The next line contains _n_ integers — the balls' radii _r_1, _r_2, ..., _r__n_ (1u2009≤u2009_r__i_u2009≤u2009109). The balls' radii can coincide. Output Print on the first line a single number _k_ — the maximum number of the snowmen. Next _k_ lines should contain the snowmen's descriptions. The description of each snowman should consist of three space-separated numbers — the big ball's radius, the medium ball's radius and the small ball's radius. It is allowed to print the snowmen in any order. If there are several solutions, print any of them. Examples Input 7 1 2 3 4 5 6 7 Output 2 3 2 1 6 5 4 Input 3 2 2 3 Output 0 | 1,800 | false | true | false | false | true | false | false | true | false | false | 9,316 |
1327F | You are given three integers $$$n$$$, $$$k$$$, $$$m$$$ and $$$m$$$ conditions $$$(l_1, r_1, x_1), (l_2, r_2, x_2), dots, (l_m, r_m, x_m)$$$. Calculate the number of distinct arrays $$$a$$$, consisting of $$$n$$$ integers such that: $$$0 le a_i < 2^k$$$ for each $$$1 le i le n$$$; bitwise AND of numbers $$$a[l_i] & a[l_i + 1] & dots & a[r_i] = x_i$$$ for each $$$1 le i le m$$$. Two arrays $$$a$$$ and $$$b$$$ are considered different if there exists such a position $$$i$$$ that $$$a_i eq b_i$$$. The number can be pretty large so print it modulo $$$998244353$$$. Input The first line contains three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 le n le 5 cdot 10^5$$$, $$$1 le k le 30$$$, $$$0 le m le 5 cdot 10^5$$$) — the length of the array $$$a$$$, the value such that all numbers in $$$a$$$ should be smaller than $$$2^k$$$ and the number of conditions, respectively. Each of the next $$$m$$$ lines contains the description of a condition $$$l_i$$$, $$$r_i$$$ and $$$x_i$$$ ($$$1 le l_i le r_i le n$$$, $$$0 le x_i < 2^k$$$) — the borders of the condition segment and the required bitwise AND value on it. Output Print a single integer — the number of distinct arrays $$$a$$$ that satisfy all the above conditions modulo $$$998244353$$$. Examples Input 5 2 3 1 3 2 2 5 0 3 3 3 Note You can recall what is a bitwise AND operation [here]( the first example, the answer is the following arrays: $$$[3, 3, 7, 6]$$$, $$$[3, 7, 7, 6]$$$ and $$$[7, 3, 7, 6]$$$. | 2,500 | false | false | false | true | true | false | false | false | false | false | 4,073 |
73D | Vasya plays FreeDiv. In this game he manages a huge state, which has _n_ cities and _m_ two-way roads between them. Unfortunately, not from every city you can reach any other one moving along these roads. Therefore Vasya decided to divide the state into provinces so that in every province, one could reach from every city all the cities of the province, but there are no roads between provinces. Unlike other turn-based strategies, in FreeDiv a player has the opportunity to build tunnels between cities. The tunnels are two-way roads along which one can move armies undetected by the enemy. However, no more than one tunnel can be connected to each city. As for Vasya, he wants to build a network of tunnels so that any pair of cities in his state were reachable by some path consisting of roads and a tunnels. But at that no more than _k_ tunnels are connected to each province (otherwise, the province will be difficult to keep in case other provinces are captured by enemy armies). Vasya discovered that maybe he will not be able to build such a network for the current condition of the state. Maybe he'll have first to build several roads between cities in different provinces to merge the provinces. Your task is to determine the minimum number of roads Vasya needs to build so that it was possible to build the required network of tunnels in the resulting state. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009106,u20090u2009≤u2009_m_u2009≤u2009106). Each of the next _m_ lines contains two integers. They are the numbers of cities connected by a corresponding road. No road connects city to itself and there is at most one road between each pair of cities. Output Print a single number, the minimum number of additional roads. Note In the first example only one province exists, so it is not necessary to build any tunnels or roads. In the second example two provinces exist. It is possible to merge the provinces by building a tunnel between cities 1 and 3. In the third example at least one additional road is necessary. For example it is possible to build additional road between cities 1 and 2 and build two tunnels between cities 1 and 3, 2 and 4 after that. | 2,200 | false | true | false | false | false | false | false | false | false | true | 9,599 |
1737E | 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 combinatorics dp math probabilities *2500 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E. Ela Goes Hiking time limit per test2.5 seconds memory limit per test256 megabytes Ela likes to go hiking a lot. She loves nature and exploring the various creatures it offers. One day, she saw a strange type of ant, with a cannibalistic feature. More specifically, an ant would eat any ants that it sees which is smaller than it. Curious about this feature from a new creature, Ela ain't furious. She conducts a long, non-dubious, sentimental experiment. She puts $$$n$$$ cannibalistic ants in a line on a long wooden stick. Initially, the ants have the same weight of $$$1$$$. The distance between any two consecutive ants is the same. The distance between the first ant in the line to the left end and the last ant in the line to the right end is also the same as the distance between the ants. Each ant starts moving towards the left-end or the right-end randomly and equiprobably, at the same constant pace throughout the experiment. Two ants will crash if they are standing next to each other in the line and moving in opposite directions, and ants will change direction immediately when they reach the end of the stick. Ela can't determine the moving direction of each ant, but she understands very well their behavior when crashes happen. If a crash happens between two ants of different weights, the heavier one will eat the lighter one, and gain the weight of the lighter one. After that, the heavier and will continue walking in the same direction. In other words, if the heavier one has weight $$$x$$$ and walking to the right, the lighter one has weight $$$y$$$ and walking to the left ($$$x > y$$$), then after the crash, the lighter one will diminish, and the heavier one will have weight $$$x + y$$$ and continue walking to the right. If a crash happens between two ants with the same weight, the one walking to the left end of the stick will eat the one walking to the right, and then continue walking in the same direction. In other words, if one ant of weight $$$x$$$ walking to the left, crashes with another ant of weight $$$x$$$ walking to the right, the one walking to the right will disappear, and the one walking to the left will have to weight $$$2x$$$ and continue walking to the left. Please, check the example in the "Note" section, which will demonstrate the ants' behavior as above. We can prove that after a definite amount of time, there will be only one last ant standing. Initially, each ant can randomly and equiprobably move to the left or the right, which generates $$$2^n$$$ different cases of initial movements for the whole pack. For each position in the line, calculate the probability that the ant begins in that position and survives. Output it modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The description of the test cases follows. The only line of each test contains an integer $$$n$$$ ($$$1 le n le 10^6$$$) — the number of ants in the experiment. It is guaranteed that the sum of $$$n$$$ in all tests will not exceed $$$10^6$$$. Output For each test, print $$$n$$$ lines. $$$i$$$-th line contains a single number that denotes the survival probability of the $$$i$$$-th ant in the line modulo $$$10^9 + 7$$$. Example input 3 4 5 2 output 0 250000002 250000002 500000004 0 250000002 250000002 250000002 250000002 0 1 Note Here is the example of $$$6$$$ ants moving on the branch. An ant's movement will be denoted by either a character $$$L$$$ or $$$R$$$. Initially, the pack of ants on the branch will move as $$$RLRRLR$$$. Here's how the behavior of the pack demonstrated: Initially, the ants are positioned as above. After a while, the ant with index $$$2$$$ (walking to the left) will crash with the ant with index $$$1$$$ (walking to the right). The two ants have the same weight, therefore, ant $$$2$$$ will eat ant $$$1$$$ and gain its weight to $$$2$$$. The same thing happens with ant $$$5$$$ and ant $$$4$$$. The ant $$$6$$$ will walk to the end of the stick, therefore changing its direction. After that, the ant with index $$$5$$$ will crash with the ant with index $$$3$$$. Since ant $$$5$$$ is more heavy (weight=$$$2$$$) than ant $$$3$$$ (weight=$$$1$$$), ant $$$5$$$ will eat ant $$$3$$$ and gain its weight to $$$3$$$. Ant $$$2$$$ will walk to the end of the stick, therefore changing its direction. After that, the ant with index $$$5$$$ will crash with the ant with index $$$2$$$. Since ant $$$5$$$ is more heavy (weight=$$$3$$$) than ant $$$2$$$ (weight=$$$2$$$), ant $$$5$$$ will eat ant $$$2$$$ and gain its weight to $$$5$$$. Lastly, after ant $$$5$$$ walk to the end of the branch and changes its direction, ant $$$5$$$ will eat ant $$$6$$$ and be the last ant standing. Codeforces (c) | 2,500 | true | false | false | true | false | false | false | false | false | false | 1,864 |
960C | Pikachu had an array with him. He wrote down all the non-empty subsequences of the array on paper. Note that an array of size _n_ has 2_n_u2009-u20091 non-empty subsequences in it. Pikachu being mischievous as he always is, removed all the subsequences in which Maximum_element_of_the_subsequence u2009-u2009 Minimum_element_of_subsequence u2009≥u2009_d_ Pikachu was finally left with _X_ subsequences. However, he lost the initial array he had, and now is in serious trouble. He still remembers the numbers _X_ and _d_. He now wants you to construct any such array which will satisfy the above conditions. All the numbers in the final array should be positive integers less than 1018. Note the number of elements in the output array should not be more than 104. If no answer is possible, print u2009-u20091. Output Output should consist of two lines. First line should contain a single integer _n_ (1u2009≤u2009_n_u2009≤u200910u2009000)— the number of integers in the final array. Second line should consist of _n_ space separated integers — _a_1,u2009_a_2,u2009... ,u2009_a__n_ (1u2009≤u2009_a__i_u2009<u20091018). If there is no answer, print a single integer -1. If there are multiple answers, print any of them. Note In the output of the first example case, the remaining subsequences after removing those with Maximum_element_of_the_subsequence u2009-u2009 Minimum_element_of_subsequence u2009≥u20095 are [5],u2009[5,u20097],u2009[5,u20096],u2009[5,u20097,u20096],u2009[50],u2009[7],u2009[7,u20096],u2009[15],u2009[6],u2009[100]. There are 10 of them. Hence, the array [5,u200950,u20097,u200915,u20096,u2009100] is valid. Similarly, in the output of the second example case, the remaining sub-sequences after removing those with Maximum_element_of_the_subsequence u2009-u2009 Minimum_element_of_subsequence u2009≥u20092 are [10],u2009[100],u2009[1000],u2009[10000]. There are 4 of them. Hence, the array [10,u2009100,u20091000,u200910000] is valid. | 1,700 | false | true | true | false | false | true | false | false | false | false | 5,869 |
1548A | When you play the game of thrones, you win, or you die. There is no middle ground. Cersei Lannister, A Game of Thrones by George R.xa0R. Martin There are $$$n$$$ nobles, numbered from $$$1$$$ to $$$n$$$. Noble $$$i$$$ has a power of $$$i$$$. There are also $$$m$$$ "friendships". A friendship between nobles $$$a$$$ and $$$b$$$ is always mutual. A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. 1. Add a friendship between nobles $$$u$$$ and $$$v$$$. 2. Remove a friendship between nobles $$$u$$$ and $$$v$$$. 3. Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles. Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive! Input The first line contains the integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2cdot 10^5$$$, $$$0 le m le 2cdot 10^5$$$) — the number of nobles and number of original friendships respectively. The next $$$m$$$ lines each contain the integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$, $$$u e v$$$), describing a friendship. No friendship is listed twice. The next line contains the integer $$$q$$$ ($$$1 le q le 2cdot {10}^{5}$$$) — the number of queries. The next $$$q$$$ lines contain the queries themselves, each query has one of the following three formats. $$$1$$$ $$$u$$$ $$$v$$$ ($$$1 le u,v le n$$$, $$$u e v$$$) — add a friendship between $$$u$$$ and $$$v$$$. It is guaranteed that $$$u$$$ and $$$v$$$ are not friends at this moment. $$$2$$$ $$$u$$$ $$$v$$$ ($$$1 le u,v le n$$$, $$$u e v$$$) — remove a friendship between $$$u$$$ and $$$v$$$. It is guaranteed that $$$u$$$ and $$$v$$$ are friends at this moment. $$$3$$$ — print the answer to the process described in the statement. Output For each type $$$3$$$ query print one integer to a new line. It is guaranteed that there will be at least one type $$$3$$$ query. Examples Input 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 Note Consider the first example. In the first type 3 query, we have the diagram below. In the first round of the process, noble $$$1$$$ is weaker than all of his friends ($$$2$$$ and $$$3$$$), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble $$$3$$$ is weaker than his only friend, noble $$$4$$$, and is therefore killed. At this point, the process ends, and the answer is $$$2$$$. In the second type 3 query, the only surviving noble is $$$4$$$. The second example consists of only one type $$$3$$$ query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is $$$1$$$, since only one noble survives. | 1,400 | false | true | false | false | false | false | true | false | false | true | 2,906 |
1607A | You are given a keyboard that consists of $$$26$$$ keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter. You have to type the word $$$s$$$ on this keyboard. It also consists only of lowercase Latin letters. To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it. Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word. For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions $$$8$$$, $$$5$$$, $$$12$$$ and $$$15$$$, respectively. Therefore, it will take $$$5 - 8 + 12 - 5 + 12 - 12 + 15 - 12 = 13$$$ units of time to type the word "hello". Determine how long it will take to print the word $$$s$$$. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of a description contains a keyboardxa0— a string of length $$$26$$$, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word $$$s$$$. The word has a length from $$$1$$$ to $$$50$$$ letters inclusive and consists of lowercase Latin letters. Output Print $$$t$$$ lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word $$$s$$$ on the given keyboard. Example Input 5 abcdefghijklmnopqrstuvwxyz hello abcdefghijklmnopqrstuvwxyz i abcdefghijklmnopqrstuvwxyz codeforces qwertyuiopasdfghjklzxcvbnm qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qwertyuiopasdfghjklzxcvbnm abacaba | 800 | false | false | true | false | false | false | false | false | false | false | 2,623 |
623C | Programmer Sasha is a student at MIPT (Moscow Institute of Physics and Technology) and he needs to make a laboratory work to pass his finals. A laboratory unit is a plane with standard coordinate axes marked on it. Physicists from Moscow Institute of Physics and Technology charged the axes by large electric charges: axis _X_ is positive and axis _Y_ is negative. Experienced laboratory worker marked _n_ points with integer coordinates (_x__i_,u2009_y__i_) on the plane and stopped the time. Sasha should use "atomic tweezers" to place elementary particles in these points. He has an unlimited number of electrons (negatively charged elementary particles) and protons (positively charged elementary particles). He can put either an electron or a proton at each marked point. As soon as all marked points are filled with particles, laboratory worker will turn on the time again and the particles will come in motion and after some time they will stabilize in equilibrium. The objective of the laboratory work is to arrange the particles in such a way, that the diameter of the resulting state (the maximum distance between the pairs of points of the set) is as small as possible. Since Sasha is a programmer, he naively thinks that all the particles will simply "fall" into their projections on the corresponding axes: electrons will fall on axis _X_, while protons will fall on axis _Y_. As we are programmers too, we will consider the same model as Sasha. That is, a particle gets from point (_x_,u2009_y_) to point (_x_,u20090) if it is an electron and to point (0,u2009_y_) if it is a proton. As the laboratory has high background radiation and Sasha takes care of his laptop, he did not take it with him, and now he can't write a program that computes the minimum possible diameter of the resulting set. Therefore, you will have to do it for him. Print a square of the minimum possible diameter of the set. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of points marked on the plane. Each of the next _n_ lines contains two integers _x__i_ and _y__i_ (u2009-u2009108u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009108)xa0— the coordinates of the _i_-th point. It is guaranteed that no two points coincide. Note In the first sample Sasha puts electrons at all points, all particles eventually fall at a single point (1,u20090). In the second sample Sasha puts an electron at point (1,u200910), and a proton at point (10,u20091). The result is a set of two points (1,u20090) and (0,u20091), which has a diameter of . | 2,900 | false | false | false | true | false | false | false | true | false | false | 7,338 |
585A | Gennady is one of the best child dentists in Berland. Today _n_ children got an appointment with him, they lined up in front of his office. All children love to cry loudly at the reception at the dentist. We enumerate the children with integers from 1 to _n_ in the order they go in the line. Every child is associated with the value of his cofidence _p__i_. The children take turns one after another to come into the office; each time the child that is the first in the line goes to the doctor. While Gennady treats the teeth of the _i_-th child, the child is crying with the volume of _v__i_. At that the confidence of the first child in the line is reduced by the amount of _v__i_, the second one — by value _v__i_u2009-u20091, and so on. The children in the queue after the _v__i_-th child almost do not hear the crying, so their confidence remains unchanged. If at any point in time the confidence of the _j_-th child is less than zero, he begins to cry with the volume of _d__j_ and leaves the line, running towards the exit, without going to the doctor's office. At this the confidence of all the children after the _j_-th one in the line is reduced by the amount of _d__j_. All these events occur immediately one after the other in some order. Some cries may lead to other cries, causing a chain reaction. Once in the hallway it is quiet, the child, who is first in the line, goes into the doctor's office. Help Gennady the Dentist to determine the numbers of kids, whose teeth he will cure. Print their numbers in the chronological order. Input The first line of the input contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u20094000) — the number of kids in the line. Next _n_ lines contain three integers each _v__i_,u2009_d__i_,u2009_p__i_ (1u2009≤u2009_v__i_,u2009_d__i_,u2009_p__i_u2009≤u2009106) — the volume of the cry in the doctor's office, the volume of the cry in the hall and the confidence of the _i_-th child. Output In the first line print number _k_ — the number of children whose teeth Gennady will cure. In the second line print _k_ integers — the numbers of the children who will make it to the end of the line in the increasing order. Examples Input 5 4 2 2 4 1 2 5 2 4 3 3 5 5 1 2 Input 5 4 5 1 5 3 9 4 1 2 2 1 8 4 1 9 Note In the first example, Gennady first treats the teeth of the first child who will cry with volume 4. The confidences of the remaining children will get equal to u2009-u20092,u20091,u20093,u20091, respectively. Thus, the second child also cries at the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 0,u20092,u20090. Then the third child will go to the office, and cry with volume 5. The other children won't bear this, and with a loud cry they will run to the exit. In the second sample, first the first child goes into the office, he will cry with volume 4. The confidence of the remaining children will be equal to 5,u2009u2009-u20091,u20096,u20098. Thus, the third child will cry with the volume of 1 and run to the exit. The confidence of the remaining children will be equal to 5,u20095,u20097. After that, the second child goes to the office and cry with the volume of 5. The confidences of the remaining children will be equal to 0,u20093. Then the fourth child will go into the office and cry with the volume of 2. Because of this the confidence of the fifth child will be 1, and he will go into the office last. | 1,800 | false | false | true | false | false | false | true | false | false | false | 7,505 |
282E | The Bitlandians are quite weird people. They have their own problems and their own solutions. They have their own thoughts and their own beliefs, they have their own values and their own merits. They have their own dishes and their own sausages! In Bitland a sausage is an array of integers! A sausage's deliciousness is equal to the bitwise excluding OR (the _xor_ operation) of all integers in that sausage. One day, when Mr. Bitkoch (the local cook) was going to close his BitRestaurant, BitHaval and BitAryo, the most famous citizens of Bitland, entered the restaurant and each ordered a sausage. But Mr. Bitkoch had only one sausage left. So he decided to cut a prefix (several, may be zero, first array elements) of the sausage and give it to BitHaval and a postfix (several, may be zero, last array elements) of the sausage and give it to BitAryo. Note that one or both pieces of the sausage can be empty. Of course, the cut pieces mustn't intersect (no array element can occur in both pieces). The pleasure of BitHaval and BitAryo is equal to the bitwise XOR of their sausages' deliciousness. An empty sausage's deliciousness equals zero. Find a way to cut a piece of sausage for BitHaval and BitAryo that maximizes the pleasure of these worthy citizens. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u20091012) — Mr. Bitkoch's sausage. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | 2,200 | false | false | false | false | true | false | false | false | false | false | 8,704 |
1230B | Problem - 1230B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation *1000 No tag edit access → Contest materials — the number of digits in the decimal representation of $$$S$$$ and the maximum allowed number of changed digits. The second line contains the integer $$$S$$$. It's guaranteed that $$$S$$$ has exactly $$$n$$$ digits and doesn't contain any leading zeroes. Output Output the minimal possible value of $$$S$$$ which Ania can end with. Note that the resulting integer should also have $$$n$$$ digits. Examples Input 5 3 51528 Output 10028 Input 3 2 102 Output 100 Input 1 1 1 Output 0 Note A number has leading zeroes if it consists of at least two digits and its first digit is $$$0$$$. For example, numbers $$$00$$$, $$$00069$$$ and $$$0101$$$ have leading zeroes, while $$$0$$$, $$$3000$$$ and $$$1010$$$ don't have leading zeroes. | 1,000 | false | true | true | false | false | false | false | false | false | false | 4,528 |
724C | There are _k_ sensors located in the rectangular room of size _n_u2009×u2009_m_ meters. The _i_-th sensor is located at point (_x__i_,u2009_y__i_). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0,u20090) and (_n_,u2009_m_). Walls of the room are parallel to coordinate axes. At the moment 0, from the point (0,u20090) the laser ray is released in the direction of point (1,u20091). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1,u20091) in exactly one second after the start. When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops. For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print u2009-u20091 for such sensors. Input The first line of the input contains three integers _n_, _m_ and _k_ (2u2009≤u2009_n_,u2009_m_u2009≤u2009100u2009000, 1u2009≤u2009_k_u2009≤u2009100u2009000)xa0— lengths of the room's walls and the number of sensors. Each of the following _k_ lines contains two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_u2009-u20091, 1u2009≤u2009_y__i_u2009≤u2009_m_u2009-u20091)xa0— coordinates of the sensors. It's guaranteed that no two sensors are located at the same point. Note In the first sample, the ray will consequently pass through the points (0,u20090), (1,u20091), (2,u20092), (3,u20093). Thus, it will stop at the point (3,u20093) after 3 seconds. In the second sample, the ray will consequently pass through the following points: (0,u20090), (1,u20091), (2,u20092), (3,u20093), (2,u20094), (1,u20093), (0,u20092), (1,u20091), (2,u20090), (3,u20091), (2,u20092), (1,u20093), (0,u20094). The ray will stop at the point (0,u20094) after 12 seconds. It will reflect at the points (3,u20093), (2,u20094), (0,u20092), (2,u20090) and (3,u20091). | 1,800 | true | true | true | false | false | false | false | false | true | false | 6,912 |
2002D1 | This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $$$n$$$ and $$$q$$$ are lower. You can make hacks only if both versions of the problem are solved. You are given a perfect binary tree$$$^dagger$$$ consisting of $$$n$$$ vertices. The vertices are numbered from $$$1$$$ to $$$n$$$, and the root is the vertex $$$1$$$. You are also given a permutation $$$p_1, p_2, ldots, p_n$$$ of $$$[1,2,ldots,n]$$$. You need to answer $$$q$$$ queries. For each query, you are given two integers $$$x$$$, $$$y$$$; you need to swap $$$p_x$$$ and $$$p_y$$$ and determine if $$$p_1, p_2, ldots, p_n$$$ is a valid DFS order$$$^ddagger$$$ of the given tree. Please note that the swaps are persistent through queries. $$$^dagger$$$ A perfect binary tree is a tree with vertex $$$1$$$ as its root, with size $$$n=2^k-1$$$ for a positive integer $$$k$$$, and where the parent of each vertex $$$i$$$ ($$$1<ile n$$$) is $$$leftlfloorfrac{i}{2} ight floor$$$. Thus, all leaves of this tree are at a distance $$$k - 1$$$ from the root. $$$^ddagger$$$ A DFS order is found by calling the following $$$ exttt{dfs}$$$ function on the given tree. ``` dfs_order = []function dfs(v): append v to the back of dfs_order pick an arbitrary permutation s of children of v for child in s: dfs(child)dfs(1) ``` Note that the DFS order is not unique. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1le tle10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$q$$$ ($$$3le nle 65,535$$$, $$$2le qle 5 cdot 10^4$$$)xa0— the number of vertices in the tree and the number of queries. It is guaranteed that $$$n=2^k-1$$$ for a positive integer $$$k$$$. The next line contains $$$n-1$$$ integers $$$a_2,a_3,ldots,a_n$$$ ($$$1le a_i<i$$$)xa0— the parent of each vertex in the given tree. It is guaranteed that $$$a_i=leftlfloorfrac{i}{2} ight floor$$$. The next line contains $$$n$$$ integers $$$p_1,p_2,ldots,p_n$$$ ($$$1le p_ile n$$$, all $$$p_i$$$ are distinct)xa0— the initial permutation $$$p$$$. The next $$$q$$$ lines each contain two integers $$$x$$$, $$$y$$$ ($$$1le x,yle n,x eq y$$$)xa0— the positions of the elements to swap in the permutation. It is guaranteed that the sum of all $$$n$$$ does not exceed $$$65,535$$$, and the sum of all $$$q$$$ does not exceed $$$5 cdot 10^4$$$. Output For each test case, print $$$q$$$ lines corresponding to the $$$q$$$ queries. For each query, output $$$ exttt{YES}$$$ if there is a DFS order that exactly equals the current permutation, and output $$$ exttt{NO}$$$ otherwise. You can output $$$ exttt{Yes}$$$ and $$$ exttt{No}$$$ in any case (for example, strings $$$ exttt{yEs}$$$, $$$ exttt{yes}$$$, $$$ exttt{Yes}$$$ and $$$ exttt{YES}$$$ will be recognized as a positive response). Example Input 2 3 3 1 1 1 2 3 2 3 3 2 1 3 7 4 1 1 2 2 3 3 1 2 3 4 5 6 7 3 5 2 5 3 7 4 6 Output YES YES NO YES NO NO YES Note In the first test case, the permutation $$$p_1, p_2, ldots, p_n$$$ after each modification is $$$[1,3,2],[1,2,3],[3,2,1]$$$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order. In the second test case, the permutation $$$p_1, p_2, ldots, p_n$$$ after each modification is $$$[1,2,5,4,3,6,7],[1,3,5,4,2,6,7],[1,3,7,4,2,6,5],[1,3,7,6,2,4,5]$$$, respectively. | 1,900 | false | false | false | false | true | false | true | false | false | true | 246 |
200B | Problem - 200B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation math *800 No tag edit access → Contest materials ") ") ") — the number of orange-containing drinks in Vasya's fridge. The second line contains _n_ integers _p__i_ (0u2009≤u2009_p__i_u2009≤u2009100) — the volume fraction of orange juice in the _i_-th drink, in percent. The numbers are separated by a space. Output Print the volume fraction in percent of orange juice in Vasya's cocktail. The answer will be considered correct if the absolute or relative error does not exceed 10u2009u2009-u20094. Examples Input 3 50 50 100 Output 66.666666666667 Input 4 0 25 50 75 Output 37.500000000000 Note Note to the first sample: let's assume that Vasya takes _x_ milliliters of each drink from the fridge. Then the volume of pure juice in the cocktail will equal milliliters. The total cocktail's volume equals 3·_x_ milliliters, so the volume fraction of the juice in the cocktail equals , that is, 66.(6) percent. | 800 | true | false | true | false | false | false | false | false | false | false | 9,043 |
384B | Iahub wants to enhance his multitasking abilities. In order to do this, he wants to sort _n_ arrays simultaneously, each array consisting of _m_ integers. Iahub can choose a pair of distinct indices _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_m_,u2009_i_u2009≠u2009_j_). Then in each array the values at positions _i_ and _j_ are swapped only if the value at position _i_ is strictly greater than the value at position _j_. Iahub wants to find an array of pairs of distinct indices that, chosen in order, sort all of the _n_ arrays in ascending or descending order (the particular order is given in input). The size of the array can be at most (at most pairs). Help Iahub, find any suitable array. Input The first line contains three integers _n_ (1u2009≤u2009u2009_n_u2009≤u20091000), _m_ (1u2009≤u2009_m_u2009≤u2009u2009100) and _k_. Integer _k_ is 0 if the arrays must be sorted in ascending order, and 1 if the arrays must be sorted in descending order. Each line _i_ of the next _n_ lines contains _m_ integers separated by a space, representing the _i_-th array. For each element _x_ of the array _i_, 1u2009≤u2009_x_u2009≤u2009106 holds. Output On the first line of the output print an integer _p_, the size of the array (_p_ can be at most ). Each of the next _p_ lines must contain two distinct integers _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_m_,u2009_i_u2009≠u2009_j_), representing the chosen indices. If there are multiple correct answers, you can print any. Examples Input 2 5 0 1 3 2 5 4 1 4 3 2 5 Note Consider the first sample. After the first operation, the arrays become [1,u20093,u20092,u20095,u20094] and [1,u20092,u20093,u20094,u20095]. After the second operation, the arrays become [1,u20092,u20093,u20095,u20094] and [1,u20092,u20093,u20094,u20095]. After the third operation they become [1,u20092,u20093,u20094,u20095] and [1,u20092,u20093,u20094,u20095]. | 1,500 | false | true | true | false | false | false | false | false | true | false | 8,292 |
769D | Vasya has the sequence consisting of _n_ integers. Vasya consider the pair of integers _x_ and _y_ k-interesting, if their binary representation differs from each other exactly in _k_ bits. For example, if _k_u2009=u20092, the pair of integers _x_u2009=u20095 and _y_u2009=u20093 is k-interesting, because their binary representation _x_=101 and _y_=011 differs exactly in two bits. Vasya wants to know how many pairs of indexes (_i_, _j_) are in his sequence so that _i_u2009<u2009_j_ and the pair of integers _a__i_ and _a__j_ is k-interesting. Your task is to help Vasya and determine this number. Input The first line contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009105, 0u2009≤u2009_k_u2009≤u200914) — the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ. The second line contains the sequence _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009104), which Vasya has. Output Print the number of pairs (_i_, _j_) so that _i_u2009<u2009_j_ and the pair of integers _a__i_ and _a__j_ is k-interesting. Examples Input 6 0 200 100 100 100 200 200 Note In the first test there are 4 k-interesting pairs: (1, 3), (1, 4), (2, 3), (2, 4). In the second test _k_u2009=u20090. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs: (1, 5), (1, 6), (2, 3), (2, 4), (3, 4), (5, 6). | 1,700 | false | false | false | false | false | false | true | false | false | false | 6,703 |
1370D | # Odd-Even Subsequence Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Ashish has an array a of size n.A subsequence of a is defined as a sequence that can be obtained from a by deleting some elements (possibly none), without changing the order of the remaining elements. Consider a subsequence s of a. He defines the cost of s as the minimum between: • The maximum among all elements at odd indices of s. • The maximum among all elements at even indices of s.Note that the index of an element is its index in s, rather than its index in a. The positions are numbered from 1. So, the cost of s is equal to min (max (s1, s 3, s 5, . . . ), max (s2, s 4, s 6, . . . )) .For example, the cost of {7, 5, 6} is min (max (7 , 6) , max (5)) = min (7 , 5) = 5 .Help him find the minimum cost of a subsequence of size k. # Input The first line contains two integers n and k (2 ≤ k ≤ n ≤ 2 · 10 5) x16 the size of the array a and the size of the subsequence. The next line contains n integers a1, a 2, . . . , a n (1 ≤ ai ≤ 10 9) x16 the elements of the array a. # Output Output a single integer x16 the minimum cost of a subsequence of size k. # Examples standard input standard output 4 2 1 2 3 4 14 3 1 2 3 4 25 3 5 3 4 2 6 26 4 5 3 50 2 4 5 3 # Note In the first test, consider the subsequence s = {1, 3}. Here the cost is equal to min (max (1) , max (3)) = 1 .In the second test, consider the subsequence s = {1, 2, 4}. Here the cost is equal to min (max (1 , 4) , max (2)) = 2 .In the fourth test, consider the subsequence s = {3, 50 , 2, 4}. Here the cost is equal to min (max (3 , 2) , max (50 , 4)) = 3 . Page 1 of 1 | 2,000 | false | true | true | true | false | false | false | true | false | false | 3,818 |
743D | Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes. They took _n_ prizes for the contestants and wrote on each of them a unique id (integer from 1 to _n_). A gift _i_ is characterized by integer _a__i_xa0— pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with _n_ vertices. The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts. Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible. Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20092·105)xa0— the number of gifts. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109)xa0— the pleasantness of the gifts. The next (_n_u2009-u20091) lines contain two numbers each. The _i_-th of these lines contains integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_)xa0— the description of the tree's edges. It means that gifts with numbers _u__i_ and _v__i_ are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: _v__i_ hangs on _u__i_ or _u__i_ hangs on _v__i_. It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts. Output If it is possible for Chloe and Vladik to choose prizes without fighting, print single integerxa0— the maximum possible sum of pleasantness they can get together. Otherwise print Impossible. | 1,800 | false | false | false | true | false | false | false | false | false | true | 6,826 |
1146A | Problem - 1146A - Codeforces =============== xa0 ]( "Forethought Future Cup") are not. Alice can erase some characters from her string $$$s$$$. She would like to know what is the longest string remaining after erasing some characters (possibly zero) to get a good string. It is guaranteed that the string has at least one "a" in it, so the answer always exists. Input The first line contains a string $$$s$$$ ($$$1 leq s leq 50$$$) consisting of lowercase English letters. It is guaranteed that there is at least one "a" in $$$s$$$. Output Print a single integer, the length of the longest good string that Alice can get after erasing some characters from $$$s$$$. Examples Input xaxxxxa Output 3 Input aaabaa Output 6 Note In the first example, it's enough to erase any four of the "x"s. The answer is $$$3$$$ since that is the maximum number of characters that can remain. In the second example, we don't need to erase any characters. | 800 | false | false | true | false | false | false | false | false | false | false | 4,991 |
2023C | You are given two strongly connected$$$^{dagger}$$$ directed graphs, each with exactly $$$n$$$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature — the length of any cycle in these graphs is divisible by $$$k$$$. Each of the $$$2n$$$ vertices belongs to exactly one of two types: incoming or outgoing. For each vertex, its type is known to you. You need to determine whether it is possible to draw exactly $$$n$$$ directed edges between the source graphs such that the following four conditions are met: The ends of any added edge lie in different graphs. From each outgoing vertex, exactly one added edge originates. Into each incoming vertex, exactly one added edge enters. In the resulting graph, the length of any cycle is divisible by $$$k$$$. $$$^{dagger}$$$A strongly connected graph is a graph in which there is a path from every vertex to every other vertex. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 2 cdot 10^5$$$) — the number of vertices in each graph and the value by which the length of each cycle is divisible. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i in {0, 1}$$$). If $$$a_i = 0$$$, then vertex $$$i$$$ of the first graph is incoming. If $$$a_i = 1$$$, then vertex $$$i$$$ of the first graph is outgoing. The third line of each test case contains a single integer $$$m_1$$$ ($$$1 le m_1 le 5 cdot 10^5$$$) — the number of edges in the first graph. The next $$$m_1$$$ lines contain descriptions of the edges of the first graph. The $$$i$$$-th of them contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 le v_i, u_i le n$$$) — an edge in the first graph leading from vertex $$$v_i$$$ to vertex $$$u_i$$$. Next, in the same format, follows the description of the second graph. The next line contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$b_i in {0, 1}$$$). If $$$b_i = 0$$$, then vertex $$$i$$$ of the second graph is incoming. If $$$b_i = 1$$$, then vertex $$$i$$$ of the second graph is outgoing. The next line contains a single integer $$$m_2$$$ ($$$1 le m_2 le 5 cdot 10^5$$$) — the number of edges in the second graph. The next $$$m_2$$$ lines contain descriptions of the edges of the second graph. The $$$i$$$-th of them contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 le v_i, u_i le n$$$) — an edge in the second graph leading from vertex $$$v_i$$$ to vertex $$$u_i$$$. It is guaranteed that both graphs are strongly connected, and the lengths of all cycles are divisible by $$$k$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. It is guaranteed that the sum of $$$m_1$$$ and the sum of $$$m_2$$$ over all test cases does not exceed $$$5 cdot 10^5$$$. Output For each test case, output "YES" (without quotes) if it is possible to draw $$$n$$$ new edges such that all conditions are met, and "NO" (without quotes) otherwise. You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Note In the first test case, it is possible to draw edges from the first graph to the second graph as $$$(1, 3)$$$ and $$$(4, 2)$$$ (the first number in the pair is the vertex number in the first graph, and the second number in the pair is the vertex number in the second graph), and from the second graph to the first graph as $$$(1, 2)$$$, $$$(4, 3)$$$ (the first number in the pair is the vertex number in the second graph, and the second number in the pair is the vertex number in the first graph). In the second test case, there are a total of $$$4$$$ incoming vertices and $$$2$$$ outgoing vertices, so it is not possible to draw $$$3$$$ edges. | 2,400 | false | true | true | false | false | true | false | false | false | true | 129 |
364A | Problem - 364A - 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 data structures implementation *1600 No tag edit access → Contest materials that are the elements of the rectangle equals _a_ in each rectangle. A rectangle in a matrix is a group of four integers (_x_,u2009_y_,u2009_z_,u2009_t_) (_x_u2009≤u2009_y_,u2009_z_u2009≤u2009_t_). The elements of the rectangle are all cells (_i_,u2009_j_) such that _x_u2009≤u2009_i_u2009≤u2009_y_,u2009_z_u2009≤u2009_j_u2009≤u2009_t_. Input The first line contains integer _a_ (0u2009≤u2009_a_u2009≤u2009109), the second line contains a string of decimal integers _s_ (1u2009≤u2009_s_u2009≤u20094000). Output Print a single integer — the answer to a problem. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 10 12345 Output 6 Input 16 439873893693495623498263984765 Output 40 | 1,600 | false | false | true | false | true | false | false | false | false | false | 8,377 |
757A | Bash wants to become a Pokemon master one day. Although he liked a lot of Pokemon, he has always been fascinated by Bulbasaur the most. Soon, things started getting serious and his fascination turned into an obsession. Since he is too young to go out and catch Bulbasaur, he came up with his own way of catching a Bulbasaur. Each day, he takes the front page of the newspaper. He cuts out the letters one at a time, from anywhere on the front page of the newspaper to form the word "Bulbasaur" (without quotes) and sticks it on his wall. Bash is very particular about casexa0— the first letter of "Bulbasaur" must be upper case and the rest must be lower case. By doing this he thinks he has caught one Bulbasaur. He then repeats this step on the left over part of the newspaper. He keeps doing this until it is not possible to form the word "Bulbasaur" from the newspaper. Given the text on the front page of the newspaper, can you tell how many Bulbasaurs he will catch today? Note: uppercase and lowercase letters are considered different. Input Input contains a single line containing a string _s_ (1u2009u2009≤u2009u2009_s_u2009u2009≤u2009u2009105)xa0— the text on the front page of the newspaper without spaces and punctuation marks. _s_ is the length of the string _s_. The string _s_ contains lowercase and uppercase English letters, i.e. . Output Output a single integer, the answer to the problem. Examples Input aBddulbasaurrgndgbualdBdsagaurrgndbb Note In the first case, you could pick: Bulbbasaur. In the second case, there is no way to pick even a single Bulbasaur. In the third case, you can rearrange the string to BulbasaurBulbasauraddrgndgddgargndbb to get two words "Bulbasaur". | 1,000 | false | false | true | false | false | false | false | false | false | false | 6,764 |
1815F | You are given an undirected graph with $$$n$$$ vertices and $$$3m$$$ edges. The graph may contain multi-edges, but does not contain self-loops. The graph satisfies the following property: the given edges can be divided into $$$m$$$ groups of $$$3$$$, such that each group is a triangle. A triangle is defined as three edges $$$(a,b)$$$, $$$(b,c)$$$ and $$$(c,a)$$$ for some three distinct vertices $$$a,b,c$$$ ($$$1 leq a,b,c leq n$$$). Initially, each vertex $$$v$$$ has a non-negative integer weight $$$a_v$$$. For every edge $$$(u,v)$$$ in the graph, you should perform the following operation exactly once: Choose an integer $$$x$$$ between $$$1$$$ and $$$4$$$. Then increase both $$$a_u$$$ and $$$a_v$$$ by $$$x$$$. After performing all operations, the following requirement should be satisfied: if $$$u$$$ and $$$v$$$ are connected by an edge, then $$$a_u e a_v$$$. It can be proven this is always possible under the constraints of the task. Output a way to do so, by outputting the choice of $$$x$$$ for each edge. It is easy to see that the order of operations does not matter. If there are multiple valid answers, output any. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$3 le n le 10^6$$$, $$$1 le m le 4 cdot 10^5$$$) — denoting the graph have $$$n$$$ vertices and $$$3m$$$ edges. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$0 leq a_i leq 10^6$$$) — the initial weights of each vertex. Then $$$m$$$ lines follow. The $$$i$$$-th line contains three integers $$$a_i$$$, $$$b_i$$$, $$$c_i$$$ ($$$1 leq a_i < b_i < c_i leq n$$$) — denotes that three edges $$$(a_i,b_i)$$$, $$$(b_i,c_i)$$$ and $$$(c_i,a_i)$$$. Note that the graph may contain multi-edges: a pair $$$(x,y)$$$ may appear in multiple triangles. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$4 cdot 10^5$$$. Output For each test case, output $$$m$$$ lines of $$$3$$$ integers each. The $$$i$$$-th line should contains three integers $$$e_{ab},e_{bc},e_{ca}$$$ ($$$1 leq e_{ab}, e_{bc} , e_{ca} leq 4$$$), denoting the choice of value $$$x$$$ for edges $$$(a_i, b_i)$$$, $$$(b_i,c_i)$$$ and $$$(c_i, a_i)$$$ respectively. Example Input 4 4 1 0 0 0 0 1 2 3 5 2 0 0 0 0 0 1 2 3 1 4 5 4 4 3 4 5 6 1 2 3 1 2 4 1 3 4 2 3 4 5 4 0 1000000 412 412 412 1 2 3 1 4 5 2 4 5 3 4 5 Output 2 1 3 2 3 3 4 3 3 3 1 2 2 2 3 2 3 4 3 1 1 2 3 4 1 2 4 4 4 3 4 1 1 Note In the first test case, the initial weights are $$$[0,0,0,0]$$$. We have added values as follows: Added $$$2$$$ to vertices $$$1$$$ and $$$2$$$ Added $$$1$$$ to vertices $$$2$$$ and $$$3$$$ Added $$$3$$$ to vertices $$$3$$$ and $$$1$$$ The final weights are $$$[5,3,4,0]$$$. The output is valid because $$$a_1 eq a_2$$$, $$$a_1 eq a_3$$$, $$$a_2 eq a_3$$$, and that all chosen values are between $$$1$$$ and $$$4$$$. In the second test case, the initial weights are $$$[0,0,0,0,0]$$$. The weights after the operations are $$$[12,5,6,7,6]$$$. The output is valid because $$$a_1 eq a_2$$$, $$$a_1 eq a_3$$$, $$$a_2 eq a_3$$$, and that $$$a_1 eq a_4$$$, $$$a_1 eq a_5$$$, $$$a_4 eq a_5$$$, and that all chosen values are between $$$1$$$ and $$$4$$$. In the third test case, the initial weights are $$$[3,4,5,6]$$$. The weights after the operations are $$$[19,16,17,20]$$$, and all final weights are distinct, which means no two adjacent vertices have the same weight. | 3,500 | true | false | false | false | false | true | false | false | false | true | 1,376 |
1700A | You are given a table $$$a$$$ of size $$$n imes m$$$. We will consider the table rows numbered from top to bottom from $$$1$$$ to $$$n$$$, and the columns numbered from left to right from $$$1$$$ to $$$m$$$. We will denote a cell that is in the $$$i$$$-th row and in the $$$j$$$-th column as $$$(i, j)$$$. In the cell $$$(i, j)$$$ there is written a number $$$(i - 1) cdot m + j$$$, that is $$$a_{ij} = (i - 1) cdot m + j$$$. A turtle initially stands in the cell $$$(1, 1)$$$ and it wants to come to the cell $$$(n, m)$$$. From the cell $$$(i, j)$$$ it can in one step go to one of the cells $$$(i + 1, j)$$$ or $$$(i, j + 1)$$$, if it exists. A path is a sequence of cells in which for every two adjacent in the sequence cells the following satisfies: the turtle can reach from the first cell to the second cell in one step. A cost of a path is the sum of numbers that are written in the cells of the path. For example, with $$$n = 2$$$ and $$$m = 3$$$ the table will look as shown above. The turtle can take the following path: $$$(1, 1) ightarrow (1, 2) ightarrow (1, 3) ightarrow (2, 3)$$$. The cost of such way is equal to $$$a_{11} + a_{12} + a_{13} + a_{23} = 12$$$. On the other hand, the paths $$$(1, 1) ightarrow (1, 2) ightarrow (2, 2) ightarrow (2, 1)$$$ and $$$(1, 1) ightarrow (1, 3)$$$ are incorrect, because in the first path the turtle can't make a step $$$(2, 2) ightarrow (2, 1)$$$, and in the second path it can't make a step $$$(1, 1) ightarrow (1, 3)$$$. You are asked to tell the turtle a minimal possible cost of a path from the cell $$$(1, 1)$$$ to the cell $$$(n, m)$$$. Please note that the cells $$$(1, 1)$$$ and $$$(n, m)$$$ are a part of the way. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of test cases. The description of test cases follows. A single line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^4$$$) — the number of rows and columns of the table $$$a$$$ respectively. Note In the first test case the only possible path consists of a single cell $$$(1, 1)$$$. The path with the minimal cost in the second test case is shown in the statement. In the fourth and the fifth test cases there is only one path from $$$(1, 1)$$$ to $$$(n, m)$$$. Both paths visit every cell in the table. | 800 | true | true | false | false | false | true | false | false | false | false | 2,082 |
106D | Our brave travelers reached an island where pirates had buried treasure. However as the ship was about to moor, the captain found out that some rat ate a piece of the treasure map. The treasure map can be represented as a rectangle _n_u2009×u2009_m_ in size. Each cell stands for an islands' square (the square's side length equals to a mile). Some cells stand for the sea and they are impenetrable. All other cells are penetrable (i.e. available) and some of them contain local sights. For example, the large tree on the hills or the cave in the rocks. Besides, the map also has a set of _k_ instructions. Each instruction is in the following form: "Walk _n_ miles in the _y_ direction" The possible directions are: north, south, east, and west. If you follow these instructions carefully (you should fulfill all of them, one by one) then you should reach exactly the place where treasures are buried. Unfortunately the captain doesn't know the place where to start fulfilling the instructions — as that very piece of the map was lost. But the captain very well remembers that the place contained some local sight. Besides, the captain knows that the whole way goes through the island's penetrable squares. The captain wants to know which sights are worth checking. He asks you to help him with that. Input The first line contains two integers _n_ and _m_ (3u2009≤u2009_n_,u2009_m_u2009≤u20091000). Then follow _n_ lines containing _m_ integers each — the island map's description. "#" stands for the sea. It is guaranteed that all cells along the rectangle's perimeter are the sea. "." stands for a penetrable square without any sights and the sights are marked with uppercase Latin letters from "A" to "Z". Not all alphabet letters can be used. However, it is guaranteed that at least one of them is present on the map. All local sights are marked by different letters. The next line contains number _k_ (1u2009≤u2009_k_u2009≤u2009105), after which _k_ lines follow. Each line describes an instruction. Each instruction possesses the form "_dir_ _len_", where _dir_ stands for the direction and _len_ stands for the length of the way to walk. _dir_ can take values "N", "S", "W" and "E" for North, South, West and East correspondingly. At that, north is to the top, South is to the bottom, west is to the left and east is to the right. _len_ is an integer from 1 to 1000. Output Print all local sights that satisfy to the instructions as a string without any separators in the alphabetical order. If no sight fits, print "no solution" without the quotes. Examples Input 6 10 ########## #K#..##### #.#..##.## #..L.#...# ###D###A.# ########## 4 N 2 S 1 E 1 W 2 Input 3 4 #### #.A# #### 2 W 1 N 2 | 1,700 | false | false | true | false | false | false | true | false | false | false | 9,458 |
869E | Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around the abandoned Eikou Cram School building, Oshino's makeshift residence. The space is represented by a rectangular grid of _n_u2009×u2009_m_ cells, arranged into _n_ rows and _m_ columns. The _c_-th cell in the _r_-th row is denoted by (_r_,u2009_c_). Oshino places and removes barriers around rectangular areas of cells. Specifically, an action denoted by "1 _r_1 _c_1 _r_2 _c_2" means Oshino's placing barriers around a rectangle with two corners being (_r_1,u2009_c_1) and (_r_2,u2009_c_2) and sides parallel to squares sides. Similarly, "2 _r_1 _c_1 _r_2 _c_2" means Oshino's removing barriers around the rectangle. Oshino ensures that no barriers staying on the ground share any common points, nor do they intersect with boundaries of the _n_u2009×u2009_m_ area. Sometimes Koyomi tries to walk from one cell to another carefully without striding over barriers, in order to avoid damaging various items on the ground. "3 _r_1 _c_1 _r_2 _c_2" means that Koyomi tries to walk from (_r_1,u2009_c_1) to (_r_2,u2009_c_2) without crossing barriers. And you're here to tell Koyomi the feasibility of each of his attempts. Input The first line of input contains three space-separated integers _n_, _m_ and _q_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092u2009500, 1u2009≤u2009_q_u2009≤u2009100u2009000) — the number of rows and columns in the grid, and the total number of Oshino and Koyomi's actions, respectively. The following _q_ lines each describes an action, containing five space-separated integers _t_, _r_1, _c_1, _r_2, _c_2 (1u2009≤u2009_t_u2009≤u20093, 1u2009≤u2009_r_1,u2009_r_2u2009≤u2009_n_, 1u2009≤u2009_c_1,u2009_c_2u2009≤u2009_m_) — the type and two coordinates of an action. Additionally, the following holds depending on the value of _t_: If _t_u2009=u20091: 2u2009≤u2009_r_1u2009≤u2009_r_2u2009≤u2009_n_u2009-u20091, 2u2009≤u2009_c_1u2009≤u2009_c_2u2009≤u2009_m_u2009-u20091; If _t_u2009=u20092: 2u2009≤u2009_r_1u2009≤u2009_r_2u2009≤u2009_n_u2009-u20091, 2u2009≤u2009_c_1u2009≤u2009_c_2u2009≤u2009_m_u2009-u20091, the specified group of barriers exist on the ground before the removal. If _t_u2009=u20093: no extra restrictions. Output For each of Koyomi's attempts (actions with _t_u2009=u20093), output one line — containing "Yes" (without quotes) if it's feasible, and "No" (without quotes) otherwise. Examples Input 5 6 5 1 2 2 4 5 1 3 3 3 3 3 4 4 1 1 2 2 2 4 5 3 1 1 4 4 Input 2500 2500 8 1 549 1279 1263 2189 1 303 795 1888 2432 1 2227 622 2418 1161 3 771 2492 1335 1433 1 2017 2100 2408 2160 3 48 60 798 729 1 347 708 1868 792 3 1940 2080 377 1546 Note For the first example, the situations of Koyomi's actions are illustrated below. | 2,400 | false | false | false | false | true | false | false | false | false | false | 6,262 |
229D | The city of D consists of _n_ towers, built consecutively on a straight line. The height of the tower that goes _i_-th (from left to right) in the sequence equals _h__i_. The city mayor decided to rebuild the city to make it beautiful. In a beautiful city all towers are are arranged in non-descending order of their height from left to right. The rebuilding consists of performing several (perhaps zero) operations. An operation constitutes using a crane to take any tower and put it altogether on the top of some other neighboring tower. In other words, we can take the tower that stands _i_-th and put it on the top of either the (_i_u2009-u20091)-th tower (if it exists), or the (_i_u2009+u20091)-th tower (of it exists). The height of the resulting tower equals the sum of heights of the two towers that were put together. After that the two towers can't be split by any means, but more similar operations can be performed on the resulting tower. Note that after each operation the total number of towers on the straight line decreases by 1. Help the mayor determine the minimum number of operations required to make the city beautiful. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095000)xa0— the number of towers in the city. The next line contains _n_ space-separated integers: the _i_-th number _h__i_ (1u2009≤u2009_h__i_u2009≤u2009105) determines the height of the tower that is _i_-th (from left to right) in the initial tower sequence. Output Print a single integer — the minimum number of operations needed to make the city beautiful. | 2,100 | false | true | false | true | false | false | false | false | false | false | 8,920 |
1477D | Nezzar designs a brand new game "Hidden Permutations" and shares it with his best friend, Nanako. At the beginning of the game, Nanako and Nezzar both know integers $$$n$$$ and $$$m$$$. The game goes in the following way: Firstly, Nezzar hides two permutations $$$p_1,p_2,ldots,p_n$$$ and $$$q_1,q_2,ldots,q_n$$$ of integers from $$$1$$$ to $$$n$$$, and Nanako secretly selects $$$m$$$ unordered pairs $$$(l_1,r_1),(l_2,r_2),ldots,(l_m,r_m)$$$; After that, Nanako sends his chosen pairs to Nezzar; On receiving those $$$m$$$ unordered pairs, Nezzar checks if there exists $$$1 le i le m$$$, such that $$$(p_{l_i}-p_{r_i})$$$ and $$$(q_{l_i}-q_{r_i})$$$ have different signs. If so, Nezzar instantly loses the game and gets a score of $$$-1$$$. Otherwise, the score Nezzar gets is equal to the number of indices $$$1 le i le n$$$ such that $$$p_i eq q_i$$$. However, Nezzar accidentally knows Nanako's unordered pairs and decides to take advantage of them. Please help Nezzar find out two permutations $$$p$$$ and $$$q$$$ such that the score is maximized. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 5 cdot 10^5$$$) — the number of test cases. The first line of each test case contains two integers $$$n,m$$$ ($$$1 le n le 5 cdot 10^5, 0 le m le min(frac{n(n-1)}{2},5 cdot 10^5)$$$). Then $$$m$$$ lines follow, $$$i$$$-th of them contains two integers $$$l_i,r_i$$$ ($$$1 le l_i,r_i le n$$$, $$$l_i eq r_i$$$), describing the $$$i$$$-th unordered pair Nanako chooses. It is guaranteed that all $$$m$$$ unordered pairs are distinct. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$5 cdot 10^5$$$, and the sum of $$$m$$$ for all test cases does not exceed $$$5cdot 10^5$$$. Output For each test case, print two permutations $$$p_1,p_2,ldots,p_n$$$ and $$$q_1,q_2,ldots,q_n$$$ such that the score Nezzar gets is maximized. Example Input 3 4 2 1 2 3 4 6 4 1 2 1 3 3 5 3 6 2 1 1 2 Output 1 2 3 4 3 4 1 2 2 3 4 1 6 5 1 4 3 2 5 6 1 2 1 2 Note For first test case, for each pair given by Nanako: for the first pair $$$(1,2)$$$: $$$p_1 - p_2 = 1 - 2 = -1$$$, $$$q_1 - q_2 = 3 - 4 = -1$$$, they have the same sign; for the second pair $$$(3,4)$$$: $$$p_3 - p_4 = 3 - 4 = -1$$$, $$$q_3 - q_4 = 1 - 2 = -1$$$, they have the same sign. As Nezzar does not lose instantly, Nezzar gains the score of $$$4$$$ as $$$p_i eq q_i$$$ for all $$$1 leq i leq 4$$$. Obviously, it is the maximum possible score Nezzar can get. | 2,800 | false | false | false | false | false | true | false | false | false | true | 3,287 |
158C | Vasya is writing an operating system shell, and it should have commands for working with directories. To begin with, he decided to go with just two commands: cd (change the current directory) and pwd (display the current directory). Directories in Vasya's operating system form a traditional hierarchical tree structure. There is a single root directory, denoted by the slash character "/". Every other directory has a name — a non-empty string consisting of lowercase Latin letters. Each directory (except for the root) has a parent directory — the one that contains the given directory. It is denoted as "..". The command cd takes a single parameter, which is a path in the file system. The command changes the current directory to the directory specified by the path. The path consists of the names of directories separated by slashes. The name of the directory can be "..", which means a step up to the parent directory. «..» can be used in any place of the path, maybe several times. If the path begins with a slash, it is considered to be an absolute path, that is, the directory changes to the specified one, starting from the root. If the parameter begins with a directory name (or ".."), it is considered to be a relative path, that is, the directory changes to the specified directory, starting from the current one. The command pwd should display the absolute path to the current directory. This path must not contain "..". Initially, the current directory is the root. All directories mentioned explicitly or passed indirectly within any command cd are considered to exist. It is guaranteed that there is no attempt of transition to the parent directory of the root directory. Input The first line of the input data contains the single integer _n_ (1u2009≤u2009_n_u2009≤u200950) — the number of commands. Then follow _n_ lines, each contains one command. Each of these lines contains either command pwd, or command cd, followed by a space-separated non-empty parameter. The command parameter cd only contains lower case Latin letters, slashes and dots, two slashes cannot go consecutively, dots occur only as the name of a parent pseudo-directory. The command parameter cd does not end with a slash, except when it is the only symbol that points to the root directory. The command parameter has a length from 1 to 200 characters, inclusive. Directories in the file system can have the same names. Output For each command pwd you should print the full absolute path of the given directory, ending with a slash. It should start with a slash and contain the list of slash-separated directories in the order of being nested from the root to the current folder. It should contain no dots. | 1,400 | false | false | true | false | true | false | false | false | false | false | 9,243 |
587D | Duff is one if the heads of Mafia in her country, Andarz Gu. Andarz Gu has _n_ cities (numbered from 1 to _n_) connected by _m_ bidirectional roads (numbered by 1 to _m_). Each road has a destructing time, and a color. _i_-th road connects cities _v__i_ and _u__i_ and its color is _c__i_ and its destructing time is _t__i_. Mafia wants to destruct a matching in Andarz Gu. A matching is a subset of roads such that no two roads in this subset has common endpoint. They can destruct these roads in parallel, i. e. the total destruction time is a maximum over destruction times of all selected roads. They want two conditions to be satisfied: 1. The remaining roads form a proper coloring. 2. Destructing time of this matching is minimized. The remaining roads after destructing this matching form a proper coloring if and only if no two roads of the same color have same endpoint, or, in the other words, edges of each color should form a matching. There is no programmer in Mafia. That's why Duff asked for your help. Please help her and determine which matching to destruct in order to satisfied those conditions (or state that this is not possible). Input The first line of input contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u20095u2009×u2009104 and 1u2009≤u2009_m_u2009≤u20095u2009×u2009104), number of cities and number of roads in the country. The next _m_ lines contain the the roads. _i_u2009-u2009_th_ of them contains four integers _v__i_,u2009_u__i_,u2009_c__i_ and _t__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_, _v__i_u2009≠u2009_u__i_ and 1u2009≤u2009_c__i_,u2009_t__i_u2009≤u2009109 for each 1u2009≤u2009_i_u2009≤u2009_m_). Output In the first line of input, print "Yes" (without quotes) if satisfying the first condition is possible and "No" (without quotes) otherwise. If it is possible, then you have to print two integers _t_ and _k_ in the second line, the minimum destructing time and the number of roads in the matching (). In the third line print _k_ distinct integers separated by spaces, indices of the roads in the matching in any order. Roads are numbered starting from one in order of their appearance in the input. If there's more than one solution, print any of them. Examples Input 5 7 2 1 3 7 3 1 1 6 5 4 1 8 4 5 1 1 3 2 2 3 4 5 2 5 2 3 2 4 Input 3 5 3 2 1 3 1 3 1 1 3 2 1 4 1 3 2 2 1 3 2 10 Note Graph of Andarz Gu in the first sample case is as follows: A solution would be to destruct the roads with crosses. Graph of Andarz Gu in the second sample case is as follows: | 3,100 | false | false | false | false | false | false | false | true | false | false | 7,494 |
149B | Having stayed home alone, Petya decided to watch forbidden films on the Net in secret. "What ungentlemanly behavior!" — you can say that, of course, but don't be too harsh on the kid. In his country films about the Martians and other extraterrestrial civilizations are forbidden. It was very unfair to Petya as he adored adventure stories that featured lasers and robots. Today Petya is watching a shocking blockbuster about the Martians called "R2:D2". What can "R2:D2" possibly mean? It might be the Martian time represented in the Martian numeral system. Petya knows that time on Mars is counted just like on the Earth (that is, there are 24 hours and each hour has 60 minutes). The time is written as "_a_:_b_", where the string _a_ stands for the number of hours (from 0 to 23 inclusive), and string _b_ stands for the number of minutes (from 0 to 59 inclusive). The only thing Petya doesn't know is in what numeral system the Martian time is written. Your task is to print the radixes of all numeral system which can contain the time "_a_:_b_". Input The first line contains a single string as "_a_:_b_" (without the quotes). There _a_ is a non-empty string, consisting of numbers and uppercase Latin letters. String _a_ shows the number of hours. String _b_ is a non-empty string that consists of numbers and uppercase Latin letters. String _b_ shows the number of minutes. The lengths of strings _a_ and _b_ are from 1 to 5 characters, inclusive. Please note that strings _a_ and _b_ can have leading zeroes that do not influence the result in any way (for example, string "008:1" in decimal notation denotes correctly written time). We consider characters 0, 1, ..., 9 as denoting the corresponding digits of the number's representation in some numeral system, and characters A, B, ..., Z correspond to numbers 10, 11, ..., 35. Output Print the radixes of the numeral systems that can represent the time "_a_:_b_" in the increasing order. Separate the numbers with spaces or line breaks. If there is no numeral system that can represent time "_a_:_b_", print the single integer 0. If there are infinitely many numeral systems that can represent the time "_a_:_b_", print the single integer -1. Note that on Mars any positional numeral systems with positive radix strictly larger than one are possible. Examples Output 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Note Let's consider the first sample. String "11:20" can be perceived, for example, as time 4:6, represented in the ternary numeral system or as time 17:32 in hexadecimal system. Let's consider the second sample test. String "2A:13" can't be perceived as correct time in any notation. For example, let's take the base-11 numeral notation. There the given string represents time 32:14 that isn't a correct time. Let's consider the third sample. String "000B:00001" can be perceived as a correct time in the infinite number of numeral systems. If you need an example, you can take any numeral system with radix no less than 12. | 1,600 | false | false | true | false | false | false | false | false | false | false | 9,280 |
356B | Xenia is an amateur programmer. Today on the IT lesson she learned about the Hamming distance. The Hamming distance between two strings _s_u2009=u2009_s_1_s_2... _s__n_ and _t_u2009=u2009_t_1_t_2... _t__n_ of equal length _n_ is value . Record [_s__i_u2009≠u2009_t__i_] is the Iverson notation and represents the following: if _s__i_u2009≠u2009_t__i_, it is one, otherwise — zero. Now Xenia wants to calculate the Hamming distance between two long strings _a_ and _b_. The first string _a_ is the concatenation of _n_ copies of string _x_, that is, . The second string _b_ is the concatenation of _m_ copies of string _y_. Help Xenia, calculate the required Hamming distance, given _n_,u2009_x_,u2009_m_,u2009_y_. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091012). The second line contains a non-empty string _x_. The third line contains a non-empty string _y_. Both strings consist of at most 106 lowercase English letters. It is guaranteed that strings _a_ and _b_ that you obtain from the input have the same length. Output Print a single integer — the required Hamming distance. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Note In the first test case string _a_ is the same as string _b_ and equals 100 letters a. As both strings are equal, the Hamming distance between them is zero. In the second test case strings _a_ and _b_ differ in their 3-rd, 5-th, 6-th and 7-th characters. Thus, the Hamming distance equals 4. In the third test case string _a_ is rzrrzr and string _b_ is azazaz. The strings differ in all characters apart for the second one, the Hamming distance between them equals 5. | 1,900 | true | false | true | false | false | false | false | false | false | false | 8,410 |
1423B | The pandemic is upon us, and the world is in shortage of the most important resource: toilet paper. As one of the best prepared nations for this crisis, BubbleLand promised to help all other world nations with this valuable resource. To do that, the country will send airplanes to other countries carrying toilet paper. In BubbleLand, there are $$$N$$$ toilet paper factories, and $$$N$$$ airports. Because of how much it takes to build a road, and of course legal issues, every factory must send paper to only one airport, and every airport can only take toilet paper from one factory. Also, a road can't be built between all airport-factory pairs, again because of legal issues. Every possible road has number $$$d$$$ given, number of days it takes to build that road. Your job is to choose $$$N$$$ factory-airport pairs, such that if the country starts building all roads at the same time, it takes the least amount of days to complete them. Input The first line contains two integers $$$N$$$ $$$(1 leq N leq 10^4)$$$ - number of airports/factories, and $$$M$$$ $$$(1 leq M leq 10^5)$$$ - number of available pairs to build a road between. On next $$$M$$$ lines, there are three integers $$$u$$$, $$$v$$$ $$$(1 leq u,v leq N)$$$, $$$d$$$ $$$(1 leq d leq 10^9)$$$ - meaning that you can build a road between airport $$$u$$$ and factory $$$v$$$ for $$$d$$$ days. Output If there are no solutions, output -1. If there exists a solution, output the minimal number of days to complete all roads, equal to maximal $$$d$$$ among all chosen roads. Example Input 3 5 1 2 1 2 3 2 3 3 3 2 1 4 2 2 5 | 1,900 | false | false | false | false | false | false | false | true | false | true | 3,562 |
224B | You've got an array _a_, consisting of _n_ integers: _a_1,u2009_a_2,u2009...,u2009_a__n_. Your task is to find a minimal by inclusion segment [_l_,u2009_r_] (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_) such, that among numbers _a__l_,u2009xa0_a__l_u2009+u20091,u2009xa0...,u2009xa0_a__r_ there are exactly _k_ distinct numbers. Segment [_l_,u2009_r_] (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_; _l_,u2009_r_ are integers) of length _m_u2009=u2009_r_u2009-u2009_l_u2009+u20091, satisfying the given property, is called minimal by inclusion, if there is no segment [_x_,u2009_y_] satisfying the property and less then _m_ in length, such that 1u2009≤u2009_l_u2009≤u2009_x_u2009≤u2009_y_u2009≤u2009_r_u2009≤u2009_n_. Note that the segment [_l_,u2009_r_] doesn't have to be minimal in length among all segments, satisfying the given property. Input The first line contains two space-separated integers: _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009105). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_xa0— elements of the array _a_ (1u2009≤u2009_a__i_u2009≤u2009105). Output Print a space-separated pair of integers _l_ and _r_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_) such, that the segment [_l_,u2009_r_] is the answer to the problem. If the sought segment does not exist, print "-1 -1" without the quotes. If there are multiple correct answers, print any of them. Note In the first sample among numbers _a_1 and _a_2 there are exactly two distinct numbers. In the second sample segment [2,u20095] is a minimal by inclusion segment with three distinct numbers, but it is not minimal in length among such segments. In the third sample there is no segment with four distinct numbers. | 1,500 | false | false | true | false | false | false | false | false | false | false | 8,941 |
120C | Problem - 120C - Codeforces =============== xa0 . The second line contains _n_ integers _a_1, _a_2, ..., _a__n_, separated by spaces (1u2009≤u2009_a__i_u2009≤u2009100). Output Print a single number — how many kilos of honey gets Piglet. Examples Input 3 3 15 8 10 Output 9 | 1,100 | true | false | true | false | false | false | false | false | false | false | 9,404 |
844A | Problem - 844A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy implementation strings *1000 No tag edit access → Contest materials . Second line of input contains integer _k_ (1u2009≤u2009_k_u2009≤u200926). Output Print single line with a minimum number of necessary changes, or the word «impossible» (without quotes) if it is impossible. Examples Input yandex 6 Output 0 Input yahoo 5 Output 1 Input google 7 Output impossible Note In the first test case string contains 6 different letters, so we don't need to change anything. In the second test case string contains 4 different letters: {'_a_',u2009'_h_',u2009'_o_',u2009'_y_'}. To get 5 different letters it is necessary to change one occurrence of '_o_' to some letter, which doesn't occur in the string, for example, {'_b_'}. In the third test case, it is impossible to make 7 different letters because the length of the string is 6. | 1,000 | false | true | true | false | false | false | false | false | false | false | 6,386 |
688A | Arya has _n_ opponents in the school. Each day he will fight with all opponents who are present this day. His opponents have some fighting plan that guarantees they will win, but implementing this plan requires presence of them all. That means if one day at least one of Arya's opponents is absent at the school, then Arya will beat all present opponents. Otherwise, if all opponents are present, then they will beat Arya. For each opponent Arya knows his schedulexa0— whether or not he is going to present on each particular day. Tell him the maximum number of consecutive days that he will beat all present opponents. Note, that if some day there are no opponents present, Arya still considers he beats all the present opponents. Input The first line of the input contains two integers _n_ and _d_ (1u2009≤u2009_n_,u2009_d_u2009≤u2009100)xa0— the number of opponents and the number of days, respectively. The _i_-th of the following _d_ lines contains a string of length _n_ consisting of characters '0' and '1'. The _j_-th character of this string is '0' if the _j_-th opponent is going to be absent on the _i_-th day. Output Print the only integerxa0— the maximum number of consecutive days that Arya will beat all present opponents. Examples Input 4 5 1101 1111 0110 1011 1111 Note In the first and the second samples, Arya will beat all present opponents each of the _d_ days. In the third sample, Arya will beat his opponents on days 1, 3 and 4 and his opponents will beat him on days 2 and 5. Thus, the maximum number of consecutive winning days is 2, which happens on days 3 and 4. | 800 | false | false | true | false | false | false | false | false | false | false | 7,071 |
1536E | Omkar's most recent follower, Ajit, has entered the Holy Forest. Ajit realizes that Omkar's forest is an $$$n$$$ by $$$m$$$ grid ($$$1 leq n, m leq 2000$$$) of some non-negative integers. Since the forest is blessed by Omkar, it satisfies some special conditions: 1. For any two adjacent (sharing a side) cells, the absolute value of the difference of numbers in them is at most $$$1$$$. 2. If the number in some cell is strictly larger than $$$0$$$, it should be strictly greater than the number in at least one of the cells adjacent to it. Unfortunately, Ajit is not fully worthy of Omkar's powers yet. He sees each cell as a "0" or a "#". If a cell is labeled as "0", then the number in it must equal $$$0$$$. Otherwise, the number in it can be any nonnegative integer. Determine how many different assignments of elements exist such that these special conditions are satisfied. Two assignments are considered different if there exists at least one cell such that the numbers written in it in these assignments are different. Since the answer may be enormous, find the answer modulo $$$10^9+7$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 100$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 2000, nm geq 2$$$) – the dimensions of the forest. $$$n$$$ lines follow, each consisting of one string of $$$m$$$ characters. Each of these characters is either a "0" or a "#". It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, print one integer: the number of valid configurations modulo $$$10^9+7$$$. Example Input 4 3 4 0000 00#0 0000 2 1 # # 1 2 ## 6 29 ############################# #000##0###0##0#0####0####000# #0#0##00#00##00####0#0###0#0# #0#0##0#0#0##00###00000##00## #000##0###0##0#0##0###0##0#0# ############################# Note For the first test case, the two valid assignments are $$$0000 0000 0000$$$ and $$$0000 0010 0000$$$ | 2,300 | true | false | false | false | false | false | false | false | false | true | 2,964 |
1883A | You are given a four-digit pin code consisting of digits from $$$0$$$ to $$$9$$$ that needs to be entered. Initially, the cursor points to the digit $$$1$$$. In one second, you can perform exactly one of the following two actions: Press the cursor to display the current digit, Move the cursor to any adjacent digit. The image above shows the device you are using to enter the pin code. For example, for the digit $$$5$$$, the adjacent digits are $$$4$$$ and $$$6$$$, and for the digit $$$0$$$, there is only one adjacent digit, $$$9$$$. Determine the minimum number of seconds required to enter the given four-digit pin code. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) - the number of the test cases. This is followed by their description. The single line of each test case describes the pin code as a string of length $$$4$$$, consisting of digits from $$$0$$$ to $$$9$$$. Output For each test case, output the minimum number of seconds required to enter the given pin code. Example Input 10 1111 1236 1010 1920 9273 0000 7492 8543 0294 8361 Output 4 9 31 27 28 13 25 16 33 24 Note In the first test case, the cursor needs to be pressed $$$4$$$ times. In the second test case, it can be done in $$$9$$$ seconds as follows: Press the cursor. Move the cursor to the digit $$$2$$$. Press the cursor. Move the cursor to the digit $$$3$$$. Press the cursor. Move the cursor to the digit $$$4$$$. Move the cursor to the digit $$$5$$$. Move the cursor to the digit $$$6$$$. Press the cursor. | 800 | true | false | false | false | false | false | false | false | false | false | 991 |
1761A | You are given three integers $$$n$$$, $$$a$$$, and $$$b$$$. Determine if there exist two permutations $$$p$$$ and $$$q$$$ of length $$$n$$$, for which the following conditions hold: The length of the longest common prefix of $$$p$$$ and $$$q$$$ is $$$a$$$. The length of the longest common suffix of $$$p$$$ and $$$q$$$ is $$$b$$$. A permutation of length $$$n$$$ is an array containing each integer from $$$1$$$ to $$$n$$$ exactly once. 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 Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1leq tleq 10^4$$$)xa0— the number of test cases. The description of test cases follows. The only line of each test case contains three integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1leq a,bleq nleq 100$$$). Output For each test case, if such a pair of permutations exists, output "Yes"; otherwise, output "No". You can output each letter in any case (upper or lower). Example Input 4 1 1 1 2 1 2 3 1 1 4 1 1 Note In the first test case, $$$[1]$$$ and $$$[1]$$$ form a valid pair. In the second test case and the third case, we can show that such a pair of permutations doesn't exist. In the fourth test case, $$$[1,2,3,4]$$$ and $$$[1,3,2,4]$$$ form a valid pair. | 800 | false | false | false | false | false | true | true | false | false | false | 1,748 |
1030F | There is an infinite line consisting of cells. There are $$$n$$$ boxes in some cells of this line. The $$$i$$$-th box stands in the cell $$$a_i$$$ and has weight $$$w_i$$$. All $$$a_i$$$ are distinct, moreover, $$$a_{i - 1} < a_i$$$ holds for all valid $$$i$$$. You would like to put together some boxes. Putting together boxes with indices in the segment $$$[l, r]$$$ means that you will move some of them in such a way that their positions will form some segment $$$[x, x + (r - l)]$$$. In one step you can move any box to a neighboring cell if it isn't occupied by another box (i.e. you can choose $$$i$$$ and change $$$a_i$$$ by $$$1$$$, all positions should remain distinct). You spend $$$w_i$$$ units of energy moving the box $$$i$$$ by one cell. You can move any box any number of times, in arbitrary order. Sometimes weights of some boxes change, so you have queries of two types: 1. $$$id$$$ $$$nw$$$ — weight $$$w_{id}$$$ of the box $$$id$$$ becomes $$$nw$$$. 2. $$$l$$$ $$$r$$$ — you should compute the minimum total energy needed to put together boxes with indices in $$$[l, r]$$$. Since the answer can be rather big, print the remainder it gives when divided by $$$1000,000,007 = 10^9 + 7$$$. Note that the boxes are not moved during the query, you only should compute the answer. Note that you should minimize the answer, not its remainder modulo $$$10^9 + 7$$$. So if you have two possible answers $$$2 cdot 10^9 + 13$$$ and $$$2 cdot 10^9 + 14$$$, you should choose the first one and print $$$10^9 + 6$$$, even though the remainder of the second answer is $$$0$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 2 cdot 10^5$$$) — the number of boxes and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots a_n$$$ ($$$1 le a_i le 10^9$$$) — the positions of the boxes. All $$$a_i$$$ are distinct, $$$a_{i - 1} < a_i$$$ holds for all valid $$$i$$$. The third line contains $$$n$$$ integers $$$w_1, w_2, dots w_n$$$ ($$$1 le w_i le 10^9$$$) — the initial weights of the boxes. Next $$$q$$$ lines describe queries, one query per line. Each query is described in a single line, containing two integers $$$x$$$ and $$$y$$$. If $$$x < 0$$$, then this query is of the first type, where $$$id = -x$$$, $$$nw = y$$$ ($$$1 le id le n$$$, $$$1 le nw le 10^9$$$). If $$$x > 0$$$, then the query is of the second type, where $$$l = x$$$ and $$$r = y$$$ ($$$1 le l_j le r_j le n$$$). $$$x$$$ can not be equal to $$$0$$$. Output For each query of the second type print the answer on a separate line. Since answer can be large, print the remainder it gives when divided by $$$1000,000,007 = 10^9 + 7$$$. Example Input 5 8 1 2 6 7 10 1 1 1 1 2 1 1 1 5 1 3 3 5 -3 5 -1 10 1 4 2 5 Note Let's go through queries of the example: 1. $$$1 1$$$ — there is only one box so we don't need to move anything. 2. $$$1 5$$$ — we can move boxes to segment $$$[4, 8]$$$: $$$1 cdot 1 - 4 + 1 cdot 2 - 5 + 1 cdot 6 - 6 + 1 cdot 7 - 7 + 2 cdot 10 - 8 = 10$$$. 3. $$$1 3$$$ — we can move boxes to segment $$$[1, 3]$$$. 4. $$$3 5$$$ — we can move boxes to segment $$$[7, 9]$$$. 5. $$$-3 5$$$ — $$$w_3$$$ is changed from $$$1$$$ to $$$5$$$. 6. $$$-1 10$$$ — $$$w_1$$$ is changed from $$$1$$$ to $$$10$$$. The weights are now equal to $$$w = [10, 1, 5, 1, 2]$$$. 7. $$$1 4$$$ — we can move boxes to segment $$$[1, 4]$$$. 8. $$$2 5$$$ — we can move boxes to segment $$$[5, 8]$$$. | 2,500 | false | false | false | false | true | false | false | false | false | false | 5,550 |
1029F | There is an infinite board of square tiles. Initially all tiles are white. Vova has a red marker and a blue marker. Red marker can color $$$a$$$ tiles. Blue marker can color $$$b$$$ tiles. If some tile isn't white then you can't use marker of any color on it. Each marker must be drained completely, so at the end there should be exactly $$$a$$$ red tiles and exactly $$$b$$$ blue tiles across the board. Vova wants to color such a set of tiles that: they would form a rectangle, consisting of exactly $$$a+b$$$ colored tiles; all tiles of at least one color would also form a rectangle. Here are some examples of correct colorings: Here are some examples of incorrect colorings: Among all correct colorings Vova wants to choose the one with the minimal perimeter. What is the minimal perimeter Vova can obtain? It is guaranteed that there exists at least one correct coloring. Input A single line contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^{14}$$$) — the number of tiles red marker should color and the number of tiles blue marker should color, respectively. Output Print a single integer — the minimal perimeter of a colored rectangle Vova can obtain by coloring exactly $$$a$$$ tiles red and exactly $$$b$$$ tiles blue. It is guaranteed that there exists at least one correct coloring. Note The first four examples correspond to the first picture of the statement. Note that for there exist multiple correct colorings for all of the examples. In the first example you can also make a rectangle with sides $$$1$$$ and $$$8$$$, though its perimeter will be $$$18$$$ which is greater than $$$8$$$. In the second example you can make the same resulting rectangle with sides $$$3$$$ and $$$4$$$, but red tiles will form the rectangle with sides $$$1$$$ and $$$3$$$ and blue tiles will form the rectangle with sides $$$3$$$ and $$$3$$$. | 2,000 | true | false | false | false | false | false | true | true | false | false | 5,556 |
1148A | Problem - 1148A - Codeforces =============== xa0 xa0— the number of strings "a", "b" and "ab" respectively. Output Print a single numberxa0— the maximum possible length of the good string you can obtain. Examples Input 1 1 1 Output 4 Input 2 1 2 Output 7 Input 3 5 2 Output 11 Input 2 2 1 Output 6 Input 1000000000 1000000000 1000000000 Output 4000000000 Note In the first example the optimal string is "baba". In the second example the optimal string is "abababa". In the third example the optimal string is "bababababab". In the fourth example the optimal string is "ababab". | 800 | false | true | false | false | false | false | false | false | false | false | 4,977 |
609A | Problem - 609A - Codeforces =============== xa0 ]( "Educational Codeforces Round 3") — the number of USB flash drives. The second line contains positive integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the size of Sean's file. Each of the next _n_ lines contains positive integer _a__i_ (1u2009≤u2009_a__i_u2009≤u20091000) — the sizes of USB flash drives in megabytes. It is guaranteed that the answer exists, i. e. the sum of all _a__i_ is not less than _m_. Output Print the minimum number of USB flash drives to write Sean's file, if he can split the file between drives. Examples Input 3 5 2 1 3 Output 2 Input 3 6 2 3 2 Output 3 Input 2 5 5 10 Output 1 Note In the first example Sean needs only two USB flash drives — the first and the third. In the second example Sean needs all three USB flash drives. In the third example Sean needs only one USB flash drive and he can use any available USB flash drive — the first or the second. | 800 | false | true | true | false | false | false | false | false | true | false | 7,412 |
1984C2 | The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved. You are given an array $$$a$$$ of length $$$n$$$. Start with $$$c = 0$$$. Then, for each $$$i$$$ from $$$1$$$ to $$$n$$$ (in increasing order) do exactly one of the following: Option $$$1$$$: set $$$c$$$ to $$$c + a_i$$$. Option $$$2$$$: set $$$c$$$ to $$$c + a_i$$$, where $$$x$$$ is the absolute value of $$$x$$$. Let the maximum final value of $$$c$$$ after the procedure described above be equal to $$$k$$$. Find the number of unique procedures that result in $$$c = k$$$. Two procedures are different if at any index $$$i$$$, one procedure chose option $$$1$$$ and another chose option $$$2$$$, even if the value of $$$c$$$ is equal for both procedures after that turn. Since the answer may be large, output it modulo $$$998,244,353$$$. 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 a single integer $$$n$$$ ($$$2 leq n leq 2 cdot 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$). The sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output a single integerxa0— the number of unique procedures that result in $$$c = k$$$, modulo $$$998,244,353$$$. Example Input 5 4 2 -5 3 -3 8 1 4 3 4 1 4 3 4 3 -1 -2 -3 4 -1000000000 1000000000 1000000000 1000000000 4 1 9 8 4 Note In the first test case, it can be shown that our maximal final value of $$$c$$$ is $$$3$$$. There are $$$12$$$ ways to achieve this because in order to get $$$3$$$, we have to take absolute value at indices $$$2$$$ or $$$4$$$, or both, resulting in $$$3$$$ ways. For the other two indices, it doesn't change the value whether we take absolute value or not, so we have $$$2 cdot 2 = 4$$$ ways for them. In total, we have $$$3 cdot 4 = 12$$$ ways. In the second test case, taking the absolute value will never change anything, so we can either take absolute value or not, for every index. This gives us $$$2^8 = 256$$$ possible ways. | 1,700 | true | true | false | true | false | false | false | false | false | false | 380 |
1673D | Long ago, you thought of two finite denoting the number of testcases. The first line of each testcase contains three integers $$$b$$$, $$$q$$$ and $$$y$$$ ($$$-10^9leq bleq 10^9$$$, $$$1leq qleq 10^9$$$, $$$2leq yleq 10^9$$$) denoting the first term, common difference and number of terms of $$$B$$$ respectively. The second line of each testcase contains three integers $$$c$$$, $$$r$$$ and $$$z$$$ ($$$-10^9leq cleq 10^9$$$, $$$1leq rleq 10^9$$$, $$$2leq zleq 10^9$$$) denoting the first term, common difference and number of terms of $$$C$$$ respectively. Output For each testcase, print a single line containing a single integer. If there are infinitely many finite arithmetic progressions which could be your lost progression $$$A$$$, print $$$-1$$$. Otherwise, print the number of finite arithmetic progressions which could be your lost progression $$$A$$$ modulo $$$10^9+7$$$. In particular, if there are no such finite arithmetic progressions, print $$$0$$$. Example Input 8 -3 1 7 -1 2 4 -9 3 11 0 6 3 2 5 5 7 5 4 2 2 11 10 5 3 0 2 9 2 4 3 -11 4 12 1 12 2 -27 4 7 -17 8 2 -8400 420 1000000000 0 4620 10 Output 0 10 -1 0 -1 21 0 273000 Note For the first testcase, $$$B={-3,-2,-1,0,1,2,3}$$$ and $$$C={-1,1,3,5}$$$. There is no such arithmetic progression which can be equal to $$$A$$$ because $$$5$$$ is not present in $$$B$$$ and for any $$$A$$$, $$$5$$$ should not be present in $$$C$$$ also. For the second testcase, $$$B={-9,-6,-3,0,3,6,9,12,15,18,21}$$$ and $$$C={0,6,12}$$$. There are $$$10$$$ possible arithmetic progressions which can be $$$A$$$: $$${0,6,12}$$$ $$${0,2,4,6,8,10,12}$$$ $$${0,2,4,6,8,10,12,14}$$$ $$${0,2,4,6,8,10,12,14,16}$$$ $$${-2,0,2,4,6,8,10,12}$$$ $$${-2,0,2,4,6,8,10,12,14}$$$ $$${-2,0,2,4,6,8,10,12,14,16}$$$ $$${-4,-2,0,2,4,6,8,10,12}$$$ $$${-4,-2,0,2,4,6,8,10,12,14}$$$ $$${-4,-2,0,2,4,6,8,10,12,14,16}$$$ For the third testcase, $$$B={2,7,12,17,22}$$$ and $$$C={7,12,17,22}$$$. There are infinitely many arithmetic progressions which can be $$$A$$$ like: $$${7,12,17,22}$$$ $$${7,12,17,22,27}$$$ $$${7,12,17,22,27,32}$$$ $$${7,12,17,22,27,32,37}$$$ $$${7,12,17,22,27,32,37,42}$$$ $$$ldots$$$ | 1,900 | true | false | false | false | false | false | false | false | false | false | 2,232 |
230A | Kirito is stuck on a level of the MMORPG he is playing now. To move on in the game, he's got to defeat all _n_ dragons that live on this level. Kirito and the dragons have strength, which is represented by an integer. In the duel between two opponents the duel's outcome is determined by their strength. Initially, Kirito's strength equals _s_. If Kirito starts duelling with the _i_-th (1u2009≤u2009_i_u2009≤u2009_n_) dragon and Kirito's strength is not greater than the dragon's strength _x__i_, then Kirito loses the duel and dies. But if Kirito's strength is greater than the dragon's strength, then he defeats the dragon and gets a bonus strength increase by _y__i_. Kirito can fight the dragons in any order. Determine whether he can move on to the next level of the game, that is, defeat all dragons without a single loss. Input The first line contains two space-separated integers _s_ and _n_ (1u2009≤u2009_s_u2009≤u2009104, 1u2009≤u2009_n_u2009≤u2009103). Then _n_ lines follow: the _i_-th line contains space-separated integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u2009104, 0u2009≤u2009_y__i_u2009≤u2009104) — the _i_-th dragon's strength and the bonus for defeating it. Output On a single line print "YES" (without the quotes), if Kirito can move on to the next level and print "NO" (without the quotes), if he can't. Note In the first sample Kirito's strength initially equals 2. As the first dragon's strength is less than 2, Kirito can fight it and defeat it. After that he gets the bonus and his strength increases to 2u2009+u200999u2009=u2009101. Now he can defeat the second dragon and move on to the next level. In the second sample Kirito's strength is too small to defeat the only dragon and win. | 1,000 | false | true | false | false | false | false | false | false | true | false | 8,918 |
2035C | Alya has been given a hard problem. Unfortunately, she is too busy running for student council. Please solve this problem for her. Given an integer $$$n$$$, construct a permutation $$$p$$$ of integers $$$1, 2, ldots, n$$$ that maximizes the value of $$$k$$$ (which is initially $$$0$$$) after the following process. Perform $$$n$$$ operations, on the $$$i$$$-th operation ($$$i=1, 2, dots, n$$$), If $$$i$$$ is odd, $$$k=k,&,p_i$$$, where $$$&$$$ denotes the xa0— the number of test cases. The only line of each test case contains a single integer $$$n$$$ ($$$5le nle 2 cdot 10^5$$$)xa0— the length of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the maximum value of $$$k$$$ in the first line and output the permutation $$$p_1, p_2,ldots, p_n$$$ in the second line. If there are multiple such permutations, output any. Example Output 5 2 1 3 4 5 7 1 2 4 6 5 3 7 2 4 5 1 3 6 7 15 2 4 5 1 3 6 7 8 9 2 4 5 6 7 1 3 8 9 15 1 2 3 4 5 6 8 10 9 7 Note For the first test case, the value of $$$k$$$ is determined as follows: $$$k = 0$$$ initially. On the $$$1$$$st operation, $$$1$$$ is odd, so Alya sets $$$k$$$ to be $$$k&p_1 = 0&2 = 0$$$. On the $$$2$$$nd operation, $$$2$$$ is even, so Alya sets $$$k$$$ to be $$$kp_2 = 01 = 1$$$. On the $$$3$$$rd operation, $$$3$$$ is odd, so Alya sets $$$k$$$ to be $$$k&p_3 = 1&3 = 1$$$. On the $$$4$$$th operation, $$$4$$$ is even, so Alya sets $$$k$$$ to be $$$kp_4 = 14 = 5$$$. On the $$$5$$$th operation, $$$5$$$ is odd, so Alya sets $$$k$$$ to be $$$k&p_5 = 5&5 = 5$$$. The final value of $$$k$$$ is $$$5$$$. It can be shown that the final value of $$$k$$$ is at most $$$5$$$ for all permutations of length $$$5$$$. Another valid output is $$$[2, 3, 1, 4, 5]$$$. For the second test case, the final value of $$$k$$$ is $$$7$$$. It can be shown that the final value of $$$k$$$ is at most $$$7$$$ for all permutations of length $$$6$$$. Other valid outputs include $$$[2, 4, 1, 6, 3, 5]$$$ and $$$[5, 2, 6, 1, 3, 4]$$$. | 1,400 | true | false | false | false | false | true | false | false | false | false | 59 |
1931E | Sasha gave Anna a list $$$a$$$ of $$$n$$$ integers for Valentine's Day. Anna doesn't need this list, so she suggests destroying it by playing a game. Players take turns. Sasha is a gentleman, so he gives Anna the right to make the first move. On her turn, Anna must choose an element $$$a_i$$$ from the list and reverse the sequence of its digits. For example, if Anna chose the element with a value of $$$42$$$, it would become $$$24$$$; if Anna chose the element with a value of $$$1580$$$, it would become $$$851$$$. Note that leading zeros are removed. After such a turn, the number of elements in the list does not change. On his turn, Sasha must extract two elements $$$a_i$$$ and $$$a_j$$$ ($$$i e j$$$) from the list, concatenate them in any order and insert the result back into the list. For example, if Sasha chose the elements equal to $$$2007$$$ and $$$19$$$, he would remove these two elements from the list and add the integer $$$200719$$$ or $$$192007$$$. After such a turn, the number of elements in the list decreases by $$$1$$$. Players can't skip turns. The game ends when Sasha can't make a move, i.e. after Anna's move there is exactly one number left in the list. If this integer is not less than $$$10^m$$$ (i.e., $$$ge 10^m$$$), Sasha wins. Otherwise, Anna wins. It can be shown that the game will always end. Determine who will win if both players play optimally. Input 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 integers $$$n$$$, $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le m le 2 cdot 10^6$$$)xa0— the number of integers in the list and the parameter determining when Sasha wins. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the list that Sasha gave to Anna. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output: "Sasha", if Sasha wins with optimal play; "Anna", if Anna wins with optimal play. Example Input 9 2 2 14 2 3 5 9 56 1 4 10 1 2007 800 1580 4 5 5000 123 30 4 10 10 6 4 6 2 3 1 10 9 10 7 1 1 6 1 1 10 8 9 1 2 9 10 10 2 10 2 4 5 10 10 10 10 Output Sasha Anna Anna Sasha Sasha Anna Anna Anna Sasha Note Consider the first test case. Anna can reverse the integer $$$2$$$, then Sasha can concatenate the integers $$$2$$$ and $$$14$$$, obtaining the integer $$$214$$$, which is greater than $$$10^2 = 100$$$. If Anna had reversed the integer $$$14$$$, Sasha would have concatenated the integers $$$41$$$ and $$$2$$$, obtaining the integer $$$412$$$, which is greater than $$$10^2 = 100$$$. Anna has no other possible moves, so she loses. | 1,400 | true | true | false | false | false | false | false | false | true | false | 697 |
1992C | Gorilla and Noobish_Monk found three numbers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$m < k$$$). They decided to construct a permutation$$$^{dagger}$$$ of length $$$n$$$. For the permutation, Noobish_Monk came up with the following function: $$$g(i)$$$ is the sum of all the numbers in the permutation on a prefix of length $$$i$$$ that are not greater than $$$m$$$. Similarly, Gorilla came up with the function $$$f$$$, where $$$f(i)$$$ is the sum of all the numbers in the permutation on a prefix of length $$$i$$$ that are not less than $$$k$$$. A prefix of length $$$i$$$ is a subarray consisting of the first $$$i$$$ elements of the original array. For example, if $$$n = 5$$$, $$$m = 2$$$, $$$k = 5$$$, and the permutation is $$$[5, 3, 4, 1, 2]$$$, then: $$$f(1) = 5$$$, because $$$5 ge 5$$$; $$$g(1) = 0$$$, because $$$5 > 2$$$; $$$f(2) = 5$$$, because $$$3 < 5$$$; $$$g(2) = 0$$$, because $$$3 > 2$$$; $$$f(3) = 5$$$, because $$$4 < 5$$$; $$$g(3) = 0$$$, because $$$4 > 2$$$; $$$f(4) = 5$$$, because $$$1 < 5$$$; $$$g(4) = 1$$$, because $$$1 le 2$$$; $$$f(5) = 5$$$, because $$$2 < 5$$$; $$$g(5) = 1 + 2 = 3$$$, because $$$2 le 2$$$. Help them find a permutation for which the value of $$$left(sum_{i=1}^n f(i) - sum_{i=1}^n g(i) ight)$$$ is maximized. $$$^{dagger}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation (as $$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation (as $$$n=3$$$, but $$$4$$$ appears in the array). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) xa0— the number of test cases. The only line of each case contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$2le n le 10^5$$$; $$$1 le m < k le n$$$)xa0— the size of the permutation to be constructed and two integers. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the permutation xa0— a set of numbers that satisfies the conditions of the problem. If there are multiple solutions, output any of them. | 900 | true | false | false | false | false | true | false | false | false | false | 317 |
855A | Harry Potter is on a mission to destroy You-Know-Who's Horcruxes. The first Horcrux that he encountered in the Chamber of Secrets is Tom Riddle's diary. The diary was with Ginny and it forced her to open the Chamber of Secrets. Harry wants to know the different people who had ever possessed the diary to make sure they are not under its influence. He has names of _n_ people who possessed the diary in order. You need to tell, for each person, if he/she possessed the diary at some point before or not. Formally, for a name _s__i_ in the _i_-th line, output "YES" (without quotes) if there exists an index _j_ such that _s__i_u2009=u2009_s__j_ and _j_u2009<u2009_i_, otherwise, output "NO" (without quotes). Input First line of input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the number of names in the list. Next _n_ lines each contain a string _s__i_, consisting of lowercase English letters. The length of each string is between 1 and 100. Output Output _n_ lines each containing either "YES" or "NO" (without quotes), depending on whether this string was already present in the stream or not. You can print each letter in any case (upper or lower). Examples Input 6 tom lucius ginny harry ginny harry Note In test case 1, for _i_u2009=u20095 there exists _j_u2009=u20093 such that _s__i_u2009=u2009_s__j_ and _j_u2009<u2009_i_, which means that answer for _i_u2009=u20095 is "YES". | 800 | false | false | true | false | false | false | true | false | false | false | 6,327 |
1821C | You are given a string $$$s$$$, consisting of lowercase Latin letters. In one operation, you can select several (one or more) positions in it such that no two selected positions are adjacent to each other. Then you remove the letters on the selected positions from the string. The resulting parts are concatenated without changing their order. What is the smallest number of operations required to make all the letters in $$$s$$$ the same? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The only line of each testcase contains a string $$$s$$$, consisting of lowercase Latin letters. Its length is from $$$1$$$ to $$$2 cdot 10^5$$$. The total length of the strings over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print a single integerxa0— the smallest number of operations required to make all the letters in the given string $$$s$$$ the same. Example Input 5 abacaba codeforces oooooooo abcdef mewheniseearulhiiarul Note In the first testcase, you can select positions $$$2, 4$$$ and $$$6$$$ and remove the corresponding letters 'b', 'c' and 'b'. In the third testcase, the letters in the string are already the same, so you don't have to make any operations. In the fourth testcase, one of the possible solutions in $$$2$$$ operations is the following. You can select positions $$$1, 4, 6$$$ first. The string becomes "bce". Then select positions $$$1$$$ and $$$3$$$. The string becomes "c". All letters in it are the same, since it's just one letter. | 1,300 | true | false | true | false | false | false | true | false | false | false | 1,355 |
1185F | A company of $$$n$$$ friends wants to order exactly two pizzas. It is known that in total there are $$$9$$$ pizza ingredients in nature, which are denoted by integers from $$$1$$$ to $$$9$$$. Each of the $$$n$$$ friends has one or more favorite ingredients: the $$$i$$$-th of friends has the number of favorite ingredients equal to $$$f_i$$$ ($$$1 le f_i le 9$$$) and your favorite ingredients form the sequence $$$b_{i1}, b_{i2}, dots, b_{if_i}$$$ ($$$1 le b_{it} le 9$$$). The website of CodePizza restaurant has exactly $$$m$$$ ($$$m ge 2$$$) pizzas. Each pizza is characterized by a set of $$$r_j$$$ ingredients $$$a_{j1}, a_{j2}, dots, a_{jr_j}$$$ ($$$1 le r_j le 9$$$, $$$1 le a_{jt} le 9$$$) , which are included in it, and its price is $$$c_j$$$. Help your friends choose exactly two pizzas in such a way as to please the maximum number of people in the company. It is known that a person is pleased with the choice if each of his/her favorite ingredients is in at least one ordered pizza. If there are several ways to choose two pizzas so as to please the maximum number of friends, then choose the one that minimizes the total price of two pizzas. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5, 2 le m le 10^5$$$) — the number of friends in the company and the number of pizzas, respectively. Next, the $$$n$$$ lines contain descriptions of favorite ingredients of the friends: the $$$i$$$-th of them contains the number of favorite ingredients $$$f_i$$$ ($$$1 le f_i le 9$$$) and a sequence of distinct integers $$$b_{i1}, b_{i2}, dots, b_{if_i}$$$ ($$$1 le b_{it} le 9$$$). Next, the $$$m$$$ lines contain pizza descriptions: the $$$j$$$-th of them contains the integer price of the pizza $$$c_j$$$ ($$$1 le c_j le 10^9$$$), the number of ingredients $$$r_j$$$ ($$$1 le r_j le 9$$$) and the ingredients themselves as a sequence of distinct integers $$$a_{j1}, a_{j2}, dots, a_{jr_j}$$$ ($$$1 le a_{jt} le 9$$$). Output Output two integers $$$j_1$$$ and $$$j_2$$$ ($$$1 le j_1,j_2 le m$$$, $$$j_1 e j_2$$$) denoting the indices of two pizzas in the required set. If there are several solutions, output any of them. Pizza indices can be printed in any order. Examples Input 3 4 2 6 7 4 2 3 9 5 3 2 3 9 100 1 7 400 3 3 2 5 100 2 9 2 500 3 2 9 5 Input 4 3 1 1 1 2 1 3 1 4 10 4 1 2 3 4 20 4 1 2 3 4 30 4 1 2 3 4 Input 1 5 9 9 8 7 6 5 4 3 2 1 3 4 1 2 3 4 1 4 5 6 7 8 4 4 1 3 5 7 1 4 2 4 6 8 5 4 1 9 2 8 | 2,100 | false | false | false | false | false | false | true | false | false | false | 4,774 |
1363A | Shubham has an array $$$a$$$ of size $$$n$$$, and wants to select exactly $$$x$$$ elements from it, such that their sum is odd. These elements do not have to be consecutive. The elements of the array are not guaranteed to be distinct. Tell him whether he can do so. Input The first line of the input contains a single integer $$$t$$$ $$$(1le t le 100)$$$xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ $$$(1 le x le n le 1000)$$$xa0— the length of the array and the number of elements you need to choose. The next line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ $$$(1 le a_i le 1000)$$$xa0— elements of the array. Output For each test case, print "Yes" or "No" depending on whether it is possible to choose $$$x$$$ elements such that their sum is odd. You may print every letter in any case you want. Example Input 5 1 1 999 1 1 1000 2 1 51 50 2 2 51 50 3 3 101 102 103 Note For $$$1$$$st case: We must select element $$$999$$$, and the sum is odd. For $$$2$$$nd case: We must select element $$$1000$$$, so overall sum is not odd. For $$$3$$$rd case: We can select element $$$51$$$. For $$$4$$$th case: We must select both elements $$$50$$$ and $$$51$$$ xa0— so overall sum is odd. For $$$5$$$th case: We must select all elements xa0— but overall sum is not odd. | 1,200 | true | false | true | false | false | false | true | false | false | false | 3,868 |
1250E | The coronation of King Berl XXII is soon! The whole royal family, including $$$n$$$ daughters of Berl XXII, will be present. The King has ordered his jeweler to assemble $$$n$$$ beautiful necklaces, so each of the princesses could wear exactly one necklace during the ceremony — and now these necklaces are finished. Each necklace consists of $$$m$$$ gems attached to a gold chain. There are two types of gems used in the necklaces — emeralds and sapphires. So, each necklace can be represented by a sequence of $$$m$$$ gems (listed from left to right), and each gem is either an emerald or a sapphire. Formally, the $$$i$$$-th necklace can be represented by a binary string $$$s_i$$$ of length $$$m$$$; if the $$$j$$$-th character of $$$s_i$$$ is 0, then the $$$j$$$-th gem in the $$$i$$$-th necklace is an emerald; otherwise, this gem is a sapphire. Now, looking at the necklaces, the King is afraid that some of his daughters may envy the other daughters' necklaces. He wants all necklaces to look similar. Two necklaces are considered similar if there are at least $$$k$$$ positions where these necklaces contain the same type of gems. For example, if there is a necklace represented by a sequence 01010111 and a necklace represented by a sequence 01100000, then there are $$$3$$$ positions where these necklaces contain the same type of gems (both first gems are emeralds, both second gems are sapphires, and both fifth gems are emeralds). So if $$$k = 3$$$, these necklaces are similar, and if $$$k = 4$$$, they are not similar. The King thinks that if two of his daughters notice that their necklaces are not similar, then they may have a conflict — and, obviously, he doesn't want any conflicts during the coronation! So Berl XXII wants to tell some of his daughters to wear their necklaces backward. If a necklace is worn backward, then the sequence of gems in this necklace is reversed. For example, if a necklace is represented by a sequence 01100, then, if worn backward, it would be represented by a sequence 00110. The King wants to find the minimum number of necklaces to be worn backward during the coronation so that there are no conflicts. Berl XXII is too busy with preparation for the coronation, so he ordered you to resolve this issue for him. Help him — and he will give you a truly royal reward! Input The first line contains one integer $$$t$$$ ($$$1 le t le 50$$$) — the number of test cases. Then the test cases follow. Each test case begins with a line containing three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 50$$$, $$$1 le k le m le 50$$$) — the number of necklaces, the number of gems in each necklace, and the minimum number of positions where two necklaces have to have the same type of gems in order to look similar, respectively. Then $$$n$$$ lines follow, the $$$i$$$-th of them contains a binary string $$$s_i$$$ of length $$$m$$$ representing the $$$i$$$-th necklace. Output For each test case, print the answer as follows. If it is impossible to avoid the conflict, print -1 on a single line. In this case you should not output anything else for that test case. Otherwise, the first line of the test case answer should contain the single integer $$$d$$$ — the minimum number of necklaces that are to be worn backward. The second line of the test case answer should contain the numbers of these necklaces (integers from $$$1$$$ to $$$n$$$) in any order. If $$$d = 0$$$ then leave the second line of the test case answer empty. If there are multiple answers, you may print any of them. | 2,300 | false | false | true | false | false | false | false | false | false | true | 4,451 |
1517H | After hearing the story of Dr. Zhang, Wowo decides to plan his own flight around the world. He already chose $$$n$$$ checkpoints in the world map. Due to the landform and the clouds, he cannot fly too high or too low. Formally, let $$$b_i$$$ be the height of Wowo's aircraft at checkpoint $$$i$$$, $$$x_i^-le b_ile x_i^+$$$ should be satisfied for all integers $$$i$$$ between $$$1$$$ and $$$n$$$, where $$$x_i^-$$$ and $$$x_i^+$$$ are given integers. The angle of Wowo's aircraft is also limited. For example, it cannot make a $$$90$$$-degree climb. Formally, $$$y_i^-le b_i-b_{i-1}le y_i^+$$$ should be satisfied for all integers $$$i$$$ between $$$2$$$ and $$$n$$$, where $$$y_i^-$$$ and $$$y_i^+$$$ are given integers. The final limitation is the speed of angling up or angling down. An aircraft should change its angle slowly for safety concerns. Formally, $$$z_i^- le (b_i - b_{i-1}) - (b_{i-1} - b_{i-2}) le z_i^+$$$ should be satisfied for all integers $$$i$$$ between $$$3$$$ and $$$n$$$, where $$$z_i^-$$$ and $$$z_i^+$$$ are given integers. Taking all these into consideration, Wowo finds that the heights at checkpoints are too hard for him to choose. Please help Wowo decide whether there exists a sequence of real numbers $$$b_1, ldots, b_n$$$ satisfying all the contraints above. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 66,666$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 100,000$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i^-$$$, $$$x_i^+$$$ ($$$-10^8le x_i^-le x_i^+le 10^8$$$) denoting the lower and upper bound of $$$b_i$$$. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$y_{i+1}^-$$$, $$$y_{i+1}^+$$$ ($$$-10^8le y_{i+1}^-le y_{i+1}^+le 10^8$$$) denoting the lower and upper bound of $$$b_{i+1}-b_i$$$. The $$$i$$$-th of the next $$$n-2$$$ lines contains two integers $$$z_{i+2}^-$$$, $$$z_{i+2}^+$$$ ($$$-10^8le z_{i+2}^-le z_{i+2}^+le 10^8$$$) denoting the lower and upper bound of $$$(b_{i+2}-b_{i+1}) - (b_{i+1}-b_i)$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200,000$$$. It is guaranteed that relaxing every constraint by $$$10^{-6}$$$ (i.e., decrease $$$x_i^-, y_i^-, z_i^-$$$ by $$$10^{-6}$$$ and increase $$$x_i^+, y_i^+, z_i^+$$$ by $$$10^{-6}$$$) will not change the answer. Output For each test case, output YES if a sequence $$$b_1,ldots, b_n$$$ satisfying the constraints exists and NO otherwise. The sequence $$$b_1,ldots, b_n$$$ is not required. Example Input 4 3 0 1 0 1 0 1 1 1 1 1 -100 100 3 -967 541 -500 834 -724 669 -858 978 -964 962 -645 705 4 0 0 0 1 0 1 1 1 0 1 0 1 0 1 0 0 0 0 4 0 0 33 34 65 66 100 100 0 100 0 100 0 100 0 0 0 0 Note In the first test case, all $$$b_i$$$'s are in $$$[0,1]$$$. Because of the constraints $$$1=y_2^-le b_2-b_1le y_2^+=1$$$, $$$b_2-b_1$$$ must be $$$1$$$. So $$$b_2=1$$$ and $$$b_1=0$$$ must hold. Then by $$$1=y_3^-le b_3-b_2le y_3^+=1$$$, $$$b_3$$$ equals $$$2$$$. This contradicts the constraint of $$$b_3le 1$$$. So no solution exists. In the second test case, we can let all $$$b_i$$$'s be $$$0$$$. In the third test case, one possible solution is $$$b_1=0$$$, $$$b_2=1/3$$$, $$$b_3=2/3$$$, $$$b_4=1$$$. | 3,500 | false | false | false | true | false | false | false | false | false | false | 3,070 |
691C | Problem - 691C - Codeforces =============== xa0 | 1,800 | false | false | true | false | false | false | false | false | false | false | 7,045 |
1003B | Problem - 1003B - 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 *1300 No tag edit access → Contest materials ") such that $$$s_i e s_{i + 1}$$$. It is guaranteed that the answer always exists. For example, for the string "01010" there are four indices $$$i$$$ such that $$$1 le i < n$$$ and $$$s_i e s_{i + 1}$$$ ($$$i = 1, 2, 3, 4$$$). For the string "111001" there are two such indices $$$i$$$ ($$$i = 3, 5$$$). Recall that binary string is a non-empty sequence of characters where each character is either 0 or 1. Input The first line of the input contains three integers $$$a$$$, $$$b$$$ and $$$x$$$ ($$$1 le a, b le 100, 1 le x < a + b)$$$. Output Print only one string $$$s$$$, where $$$s$$$ is any binary string satisfying conditions described above. It is guaranteed that the answer always exists. Examples Input 2 2 1 Output 1100 Input 3 3 3 Output 101100 Input 5 3 6 Output 01010100 Note All possible answers for the first example: 1100; 0011. All possible answers for the second example: 110100; 101100; 110010; 100110; 011001; 001101; 010011; 001011. | 1,300 | false | false | false | false | false | true | false | false | false | false | 5,673 |
123A | Problem - 123A - 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 number theory strings *1300 No tag edit access → Contest materials the following condition was fulfilled _s__p_u2009=u2009_s__p_u2009×u2009_i_. If the answer is positive, find one way to rearrange the characters. Input The only line contains the initial string _s_, consisting of small Latin letters (1u2009≤u2009_s_u2009≤u20091000). Output If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line "YES" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string "NO". Examples Input abc Output YES abc Input abcd Output NO Input xxxyxxx Output YES xxxxxxy Note In the first sample any of the six possible strings will do: "abc", "acb", "bac", "bca", "cab" or "cba". In the second sample no letter permutation will satisfy the condition at _p_u2009=u20092 (_s_2u2009=u2009_s_4). In the third test any string where character "y" doesn't occupy positions 2, 3, 4, 6 will be valid. | 1,300 | false | false | true | false | false | false | false | false | false | false | 9,389 |
573B | Problem - 573B - 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 data structures dp math *1600 No tag edit access → Contest materials adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ space-separated integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009≤u2009_h__i_u2009≤u2009109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation. | 1,600 | true | false | false | true | true | false | false | true | false | false | 7,558 |
486E | The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson. Nam created a sequence _a_ consisting of _n_ (1u2009≤u2009_n_u2009≤u2009105) elements _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105). A subsequence _a__i_1,u2009_a__i_2,u2009...,u2009_a__i__k_ where 1u2009≤u2009_i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__k_u2009≤u2009_n_ is called increasing if _a__i_1u2009<u2009_a__i_2u2009<u2009_a__i_3u2009<u2009...u2009<u2009_a__i__k_. An increasing subsequence is called longest if it has maximum length among all increasing subsequences. Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes _i_ (1u2009≤u2009_i_u2009≤u2009_n_), into three groups: 1. group of all _i_ such that _a__i_ belongs to no longest increasing subsequences. 2. group of all _i_ such that _a__i_ belongs to at least one but not every longest increasing subsequence. 3. group of all _i_ such that _a__i_ belongs to every longest increasing subsequence. Since the number of longest increasing subsequences of _a_ may be very large, categorizing process is very difficult. Your task is to help him finish this job. Input The first line contains the single integer _n_ (1u2009≤u2009_n_u2009≤u2009105) denoting the number of elements of sequence _a_. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105). Output Print a string consisting of _n_ characters. _i_-th character should be '1', '2' or '3' depending on which group among listed above index _i_ belongs to. Note In the second sample, sequence _a_ consists of 4 elements: {_a_1,u2009_a_2,u2009_a_3,u2009_a_4} = {1,u20093,u20092,u20095}. Sequence _a_ has exactly 2 longest increasing subsequences of length 3, they are {_a_1,u2009_a_2,u2009_a_4} = {1,u20093,u20095} and {_a_1,u2009_a_3,u2009_a_4} = {1,u20092,u20095}. In the third sample, sequence _a_ consists of 4 elements: {_a_1,u2009_a_2,u2009_a_3,u2009_a_4} = {1,u20095,u20092,u20093}. Sequence _a_ have exactly 1 longest increasing subsequence of length 3, that is {_a_1,u2009_a_3,u2009_a_4} = {1,u20092,u20093}. | 2,200 | true | true | false | true | true | false | false | false | false | false | 7,891 |
1575M | Mr. Chanek's city can be represented as a plane. He wants to build a housing complex in the city. There are some telephone poles on the plane, which is represented by a grid $$$a$$$ of size $$$(n + 1) imes (m + 1)$$$. There is a telephone pole at $$$(x, y)$$$ if $$$a_{x, y} = 1$$$. For each point $$$(x, y)$$$, define $$$S(x, y)$$$ as the square of the Euclidean distance between the nearest pole and $$$(x, y)$$$. Formally, the square of the Euclidean distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ is $$$(x_2 - x_1)^2 + (y_2 - y_1)^2$$$. To optimize the building plan, the project supervisor asks you the sum of all $$$S(x, y)$$$ for each $$$0 leq x leq n$$$ and $$$0 leq y leq m$$$. Help him by finding the value of $$$sum_{x=0}^{n} {sum_{y=0}^{m} {S(x, y)}}$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 leq n, m < 2000$$$) — the size of the grid. Then $$$(n + 1)$$$ lines follow, each containing $$$(m + 1)$$$ integers $$$a_{i, j}$$$ ($$$0 leq a_{i, j} leq 1$$$) — the grid denoting the positions of telephone poles in the plane. There is at least one telephone pole in the given grid. Output Output an integer denoting the value of $$$sum_{x=0}^{n} {sum_{y=0}^{m} {S(x, y)}}$$$. Examples Input 5 4 10010 00000 01000 00001 00100 00010 Note In the first example, the nearest telephone pole for the points $$$(0,0)$$$, $$$(1,0)$$$, $$$(2,0)$$$, $$$(0,1)$$$, $$$(1,1)$$$, and $$$(2,1)$$$ is at $$$(0, 0)$$$. While the nearest telephone pole for the points $$$(0, 2)$$$, $$$(1,2)$$$, and $$$(2,2)$$$ is at $$$(0, 2)$$$. Thus, $$$sum_{x=0}^{n} {sum_{y=0}^{m} {S(x, y)}} = (0 + 1 + 4) + (1 + 2 + 5) + (0 + 1 + 4) = 18$$$. | 2,400 | false | false | false | false | true | false | false | false | false | false | 2,756 |
1618B | Polycarp has come up with a new game to play with you. He calls it "A missing bigram". A bigram of a word is a sequence of two adjacent letters in it. For example, word "abbaaba" contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba". The game goes as follows. First, Polycarp comes up with a word, consisting only of lowercase letters 'a' and 'b'. Then, he writes down all its bigrams on a whiteboard in the same order as they appear in the word. After that, he wipes one of them off the whiteboard. Finally, Polycarp invites you to guess what the word that he has come up with was. Your goal is to find any word such that it's possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with. The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0— the number of testcases. The first line of each testcase contains a single integer $$$n$$$ ($$$3 le n le 100$$$)xa0— the length of the word Polycarp has come up with. The second line of each testcase contains $$$n-2$$$ bigrams of that word, separated by a single space. Each bigram consists of two letters, each of them is either 'a' or 'b'. Additional constraint on the input: there exists at least one string such that it is possible to write down all its bigrams, except one, so that the resulting sequence is the same as the sequence in the input. In other words, the answer exists. Output For each testcase print a word, consisting of $$$n$$$ letters, each of them should be either 'a' or 'b'. It should be possible to write down all its bigrams and remove one of them, so that the resulting sequence of bigrams is the same as the one Polycarp ended up with. The tests are generated in such a way that the answer exists. If there are multiple answers, you can print any of them. Example Input 4 7 ab bb ba aa ba 7 ab ba aa ab ba 3 aa 5 bb ab bb Output abbaaba abaabaa baa bbabb Note The first two testcases from the example are produced from the word "abbaaba". As listed in the statement, it contains bigrams "ab", "bb", "ba", "aa", "ab" and "ba". In the first testcase, the $$$5$$$-th bigram is removed. In the second testcase, the $$$2$$$-nd bigram is removed. However, that sequence could also have been produced from the word "abaabaa". It contains bigrams "ab", "ba", "aa", "ab", "ba" and "aa". The missing bigram is the $$$6$$$-th one. In the third testcase, all of "baa", "aab" and "aaa" are valid answers. | 800 | false | false | true | false | false | false | false | false | false | false | 2,541 |
1955A | Problem - 1955A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags math *800 No tag edit access → Contest materials ") xa0— the number of test cases. The first and only line of each test case contains three integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 le n le 100$$$, $$$1 le a, b le 30$$$)xa0— the number of yogurts Maxim wants to buy, the price for one yogurt, and the price for two yogurts on promotion. Output For each test case, print in a separate line the minimum cost of buying $$$n$$$ yogurts at "Vosmiorochka". Example Input 4 2 5 9 3 5 9 3 5 11 4 5 11 Output 9 14 15 20 Note In the third test case of the example, it is more advantageous to buy three yogurts for $$$15$$$ burles than two for $$$11$$$ and one for $$$5$$$. In the fourth test case of the example, you need to buy four yogurts, each for $$$5$$$ burles. | 800 | true | false | false | false | false | false | false | false | false | false | 548 |
723D | The map of Berland is a rectangle of the size _n_u2009×u2009_m_, which consists of cells of size 1u2009×u20091. Each cell is either land or water. The map is surrounded by the ocean. Lakes are the maximal regions of water cells, connected by sides, which are not connected with the ocean. Formally, lake is a set of water cells, such that it's possible to get from any cell of the set to any other without leaving the set and moving only to cells adjacent by the side, none of them is located on the border of the rectangle, and it's impossible to add one more water cell to the set such that it will be connected with any other cell. You task is to fill up with the earth the minimum number of water cells so that there will be exactly _k_ lakes in Berland. Note that the initial number of lakes on the map is not less than _k_. Input The first line of the input contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u200950, 0u2009≤u2009_k_u2009≤u200950)xa0— the sizes of the map and the number of lakes which should be left on the map. The next _n_ lines contain _m_ characters each — the description of the map. Each of the characters is either '.' (it means that the corresponding cell is water) or '*' (it means that the corresponding cell is land). It is guaranteed that the map contain at least _k_ lakes. Output In the first line print the minimum number of cells which should be transformed from water to land. In the next _n_ lines print _m_ symbols — the map after the changes. The format must strictly follow the format of the map in the input data (there is no need to print the size of the map). If there are several answers, print any of them. It is guaranteed that the answer exists on the given data. Examples Input 5 4 1 *** *.. *** **. ..* Output 1 *** *.. *** *** ..* Note In the first example there are only two lakes — the first consists of the cells (2,u20092) and (2,u20093), the second consists of the cell (4,u20093). It is profitable to cover the second lake because it is smaller. Pay attention that the area of water in the lower left corner is not a lake because this area share a border with the ocean. | 1,600 | false | true | true | false | false | false | false | false | false | true | 6,917 |
557C | Arthur has bought a beautiful big table into his new flat. When he came home, Arthur noticed that the new table is unstable. In total the table Arthur bought has _n_ legs, the length of the _i_-th leg is _l__i_. Arthur decided to make the table stable and remove some legs. For each of them Arthur determined number _d__i_xa0—xa0the amount of energy that he spends to remove the _i_-th leg. A table with _k_ legs is assumed to be stable if there are more than half legs of the maximum length. For example, to make a table with 5 legs stable, you need to make sure it has at least three (out of these five) legs of the maximum length. Also, a table with one leg is always stable and a table with two legs is stable if and only if they have the same lengths. Your task is to help Arthur and count the minimum number of energy units Arthur should spend on making the table stable. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0—xa0the initial number of legs in the table Arthur bought. The second line of the input contains a sequence of _n_ integers _l__i_ (1u2009≤u2009_l__i_u2009≤u2009105), where _l__i_ is equal to the length of the _i_-th leg of the table. The third line of the input contains a sequence of _n_ integers _d__i_ (1u2009≤u2009_d__i_u2009≤u2009200), where _d__i_ is the number of energy units that Arthur spends on removing the _i_-th leg off the table. Output Print a single integer — the minimum number of energy units that Arthur needs to spend in order to make the table stable. Examples Input 6 2 2 1 1 3 3 4 3 5 5 2 1 | 1,900 | true | true | false | true | true | false | true | false | true | false | 7,606 |
633D | Problem - 633D - Codeforces =============== xa0 ]( "Announcement") xa0— the length of the sequence _a__i_. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009≤u2009109). Output Print the length of the longest possible Fibonacci-ish prefix of the given sequence after rearrangement. Examples Input 3 1 2 -1 Output 3 Input 5 28 35 7 14 21 Output 4 Note In the first sample, if we rearrange elements of the sequence as u2009-u20091, 2, 1, the whole sequence _a__i_ would be Fibonacci-ish. In the second sample, the optimal way to rearrange elements is , , , , 28. | 2,000 | true | false | true | true | false | false | true | false | false | false | 7,272 |
1305A | Kuroni has $$$n$$$ daughters. As gifts for them, he bought $$$n$$$ necklaces and $$$n$$$ bracelets: the $$$i$$$-th necklace has a brightness $$$a_i$$$, where all the $$$a_i$$$ are pairwise distinct (i.e. all $$$a_i$$$ are different), the $$$i$$$-th bracelet has a brightness $$$b_i$$$, where all the $$$b_i$$$ are pairwise distinct (i.e. all $$$b_i$$$ are different). Kuroni wants to give exactly one necklace and exactly one bracelet to each of his daughters. To make sure that all of them look unique, the total brightnesses of the gifts given to each daughter should be pairwise distinct. Formally, if the $$$i$$$-th daughter receives a necklace with brightness $$$x_i$$$ and a bracelet with brightness $$$y_i$$$, then the sums $$$x_i + y_i$$$ should be pairwise distinct. Help Kuroni to distribute the gifts. For example, if the brightnesses are $$$a = [1, 7, 5]$$$ and $$$b = [6, 1, 2]$$$, then we may distribute the gifts as follows: Give the third necklace and the first bracelet to the first daughter, for a total brightness of $$$a_3 + b_1 = 11$$$. Give the first necklace and the third bracelet to the second daughter, for a total brightness of $$$a_1 + b_3 = 3$$$. Give the second necklace and the second bracelet to the third daughter, for a total brightness of $$$a_2 + b_2 = 8$$$. Here is an example of an invalid distribution: Give the first necklace and the first bracelet to the first daughter, for a total brightness of $$$a_1 + b_1 = 7$$$. Give the second necklace and the second bracelet to the second daughter, for a total brightness of $$$a_2 + b_2 = 8$$$. Give the third necklace and the third bracelet to the third daughter, for a total brightness of $$$a_3 + b_3 = 7$$$. This distribution is invalid, as the total brightnesses of the gifts received by the first and the third daughter are the same. Don't make them this upset! Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$) xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$) xa0— the number of daughters, necklaces and bracelets. The second line of each test case contains $$$n$$$ distinct integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 1000$$$) xa0— the brightnesses of the necklaces. The third line of each test case contains $$$n$$$ distinct integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 1000$$$) xa0— the brightnesses of the bracelets. Output For each test case, print a line containing $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$, representing that the $$$i$$$-th daughter receives a necklace with brightness $$$x_i$$$. In the next line print $$$n$$$ integers $$$y_1, y_2, dots, y_n$$$, representing that the $$$i$$$-th daughter receives a bracelet with brightness $$$y_i$$$. The sums $$$x_1 + y_1, x_2 + y_2, dots, x_n + y_n$$$ should all be distinct. The numbers $$$x_1, dots, x_n$$$ should be equal to the numbers $$$a_1, dots, a_n$$$ in some order, and the numbers $$$y_1, dots, y_n$$$ should be equal to the numbers $$$b_1, dots, b_n$$$ in some order. It can be shown that an answer always exists. If there are multiple possible answers, you may print any of them. Example Input 2 3 1 8 5 8 4 5 3 1 7 5 6 1 2 Output 1 8 5 8 4 5 5 1 7 6 2 1 Note In the first test case, it is enough to give the $$$i$$$-th necklace and the $$$i$$$-th bracelet to the $$$i$$$-th daughter. The corresponding sums are $$$1 + 8 = 9$$$, $$$8 + 4 = 12$$$, and $$$5 + 5 = 10$$$. The second test case is described in the statement. | 800 | false | true | false | false | false | true | true | false | true | false | 4,164 |
72B | The INI file format is a de facto standard for configuration files. INI files are simple text files with a basic structure. They are commonly associated with Microsoft Windows, but are also used on other platforms. Each line in INI-file stands for key-value mapping or defines new section. A key-value line has a format "key=value",where key — is the name of some property, and value — it's value. It is possible that it will be spaces from the both sides of key and/or value, the spaces should be ignored. A section line has a format "[section]". It means that all key-value lines after it define properties of the specified section. Of cause, the following section line changes the current section. A section line may have spaces around any of brackets. Also you should ignore comment lines — the first non-space character of comment line is ";". You task is to write the program which will format given INI-file in a special way: first, print key-value lines which do not belong to any section; print all the sections in the lexicographical (alphabetical) order of their names; inside each of two previous items, order key-value lines lexicographically by "key"; if there are more than one key-value lines with the same key inside a single section (or outside any sections), leave only one line (which appears later in the input data); remove all redundant spaces and lines. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009510) — the number of lines in given INI-file. The rest of the input contains a valid INI-file in _n_ lines. Values of section, key and value contain only Latin letters, digits, "." and/or "-". Each line has length not exceeding 255 characters and not less than 1 character. The total length of all the lines does’t exceed 10000. Output Print formatted INI-file. Examples Input 11 a= 1 b=a a = 2 ; comment [z] 1=2 [y] 2=3 [z] 2=1 [w] Output a=2 b=a [w] [y] 2=3 [z] 1=2 2=1 | 2,200 | false | false | true | false | false | false | false | false | false | false | 9,610 |
913C | A New Year party is not a New Year party without lemonade! As usual, you are expecting a lot of guests, and buying lemonade has already become a pleasant necessity. Your favorite store sells lemonade in bottles of _n_ different volumes at different costs. A single bottle of type _i_ has volume 2_i_u2009-u20091 liters and costs _c__i_ roubles. The number of bottles of each type in the store can be considered infinite. You want to buy at least _L_ liters of lemonade. How many roubles do you have to spend? Input The first line contains two integers _n_ and _L_ (1u2009≤u2009_n_u2009≤u200930; 1u2009≤u2009_L_u2009≤u2009109)xa0— the number of types of bottles in the store and the required amount of lemonade in liters, respectively. The second line contains _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u2009109)xa0— the costs of bottles of different types. Output Output a single integerxa0— the smallest number of roubles you have to pay in order to buy at least _L_ liters of lemonade. Examples Input 4 3 10000 1000 100 10 Input 4 3 10 100 1000 10000 Input 5 787787787 123456789 234567890 345678901 456789012 987654321 Note In the first example you should buy one 8-liter bottle for 90 roubles and two 2-liter bottles for 30 roubles each. In total you'll get 12 liters of lemonade for just 150 roubles. In the second example, even though you need only 3 liters, it's cheaper to buy a single 8-liter bottle for 10 roubles. In the third example it's best to buy three 1-liter bottles for 10 roubles each, getting three liters for 30 roubles. | 1,600 | false | true | false | true | false | false | false | false | false | false | 6,089 |
135C | Little Petya very much likes playing with little Masha. Recently he has received a game called "Zero-One" as a gift from his mother. Petya immediately offered Masha to play the game with him. Before the very beginning of the game several cards are lain out on a table in one line from the left to the right. Each card contains a digit: 0 or 1. Players move in turns and Masha moves first. During each move a player should remove a card from the table and shift all other cards so as to close the gap left by the removed card. For example, if before somebody's move the cards on the table formed a sequence 01010101, then after the fourth card is removed (the cards are numbered starting from 1), the sequence will look like that: 0100101. The game ends when exactly two cards are left on the table. The digits on these cards determine the number in binary notation: the most significant bit is located to the left. Masha's aim is to minimize the number and Petya's aim is to maximize it. An unpleasant accident occurred before the game started. The kids spilled juice on some of the cards and the digits on the cards got blurred. Each one of the spoiled cards could have either 0 or 1 written on it. Consider all possible variants of initial arrangement of the digits (before the juice spilling). For each variant, let's find which two cards are left by the end of the game, assuming that both Petya and Masha play optimally. An ordered pair of digits written on those two cards is called an outcome. Your task is to find the set of outcomes for all variants of initial digits arrangement. Input The first line contains a sequence of characters each of which can either be a "0", a "1" or a "?". This sequence determines the initial arrangement of cards on the table from the left to the right. The characters "?" mean that the given card was spoiled before the game. The sequence's length ranges from 2 to 105, inclusive. Output Print the set of outcomes for all possible initial digits arrangements. Print each possible outcome on a single line. Each outcome should be represented by two characters: the digits written on the cards that were left by the end of the game. The outcomes should be sorted lexicographically in ascending order (see the first sample). Note In the first sample all 16 variants of numbers arrangement are possible. For the variant 0000 the outcome is 00. For the variant 1111 the outcome is 11. For the variant 0011 the outcome is 01. For the variant 1100 the outcome is 10. Regardless of outcomes for all other variants the set which we are looking for will contain all 4 possible outcomes. In the third sample only 2 variants of numbers arrangement are possible: 111 and 101. For the variant 111 the outcome is 11. For the variant 101 the outcome is 01, because on the first turn Masha can remove the first card from the left after which the game will end. | 1,900 | false | true | false | false | false | true | false | false | false | false | 9,335 |
279C | You've got an array, consisting of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Also, you've got _m_ queries, the _i_-th query is described by two integers _l__i_,u2009_r__i_. Numbers _l__i_,u2009_r__i_ define a subsegment of the original array, that is, the sequence of numbers _a__l__i_,u2009_a__l__i_u2009+u20091,u2009_a__l__i_u2009+u20092,u2009...,u2009_a__r__i_. For each query you should check whether the corresponding segment is a ladder. A ladder is a sequence of integers _b_1,u2009_b_2,u2009...,u2009_b__k_, such that it first doesn't decrease, then doesn't increase. In other words, there is such integer _x_ (1u2009≤u2009_x_u2009≤u2009_k_), that the following inequation fulfills: _b_1u2009≤u2009_b_2u2009≤u2009...u2009≤u2009_b__x_u2009≥u2009_b__x_u2009+u20091u2009≥u2009_b__x_u2009+u20092...u2009≥u2009_b__k_. Note that the non-decreasing and the non-increasing sequences are also considered ladders. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the number of array elements and the number of queries. The second line contains the sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), where number _a__i_ stands for the _i_-th array element. The following _m_ lines contain the description of the queries. The _i_-th line contains the description of the _i_-th query, consisting of two integers _l__i_, _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_) — the boundaries of the subsegment of the initial array. The numbers in the lines are separated by single spaces. Output Print _m_ lines, in the _i_-th line print word "Yes" (without the quotes), if the subsegment that corresponds to the _i_-th query is the ladder, or word "No" (without the quotes) otherwise. | 1,700 | false | false | true | true | false | false | false | false | false | false | 8,718 |
382C | Everybody knows what an arithmetic progression is. Let us remind you just in case that an arithmetic progression is such sequence of numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ of length _n_, that the following condition fulfills: _a_2u2009-u2009_a_1u2009=u2009_a_3u2009-u2009_a_2u2009=u2009_a_4u2009-u2009_a_3u2009=u2009...u2009=u2009_a__i_u2009+u20091u2009-u2009_a__i_u2009=u2009...u2009=u2009_a__n_u2009-u2009_a__n_u2009-u20091. For example, sequences [1, 5], [10], [5, 4, 3] are arithmetic progressions and sequences [1, 3, 2], [1, 2, 4] are not. Alexander has _n_ cards containing integers. Arthur wants to give Alexander exactly one more card with a number so that he could use the resulting _n_u2009+u20091 cards to make an arithmetic progression (Alexander has to use all of his cards). Arthur has already bought a card but he hasn't written a number on it. Help him, print all integers that you can write on a card so that the described condition fulfilled. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of cards. The next line contains the sequence of integers — the numbers on Alexander's cards. The numbers are positive integers, each of them doesn't exceed 108. Output If Arthur can write infinitely many distinct integers on the card, print on a single line -1. Otherwise, print on the first line the number of integers that suit you. In the second line, print the numbers in the increasing order. Note that the numbers in the answer can exceed 108 or even be negative (see test samples). | 1,700 | false | false | true | false | false | false | false | false | true | false | 8,301 |
615B | This Christmas Santa gave Masha a magic picture and a pencil. The picture consists of _n_ points connected by _m_ segments (they might cross in any way, that doesn't matter). No two segments connect the same pair of points, and no segment connects the point to itself. Masha wants to color some segments in order paint a hedgehog. In Mashas mind every hedgehog consists of a tail and some spines. She wants to paint the tail that satisfies the following conditions: 1. Only segments already presented on the picture can be painted; 2. The tail should be continuous, i.e. consists of some sequence of points, such that every two neighbouring points are connected by a colored segment; 3. The numbers of points from the beginning of the tail to the end should strictly increase. Masha defines the length of the tail as the number of points in it. Also, she wants to paint some spines. To do so, Masha will paint all the segments, such that one of their ends is the endpoint of the tail. Masha defines the beauty of a hedgehog as the length of the tail multiplied by the number of spines. Masha wants to color the most beautiful hedgehog. Help her calculate what result she may hope to get. Note that according to Masha's definition of a hedgehog, one segment may simultaneously serve as a spine and a part of the tail (she is a little girl after all). Take a look at the picture for further clarifications. Input First line of the input contains two integers _n_ and _m_(2u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_m_u2009≤u2009200u2009000)xa0— the number of points and the number segments on the picture respectively. Then follow _m_ lines, each containing two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_)xa0— the numbers of points connected by corresponding segment. It's guaranteed that no two segments connect the same pair of points. Output Print the maximum possible value of the hedgehog's beauty. Examples Input 8 6 4 5 3 5 2 5 1 2 2 8 6 7 Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Note The picture below corresponds to the first sample. Segments that form the hedgehog are painted red. The tail consists of a sequence of points with numbers 1, 2 and 5. The following segments are spines: (2, 5), (3, 5) and (4, 5). Therefore, the beauty of the hedgehog is equal to 3·3u2009=u20099. | 1,600 | false | false | false | true | false | false | false | false | false | true | 7,379 |
1926G | Vladislav has a son who really wanted to go to MIT. The college dormitory at MIT (Moldova Institute of Technology) can be represented as a tree with $$$n$$$ vertices, each vertex being a room with exactly one student. A tree is a connected undirected graph with $$$n$$$ vertices and $$$n-1$$$ edges. Tonight, there are three types of students: students who want to party and play music (marked with $$$ exttt{P}$$$), students who wish to sleep and enjoy silence (marked with $$$ exttt{S}$$$), and students who don't care (marked with $$$ exttt{C}$$$). Initially, all the edges are thin walls which allow music to pass through, so when a partying student puts music on, it will be heard in every room. However, we can place some thick walls on any edgesxa0— thick walls don't allow music to pass through them. The university wants to install some thick walls so that every partying student can play music, and no sleepy student can hear it. Because the university lost a lot of money in a naming rights lawsuit, they ask you to find the minimum number of thick walls they will need to use. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 leq n leq 10^5$$$)xa0— the number of vertices in the tree. The second line of each test case contains $$$n-1$$$ integers $$$a_2, dots , a_n$$$ ($$$1 leq a_i < i$$$)xa0— it means there is an edge between $$$i$$$ and $$$a_i$$$ in the tree. The third line of each test case contains a string $$$s$$$ of length $$$n$$$ consisting of characters $$$ exttt{P}$$$, $$$ exttt{S}$$$, and $$$ exttt{C}$$$, denoting that student $$$i$$$ is of type $$$s_i$$$. The sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integerxa0— the minimum number of thick walls needed. Example Input 3 3 1 1 CSP 4 1 2 2 PCSS 4 1 2 2 PPSS Note In the first case, we can install one thick wall between rooms $$$1$$$ and $$$2$$$, as shown below. We cannot install $$$0$$$ walls, since then the music from room 3 will reach room 2 where a student wants to sleep, so the answer is $$$1$$$. There are other valid solutions. | 1,900 | false | true | true | true | false | false | false | false | false | true | 731 |
1237C2 | This is a harder version of the problem. In this version, $$$n le 50,000$$$. There are $$$n$$$ distinct points in three-dimensional space numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th point has coordinates $$$(x_i, y_i, z_i)$$$. The number of points $$$n$$$ is even. You'd like to remove all $$$n$$$ points using a sequence of $$$frac{n}{2}$$$ snaps. In one snap, you can remove any two points $$$a$$$ and $$$b$$$ that have not been removed yet and form a perfectly balanced pair. A pair of points $$$a$$$ and $$$b$$$ is perfectly balanced if no other point $$$c$$$ (that has not been removed yet) lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$. Formally, point $$$c$$$ lies within the axis-aligned minimum bounding box of points $$$a$$$ and $$$b$$$ if and only if $$$min(x_a, x_b) le x_c le max(x_a, x_b)$$$, $$$min(y_a, y_b) le y_c le max(y_a, y_b)$$$, and $$$min(z_a, z_b) le z_c le max(z_a, z_b)$$$. Note that the bounding box might be degenerate. Find a way to remove all points in $$$frac{n}{2}$$$ snaps. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 50,000$$$; $$$n$$$ is even), denoting the number of points. Each of the next $$$n$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$, $$$z_i$$$ ($$$-10^8 le x_i, y_i, z_i le 10^8$$$), denoting the coordinates of the $$$i$$$-th point. No two points coincide. Output Output $$$frac{n}{2}$$$ pairs of integers $$$a_i, b_i$$$ ($$$1 le a_i, b_i le n$$$), denoting the indices of points removed on snap $$$i$$$. Every integer between $$$1$$$ and $$$n$$$, inclusive, must appear in your output exactly once. We can show that it is always possible to remove all points. If there are many solutions, output any of them. Examples Input 6 3 1 0 0 3 0 2 2 0 1 0 0 1 3 0 0 1 0 Input 8 0 1 1 1 0 1 1 1 0 1 1 1 2 2 2 3 2 2 2 3 2 2 2 3 Note In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on $$$z = 0$$$ plane). Note that order of removing matters: for example, points $$$5$$$ and $$$1$$$ don't form a perfectly balanced pair initially, but they do after point $$$3$$$ is removed. | 1,900 | false | true | true | false | false | true | false | true | true | false | 4,509 |
1927E | You are given two integers $$$n$$$ and $$$k$$$ ($$$k le n$$$), where $$$k$$$ is even. A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in any order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation (as $$$2$$$ appears twice in the array) and $$$[0,1,2]$$$ is also not a permutation (as $$$n=3$$$, but $$$3$$$ is not present in the array). Your task is to construct a $$$k$$$-level permutation of length $$$n$$$. A permutation is called $$$k$$$-level if, among all the sums of continuous segments of length $$$k$$$ (of which there are exactly $$$n - k + 1$$$), any two sums differ by no more than $$$1$$$. More formally, to determine if the permutation $$$p$$$ is $$$k$$$-level, first construct an array $$$s$$$ of length $$$n - k + 1$$$, where $$$s_i=sum_{j=i}^{i+k-1} p_j$$$, i.e., the $$$i$$$-th element is equal to the sum of $$$p_i, p_{i+1}, dots, p_{i+k-1}$$$. A permutation is called $$$k$$$-level if $$$max(s) - min(s) le 1$$$. Find any $$$k$$$-level permutation of length $$$n$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. This is followed by the description of the test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 2 cdot 10^5$$$, $$$k$$$ is even), where $$$n$$$ is the length of the desired permutation. It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output any $$$k$$$-level permutation of length $$$n$$$. It is guaranteed that such a permutation always exists given the constraints. Note In the second test case of the example: $$$p_1 + p_2 = 3 + 1 = 4$$$; $$$p_2 + p_3 = 1 + 2 = 3$$$. The maximum among the sums is $$$4$$$, and the minimum is $$$3$$$. | 1,400 | true | false | false | false | false | true | false | false | false | false | 726 |
1439B | You are given an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. Also, you are given an integer $$$k$$$. Find either a clique of size $$$k$$$ or a non-empty subset of vertices such that each vertex of this subset has at least $$$k$$$ neighbors in the subset. If there are no such cliques and subsets report about it. A subset of vertices is called a clique of size $$$k$$$ if its size is $$$k$$$ and there exists an edge between every two vertices from the subset. A vertex is called a neighbor of the other vertex if there exists an edge between them. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$)xa0— the number of test cases. The next lines contain descriptions of test cases. The first line of the description of each test case contains three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 leq n, m, k leq 10^5$$$, $$$k leq n$$$). Each of the next $$$m$$$ lines contains two integers $$$u, v$$$ $$$(1 leq u, v leq n, u eq v)$$$, denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that there are no self-loops or multiple edges. It is guaranteed that the sum of $$$n$$$ for all test cases and the sum of $$$m$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case: If you found a subset of vertices such that each vertex of this subset has at least $$$k$$$ neighbors in the subset in the first line output $$$1$$$ and the size of the subset. On the second line output the vertices of the subset in any order. If you found a clique of size $$$k$$$ then in the first line output $$$2$$$ and in the second line output the vertices of the clique in any order. If there are no required subsets and cliques print $$$-1$$$. If there exists multiple possible answers you can print any of them. Example Input 3 5 9 4 1 2 1 3 1 4 1 5 2 3 2 4 2 5 3 4 3 5 10 15 3 1 2 2 3 3 4 4 5 5 1 1 7 2 8 3 9 4 10 5 6 7 10 10 8 8 6 6 9 9 7 4 5 4 1 2 2 3 3 4 4 1 1 3 Output 2 4 1 2 3 1 10 1 2 3 4 5 6 7 8 9 10 -1 Note In the first test case: the subset $$${1, 2, 3, 4}$$$ is a clique of size $$$4$$$. In the second test case: degree of each vertex in the original graph is at least $$$3$$$. So the set of all vertices is a correct answer. In the third test case: there are no cliques of size $$$4$$$ or required subsets, so the answer is $$$-1$$$. | 2,600 | false | false | false | false | true | true | false | false | false | true | 3,470 |
1715B | Stanley defines the beauty of an array $$$a$$$ of length $$$n$$$, which contains non-negative integers, as follows: $$$$$$sumlimits_{i = 1}^{n} left lfloor frac{a_{i}}{k} ight floor,$$$$$$ which means that we divide each element by $$$k$$$, round it down, and sum up the resulting values. Stanley told Sam the integer $$$k$$$ and asked him to find an array $$$a$$$ of $$$n$$$ non-negative integers, such that the beauty is equal to $$$b$$$ and the sum of elements is equal to $$$s$$$. Help Samxa0— find any of the arrays satisfying the conditions above. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains integers $$$n$$$, $$$k$$$, $$$b$$$, $$$s$$$ ($$$1 leq n leq 10^{5}$$$, $$$1 leq k leq 10^{9}$$$, $$$0 leq b leq 10^{9}$$$, $$$0 leq s leq 10^{18}$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print $$$-1$$$ if such array $$$a$$$ does not exist. Otherwise print $$$n$$$ non-negative integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_{i} leq 10^{18}$$$)xa0— the answer. Example Input 8 1 6 3 100 3 6 3 12 3 6 3 19 5 4 7 38 5 4 7 80 99978 1000000000 100000000 1000000000000000000 1 1 0 0 4 1000000000 1000000000 1000000000000000000 Output -1 -1 0 0 19 0 3 3 3 29 -1 -1 0 0 0 0 1000000000000000000 Note In the first, the second, the fifth and the sixth test cases of the example it is possible to show that such array does not exist. In the third testcase of the example $$$a = [0, 0, 19]$$$. The sum of elements in it is equal to 19, the beauty of it is equal to $$$left ( left lfloor frac{0}{6} ight floor + left lfloor frac{0}{6} ight floor + left lfloor frac{19}{6} ight floor ight ) = (0 + 0 + 3) = 3$$$. In the fourth testcase of the example $$$a = [0, 3, 3, 3, 29]$$$. The sum of elements in it is equal to $$$38$$$, the beauty of it is equal to $$$(0 + 0 + 0 + 0 + 7) = 7$$$. | 1,000 | true | true | false | false | false | true | false | false | false | false | 1,992 |
352B | Problem - 352B - 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 *1300 No tag edit access → Contest materials . These numbers, sorted in the increasing order, must form an arithmetic progression. Help Jeff, find all _x_ that meet the problem conditions. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The next line contains integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009105). The numbers are separated by spaces. Output In the first line print integer _t_ — the number of valid _x_. On each of the next _t_ lines print two integers _x_ and _p__x_, where _x_ is current suitable value, _p__x_ is the common difference between numbers in the progression (if _x_ occurs exactly once in the sequence, _p__x_ must equal 0). Print the pairs in the order of increasing _x_. Examples Input 1 2 Output 1 2 0 Input 8 1 2 1 3 1 2 1 5 Output 4 1 2 2 4 3 0 5 0 Note In the first test 2 occurs exactly once in the sequence, ergo _p_2u2009=u20090. | 1,300 | false | false | true | false | false | false | false | false | true | false | 8,424 |
802M | Problem - 802M - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy sortings *1200 No tag edit access → Contest materials time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The marmots have prepared a very easy problem for this year's HC2 – this one. It involves numbers _n_, _k_ and a sequence of _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_. They also came up with a beautiful and riveting story for the problem statement. It explains what the input means, what the program should output, and it also reads like a good criminal. However I, Heidi, will have none of that. As my joke for today, I am removing the story from the statement and replacing it with these two unhelpful paragraphs. Now solve the problem, fools! Input The first line of the input contains two space-separated integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20092200). The second line contains _n_ space-separated integers _a_1,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009104). Output Output one number. Examples Input 8 5 1 1 1 1 1 1 1 1 Output 5 Input 10 3 16 8 2 4 512 256 32 128 64 1 Output 7 Input 5 1 20 10 50 30 46 Output 10 Input 6 6 6 6 6 6 6 6 Output 36 Input 1 1 100 Output 100 | 1,200 | false | true | false | false | false | false | false | false | true | false | 6,555 |
788E | On the 228-th international Uzhlyandian Wars strategic game tournament teams from each country are called. The teams should consist of 5 participants. The team of Uzhlyandia will consist of soldiers, because there are no gamers. Masha is a new minister of defense and gaming. The prime duty of the minister is to calculate the efficiency of the Uzhlandian army. The army consists of _n_ soldiers standing in a row, enumerated from 1 to _n_. For each soldier we know his skill in Uzhlyandian Wars: the _i_-th soldier's skill is _a__i_. It was decided that the team will consist of three players and two assistants. The skills of players should be same, and the assistants' skills should not be greater than the players' skill. Moreover, it is important for Masha that one of the assistants should stand in the row to the left of the players, and the other one should stand in the row to the right of the players. Formally, a team is five soldiers with indexes _i_, _j_, _k_, _l_, _p_, such that 1u2009≤u2009_i_u2009<u2009_j_u2009<u2009_k_u2009<u2009_l_u2009<u2009_p_u2009≤u2009_n_ and _a__i_u2009≤u2009_a__j_u2009=u2009_a__k_u2009=u2009_a__l_u2009≥u2009_a__p_. The efficiency of the army is the number of different teams Masha can choose. Two teams are considered different if there is such _i_ such that the _i_-th soldier is a member of one team, but not a member of the other team. Initially, all players are able to be players. For some reasons, sometimes some soldiers become unable to be players. Sometimes some soldiers, that were unable to be players, become able to be players. At any time any soldier is able to be an assistant. Masha wants to control the efficiency of the army, so she asked you to tell her the number of different possible teams modulo 1000000007 (109u2009+u20097) after each change. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of soldiers in Uzhlyandia. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the soldiers' skills. The third line contains single integer _m_ (1u2009≤u2009_m_u2009≤u2009105)xa0— the number of changes. The next _m_ lines contain the changes, each change is described with two integers _t_ and _x_ (1u2009≤u2009_t_u2009≤u20092, 1u2009≤u2009_x_u2009≤u2009_n_) on a separate line. If _t_u2009=u20091, then the _x_-th soldier is unable to be a player after this change. If _t_u2009=u20092, then the _x_-th soldier is able to be a player after this change. It is guaranteed that before each query of the first type the soldier is able to be a player, and before each query of the second type the soldier is unable to be a player. Output Print _m_ integersxa0— the number of distinct teams after each change. Print the answers modulo 1000000007 (109u2009+u20097). Examples Input 8 3 4 4 2 4 5 4 1 3 1 5 2 5 1 2 Note In the first example, after the first change the only team consists of soldiers [1,u20092,u20094,u20095,u20096]. After the second change any five soldiers can form a team. In the first example after the first change the only team is soldiers [1,u20092,u20093,u20097,u20098]. After the second change the possible teams are: [1,u20092,u20093,u20095,u20097], [1,u20092,u20093,u20095,u20098], [1,u20092,u20093,u20097,u20098], [1,u20092,u20095,u20097,u20098], [1,u20093,u20095,u20097,u20098], [2,u20093,u20095,u20097,u20098]. After the third change the possible teams are: [1,u20093,u20095,u20097,u20098], [2,u20093,u20095,u20097,u20098]. | 2,900 | false | false | false | false | true | false | false | false | false | false | 6,617 |
755E | PolandBall has an undirected simple graph consisting of _n_ vertices. Unfortunately, it has no edges. The graph is very sad because of that. PolandBall wanted to make it happier, adding some red edges. Then, he will add white edges in every remaining place. Therefore, the final graph will be a clique in two colors: white and red. Colorfulness of the graph is a value _min_(_d__r_,u2009_d__w_), where _d__r_ is the diameter of the red subgraph and _d__w_ is the diameter of white subgraph. The diameter of a graph is a largest value _d_ such that shortest path between some pair of vertices in it is equal to _d_. If the graph is not connected, we consider its diameter to be -1. PolandBall wants the final graph to be as neat as possible. He wants the final colorfulness to be equal to _k_. Can you help him and find any graph which satisfies PolandBall's requests? Input The only one input line contains two integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u20091000, 1u2009≤u2009_k_u2009≤u20091000), representing graph's size and sought colorfulness. Output If it's impossible to find a suitable graph, print -1. Otherwise, you can output any graph which fulfills PolandBall's requirements. First, output _m_xa0— the number of red edges in your graph. Then, you should output _m_ lines, each containing two integers _a__i_ and _b__i_, (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_) which means that there is an undirected red edge between vertices _a__i_ and _b__i_. Every red edge should be printed exactly once, you can print the edges and the vertices of every edge in arbitrary order. Remember that PolandBall's graph should remain simple, so no loops or multiple edges are allowed. Note In the first sample case, no graph can fulfill PolandBall's requirements. In the second sample case, red graph is a path from 1 to 5. Its diameter is 4. However, white graph has diameter 2, because it consists of edges 1-3, 1-4, 1-5, 2-4, 2-5, 3-5. | 2,400 | false | false | false | false | false | true | false | false | false | true | 6,773 |
456A | Problem - 456A - 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 sortings *1100 No tag edit access → Contest materials than the price of the second laptop but the quality of the first laptop is higher (strictly greater) than the quality of the second laptop. Please, check the guess of Alex. You are given descriptions of _n_ laptops. Determine whether two described above laptops exist. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of laptops. Next _n_ lines contain two integers each, _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_), where _a__i_ is the price of the _i_-th laptop, and _b__i_ is the number that represents the quality of the _i_-th laptop (the larger the number is, the higher is the quality). All _a__i_ are distinct. All _b__i_ are distinct. Output If Alex is correct, print "Happy Alex", otherwise print "Poor Alex" (without the quotes). Examples Input 2 1 2 2 1 Output Happy Alex | 1,100 | false | false | false | false | false | false | false | false | true | false | 8,014 |
315B | Sereja has got an array, consisting of _n_ integers, _a_1,u2009_a_2,u2009...,u2009_a__n_. Sereja is an active boy, so he is now going to complete _m_ operations. Each operation will have one of the three forms: 1. Make _v__i_-th array element equal to _x__i_. In other words, perform the assignment _a__v__i_u2009=u2009_x__i_. 2. Increase each array element by _y__i_. In other words, perform _n_ assignments _a__i_u2009=u2009_a__i_u2009+u2009_y__i_ (1u2009≤u2009_i_u2009≤u2009_n_). 3. Take a piece of paper and write out the _q__i_-th array element. That is, the element _a__q__i_. Help Sereja, complete all his operations. Input The first line contains integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the original array. Next _m_ lines describe operations, the _i_-th line describes the _i_-th operation. The first number in the _i_-th line is integer _t__i_ (1u2009≤u2009_t__i_u2009≤u20093) that represents the operation type. If _t__i_u2009=u20091, then it is followed by two integers _v__i_ and _x__i_, (1u2009≤u2009_v__i_u2009≤u2009_n_,u20091u2009≤u2009_x__i_u2009≤u2009109). If _t__i_u2009=u20092, then it is followed by integer _y__i_ (1u2009≤u2009_y__i_u2009≤u2009104). And if _t__i_u2009=u20093, then it is followed by integer _q__i_ (1u2009≤u2009_q__i_u2009≤u2009_n_). Output For each third type operation print value _a__q__i_. Print the values in the order, in which the corresponding queries follow in the input. Examples Input 10 11 1 2 3 4 5 6 7 8 9 10 3 2 3 9 2 10 3 1 3 10 1 1 10 2 10 2 10 3 1 3 10 3 9 | 1,200 | false | false | true | false | false | false | false | false | false | false | 8,584 |
1913D | You are given an array $$$[p_1, p_2, dots, p_n]$$$, where all elements are distinct. You can perform several (possibly zero) operations with it. In one operation, you can choose a contiguous subsegment of $$$p$$$ and remove all elements from that subsegment, except for the minimum element on that subsegment. For example, if $$$p = [3, 1, 4, 7, 5, 2, 6]$$$ and you choose the subsegment from the $$$3$$$-rd element to the $$$6$$$-th element, the resulting array is $$$[3, 1, 2, 6]$$$. An array $$$a$$$ is called reachable if it can be obtained from $$$p$$$ using several (maybe zero) aforementioned operations. Calculate the number of reachable arrays, and print it modulo $$$998244353$$$. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Each test case consists of two lines. The first line contains one integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$). The second line contains $$$n$$$ distinct integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le 10^9$$$). Additional constraint on the input: the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print one integer — the number of reachable arrays, taken modulo $$$998244353$$$. | 2,100 | false | false | false | true | true | false | false | false | false | false | 822 |
750E | A string _t_ is called nice if a string "2017" occurs in _t_ as a subsequence but a string "2016" doesn't occur in _t_ as a subsequence. For example, strings "203434107" and "9220617" are nice, while strings "20016", "1234" and "20167" aren't nice. The ugliness of a string is the minimum possible number of characters to remove, in order to obtain a nice string. If it's impossible to make a string nice by removing characters, its ugliness is u2009-u20091. Limak has a string _s_ of length _n_, with characters indexed 1 through _n_. He asks you _q_ queries. In the _i_-th query you should compute and print the ugliness of a substring (continuous subsequence) of _s_ starting at the index _a__i_ and ending at the index _b__i_ (inclusive). Input The first line of the input contains two integers _n_ and _q_ (4u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_q_u2009≤u2009200u2009000)xa0— the length of the string _s_ and the number of queries respectively. The second line contains a string _s_ of length _n_. Every character is one of digits '0'–'9'. The _i_-th of next _q_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_u2009≤u2009_b__i_u2009≤u2009_n_), describing a substring in the _i_-th query. Output For each query print the ugliness of the given substring. Examples Input 8 3 20166766 1 8 1 7 2 8 Input 15 5 012016662091670 3 4 1 14 4 15 1 13 10 15 Note In the first sample: In the first query, _ugliness_("20166766")u2009=u20094 because all four sixes must be removed. In the second query, _ugliness_("2016676")u2009=u20093 because all three sixes must be removed. In the third query, _ugliness_("0166766")u2009=u2009u2009-u20091 because it's impossible to remove some digits to get a nice string. In the second sample: In the second query, _ugliness_("01201666209167")u2009=u20092. It's optimal to remove the first digit '2' and the last digit '6', what gives a string "010166620917", which is nice. In the third query, _ugliness_("016662091670")u2009=u20091. It's optimal to remove the last digit '6', what gives a nice string "01666209170". | 2,600 | false | false | false | true | true | false | false | false | false | false | 6,789 |
1712C | An array is sorted if it has no inversions A Young Boy You are given an array of $$$n$$$ positive integers $$$a_1,a_2,ldots,a_n$$$. In one operation you do the following: 1. Choose any integer $$$x$$$. 2. For all $$$i$$$ such that $$$a_i = x$$$, do $$$a_i := 0$$$ (assign $$$0$$$ to $$$a_i$$$). Find the minimum number of operations required to sort the array in non-decreasing order. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The 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$$$ positive integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print one integerxa0— the minimum number of operations required to sort the array in non-decreasing order. Example Input 5 3 3 3 2 4 1 3 1 3 5 4 1 5 3 2 4 2 4 1 2 1 1 Note In the first test case, you can choose $$$x = 3$$$ for the operation, the resulting array is $$$[0, 0, 2]$$$. In the second test case, you can choose $$$x = 1$$$ for the first operation and $$$x = 3$$$ for the second operation, the resulting array is $$$[0, 0, 0, 0]$$$. | 1,100 | false | true | false | false | false | false | false | false | true | false | 2,011 |
1326G | Let's call a graph with $$$n$$$ vertices, each of which has it's own point $$$A_i = (x_i, y_i)$$$ with integer coordinates, a planar tree if: All points $$$A_1, A_2, ldots, A_n$$$ are different and no three points lie on the same line. The graph is a tree, i.e. there are exactly $$$n-1$$$ edges there exists a path between any pair of vertices. For all pairs of edges $$$(s_1, f_1)$$$ and $$$(s_2, f_2)$$$, such that $$$s_1 eq s_2, s_1 eq f_2,$$$ $$$f_1 eq s_2$$$, and $$$f_1 eq f_2$$$, the segments $$$A_{s_1} A_{f_1}$$$ and $$$A_{s_2} A_{f_2}$$$ don't intersect. Imagine a planar tree with $$$n$$$ vertices. Consider the convex hull of points $$$A_1, A_2, ldots, A_n$$$. Let's call this tree a spiderweb tree if for all $$$1 leq i leq n$$$ the following statements are true: All leaves (vertices with degree $$$leq 1$$$) of the tree lie on the border of the convex hull. All vertices on the border of the convex hull are leaves. An example of a spiderweb tree: The points $$$A_3, A_6, A_7, A_4$$$ lie on the convex hull and the leaf vertices of the tree are $$$3, 6, 7, 4$$$. Refer to the notes for more examples. Let's call the subset $$$S subset {1, 2, ldots, n}$$$ of vertices a subtree of the tree if for all pairs of vertices in $$$S$$$, there exists a path that contains only vertices from $$$S$$$. Note that any subtree of the planar tree is a planar tree. You are given a planar tree with $$$n$$$ vertexes. Let's call a partition of the set $$${1, 2, ldots, n}$$$ into non-empty subsets $$$A_1, A_2, ldots, A_k$$$ (i.e. $$$A_i cap A_j = emptyset$$$ for all $$$1 leq i < j leq k$$$ and $$$A_1 cup A_2 cup ldots cup A_k = {1, 2, ldots, n}$$$) good if for all $$$1 leq i leq k$$$, the subtree $$$A_i$$$ is a spiderweb tree. Two partitions are different if there exists some set that lies in one parition, but not the other. Find the number of good partitions. Since this number can be very large, find it modulo $$$998,244,353$$$. Input The first line contains an integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the number of vertices in the tree. The next $$$n$$$ lines each contain two integers $$$x_i, y_i$$$ ($$$-10^9 leq x_i, y_i leq 10^9$$$) xa0— the coordinates of $$$i$$$-th vertex, $$$A_i$$$. The next $$$n-1$$$ lines contain two integers $$$s, f$$$ ($$$1 leq s, f leq n$$$)xa0— the edges $$$(s, f)$$$ of the tree. It is guaranteed that all given points are different and that no three of them lie at the same line. Additionally, it is guaranteed that the given edges and coordinates of the points describe a planar tree. Output Print one integer xa0— the number of good partitions of vertices of the given planar tree, modulo $$$998,244,353$$$. Examples Input 4 0 0 0 1 -1 -1 1 -1 1 2 1 3 1 4 Input 5 3 2 0 -3 -5 -3 5 -5 4 5 4 2 4 1 5 2 2 3 Input 6 4 -5 0 1 -2 8 3 -10 0 -1 -4 -5 2 5 3 2 1 2 4 6 4 2 Input 8 0 0 -1 2 -2 5 -5 1 1 3 0 3 2 4 -1 -4 1 2 3 2 5 6 4 2 1 5 5 7 5 8 Note The picture for the first sample. In the first test case, all good partitions are: 1. $$${1}$$$, $$${2}$$$, $$${3}$$$, $$${4}$$$; 2. $$${1, 2}$$$, $$${3}$$$, $$${4}$$$; 3. $$${1, 3}$$$, $$${2}$$$, $$${4}$$$; 4. $$${1, 4}$$$, $$${2}$$$, $$${3}$$$; 5. $$${1, 2, 3, 4}$$$. The partition $$${1, 2, 3}$$$, $$${4}$$$ isn't good, because the subtree $$${1, 2, 3}$$$ isn't spiderweb tree, since the non-leaf vertex $$$1$$$ lies on the convex hull. The partition $$${2, 3, 4}$$$, $$${1}$$$ isn't good, because the subset $$${2, 3, 4}$$$ isn't a subtree. The picture for the second sample. In the second test case, the given tree isn't a spiderweb tree, because the leaf vertex $$$1$$$ doesn't lie on the convex hull. However, the subtree $$${2, 3, 4, 5}$$$ is a spiderweb tree. The picture for the third sample. The picture for the fourth sample. In the fourth test case, the partition $$${1, 2, 3, 4}$$$, $$${5, 6, 7, 8}$$$ is good because all subsets are spiderweb subtrees. | 3,500 | false | false | false | true | false | false | false | false | false | false | 4,079 |
748A | Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are _n_ lanes of _m_ desks each, and there are two working places at each of the desks. The lanes are numbered from 1 to _n_ from the left to the right, the desks in a lane are numbered from 1 to _m_ starting from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture). The organizers numbered all the working places from 1 to 2_nm_. The places are numbered by lanes (i.xa0e. all the places of the first lane go first, then all the places of the second lane, and so on), in a lane the places are numbered starting from the nearest to the blackboard (i.xa0e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right. The picture illustrates the first and the second samples. Santa Clause knows that his place has number _k_. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right! Input The only line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u200910u2009000, 1u2009≤u2009_k_u2009≤u20092_nm_)xa0— the number of lanes, the number of desks in each lane and the number of Santa Claus' place. Output Print two integers: the number of lane _r_, the number of desk _d_, and a character _s_, which stands for the side of the desk Santa Claus. The character _s_ should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right. Note The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example. In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right. | 800 | true | false | true | false | false | false | false | false | false | false | 6,804 |
1210G | Mateusz likes to travel! However, on his $$$42$$$nd visit to Saint Computersburg there is not much left to sightsee. That's why he decided to go to an escape room with his friends! The team has solved all riddles flawlessly. There is only one riddle remaining — a huge circular table! There are $$$n$$$ weighing scales lying on top of the table, distributed along the circle. Each scale is adjacent to exactly two other scales: for each $$$i in {1, 2, dots, n-1}$$$, the $$$i$$$-th and the $$$(i+1)$$$-th scales are adjacent to each other, as well as the first and the $$$n$$$-th scale. The $$$i$$$-th scale initially contains $$$a_i$$$ heavy coins. Mateusz can perform moves — each move consists of fetching a single coin from one scale and putting it on any adjacent scale. It turns out that the riddle will be solved when there is a specific amount of coins on each of the scales. Specifically, each scale has parameters $$$l_i$$$ and $$$r_i$$$. If each coin lies on a single scale and for each $$$i$$$, the $$$i$$$-th scale contains at least $$$l_i$$$ and at most $$$r_i$$$ coins, the riddle will be solved and Mateusz's team will win! Mateusz is aiming for the best possible time. Therefore, he wants to solved the riddle as quickly as possible. What is the minimum possible number of moves required to fulfill all the conditions? Input The first line contains an integer $$$n$$$ ($$$3 le n le 35,000$$$) — the number of weighing scales in the circle. The following $$$n$$$ lines describe the scales. The $$$i$$$-th of these lines describes the $$$i$$$-th scale and consists of three integers $$$a_i, l_i, r_i$$$ ($$$0 le a_i le 35,000$$$, $$$0 le l_i le r_i le 35,000$$$). It's guaranteed that the riddle is solvable, that is, $$$sum_{i=1}^n l_i le sum_{i=1}^n a_i le sum_{i=1}^n r_i$$$. | 3,500 | false | false | false | true | false | false | false | false | false | false | 4,629 |
915C | Problem - 915C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp greedy *1700 No tag edit access → Contest materials of the digits of _a_ to construct maximal number not exceeding _b_. No number in input and/or output can start with the digit 0. It is allowed to leave _a_ as it is. Input The first line contains integer _a_ (1u2009≤u2009_a_u2009≤u20091018). The second line contains integer _b_ (1u2009≤u2009_b_u2009≤u20091018). Numbers don't have leading zeroes. It is guaranteed that answer exists. Output Print the maximum possible number that is a permutation of digits of _a_ and is not greater than _b_. The answer can't have any leading zeroes. It is guaranteed that the answer exists. The number in the output should have exactly the same length as number _a_. It should be a permutation of digits of _a_. Examples Input 123 222 Output 213 Input 3921 10000 Output 9321 Input 4940 5000 Output 4940 | 1,700 | false | true | false | true | false | false | false | false | false | false | 6,073 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.