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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
497E | Assume that _s__k_(_n_) equals the sum of digits of number _n_ in the _k_-based notation. For example, _s_2(5)u2009=u2009_s_2(1012)u2009=u20091u2009+u20090u2009+u20091u2009=u20092, _s_3(14)u2009=u2009_s_3(1123)u2009=u20091u2009+u20091u2009+u20092u2009=u20094. The sequence of integers _a_0,u2009...,u2009_a__n_u2009-u20091 is defined as . Your task is to calculate the number of distinct subsequences of sequence _a_0,u2009...,u2009_a__n_u2009-u20091. Calculate the answer modulo 109u2009+u20097. Sequence _a_1,u2009...,u2009_a__k_ is called to be a subsequence of sequence _b_1,u2009...,u2009_b__l_, if there is a sequence of indices 1u2009≤u2009_i_1u2009<u2009...u2009<u2009_i__k_u2009≤u2009_l_, such that _a_1u2009=u2009_b__i_1, ..., _a__k_u2009=u2009_b__i__k_. In particular, an empty sequence (i.e. the sequence consisting of zero elements) is a subsequence of any sequence. Input The first line contains two space-separated numbers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20091018, 2u2009≤u2009_k_u2009≤u200930). Output In a single line print the answer to the problem modulo 109u2009+u20097. Note In the first sample the sequence _a__i_ looks as follows: (0,u20091,u20091,u20090). All the possible subsequences are: (),u2009(0),u2009(0,u20090),u2009(0,u20091),u2009(0,u20091,u20090),u2009(0,u20091,u20091),u2009(0,u20091,u20091,u20090),u2009(1),u2009(1,u20090),u2009(1,u20091),u2009(1,u20091,u20090). In the second sample the sequence _a__i_ looks as follows: (0,u20091,u20092,u20093,u20094,u20095,u20096). The subsequences of this sequence are exactly all increasing sequences formed from numbers from 0 to 6. It is easy to see that there are 27u2009=u2009128 such sequences. | 2,900 | false | false | false | true | false | false | false | false | false | false | 7,845 |
1102B | You are given an array $$$a$$$ consisting of $$$n$$$ integer numbers. You have to color this array in $$$k$$$ colors in such a way that: Each element of the array should be colored in some color; For each $$$i$$$ from $$$1$$$ to $$$k$$$ there should be at least one element colored in the $$$i$$$-th color in the array; For each $$$i$$$ from $$$1$$$ to $$$k$$$ all elements colored in the $$$i$$$-th color should be distinct. Obviously, such coloring might be impossible. In this case, print "NO". Otherwise print "YES" and any coloring (i.e. numbers $$$c_1, c_2, dots c_n$$$, where $$$1 le c_i le k$$$ and $$$c_i$$$ is the color of the $$$i$$$-th element of the given array) satisfying the conditions above. If there are multiple answers, you can print any. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 5000$$$) — the length of the array $$$a$$$ and the number of colors, respectively. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 5000$$$) — elements of the array $$$a$$$. Output If there is no answer, print "NO". Otherwise print "YES" and any coloring (i.e. numbers $$$c_1, c_2, dots c_n$$$, where $$$1 le c_i le k$$$ and $$$c_i$$$ is the color of the $$$i$$$-th element of the given array) satisfying the conditions described in the problem statement. If there are multiple answers, you can print any. Note In the first example the answer $$$2~ 1~ 2~ 1$$$ is also acceptable. In the second example the answer $$$1~ 1~ 1~ 2~ 2$$$ is also acceptable. There exist other acceptable answers for both examples. | 1,400 | false | true | false | false | false | false | false | false | true | false | 5,192 |
1987C | There are $$$n$$$ flowers in a row, the $$$i$$$-th of them initially has a positive height of $$$h_i$$$ meters. Every second, the wind will blow from the left, causing the height of some flowers to decrease. Specifically, every second, for each $$$i$$$ from $$$1$$$ to $$$n$$$, in this order, the following happens: If $$$i = n$$$ or $$$h_i > h_{i + 1}$$$, the value of $$$h_i$$$ changes to $$$max(0, h_i - 1)$$$. How many seconds will pass before $$$h_i=0$$$ for all $$$1 le i le n$$$ for the first time? Input Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of flowers. The second line of each test case contains $$$n$$$ integers $$$h_1, h_2, ldots, h_n$$$ ($$$1 le h_i le 10 ^ 9$$$)xa0— the heights of the flowers. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output a single integerxa0— the number of seconds that will pass before $$$h_i=0$$$ for all $$$1 le i le n$$$. Example Input 4 3 1 1 2 2 3 1 1 9 5 7 4 4 3 2 Note In the first test case, the flower heights change as follows: $$$[1, 1, 2] ightarrow [1, 1, 1] ightarrow [1, 1, 0] ightarrow [1, 0, 0] ightarrow [0, 0, 0]$$$. In the second test case, the flower heights change as follows: $$$[3, 1] ightarrow [2, 0] ightarrow [1, 0] ightarrow [0, 0]$$$. | 1,200 | false | true | false | true | false | false | false | false | false | false | 355 |
1324E | Vova had a pretty weird sleeping schedule. There are $$$h$$$ hours in a day. Vova will sleep exactly $$$n$$$ times. The $$$i$$$-th time he will sleep exactly after $$$a_i$$$ hours from the time he woke up. You can assume that Vova woke up exactly at the beginning of this story (the initial time is $$$0$$$). Each time Vova sleeps exactly one day (in other words, $$$h$$$ hours). Vova thinks that the $$$i$$$-th sleeping time is good if he starts to sleep between hours $$$l$$$ and $$$r$$$ inclusive. Vova can control himself and before the $$$i$$$-th time can choose between two options: go to sleep after $$$a_i$$$ hours or after $$$a_i - 1$$$ hours. Your task is to say the maximum number of good sleeping times Vova can obtain if he acts optimally. Input The first line of the input contains four integers $$$n, h, l$$$ and $$$r$$$ ($$$1 le n le 2000, 3 le h le 2000, 0 le l le r < h$$$) — the number of times Vova goes to sleep, the number of hours in a day and the segment of the good sleeping time. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i < h$$$), where $$$a_i$$$ is the number of hours after which Vova goes to sleep the $$$i$$$-th time. Output Print one integer — the maximum number of good sleeping times Vova can obtain if he acts optimally. Example Input 7 24 21 23 16 17 14 20 20 11 22 Note The maximum number of good times in the example is $$$3$$$. The story starts from $$$t=0$$$. Then Vova goes to sleep after $$$a_1 - 1$$$ hours, now the time is $$$15$$$. This time is not good. Then Vova goes to sleep after $$$a_2 - 1$$$ hours, now the time is $$$15 + 16 = 7$$$. This time is also not good. Then Vova goes to sleep after $$$a_3$$$ hours, now the time is $$$7 + 14 = 21$$$. This time is good. Then Vova goes to sleep after $$$a_4 - 1$$$ hours, now the time is $$$21 + 19 = 16$$$. This time is not good. Then Vova goes to sleep after $$$a_5$$$ hours, now the time is $$$16 + 20 = 12$$$. This time is not good. Then Vova goes to sleep after $$$a_6$$$ hours, now the time is $$$12 + 11 = 23$$$. This time is good. Then Vova goes to sleep after $$$a_7$$$ hours, now the time is $$$23 + 22 = 21$$$. This time is also good. | 1,700 | false | false | true | true | false | false | false | false | false | false | 4,095 |
87C | Problem - 87C - 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 games math *2000 No tag edit access → Contest materials ") . Output If Serozha wins, print _k_, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game. If Gena wins, print "-1" (without the quotes). Examples Input 3 Output 2 Input 6 Output -1 Input 100 Output 8 | 2,000 | true | false | false | true | false | false | false | false | false | false | 9,534 |
1238C | You are playing a game where your character should overcome different obstacles. The current problem is to come down from a cliff. The cliff has height $$$h$$$, and there is a moving platform on each height $$$x$$$ from $$$1$$$ to $$$h$$$. Each platform is either hidden inside the cliff or moved out. At first, there are $$$n$$$ moved out platforms on heights $$$p_1, p_2, dots, p_n$$$. The platform on height $$$h$$$ is moved out (and the character is initially standing there). If you character is standing on some moved out platform on height $$$x$$$, then he can pull a special lever, which switches the state of two platforms: on height $$$x$$$ and $$$x - 1$$$. In other words, the platform you are currently standing on will hide in the cliff and the platform one unit below will change it state: it will hide if it was moved out or move out if it was hidden. In the second case, you will safely land on it. Note that this is the only way to move from one platform to another. Your character is quite fragile, so it can safely fall from the height no more than $$$2$$$. In other words falling from the platform $$$x$$$ to platform $$$x - 2$$$ is okay, but falling from $$$x$$$ to $$$x - 3$$$ (or lower) is certain death. Sometimes it's not possible to come down from the cliff, but you can always buy (for donate currency) several magic crystals. Each magic crystal can be used to change the state of any single platform (except platform on height $$$h$$$, which is unaffected by the crystals). After being used, the crystal disappears. What is the minimum number of magic crystal you need to buy to safely land on the $$$0$$$ ground level? Input The first line contains one integer $$$q$$$ ($$$1 le q le 100$$$) — the number of queries. Each query contains two lines and is independent of all other queries. The first line of each query contains two integers $$$h$$$ and $$$n$$$ ($$$1 le h le 10^9$$$, $$$1 le n le min(h, 2 cdot 10^5)$$$) — the height of the cliff and the number of moved out platforms. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$h = p_1 > p_2 > dots > p_n ge 1$$$) — the corresponding moved out platforms in the descending order of their heights. The sum of $$$n$$$ over all queries does not exceed $$$2 cdot 10^5$$$. | 1,600 | true | true | false | true | false | false | false | false | false | false | 4,501 |
416D | Polycarpus develops an interesting theory about the interrelation of arithmetic progressions with just everything in the world. His current idea is that the population of the capital of Berland changes over time like an arithmetic progression. Well, or like multiple arithmetic progressions. Polycarpus believes that if he writes out the population of the capital for several consecutive years in the sequence _a_1,u2009_a_2,u2009...,u2009_a__n_, then it is convenient to consider the array as several arithmetic progressions, written one after the other. For example, sequence (8,u20096,u20094,u20092,u20091,u20094,u20097,u200910,u20092) can be considered as a sequence of three arithmetic progressions (8,u20096,u20094,u20092), (1,u20094,u20097,u200910) and (2), which are written one after another. Unfortunately, Polycarpus may not have all the data for the _n_ consecutive years (a census of the population doesn't occur every year, after all). For this reason, some values of _a__i_ u200bu200bmay be unknown. Such values are represented by number -1. For a given sequence _a_u2009=u2009(_a_1,u2009_a_2,u2009...,u2009_a__n_), which consists of positive integers and values u200bu200b-1, find the minimum number of arithmetic progressions Polycarpus needs to get _a_. To get _a_, the progressions need to be written down one after the other. Values u200bu200b-1 may correspond to an arbitrary positive integer and the values _a__i_u2009>u20090 must be equal to the corresponding elements of sought consecutive record of the progressions. Let us remind you that a finite sequence _c_ is called an arithmetic progression if the difference _c__i_u2009+u20091u2009-u2009_c__i_ of any two consecutive elements in it is constant. By definition, any sequence of length 1 is an arithmetic progression. Input The first line of the input contains integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the number of elements in the sequence. The second line contains integer values _a_1,u2009_a_2,u2009...,u2009_a__n_ separated by a space (1u2009≤u2009_a__i_u2009≤u2009109 or _a__i_u2009=u2009u2009-u20091). | 2,400 | true | true | true | false | false | false | false | false | false | false | 8,159 |
1559A | Mocha is a young girl from high school. She has learned so much interesting knowledge from her teachers, especially her math teacher. Recently, Mocha is learning about binary system and very interested in bitwise operation. This day, Mocha got a sequence $$$a$$$ of length $$$n$$$. In each operation, she can select an arbitrary interval $$$ — the number of test cases. Each test case consists of two lines. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$) — the length of the sequence. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$). Output For each test case, print one integer — the minimal value of the maximum value in the sequence. Example Input 4 2 1 2 3 1 1 3 4 3 11 3 7 5 11 7 15 3 7 Note In the first test case, Mocha can choose the interval $$$[1,2]$$$, then the sequence becomes $$$[ 0, 0]$$$, where the first element is $$$1,&,2$$$, and the second element is $$$2,&,1$$$. In the second test case, Mocha can choose the interval $$$[1,3]$$$, then the sequence becomes $$$[ 1,1,1]$$$, where the first element is $$$1,&,3$$$, the second element is $$$1,&,1$$$, and the third element is $$$3,&,1$$$. | 900 | true | false | false | false | false | true | false | false | false | false | 2,836 |
1794E | You are given an unweighted tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$ and a list of $$$n-1$$$ integers $$$a_1, a_2, ldots, a_{n-1}$$$. A tree is a connected undirected graph without cycles. You will use each element of the list to label one vertex. No vertex should be labeled twice. You can label the only remaining unlabeled vertex with any integer. A vertex $$$x$$$ is called good if it is possible to do this labeling so that for each vertex $$$i$$$, its label is the distance between $$$x$$$ and $$$i$$$. The distance between two vertices $$$s$$$ and $$$t$$$ on a tree is the minimum number of edges on a path that starts at vertex $$$s$$$ and ends at vertex $$$t$$$. Find all good vertices. Input The first line contains one integer $$$n$$$ ($$$2le nle 2cdot 10^5$$$) — the number of vertices in the tree. The second line contains $$$n - 1$$$ integers $$$a_1,a_2,ldots,a_{n-1}$$$ ($$$0le a_i < n$$$)xa0— the given list. Then, $$$n−1$$$ lines follow. Each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1le u,vle n$$$) denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the edges form a tree. Output In the first line print the number of good vertices. In the second line, print the indices of all good vertices in ascending order. Examples Input 6 2 1 0 1 2 1 2 2 3 2 4 4 5 4 6 Input 5 1 2 1 2 1 2 3 2 3 4 5 4 Note This is the tree for the first example: And these are two possible labelings with the elements on the list so that $$$2$$$ is a good vertex (left) and $$$4$$$ is a good vertex (right). The square below each vertex represents its label. The black squares contain the numbers which were on the given list and the only white square contains the only number which was not on the given list. In the second example, the only good vertex is vertex $$$3$$$. In the third example, there are no good vertices. | 2,400 | false | true | true | true | true | false | false | false | false | false | 1,510 |
2038A | There is a team of $$$n$$$ software engineers numbered from $$$1$$$ to $$$n$$$. Their boss promises to give them a bonus if they complete an additional project. The project requires $$$k$$$ units of work in total. The bonus promised to the $$$i$$$-th engineer is $$$a_i$$$ burles. The boss doesn't assign specific tasks to engineers; it is expected that every engineer will voluntarily complete some integer amount of work units. The bonus will be paid to the entire team only if the project is completed; in other words, if the total amount of voluntary work units on the project is greater than or equal to $$$k$$$. The amount of work that can be performed by each engineer is not limited. However, all engineers value their labour. The $$$i$$$-th engineer estimates one unit of their work as $$$b_i$$$ burles. If the bonus is paid, the benefit $$$s_i$$$ of the $$$i$$$-th engineer for completing $$$c$$$ units of work is defined as $$$s_i = a_i - c cdot b_i$$$. If the bonus is not paid, the engineer will not volunteer to do any work. Engineers work together for many years, so they know how the bonus is going to be distributed and how much their colleagues value the labour. That is, all $$$a_i$$$ and all $$$b_i$$$ are known to every engineer in the team. Engineers are eager to get the bonus, so they agreed on the following process of work distribution between them: the first engineer says: "I will complete $$$c_1$$$ units of work", where $$$c_1$$$ is a non-negative integer; then, the second engineer says: "I will complete $$$c_2$$$ units of work", where $$$c_2$$$ is a non-negative integer; ... and so on; finally, the $$$n$$$-th engineer says: "I will complete $$$c_n$$$ units of work", where $$$c_n$$$ is a non-negative integer. Every engineer voices $$$c_i$$$ in a way to maximize their own benefit $$$s_i$$$. If the expected benefit is going to be zero, an engineer will still agree to work to get the experience and to help their colleagues obtain the bonus. However, if the benefit is expected to be negative for some reason (an engineer needs to perform an excessive amount of work or the project is not going to be completed), that engineer will not work at all (completes zero amount of work units). Given that every engineer acts perfectly, your task is to find out the numbers $$$c_i$$$ voiced by every engineer. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 1000$$$; $$$1 le k le 10^6$$$)xa0— the number of engineers in the company and the number of work units the project requires, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the bonus which will be paid to the $$$i$$$-th engineer if the project is completed. The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_i le 1000$$$), where $$$b_i$$$ is the work unit cost for the $$$i$$$-th engineer. Output Print $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$0 le c_i le k$$$)xa0— the amount of work completed by each engineer given that every engineer behaves optimally. Note that the answer is unique. Note In the first example, engineers distributed the work across them and got the bonus, even though the benefit for the third engineer is zero. In the second example, the bonus project requires too many work units to complete, so it's more beneficial for engineers not to work at all. | 1,400 | false | true | false | false | false | false | false | false | false | false | 38 |
116A | Linear Kingdom has exactly one tram line. It has _n_ stops, numbered from 1 to _n_ in the order of tram's movement. At the _i_-th stop _a__i_ passengers exit the tram, while _b__i_ passengers enter it. The tram is empty before it arrives at the first stop. Also, when the tram arrives at the last stop, all passengers exit so that it becomes empty. Your task is to calculate the tram's minimum capacity such that the number of people inside the tram at any time never exceeds this capacity. Note that at each stop all exiting passengers exit before any entering passenger enters the tram. Input The first line contains a single number _n_ (2u2009≤u2009_n_u2009≤u20091000) — the number of the tram's stops. Then _n_ lines follow, each contains two integers _a__i_ and _b__i_ (0u2009≤u2009_a__i_,u2009_b__i_u2009≤u20091000) — the number of passengers that exits the tram at the _i_-th stop, and the number of passengers that enter the tram at the _i_-th stop. The stops are given from the first to the last stop in the order of tram's movement. Output Print a single integer denoting the minimum possible capacity of the tram (0 is allowed). Note For the first example, a capacity of 6 is sufficient: At the first stop, the number of passengers inside the tram before arriving is 0. Then, 3 passengers enter the tram, and the number of passengers inside the tram becomes 3. At the second stop, 2 passengers exit the tram (1 passenger remains inside). Then, 5 passengers enter the tram. There are 6 passengers inside the tram now. At the third stop, 4 passengers exit the tram (2 passengers remain inside). Then, 2 passengers enter the tram. There are 4 passengers inside the tram now. Finally, all the remaining passengers inside the tram exit the tram at the last stop. There are no passenger inside the tram now, which is in line with the constraints. Since the number of passengers inside the tram never exceeds 6, a capacity of 6 is sufficient. Furthermore it is not possible for the tram to have a capacity less than 6. Hence, 6 is the correct answer. | 800 | false | false | true | false | false | false | false | false | false | false | 9,423 |
534E | In Berland a bus travels along the main street of the capital. The street begins from the main square and looks like a very long segment. There are _n_ bus stops located along the street, the _i_-th of them is located at the distance _a__i_ from the central square, all distances are distinct, the stops are numbered in the order of increasing distance from the square, that is, _a__i_u2009<u2009_a__i_u2009+u20091 for all _i_ from 1 to _n_u2009-u20091. The bus starts its journey from the first stop, it passes stops 2, 3 and so on. It reaches the stop number _n_, turns around and goes in the opposite direction to stop 1, passing all the intermediate stops in the reverse order. After that, it again starts to move towards stop _n_. During the day, the bus runs non-stop on this route. The bus is equipped with the Berland local positioning system. When the bus passes a stop, the system notes down its number. One of the key features of the system is that it can respond to the queries about the distance covered by the bus for the parts of its path between some pair of stops. A special module of the system takes the input with the information about a set of stops on a segment of the path, a stop number occurs in the set as many times as the bus drove past it. This module returns the length of the traveled segment of the path (or -1 if it is impossible to determine the length uniquely). The operation of the module is complicated by the fact that stop numbers occur in the request not in the order they were visited but in the non-decreasing order. For example, if the number of stops is 6, and the part of the bus path starts at the bus stop number 5, ends at the stop number 3 and passes the stops as follows: , then the request about this segment of the path will have form: 3,u20094,u20095,u20095,u20096. If the bus on the segment of the path from stop 5 to stop 3 has time to drive past the 1-th stop (i.e., if we consider a segment that ends with the second visit to stop 3 on the way from 5), then the request will have form: 1,u20092,u20092,u20093,u20093,u20094,u20095,u20095,u20096. You will have to repeat the Berland programmers achievement and implement this function. Input The first line contains integer _n_ (2u2009≤u2009_n_u2009≤u20092·105) — the number of stops. The second line contains _n_ integers (1u2009≤u2009_a__i_u2009≤u2009109) — the distance from the _i_-th stop to the central square. The numbers in the second line go in the increasing order. The third line contains integer _m_ (1u2009≤u2009_m_u2009≤u20094·105) — the number of stops the bus visited on some segment of the path. The fourth line contains _m_ integers (1u2009≤u2009_b__i_u2009≤u2009_n_) — the sorted list of numbers of the stops visited by the bus on the segment of the path. The number of a stop occurs as many times as it was visited by a bus. It is guaranteed that the query corresponds to some segment of the path. Note The first test from the statement demonstrates the first example shown in the statement of the problem. The second test from the statement demonstrates the second example shown in the statement of the problem. In the third sample there are two possible paths that have distinct lengths, consequently, the sought length of the segment isn't defined uniquely. In the fourth sample, even though two distinct paths correspond to the query, they have the same lengths, so the sought length of the segment is defined uniquely. | 2,400 | false | true | true | false | false | true | false | false | false | false | 7,697 |
1111D | There is a colony of villains with several holes aligned in a row, where each hole contains exactly one villain. Each colony arrangement can be expressed as a string of even length, where the $$$i$$$-th character of the string represents the type of villain in the $$$i$$$-th hole. Iron Man can destroy a colony only if the colony arrangement is such that all villains of a certain type either live in the first half of the colony or in the second half of the colony. His assistant Jarvis has a special power. It can swap villains of any two holes, i.e. swap any two characters in the string; he can do this operation any number of times. Now Iron Man asks Jarvis $$$q$$$ questions. In each question, he gives Jarvis two numbers $$$x$$$ and $$$y$$$. Jarvis has to tell Iron Man the number of distinct colony arrangements he can create from the original one using his powers such that all villains having the same type as those originally living in $$$x$$$-th hole or $$$y$$$-th hole live in the same half and the Iron Man can destroy that colony arrangement. Two colony arrangements are considered to be different if there exists a hole such that different types of villains are present in that hole in the arrangements. Input The first line contains a string $$$s$$$ ($$$2 le s le 10^{5}$$$), representing the initial colony arrangement. String $$$s$$$ can have both lowercase and uppercase English letters and its length is even. The second line contains a single integer $$$q$$$ ($$$1 le q le 10^{5}$$$)xa0— the number of questions. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le s$$$, $$$x_i e y_i$$$)xa0— the two numbers given to the Jarvis for the $$$i$$$-th question. Output For each question output the number of arrangements possible modulo $$$10^9+7$$$. Note Consider the first example. For the first question, the possible arrangements are "aabb" and "bbaa", and for the second question, index $$$1$$$ contains 'a' and index $$$2$$$ contains 'b' and there is no valid arrangement in which all 'a' and 'b' are in the same half. | 2,600 | true | false | false | true | false | false | false | false | false | false | 5,138 |
95E | Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One night Petya was sleeping. He was dreaming of being the president of some island country. The country is represented by islands connected by two-way roads. Between some islands there is no road way, even through other islands, that's why the country is divided into several regions. More formally, each island belongs to exactly one region, there is a path between any two islands located in the same region; there is no path between any two islands from different regions. A region is lucky if the amount of islands in it is a lucky number. As a real president, Petya first decided to build a presidential palace. Being a lucky numbers' fan, Petya wants to position his palace in one of the lucky regions. However, it is possible that initially the country has no such regions. In this case Petya can build additional roads between different regions, thus joining them. Find the minimum number of roads needed to build to create a lucky region. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105). They are the number of islands and the number of roads correspondingly. Next _m_ lines contain road descriptions. Each road is defined by the numbers of islands that it connects: that is, by two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_). Some roads can connect an island with itself; there can be more than one road between a pair of islands. Numbers in each line are separated by exactly one space character. Output If there's no solution, output the only number "-1" (without the quotes). Otherwise, output the minimum number of roads _r_ that need to be built to get a lucky region. | 2,500 | false | false | false | true | false | false | false | false | false | true | 9,504 |
1796B | You are given two strings $$$a$$$ and $$$b$$$, consisting of lowercase Latin letters. A template $$$t$$$ is string, consisting of lowercase Latin letters and asterisks (character '*'). A template is called asterisk-minor if the number of asterisks in it is less than or equal to the number of letters in it. A string $$$s$$$ is said to be matching a template $$$t$$$ if you can replace each asterisk in $$$t$$$ with a string of lowercase Latin letters (possibly, an empty string) so that it becomes equal to $$$s$$$. Find an asterisk-minor template such that both $$$a$$$ and $$$b$$$ match it, or report that such a template doesn't exist. If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains a string $$$a$$$ ($$$1 le a le 50$$$, where $$$a$$$ is the length of $$$a$$$), consisting of lowercase Latin letters. The second line contains a string $$$b$$$ ($$$1 le b le 50$$$), consisting of lowercase Latin letters. Output For each testcase, output "NO", if there doesn't exist an asterisk-minor template that both $$$a$$$ and $$$b$$$ match. Otherwise, print "YES" in the first line and the template in the second line. If there are multiple answers, print any of them. A template should consist only of lowercase Latin letters and asterisks (character '*'). The number of asterisks should be less than or equal to the number of letters. Example Input 6 aaab zzzb codeforces atcoder codeforces tokitlx aaaa aaaaaa abcd abcd c f Output YES *b YES *co* NO YES a*a*a*a YES abcd NO Note In the first testcase, for a template "*b", you can replace the only asterisk with "aaa" to get "aaab" (which is equal to $$$a$$$) or with "zzz" to get "zzzb" (which is equal to $$$b$$$). In the third testcase, a template "*o*" is not asterisk-minor, as it contains more asterisks than letters. There are no asterisk-minor templates that both $$$a$$$ and $$$b$$$ match. In the fourth testcase, for a template "a*a*a*a", you can replace all asterisks with empty strings to get "aaaa" (which is equal to $$$a$$$) or two of them with "a" and two of them with an empty string to get "aaaaaa" (which is equal to $$$b$$$). In the fifth testcase, there are no asterisks in a template "abcd", so only "abcd" can match it (which is coincidentally both $$$a$$$ and $$$b$$$). | 1,000 | false | false | true | false | false | false | false | false | false | false | 1,501 |
38E | Problem - 38E - Codeforces =============== xa0 - Codeforces Beta Round 38 (ACM-ICPC Rules)]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp sortings *1800 No tag edit access → Contest materials - Codeforces Beta Round #38 (ACM-ICPC Rules)") ") ") ") ") ") ") ") which is the number of marbles. The next _n_ lines contain the descriptions of the marbles in pairs of integers _x__i_, _c__i_ (u2009-u2009109u2009≤u2009_x__i_,u2009_c__i_u2009≤u2009109). The numbers are space-separated. Each description is given on a separate line. No two marbles have identical initial positions. Output Output the single number — the least fine you will have to pay. Examples Input 3 2 3 3 4 1 2 Output 5 Input 4 1 7 3 1 5 10 6 1 Output 11 | 1,800 | false | false | false | true | false | false | false | false | true | false | 9,804 |
1666H | # Heroes of Might Input file: standard input Output file: standard output Time limit: 3 seconds Memory limit: 512 megabytes Recently, Hellen played her favorite game “Heroes of Might”. She had a hero with only one Rust dragon, which was attacked by another hero with a lot of peasants. Another hero had n groups of peasants, i-th of them had ai peasants in it. Unfortunately, Hellen lost that battle, but now she is wondering how big the health of the Rust dragon should be to win against such a big army of peasants? Let’s discuss how the battle goes. Initially, the Rust dragon has hd health points, and each peasant has hp health points. So i-th group of peasants has a total of H = hp · ai health points at the start of the battle. The battle consists of several rounds. In each round, two things happen: • First, the dragon chooses one group of peasants and attacks it . The health of that group is decreased by the dragon’s damage rating d. If the group has zero or less health points, it is destroyed and is removed from the game. • Second, each one of the peasant groups attacks the dragon . A group with the total current health H has d Hhp e peasants still alive and each of them decreases the dragon’s health by one. If the dragon’s health becomes zero or less at any point, it dies and Hellen loses. If all peasant groups are destroyed, Hellen wins the battle. You need to determine the smallest possible hd, which could make Hellen win if she chooses targets on each turn optimally. # Input The first line of the input contains an integer t (1 ≤ t ≤ 1000 ) x16 the number of test cases you need to solve. Each of the test cases is described by two lines. The first line contains three numbers n (1 ≤ n ≤ 1000 ), d (1 ≤ d ≤ 10 9), and hp (1 ≤ hp ≤ 10 9) x16 the number of peasant groups, the dragon’s damage rating, and the health of each peasant. The second line contains n numbers ai (1 ≤ ai ≤ 10 9; hp · ∑ ai ≤ 10 9) x16 the number of peasants in each group. The sum of n over all test cases does not exceed 1000 . # Output For each test case, output one number x16 the smallest amount of health hd that the dragon should have for Hellen to win the battle. If the dragon is never attacked by a peasant, it should still have positive health, so output 1 in this case. # Example standard input standard output 41 15 10 41 10 1 10 2 15 10 4 5 2 11 15 10 17 5126 504 Page 1 of 2 Note In the third test case, the optimal Hellen’s strategy leads to the following battle. At the start, the dragon has hd = 26 health points, and two groups of peasants have H1 = 4 · 10 and H2 = 5 · 10 health points. We’ll denote them as H1 = 40(4) and H2 = 50(5) , placing the value of d Hhp e in the brackets. hd = 26 , H1 = 40(4) , H2 = 50(5) Round 1 The dragon attacks the first group ,dealing 15 damage, leaving H1 = 25(3) . hd = 26 , H1 = 25(3) , H2 = 50(5) Peasants attack the dragon, dealing 3 + 5 damage, leaving hd = 18 . hd = 18 , H1 = 25(3) , H2 = 50(5) Round 2 The dragon attacks the first group ,dealing 15 damage, leaving H1 = 10(1) . hd = 18 , H1 = 10(1) , H2 = 50(5) Peasants attack the dragon, dealing 1 + 5 damage, leaving hd = 12 . hd = 12 , H1 = 10(1) , H2 = 50(5) Round 3 The dragon attacks the second group ,dealing 15 damage, leaving H2 = 35(4) . hd = 12 , H1 = 10(1) , H2 = 35(4) Peasants attack the dragon, dealing 1 + 4 damage, leaving hd = 7 . hd = 7 , H1 = 10(1) , H2 = 35(4) Round 4 The dragon attacks the second group ,dealing 15 damage, leaving H2 = 20(2) . hd = 7 , H1 = 10(1) , H2 = 20(2) Peasants attack the dragon, dealing 1 + 2 damage, leaving hd = 4 . hd = 4 , H1 = 10(1) , H2 = 20(2) Round 5 The dragon attacks the second group ,dealing 15 damage, leaving H2 = 5(1) hd = 4 , H1 = 10(1) , H2 = 5(1) Peasants attack the dragon, dealing 1 + 1 damage, leaving hd = 2 . hd = 2 , H1 = 10(1) , H2 = 5(1) Round 6 The dragon attacks the second group ,destroying it, so it is removed from the game. hd = 2 , H1 = 10(1) Peasants attack the dragon, dealing 1 damage, leaving hd = 1 . hd = 1 , H1 = 10(1) Round 7 The dragon attacks the first group ,destroying it, so it is removed from the game. hd = 1 Game over The dragon is still alive, Hellen wins. Page 2 of 2 | 3,500 | true | false | false | false | false | false | false | false | false | false | 2,278 |
472F | Problem - 472F - Codeforces =============== xa0 of the following form _x__i_ ^= _x__j_ (in the original task _i_ and _j_ must be different, but in this task we allow _i_ to equal _j_). The goal is to maximize the sum of all _x__i_. Now we just change the goal. You are also given _n_ integers _y_1,u2009_y_2,u2009...,u2009_y__n_. You should make _x_1,u2009_x_2,u2009...,u2009_x__n_ exactly equal to _y_1,u2009_y_2,u2009...,u2009_y__n_. In other words, for each _i_ number _x__i_ should be equal to _y__i_. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u200910000). The second line contains _n_ integers: _x_1 to _x__n_ (0u2009≤u2009_x__i_u2009≤u2009109). The third line contains _n_ integers: _y_1 to _y__n_ (0u2009≤u2009_y__i_u2009≤u2009109). Output If there is no solution, output -1. If there is a solution, then in the first line output an integer _m_ (0u2009≤u2009_m_u2009≤u20091000000) – the number of assignments you need to perform. Then print _m_ lines, each line should contain two integers _i_ and _j_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_), which denote assignment _x__i_ ^= _x__j_. If there are multiple solutions you can print any of them. We can prove that under these constraints if there exists a solution then there always exists a solution with no more than 106 operations. Examples Input 2 3 5 6 0 Output 2 1 2 2 2 Input 5 0 0 0 0 0 1 2 3 4 5 Output -1 Input 3 4 5 6 1 2 3 Output 5 3 1 1 2 2 2 2 3 3 1 Input 3 1 2 3 4 5 6 Output -1 Note Assignment _a_ ^= _b_ denotes assignment _a_ = _a_ ^ _b_, where operation "^" is bitwise XOR of two integers. | 2,700 | true | false | false | false | false | true | false | false | false | false | 7,942 |
1889F | Doremy has a rooted tree of size $$$n$$$ whose root is vertex $$$r$$$. Initially there is a number $$$w_i$$$ written on vertex $$$i$$$. Doremy can use her power to perform this operation at most $$$k$$$ times: 1. Choose a vertex $$$x$$$ ($$$1 leq x leq n$$$). 2. Let $$$s = frac{1}{T}sum_{i in T} w_i$$$ where $$$T$$$ is the set of all vertices in $$$x$$$'s subtree. 3. For all $$$i in T$$$, assign $$$w_i := s$$$. Doremy wants to know what is the lexicographically smallest$$$^dagger$$$ array $$$w$$$ after performing all the operations. Can you help her? If there are multiple answers, you may output any one. $$$^dagger$$$ For arrays $$$a$$$ and $$$b$$$ both of length $$$n$$$, $$$a$$$ is lexicographically smaller than $$$b$$$ if and only if there exist an index $$$i$$$ ($$$1 leq i le n$$$) such that $$$a_i < b_i$$$ and for all indices $$$j$$$ such that $$$j<i$$$, $$$a_j=b_j$$$ is satisfied. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line contains three integers $$$n$$$, $$$r$$$, $$$k$$$ ($$$2 le n le 5000$$$, $$$1 le r le n$$$, $$$0 le k le min(500,n)$$$). The second line contains $$$n$$$ integers $$$w_1,w_2,ldots,w_n$$$ ($$$1 le w_i le 10^6$$$). Each of the next $$$n-1$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$), representing an edge between $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ does not exceed $$$50,000$$$. Output For each test case, In the first line, output a single integer $$$cnt$$$ ($$$0 le cnt le k$$$)xa0— the number of operations you perform. Then, in the second line output $$$cnt$$$ integers $$$p_1,p_2,ldots,p_{cnt}$$$xa0— $$$x$$$ is chosen to be $$$p_i$$$ for $$$i$$$-th operation. If there are multiple answers, you may output any one. Example Input 4 6 1 1 1 9 2 6 1 8 1 2 1 3 2 4 3 6 3 5 7 7 2 3 1 3 3 1 1 2 7 1 7 2 7 4 1 5 2 3 4 6 6 5 1 3 1 3 1 1 3 5 3 5 1 5 6 3 4 1 2 3 2 1 1000000 999999 999997 2 1 1 3 Note In the first test case: At first $$$w=[1,9,2,6,1,8]$$$. You can choose some vertex $$$x$$$ to perform at most one operation. If $$$x=1$$$, $$$w=[frac{9}{2},frac{9}{2},frac{9}{2},frac{9}{2},frac{9}{2},frac{9}{2}]$$$. If $$$x=2$$$, $$$w=[1,frac{15}{2},2,frac{15}{2},1,8]$$$. If $$$x=3$$$, $$$w=[1,9,frac{11}{3},6,frac{11}{3},frac{11}{3}]$$$. If $$$x in {4, 5, 6}$$$, $$$w=[1,9,2,6,1,8]$$$. If you don't perform any operation, $$$w=[1,9,2,6,1,8]$$$. $$$w$$$ is lexicographically smallest when $$$x=2$$$. | 3,500 | false | true | false | true | true | false | false | false | false | false | 960 |
126A | Problem - 126A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search brute force math *1900 No tag edit access → Contest materials . Output Print two space-separated integers _y_1 and _y_2 (0u2009≤u2009_y_1u2009≤u2009_x_1, 0u2009≤u2009_y_2u2009≤u2009_x_2). Examples Input 10 70 100 100 25 Output 99 33 Input 300 500 1000 1000 300 Output 1000 0 Input 143 456 110 117 273 Output 76 54 Note In the second sample the hot water tap shouldn't be opened, but the cold water tap should be opened at full capacity in order to fill the bath in the quickest way possible. | 1,900 | true | false | false | false | false | false | true | true | false | false | 9,377 |
940E | Problem - 940E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures dp greedy math *2000 No tag edit access → Contest materials ") ") . The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— elements of _a_. Output Output a single integer xa0— the smallest possible sum of values of these subarrays of some partition of _a_. Examples Input 3 5 1 2 3 Output 6 Input 12 10 1 1 10 10 10 10 10 10 9 10 10 10 Output 92 Input 7 2 2 3 6 4 5 7 1 Output 17 Input 8 4 1 3 4 5 5 3 4 1 Output 23 Note In the first example any partition yields 6 as the sum. In the second example one of the optimal partitions is | 2,000 | true | true | false | true | true | false | false | false | false | false | 5,934 |
814C | Nadeko's birthday is approaching! As she decorated the room for the party, a long garland of Dianthus-shaped paper pieces was placed on a prominent part of the wall. Brother Koyomi will like it! Still unsatisfied with the garland, Nadeko decided to polish it again. The garland has _n_ pieces numbered from 1 to _n_ from left to right, and the _i_-th piece has a colour _s__i_, denoted by a lowercase English letter. Nadeko will repaint at most _m_ of the pieces to give each of them an arbitrary new colour (still denoted by a lowercase English letter). After this work, she finds out all subsegments of the garland containing pieces of only colour _c_ — Brother Koyomi's favourite one, and takes the length of the longest among them to be the Koyomity of the garland. For instance, let's say the garland is represented by "kooomo", and Brother Koyomi's favourite colour is "o". Among all subsegments containing pieces of "o" only, "ooo" is the longest, with a length of 3. Thus the Koyomity of this garland equals 3. But problem arises as Nadeko is unsure about Brother Koyomi's favourite colour, and has swaying ideas on the amount of work to do. She has _q_ plans on this, each of which can be expressed as a pair of an integer _m__i_ and a lowercase letter _c__i_, meanings of which are explained above. You are to find out the maximum Koyomity achievable after repainting the garland according to each plan. Input The first line of input contains a positive integer _n_ (1u2009≤u2009_n_u2009≤u20091u2009500) — the length of the garland. The second line contains _n_ lowercase English letters _s_1_s_2... _s__n_ as a string — the initial colours of paper pieces on the garland. The third line contains a positive integer _q_ (1u2009≤u2009_q_u2009≤u2009200u2009000) — the number of plans Nadeko has. The next _q_ lines describe one plan each: the _i_-th among them contains an integer _m__i_ (1u2009≤u2009_m__i_u2009≤u2009_n_) — the maximum amount of pieces to repaint, followed by a space, then by a lowercase English letter _c__i_ — Koyomi's possible favourite colour. | 1,600 | false | false | false | true | false | false | true | false | false | false | 6,503 |
1648E | Berland is a large country with developed airlines. In total, there are $$$n$$$ cities in the country that are historically served by the Berlaflot airline. The airline operates bi-directional flights between $$$m$$$ pairs of cities, $$$i$$$-th of them connects cities with numbers $$$a_i$$$ and $$$b_i$$$ and has a price $$$c_i$$$ for a flight in both directions. It is known that Berlaflot flights can be used to get from any city to any other (possibly with transfers), and the cost of any route that consists of several consequent flights is equal to the cost of the most expensive of them. More formally, the cost of the route from a city $$$t_1$$$ to a city $$$t_k$$$ with $$$(k-2)$$$ transfers using cities $$$t_2, t_3, t_4, ldots, t_{k - 1}$$$ is equal to the maximum cost of flights from $$$t_1$$$ to $$$t_2$$$, from $$$t_2$$$ to $$$t_3$$$, from $$$t_3$$$ to $$$t_4$$$ and so on until the flight from $$$t_{k - 1}$$$ to $$$t_k$$$. Of course, all these flights must be operated by Berlaflot. A new airline, S8 Airlines, has recently started operating in Berland. This airline provides bi-directional flights between all pairs of cities that are not connected by Berlaflot direct flights. Thus, between each pair of cities there is a flight of either Berlaflot or S8 Airlines. The cost of S8 Airlines flights is calculated as follows: for each pair of cities $$$x$$$ and $$$y$$$ that is connected by a S8 Airlines flight, the cost of this flight is equal to the minimum cost of the route between the cities $$$x$$$ and $$$y$$$ at Berlaflot according to the pricing described earlier. It is known that with the help of S8 Airlines flights you can get from any city to any other with possible transfers, and, similarly to Berlaflot, the cost of a route between any two cities that consists of several S8 Airlines flights is equal to the cost of the most expensive flight. Due to the increased competition with S8 Airlines, Berlaflot decided to introduce an air reform and change the costs of its flights. Namely, for the $$$i$$$-th of its flight between the cities $$$a_i$$$ and $$$b_i$$$, Berlaflot wants to make the cost of this flight equal to the minimum cost of the route between the cities $$$a_i$$$ and $$$b_i$$$ at S8 Airlines. Help Berlaflot managers calculate new flight costs. Input Each test consists of multiple test cases. The first line contains one integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0— the amount of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$4 le n le 200,000$$$, $$$n - 1 le m le 200,000$$$, $$$m le frac{(n - 1) (n - 2)}{2}$$$)xa0— the amount of cities in Berland and the amount of Berlaflot flights. The next $$$m$$$ lines contain the description of Berlaflot flights. The $$$i$$$-th line contains three integers $$$a_i$$$, $$$b_i$$$ and $$$c_i$$$ ($$$1 le a_i, b_i le n$$$, $$$1 le c_i le 10^9$$$)xa0— the numbers of cities that are connected with $$$i$$$-th Berlaflot flight and the price of $$$i$$$-th Berlaflot flight. It is guaranteed that no flight connects a city with itself, no two flights connect the same pair of cities. It is guaranteed that by using Berlaflot flights it is possible to get from any city to any other and by using S8 Airlines flights it is possible to get from any city to any other. Let $$$N$$$ be the sum of $$$n$$$ over all test cases and $$$M$$$ be the sum of $$$m$$$ over all test cases. It is guaranteed that $$$N, M le 200,000$$$. Output For each test case you should print $$$m$$$ integers in a single line, $$$i$$$-th of them should be the price of $$$i$$$-th Berlaflot flight after the air reform. Example Input 3 4 3 1 2 1 2 3 2 4 3 3 5 5 1 2 1 1 3 1 2 4 1 4 5 2 5 1 3 6 6 1 2 3 2 3 1 3 6 5 3 4 2 4 5 4 2 4 2 Output 3 3 3 1 1 1 2 2 4 4 5 3 4 4 Note In the first test case S8 Airlines will provide flights between these pairs of cities: $$$(1, 3)$$$, $$$(1, 4)$$$ and $$$(2, 4)$$$. The cost of a flight between cities $$$1$$$ and $$$3$$$ will be equal to $$$2$$$, since the minimum cost of the Berlaflot route is $$$2$$$xa0— the route consists of a flight between cities $$$1$$$ and $$$2$$$ costing $$$1$$$ and a flight between cities $$$2$$$ and $$$3$$$ costing $$$2$$$, the maximum cost is $$$2$$$. The cost of a flight between cities $$$1$$$ and $$$4$$$ will be $$$3$$$, since the minimum cost of the Berlaflot route is $$$3$$$xa0— the route consists of a flight between cities $$$1$$$ and $$$2$$$ costing $$$1$$$, a flight between cities $$$2$$$ and $$$3$$$ costing $$$2$$$ and a flight between cities $$$3$$$ and $$$4$$$ costing $$$3$$$, the maximum cost is $$$3$$$. The cost of a flight between cities $$$2$$$ and $$$4$$$ will be $$$3$$$, since the minimum cost of the Berlaflot route is $$$3$$$xa0— the route consists of a flight between cities $$$2$$$ and $$$3$$$ costing $$$2$$$ and a flight between cities $$$3$$$ and $$$4$$$ costing $$$3$$$, the maximum cost is $$$3$$$. After the air reform, the cost of the Berlaflot flight between cities $$$1$$$ and $$$2$$$ will be $$$3$$$, since the minimum cost of the S8 Airlines route between these cities is $$$3$$$xa0— the route consists of a flight between cities $$$1$$$ and $$$4$$$ costing $$$3$$$ and a flight between cities $$$2$$$ and $$$4$$$ costing $$$3$$$, the maximum cost is $$$3$$$. The cost of the Berlaflot flight between cities $$$2$$$ and $$$3$$$ will be $$$3$$$, since the minimum cost of the S8 Airlines route between these cities is $$$3$$$xa0— the route consists of a flight between cities $$$2$$$ and $$$4$$$ costing $$$3$$$, a flight between cities $$$1$$$ and $$$4$$$ costing $$$3$$$ and a flight between $$$1$$$ and $$$3$$$ costing $$$2$$$, the maximum cost is $$$3$$$. The cost of the Berlaflot flight between cities $$$3$$$ and $$$4$$$ will be $$$3$$$, since the minimum cost of the S8 Airlines route between these cities is $$$3$$$xa0— the route consists of a flight between cities $$$1$$$ and $$$3$$$ costing $$$2$$$ and a flight between cities $$$1$$$ and $$$4$$$ costing $$$3$$$, the maximum cost is $$$3$$$. In the second test case S8 Airlines will have the following flights: between cities $$$1$$$ and $$$4$$$ costing $$$1$$$, between cities $$$2$$$ and $$$3$$$ costing $$$1$$$, between cities $$$2$$$ and $$$5$$$ costing $$$2$$$, between cities $$$3$$$ and $$$4$$$ costing $$$1$$$ and between cities $$$3$$$ and $$$5$$$ costing $$$2$$$. | 3,200 | false | false | true | false | true | false | false | false | false | true | 2,379 |
1197F | Alice and Bob want to play a game. They have $$$n$$$ colored paper strips; the $$$i$$$-th strip is divided into $$$a_i$$$ cells numbered from $$$1$$$ to $$$a_i$$$. Each cell can have one of $$$3$$$ colors. In the beginning of the game, Alice and Bob put $$$n$$$ chips, the $$$i$$$-th chip is put in the $$$a_i$$$-th cell of the $$$i$$$-th strip. Then they take turns, Alice is first. Each player during their turn has to choose one chip and move it $$$1$$$, $$$2$$$ or $$$3$$$ cells backwards (i.u2009e. if the current cell is $$$x$$$, then the chip can be moved to the cell $$$x - 1$$$, $$$x - 2$$$ or $$$x - 3$$$). There are two restrictions: the chip cannot leave the borders of the strip (for example, if the current cell is $$$3$$$, then you can't move the chip $$$3$$$ cells backwards); and some moves may be prohibited because of color of the current cell (a matrix $$$f$$$ with size $$$3 imes 3$$$ is given, where $$$f_{i, j} = 1$$$ if it is possible to move the chip $$$j$$$ cells backwards from the cell which has color $$$i$$$, or $$$f_{i, j} = 0$$$ if such move is prohibited). The player who cannot make a move loses the game. Initially some cells may be uncolored. Bob can color all uncolored cells as he wants (but he cannot leave any cell uncolored). Let's call a coloring good if Bob can win the game no matter how Alice acts, if the cells are colored according to this coloring. Two colorings are different if at least one cell is colored in different colors in these two colorings. Bob wants you to calculate the number of good colorings. Can you do it for him? Since the answer can be really large, you have to print it modulo $$$998244353$$$. Input The first line contains one integer $$$n$$$ — the number of paper strips ($$$1 le n le 1000$$$). The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the number of cells in the $$$i$$$-th strip. The third line contains one integer $$$m$$$ ($$$1 le m le 1000$$$) — the number of cells that are already colored. Then $$$m$$$ lines follow, each describing an already colored cell. The $$$i$$$-th of these lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$c_i$$$ ($$$1 le x_i le n$$$, $$$1 le y_i le a_{x_i}$$$, $$$1 le c_i le 3$$$) denoting that the cell $$$y_i$$$ in the strip $$$x_i$$$ has color $$$c_i$$$. It is guaranteed that if $$$i e j$$$, then either $$$x_i e x_j$$$ or $$$y_i e y_j$$$ (or both). Then $$$3$$$ lines follow, $$$i$$$-th line containing $$$3$$$ numbers $$$f_{i, 1}$$$, $$$f_{i, 2}$$$, $$$f_{i, 3}$$$ ($$$0 le f_{i, j} le 1$$$). If $$$f_{i, j} = 1$$$, then it is possible to move the chip $$$j$$$ cells backwards from the cell having color $$$i$$$; if $$$f_{i, j} = 0$$$, then such move is impossible. Output Print one integer: the number of good colorings, taken modulo $$$998244353$$$. Examples Input 3 3 4 5 2 1 1 1 2 2 2 1 1 1 1 0 0 0 1 1 Input 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Input 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 | 2,700 | false | false | false | true | false | false | false | false | false | false | 4,710 |
1004E | Sonya likes ice cream very much. She eats it even during programming competitions. That is why the girl decided that she wants to open her own ice cream shops. Sonya lives in a city with $$$n$$$ junctions and $$$n-1$$$ streets between them. All streets are two-way and connect two junctions. It is possible to travel from any junction to any other using one or more streets. City Hall allows opening shops only on junctions. The girl cannot open shops in the middle of streets. Sonya has exactly $$$k$$$ friends whom she can trust. If she opens a shop, one of her friends has to work there and not to allow anybody to eat an ice cream not paying for it. Since Sonya does not want to skip an important competition, she will not work in shops personally. Sonya wants all her ice cream shops to form a simple path of the length $$$r$$$ ($$$1 le r le k$$$), i.e. to be located in different junctions $$$f_1, f_2, dots, f_r$$$ and there is street between $$$f_i$$$ and $$$f_{i+1}$$$ for each $$$i$$$ from $$$1$$$ to $$$r-1$$$. The girl takes care of potential buyers, so she also wants to minimize the maximum distance between the junctions to the nearest ice cream shop. The distance between two junctions $$$a$$$ and $$$b$$$ is equal to the sum of all the street lengths that you need to pass to get from the junction $$$a$$$ to the junction $$$b$$$. So Sonya wants to minimize $$$$$$max_{a} min_{1 le i le r} d_{a,f_i}$$$$$$ where $$$a$$$ takes a value of all possible $$$n$$$ junctions, $$$f_i$$$xa0— the junction where the $$$i$$$-th Sonya's shop is located, and $$$d_{x,y}$$$xa0— the distance between the junctions $$$x$$$ and $$$y$$$. Sonya is not sure that she can find the optimal shops locations, that is why she is asking you to help her to open not more than $$$k$$$ shops that will form a simple path and the maximum distance between any junction and the nearest shop would be minimal. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1leq kleq nleq 10^5$$$)xa0— the number of junctions and friends respectively. Each of the next $$$n-1$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$, and $$$d_i$$$ ($$$1leq u_i, v_ileq n$$$, $$$v_i eq u_i$$$, $$$1leq dleq 10^4$$$)xa0— junctions that are connected by a street and the length of this street. It is guaranteed that each pair of junctions is connected by at most one street. It is guaranteed that you can get from any junctions to any other. Note In the first example, you can choose the path 2-4, so the answer will be 4. The first example. In the second example, you can choose the path 4-1-2, so the answer will be 7. The second example. | 2,400 | false | true | false | true | true | false | false | true | false | false | 5,664 |
112B | Problem - 112B - 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 *1200 No tag edit access → Contest materials , representing the length of a square's side and the coordinates of the marked cell. It is guaranteed that 2_n_ is even. The coordinates of the marked cell are represented by a pair of numbers _x_ _y_, where _x_ represents the number of the row and _y_ represents the number of the column. The rows and columns are numbered by consecutive integers from 1 to 2_n_. The rows are numbered from top to bottom and the columns are numbered from the left to the right. Output If the square is possible to cut, print "YES", otherwise print "NO" (without the quotes). Examples Input 4 1 1 Output YES Input 2 2 2 Output NO Note A sample test from the statement and one of the possible ways of cutting the square are shown in the picture: | 1,200 | true | false | true | false | false | false | false | false | false | false | 9,436 |
111B | Problem - 111B - 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 number theory *1900 No tag edit access → Contest materials . Each of the following _n_ lines contain two space-separated integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_u2009≤u2009105, 0u2009≤u2009_y__i_u2009≤u2009_i_u2009-u20091, where _i_ is the query's ordinal number; the numeration starts with 1). If _y__i_u2009=u20090 for the query, then the answer to the query will be the number of divisors of the number _x__i_. In this case you do not need to take the previous numbers _x_ into consideration. Output For each query print the answer on a single line: the number of positive integers _k_ such that Examples Input 6 4 0 3 1 5 2 6 2 18 4 10000 3 Output 3 1 1 2 2 22 Note Let's write out the divisors that give answers for the first 5 queries: 1) 1, 2, 4 2) 3 3) 5 4) 2, 6 5) 9, 18 | 1,900 | false | false | false | false | true | false | false | true | false | false | 9,441 |
862A | Dr. Evil kidnapped Mahmoud and Ehab in the evil land because of their performance in the Evil Olympiad in Informatics (EOI). He decided to give them some problems to let them go. Dr. Evil is interested in sets, He has a set of _n_ integers. Dr. Evil calls a set of integers evil if the MEX of it is exactly _x_. the MEX of a set of integers is the minimum non-negative integer that doesn't exist in it. For example, the MEX of the set {0,u20092,u20094} is 1 and the MEX of the set {1,u20092,u20093} is 0 . Dr. Evil is going to make his set evil. To do this he can perform some operations. During each operation he can add some non-negative integer to his set or erase some element from it. What is the minimal number of operations Dr. Evil has to perform to make his set evil? Input The first line contains two integers _n_ and _x_ (1u2009≤u2009_n_u2009≤u2009100, 0u2009≤u2009_x_u2009≤u2009100)xa0— the size of the set Dr. Evil owns, and the desired MEX. The second line contains _n_ distinct non-negative integers not exceeding 100 that represent the set. Output The only line should contain one integerxa0— the minimal number of operations Dr. Evil should perform. Note For the first test case Dr. Evil should add 1 and 2 to the set performing 2 operations. For the second test case Dr. Evil should erase 0 from the set. After that, the set becomes empty, so the MEX of it is 0. In the third test case the set is already evil. | 1,000 | false | true | true | false | false | false | false | false | false | false | 6,300 |
1974B | Polycarp has a string $$$s$$$, which consists of lowercase Latin letters. He encodes this string using the following algorithm: first, he constructs a new auxiliary string $$$r$$$, which consists of all distinct letters of the string $$$s$$$, written in alphabetical order; then the encoding happens as follows: each character in the string $$$s$$$ is replaced by its symmetric character from the string $$$r$$$ (the first character of the string $$$r$$$ will be replaced by the last, the second by the second from the end, and so on). For example, encoding the string $$$s$$$="codeforces" happens as follows: the string $$$r$$$ is obtained as "cdefors"; the first character $$$s_1$$$='c' is replaced by 's'; the second character $$$s_2$$$='o' is replaced by 'e'; the third character $$$s_3$$$='d' is replaced by 'r'; ... the last character $$$s_{10}$$$='s' is replaced by 'c'. The string $$$r$$$ and replacements for $$$s$$$="codeforces". Thus, the result of encoding the string $$$s$$$="codeforces" is the string "serofedsoc". Write a program that performs decoding — that is, restores the original string $$$s$$$ from the encoding result. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the string $$$b$$$. The second line of each test case contains a string $$$b$$$ of length $$$n$$$, consisting of lowercase Latin letters — the result of encoding the original string $$$s$$$. It is guaranteed that the sum of the values of $$$n$$$ over all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the string $$$s$$$ from which the encoding result $$$b$$$ was obtained. Example Input 5 10 serofedsoc 3 ttf 9 tlrhgmaoi 1 w 15 hnndledmnhlttin Output codeforces fft algorithm w meetinthemiddle | 800 | false | false | true | false | false | false | false | false | true | false | 448 |
1759G | A sequence of $$$n$$$ numbers is called permutation if it contains all numbers from $$$1$$$ to $$$n$$$ exactly once. For example, the sequences [$$$3, 1, 4, 2$$$], [$$$1$$$] and [$$$2,1$$$] are permutations, but [$$$1,2,1$$$], [$$$0,1$$$] and [$$$1,3,4$$$]xa0— are not. For a permutation $$$p$$$ of even length $$$n$$$ you can make an array $$$b$$$ of length $$$frac{n}{2}$$$ such that: $$$b_i = max(p_{2i - 1}, p_{2i})$$$ for $$$1 le i le frac{n}{2}$$$ For example, if $$$p$$$ = [$$$2, 4, 3, 1, 5, 6$$$], then: $$$b_1 = max(p_1, p_2) = max(2, 4) = 4$$$ $$$b_2 = max(p_3, p_4) = max(3,1)=3$$$ $$$b_3 = max(p_5, p_6) = max(5,6) = 6$$$ As a result, we made $$$b$$$ = $$$[4, 3, 6]$$$. For a given array $$$b$$$, find the lexicographically minimal permutation $$$p$$$ such that you can make the given array $$$b$$$ from it. If $$$b$$$ = [$$$4,3,6$$$], then the lexicographically minimal permutation from which it can be made is $$$p$$$ = [$$$1,4,2,3,5,6$$$], since: $$$b_1 = max(p_1, p_2) = max(1, 4) = 4$$$ $$$b_2 = max(p_3, p_4) = max(2, 3) = 3$$$ $$$b_3 = max(p_5, p_6) = max(5, 6) = 6$$$ A permutation $$$x_1, x_2, dots, x_n$$$ is lexicographically smaller than a permutation $$$y_1, y_2 dots, y_n$$$ if and only if there exists such $$$i$$$ ($$$1 le i le n$$$) that $$$x_1=y_1, x_2=y_2, dots, x_{i-1}=y_{i-1}$$$ and $$$x_i<y_i$$$. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains one even integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$). The second line of each test case contains exactly $$$frac{n}{2}$$$ integers $$$b_i$$$ ($$$1 le b_i le n$$$) — elements of array $$$b$$$. It is guaranteed that the sum of $$$n$$$ values over all test cases does not exceed $$$2 cdot 10^5$$$. | 1,900 | true | true | false | false | true | true | false | true | false | false | 1,756 |
2009G3 | This is the extreme version of the problem. In this version, the output of each query is different from the easy and hard versions. It is also guaranteed that $$$r geq l+k-1$$$ for all queries. For an arbitrary array $$$b$$$, Yunli can perform the following operation any number of times: Select an index $$$i$$$. Set $$$b_i = x$$$ where $$$x$$$ is any integer she desires ($$$x$$$ is not limited to the interval $$$[1,n]$$$). Denote $$$f(b)$$$ as the minimum number of operations she needs to perform until there exists a consecutive subarray$$$^{ ext{∗}}$$$ of length at least $$$k$$$ in $$$b$$$. Yunli is given an array $$$a$$$ of size $$$n$$$ and asks you $$$q$$$ queries. In each query, you must output $$$sum_{i=l}^{r-k+1} sum_{j=i+k-1}^{r} f([a_i, a_{i+1}, ldots, a_j])$$$. $$$^{ ext{∗}}$$$If there exists a consecutive subarray of length $$$k$$$ that starts at index $$$i$$$ ($$$1 leq i leq b-k+1$$$), then $$$b_j = b_{j-1} + 1$$$ for all $$$i < j leq i+k-1$$$. Input The first line contains $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The first line of each test case contains three integers $$$n$$$, $$$k$$$, and $$$q$$$ ($$$1 leq k leq n leq 2 cdot 10^5$$$, $$$1 leq q leq 2 cdot 10^5$$$) — the length of the array, the length of the consecutive subarray, and the number of queries. The following line contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ ($$$1 leq a_i leq n$$$). The following $$$q$$$ lines contain two integers $$$l$$$ and $$$r$$$ ($$$1 leq l leq r leq n$$$, $$$r geq l+k-1$$$) — the bounds of the query. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ and the sum of $$$q$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output Output $$$sum_{i=l}^{r-k+1} sum_{j=i+k-1}^{r} f([a_i, a_{i+1}, ldots, a_j])$$$ for each query on a new line. Example Input 5 7 2 4 1 2 3 2 1 2 3 4 6 1 7 2 7 3 7 8 4 2 4 3 1 1 2 4 3 2 3 6 1 5 5 4 2 4 5 1 2 3 1 4 1 5 10 4 8 2 3 6 5 8 9 8 10 10 1 2 7 6 10 1 9 1 6 3 9 4 10 2 10 1 8 10 7 4 3 4 5 3 4 5 9 10 8 9 1 9 2 10 1 10 2 9 Output 1 3 3 3 2 7 2 4 8 6 28 7 16 20 32 19 18 15 26 9 Note In the first query of the first testcase, we can calculate the answer for the query through the following: $$$i = 4$$$ and $$$j = 5$$$: $$$f([2, 1])=1$$$ because Yunli can set $$$b_2=3$$$, making a consecutive subarray of size $$$2$$$ in $$$1$$$ move. $$$i = 4$$$ and $$$j = 6$$$: $$$f([2, 1, 2])=0$$$ because there is already a consecutive array of size $$$2$$$. $$$i = 5$$$ and $$$j = 6$$$: $$$f([1, 2])=0$$$ because there is already a consecutive array of size $$$2$$$. The answer to this query is $$$1+0+0=1$$$. | 2,700 | false | false | true | true | true | false | false | false | false | false | 192 |
1095D | There are $$$n$$$ kids, numbered from $$$1$$$ to $$$n$$$, dancing in a circle around the Christmas tree. Let's enumerate them in a clockwise direction as $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ (all these numbers are from $$$1$$$ to $$$n$$$ and are distinct, so $$$p$$$ is a permutation). Let the next kid for a kid $$$p_i$$$ be kid $$$p_{i + 1}$$$ if $$$i < n$$$ and $$$p_1$$$ otherwise. After the dance, each kid remembered two kids: the next kid (let's call him $$$x$$$) and the next kid for $$$x$$$. Each kid told you which kids he/she remembered: the kid $$$i$$$ remembered kids $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$. However, the order of $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$ can differ from their order in the circle. Example: 5 kids in a circle, $$$p=[3, 2, 4, 1, 5]$$$ (or any cyclic shift). The information kids remembered is: $$$a_{1,1}=3$$$, $$$a_{1,2}=5$$$; $$$a_{2,1}=1$$$, $$$a_{2,2}=4$$$; $$$a_{3,1}=2$$$, $$$a_{3,2}=4$$$; $$$a_{4,1}=1$$$, $$$a_{4,2}=5$$$; $$$a_{5,1}=2$$$, $$$a_{5,2}=3$$$. You have to restore the order of the kids in the circle using this information. If there are several answers, you may print any. It is guaranteed that at least one solution exists. If you are Python programmer, consider using PyPy instead of Python when you submit your code. Input The first line of the input contains one integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the number of the kids. The next $$$n$$$ lines contain $$$2$$$ integers each. The $$$i$$$-th line contains two integers $$$a_{i, 1}$$$ and $$$a_{i, 2}$$$ ($$$1 le a_{i, 1}, a_{i, 2} le n, a_{i, 1} e a_{i, 2}$$$) — the kids the $$$i$$$-th kid remembered, given in arbitrary order. Output Print $$$n$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$ — permutation of integers from $$$1$$$ to $$$n$$$, which corresponds to the order of kids in the circle. If there are several answers, you may print any (for example, it doesn't matter which kid is the first in the circle). It is guaranteed that at least one solution exists. Examples Input 5 3 5 1 4 2 4 1 5 2 3 | 1,600 | false | false | true | false | false | false | false | false | false | false | 5,234 |
1416B | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers, numbered from $$$1$$$ to $$$n$$$. You can perform the following operation no more than $$$3n$$$ times: 1. choose three integers $$$i$$$, $$$j$$$ and $$$x$$$ ($$$1 le i, j le n$$$; $$$0 le x le 10^9$$$); 2. assign $$$a_i := a_i - x cdot i$$$, $$$a_j := a_j + x cdot i$$$. After each operation, all elements of the array should be non-negative. Can you find a sequence of no more than $$$3n$$$ operations after which all elements of the array are equal? Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^4$$$) — the number of elements in the array. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^5$$$) — the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Output For each test case print the answer to it as follows: if there is no suitable sequence of operations, print $$$-1$$$; otherwise, print one integer $$$k$$$ ($$$0 le k le 3n$$$) — the number of operations in the sequence. Then print $$$k$$$ lines, the $$$m$$$-th of which should contain three integers $$$i$$$, $$$j$$$ and $$$x$$$ ($$$1 le i, j le n$$$; $$$0 le x le 10^9$$$) for the $$$m$$$-th operation. If there are multiple suitable sequences of operations, print any of them. Note that you don't have to minimize $$$k$$$. Example Input 3 4 2 16 4 18 6 1 2 3 4 5 6 5 11 19 1 1 3 Output 2 4 1 2 2 3 3 -1 4 1 2 4 2 4 5 2 3 3 4 5 1 | 2,000 | true | true | false | false | false | true | false | false | false | false | 3,601 |
64A | The package for this problem was not updated by the problem writer or Codeforces administration after we've upgraded the judging servers. To adjust the time limit constraint, a solution execution time will be multiplied by 2. For example, if your solution works for 400 ms on judging servers, then the value 800 ms will be displayed and used to determine the verdict. The following languages are only available languages for the problems from the contest Unknown Language Round 1: ActiveTcl 8.5 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. *special problem implementation *1300 No tag edit access | 1,300 | false | false | true | false | false | false | false | false | false | false | 9,655 |
1477E | In the famous Oh-Suit-United tournament, two teams are playing against each other for the grand prize of precious pepper points. The first team consists of $$$n$$$ players, and the second team consists of $$$m$$$ players. Each player has a potential: the potential of the $$$i$$$-th player in the first team is $$$a_i$$$, and the potential of the $$$i$$$-th player in the second team is $$$b_i$$$. In the tournament, all players will be on the stage in some order. There will be a scoring device, initially assigned to an integer $$$k$$$, which will be used to value the performance of all players. The scores for all players will be assigned in the order they appear on the stage. Let the potential of the current player be $$$x$$$, and the potential of the previous player be $$$y$$$ ($$$y$$$ equals $$$x$$$ for the first player). Then, $$$x-y$$$ is added to the value in the scoring device, Afterwards, if the value in the scoring device becomes negative, the value will be reset to $$$0$$$. Lastly, the player's score is assigned to the current value on the scoring device. The score of a team is the sum of the scores of all its members. As an insane fan of the first team, Nezzar desperately wants the biggest win for the first team. He now wonders what is the maximum difference between scores of the first team and the second team. Formally, let the score of the first team be $$$score_f$$$ and the score of the second team be $$$score_s$$$. Nezzar wants to find the maximum value of $$$score_f - score_s$$$ over all possible orders of players on the stage. However, situation often changes and there are $$$q$$$ events that will happen. There are three types of events: $$$1$$$ $$$pos$$$ $$$x$$$ — change $$$a_{pos}$$$ to $$$x$$$; $$$2$$$ $$$pos$$$ $$$x$$$ — change $$$b_{pos}$$$ to $$$x$$$; $$$3$$$ $$$x$$$ — tournament is held with $$$k = x$$$ and Nezzar wants you to compute the maximum value of $$$score_f - score_s$$$. Can you help Nezzar to answer the queries of the third type? Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1 le n,m le 2 cdot 10^5, 1 le q le 5 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^6$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$0 le b_i le 10^6$$$). The following $$$q$$$ lines contain descriptions of events, described in the statement, each in one of the three possible formats: $$$1$$$ $$$pos$$$ $$$x$$$ ($$$1 le pos le n$$$, $$$0 le x le 10^6$$$); $$$2$$$ $$$pos$$$ $$$x$$$ ($$$1 le pos le m$$$, $$$0 le x le 10^6$$$); $$$3$$$ $$$x$$$ ($$$0 le x le 10^6$$$). Output For each query of the third type print the answer to this query. Examples Input 3 4 3 1 2 7 3 4 5 6 3 5 1 1 10 3 5 Input 7 8 12 958125 14018 215153 35195 90380 30535 204125 591020 930598 252577 333333 999942 1236 9456 82390 3 123458 2 4 444444 3 123456 1 2 355555 3 123478 3 1111 2 6 340324 3 1111 2 8 999999 2 7 595959 3 222222 3 100 Output 1361307 1361311 1702804 1879305 1821765 1078115 1675180 Note In the first query of the first test, the tournament is held with $$$k = 5$$$. It would be optimal to arrange players in such way (here their potentials are written): $$$underline{7}$$$, $$$3$$$, $$$5$$$, $$$4$$$, $$$6$$$, $$$underline{1}$$$, $$$underline{2}$$$ (underlined numbers are potentials of players that are from the first team). The individual scores of players, numbered in the order of their appearance, are: $$$max(5 + (7 - 7), 0) = 5$$$ for the $$$underline{1}$$$-st player; $$$max(5 + (3 - 7), 0) = 1$$$ for the $$$2$$$-nd player; $$$max(1 + (5 - 3), 0) = 3$$$ for the $$$3$$$-rd player; $$$max(3 + (4 - 5), 0) = 2$$$ for the $$$4$$$-th player; $$$max(2 + (6 - 4), 0) = 4$$$ for the $$$5$$$-th player; $$$max(4 + (1 - 6), 0) = 0$$$ for the $$$underline{6}$$$-th player; $$$max(0 + (2 - 1), 0) = 1$$$ for the $$$underline{7}$$$-th player. So, $$$score_f = 5 + 0 + 1 = 6$$$ and $$$score_s = 1 + 3 + 2 + 4 = 10$$$. The score difference is $$$6 - 10 = -4$$$. It can be proven, that it is the maximum possible score difference. | 3,300 | false | true | false | false | true | false | false | false | false | false | 3,286 |
1715C | Stanley has decided to buy a new desktop PC made by the company "Monoblock", and to solve captcha on their website, he needs to solve the following task. The awesomeness of an array is the minimum number of blocks of consecutive identical numbers in which the array could be split. For example, the awesomeness of an array $$$[1, 1, 1]$$$ is $$$1$$$; $$$[5, 7]$$$ is $$$2$$$, as it could be split into blocks $$$[5]$$$ and $$$[7]$$$; $$$[1, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9]$$$ is 3, as it could be split into blocks $$$[1]$$$, $$$[7, 7, 7, 7, 7, 7, 7]$$$, and $$$[9, 9, 9, 9, 9, 9, 9, 9, 9]$$$. You are given an array $$$a$$$ of length $$$n$$$. There are $$$m$$$ queries of two integers $$$i$$$, $$$x$$$. A query $$$i$$$, $$$x$$$ means that from now on the $$$i$$$-th element of the array $$$a$$$ is equal to $$$x$$$. After each query print the sum of awesomeness values among all subsegments of array $$$a$$$. In other words, after each query you need to calculate $$$$$$sumlimits_{l = 1}^n sumlimits_{r = l}^n g(l, r),$$$$$$ where $$$g(l, r)$$$ is the awesomeness of the array $$$b = [a_l, a_{l + 1}, ldots, a_r]$$$. Input In the first line you are given with two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the array $$$a$$$. In the next $$$m$$$ lines you are given the descriptions of queries. Each line contains two integers $$$i$$$ and $$$x$$$ ($$$1 leq i leq n$$$, $$$1 leq x leq 10^9$$$). Output Print the answer to each query on a new line. Example Input 5 5 1 2 3 4 5 3 2 4 2 3 1 2 1 2 2 Note After the first query $$$a$$$ is equal to $$$[1, 2, 2, 4, 5]$$$, and the answer is $$$29$$$ because we can split each of the subsegments the following way: 1. $$$[1; 1]$$$: $$$[1]$$$, 1 block; 2. $$$[1; 2]$$$: $$$[1] + [2]$$$, 2 blocks; 3. $$$[1; 3]$$$: $$$[1] + [2, 2]$$$, 2 blocks; 4. $$$[1; 4]$$$: $$$[1] + [2, 2] + [4]$$$, 3 blocks; 5. $$$[1; 5]$$$: $$$[1] + [2, 2] + [4] + [5]$$$, 4 blocks; 6. $$$[2; 2]$$$: $$$[2]$$$, 1 block; 7. $$$[2; 3]$$$: $$$[2, 2]$$$, 1 block; 8. $$$[2; 4]$$$: $$$[2, 2] + [4]$$$, 2 blocks; 9. $$$[2; 5]$$$: $$$[2, 2] + [4] + [5]$$$, 3 blocks; 10. $$$[3; 3]$$$: $$$[2]$$$, 1 block; 11. $$$[3; 4]$$$: $$$[2] + [4]$$$, 2 blocks; 12. $$$[3; 5]$$$: $$$[2] + [4] + [5]$$$, 3 blocks; 13. $$$[4; 4]$$$: $$$[4]$$$, 1 block; 14. $$$[4; 5]$$$: $$$[4] + [5]$$$, 2 blocks; 15. $$$[5; 5]$$$: $$$[5]$$$, 1 block; which is $$$1 + 2 + 2 + 3 + 4 + 1 + 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 1 = 29$$$ in total. | 1,700 | true | false | true | false | true | false | false | false | false | false | 1,991 |
467D | After you had helped Fedor to find friends in the «Call of Soldiers 3» game, he stopped studying completely. Today, the English teacher told him to prepare an essay. Fedor didn't want to prepare the essay, so he asked Alex for help. Alex came to help and wrote the essay for Fedor. But Fedor didn't like the essay at all. Now Fedor is going to change the essay using the synonym dictionary of the English language. Fedor does not want to change the meaning of the essay. So the only change he would do: change a word from essay to one of its synonyms, basing on a replacement rule from the dictionary. Fedor may perform this operation any number of times. As a result, Fedor wants to get an essay which contains as little letters «R» (the case doesn't matter) as possible. If there are multiple essays with minimum number of «R»s he wants to get the one with minimum length (length of essay is the sum of the lengths of all the words in it). Help Fedor get the required essay. Please note that in this problem the case of letters doesn't matter. For example, if the synonym dictionary says that word cat can be replaced with word DOG, then it is allowed to replace the word Cat with the word doG. Input The first line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of words in the initial essay. The second line contains words of the essay. The words are separated by a single space. It is guaranteed that the total length of the words won't exceed 105 characters. The next line contains a single integer _n_ (0u2009≤u2009_n_u2009≤u2009105) — the number of pairs of words in synonym dictionary. The _i_-th of the next _n_ lines contains two space-separated non-empty words _x__i_ and _y__i_. They mean that word _x__i_ can be replaced with word _y__i_ (but not vise versa). It is guaranteed that the total length of all pairs of synonyms doesn't exceed 5·105 characters. All the words at input can only consist of uppercase and lowercase letters of the English alphabet. Output Print two integers — the minimum number of letters «R» in an optimal essay and the minimum length of an optimal essay. Examples Input 3 AbRb r Zz 4 xR abRb aA xr zz Z xr y Input 2 RuruRu fedya 1 ruruRU fedor | 2,400 | false | false | false | true | false | false | false | false | false | true | 7,969 |
1733D1 | This is the easy version of the problem. In this version, $$$n le 3000$$$, $$$x ge y$$$ holds. You can make hacks only if both versions of the problem are solved. You are given two binary strings $$$a$$$ and $$$b$$$, both of length $$$n$$$. You can do the following operation any number of times (possibly zero). Select two indices $$$l$$$ and $$$r$$$ ($$$l < r$$$). Change $$$a_l$$$ to $$$(1 - a_l)$$$, and $$$a_r$$$ to $$$(1 - a_r)$$$. If $$$l + 1 = r$$$, the cost of the operation is $$$x$$$. Otherwise, the cost is $$$y$$$. You have to find the minimum cost needed to make $$$a$$$ equal to $$$b$$$ or say there is no way to do so. Input The first line contains one integer $$$t$$$ ($$$1 le t le 600$$$)xa0— the number of test cases. Each test case consists of three lines. The first line of each test case contains three integers $$$n$$$, $$$x$$$, and $$$y$$$ ($$$5 le n le 3000$$$, $$$1 le y le x le 10^9$$$)xa0— the length of the strings, and the costs per operation. The second line of each test case contains the string $$$a$$$ of length $$$n$$$. The string only consists of digits $$$0$$$ and $$$1$$$. The third line of each test case contains the string $$$b$$$ of length $$$n$$$. The string only consists of digits $$$0$$$ and $$$1$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3000$$$. Output For each test case, if there is no way to make $$$a$$$ equal to $$$b$$$, print $$$-1$$$. Otherwise, print the minimum cost needed to make $$$a$$$ equal to $$$b$$$. Example Input 4 5 8 7 01001 00101 5 7 2 01000 11011 7 8 3 0111001 0100001 5 10 1 01100 01100 Note In the first test case, selecting indices $$$2$$$ and $$$3$$$ costs $$$8$$$, which is the minimum possible cost. In the second test case, we cannot make $$$a$$$ equal to $$$b$$$ using any number of operations. In the third test case, we can perform the following operations: Select indices $$$3$$$ and $$$6$$$. It costs $$$3$$$, and $$$a$$$ is 0101011 now. Select indices $$$4$$$ and $$$6$$$. It costs $$$3$$$, and $$$a$$$ is 0100001 now. The total cost is $$$6$$$. In the fourth test case, we don't have to perform any operations. | 1,400 | true | true | false | false | false | true | false | false | false | false | 1,889 |
1280D | The Oak has $$$n$$$ nesting places, numbered with integers from $$$1$$$ to $$$n$$$. Nesting place $$$i$$$ is home to $$$b_i$$$ bees and $$$w_i$$$ wasps. Some nesting places are connected by branches. We call two nesting places adjacent if there exists a branch between them. A simple path from nesting place $$$x$$$ to $$$y$$$ is given by a sequence $$$s_0, ldots, s_p$$$ of distinct nesting places, where $$$p$$$ is a non-negative integer, $$$s_0 = x$$$, $$$s_p = y$$$, and $$$s_{i-1}$$$ and $$$s_{i}$$$ are adjacent for each $$$i = 1, ldots, p$$$. The branches of The Oak are set up in such a way that for any two pairs of nesting places $$$x$$$ and $$$y$$$, there exists a unique simple path from $$$x$$$ to $$$y$$$. Because of this, biologists and computer scientists agree that The Oak is in fact, a tree. A village is a nonempty set $$$V$$$ of nesting places such that for any two $$$x$$$ and $$$y$$$ in $$$V$$$, there exists a simple path from $$$x$$$ to $$$y$$$ whose intermediate nesting places all lie in $$$V$$$. A set of villages $$$cal P$$$ is called a partition if each of the $$$n$$$ nesting places is contained in exactly one of the villages in $$$cal P$$$. In other words, no two villages in $$$cal P$$$ share any common nesting place, and altogether, they contain all $$$n$$$ nesting places. The Oak holds its annual Miss Punyverse beauty pageant. The two contestants this year are Ugly Wasp and Pretty Bee. The winner of the beauty pageant is determined by voting, which we will now explain. Suppose $$$mathcal{P}$$$ is a partition of the nesting places into $$$m$$$ villages $$$V_1, ldots, V_m$$$. There is a local election in each village. Each of the insects in this village vote for their favorite contestant. If there are strictly more votes for Ugly Wasp than Pretty Bee, then Ugly Wasp is said to win in that village. Otherwise, Pretty Bee wins. Whoever wins in the most number of villages wins. As it always goes with these pageants, bees always vote for the bee (which is Pretty Bee this year) and wasps always vote for the wasp (which is Ugly Wasp this year). Unlike their general elections, no one abstains from voting for Miss Punyverse as everyone takes it very seriously. Mayor Waspacito, and his assistant Alexwasp, wants Ugly Wasp to win. He has the power to choose how to partition The Oak into exactly $$$m$$$ villages. If he chooses the partition optimally, determine the maximum number of villages in which Ugly Wasp wins. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 100$$$) denoting the number of test cases. The next lines contain descriptions of the test cases. The first line of each test case contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$1 le m le n le 3000$$$). The second line contains $$$n$$$ space-separated integers $$$b_1, b_2, ldots, b_n$$$ ($$$0 le b_i le 10^9$$$). The third line contains $$$n$$$ space-separated integers $$$w_1, w_2, ldots, w_n$$$ ($$$0 le w_i le 10^9$$$). The next $$$n - 1$$$ lines describe the pairs of adjacent nesting places. In particular, the $$$i$$$-th of them contains two space-separated integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i eq y_i$$$) denoting the numbers of two adjacent nesting places. It is guaranteed that these pairs form a tree. It is guaranteed that the sum of $$$n$$$ in a single file is at most $$$10^5$$$. Note In the first test case, we need to partition the $$$n = 4$$$ nesting places into $$$m = 3$$$ villages. We can make Ugly Wasp win in $$$2$$$ villages via the following partition: $$${{1, 2}, {3}, {4}}$$$. In this partition, Ugly Wasp wins in village $$${1, 2}$$$, garnering $$$181$$$ votes as opposed to Pretty Bee's $$$170$$$; Ugly Wasp also wins in village $$${3}$$$, garnering $$$111$$$ votes as opposed to Pretty Bee's $$$70$$$; Ugly Wasp loses in the village $$${4}$$$, garnering $$$0$$$ votes as opposed to Pretty Bee's $$$50$$$. Thus, Ugly Wasp wins in $$$2$$$ villages, and it can be shown that this is the maximum possible number. In the second test case, we need to partition the $$$n = 2$$$ nesting places into $$$m = 1$$$ village. There is only one way to do this: $$${{1, 2}}$$$. In this partition's sole village, Ugly Wasp gets $$$563$$$ votes, and Pretty Bee also gets $$$563$$$ votes. Ugly Wasp needs strictly more votes in order to win. Therefore, Ugly Wasp doesn't win in any village. | 2,500 | false | true | false | true | false | false | false | false | false | false | 4,291 |
1399C | There are $$$n$$$ people who want to participate in a boat competition. The weight of the $$$i$$$-th participant is $$$w_i$$$. Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight. So, if there are $$$k$$$ teams $$$(a_1, b_1)$$$, $$$(a_2, b_2)$$$, $$$dots$$$, $$$(a_k, b_k)$$$, where $$$a_i$$$ is the weight of the first participant of the $$$i$$$-th team and $$$b_i$$$ is the weight of the second participant of the $$$i$$$-th team, then the condition $$$a_1 + b_1 = a_2 + b_2 = dots = a_k + b_k = s$$$, where $$$s$$$ is the total weight of each team, should be satisfied. Your task is to choose such $$$s$$$ that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 50$$$) — the number of participants. The second line of the test case contains $$$n$$$ integers $$$w_1, w_2, dots, w_n$$$ ($$$1 le w_i le n$$$), where $$$w_i$$$ is the weight of the $$$i$$$-th participant. Output For each test case, print one integer $$$k$$$: the maximum number of teams people can compose with the total weight $$$s$$$, if you choose $$$s$$$ optimally. Example Input 5 5 1 2 3 4 5 8 6 6 6 6 6 6 8 8 8 1 2 2 1 2 1 1 2 3 1 3 3 6 1 1 3 4 2 2 Note In the first test case of the example, we can reach the optimal answer for $$$s=6$$$. Then the first boat is used by participants $$$1$$$ and $$$5$$$ and the second boat is used by participants $$$2$$$ and $$$4$$$ (indices are the same as weights). In the second test case of the example, we can reach the optimal answer for $$$s=12$$$. Then first $$$6$$$ participants can form $$$3$$$ pairs. In the third test case of the example, we can reach the optimal answer for $$$s=3$$$. The answer is $$$4$$$ because we have $$$4$$$ participants with weight $$$1$$$ and $$$4$$$ participants with weight $$$2$$$. In the fourth test case of the example, we can reach the optimal answer for $$$s=4$$$ or $$$s=6$$$. In the fifth test case of the example, we can reach the optimal answer for $$$s=3$$$. Note that participant with weight $$$3$$$ can't use the boat because there is no suitable pair for him in the list. | 1,200 | false | true | false | false | false | false | true | false | false | false | 3,677 |
1350B | There are $$$n$$$ models in the shop numbered from $$$1$$$ to $$$n$$$, with sizes $$$s_1, s_2, ldots, s_n$$$. Orac will buy some of the models and will arrange them in the order of increasing numbers (i.e. indices, but not sizes). Orac thinks that the obtained arrangement is beatiful, if for any two adjacent models with indices $$$i_j$$$ and $$$i_{j+1}$$$ (note that $$$i_j < i_{j+1}$$$, because Orac arranged them properly), $$$i_{j+1}$$$ is divisible by $$$i_j$$$ and $$$s_{i_j} < s_{i_{j+1}}$$$. For example, for $$$6$$$ models with sizes $$${3, 6, 7, 7, 7, 7}$$$, he can buy models with indices $$$1$$$, $$$2$$$, and $$$6$$$, and the obtained arrangement will be beautiful. Also, note that the arrangement with exactly one model is also considered beautiful. Orac wants to know the maximum number of models that he can buy, and he may ask you these queries many times. Input The first line contains one integer $$$t (1 le tle 100)$$$: the number of queries. Each query contains two lines. The first line contains one integer $$$n (1le nle 100,000)$$$: the number of models in the shop, and the second line contains $$$n$$$ integers $$$s_1,dots,s_n (1le s_ile 10^9)$$$: the sizes of models. It is guaranteed that the total sum of $$$n$$$ is at most $$$100,000$$$. Note In the first query, for example, Orac can buy models with indices $$$2$$$ and $$$4$$$, the arrangement will be beautiful because $$$4$$$ is divisible by $$$2$$$ and $$$6$$$ is more than $$$3$$$. By enumerating, we can easily find that there are no beautiful arrangements with more than two models. In the second query, Orac can buy models with indices $$$1$$$, $$$3$$$, and $$$6$$$. By enumerating, we can easily find that there are no beautiful arrangements with more than three models. In the third query, there are no beautiful arrangements with more than one model. | 1,400 | true | false | false | true | false | false | false | false | false | false | 3,956 |
1661E | You are given a matrix $$$a$$$, consisting of $$$3$$$ rows and $$$n$$$ columns. Each cell of the matrix is either free or taken. A free cell $$$y$$$ is reachable from a free cell $$$x$$$ if at least one of these conditions hold: $$$x$$$ and $$$y$$$ share a side; there exists a free cell $$$z$$$ such that $$$z$$$ is reachable from $$$x$$$ and $$$y$$$ is reachable from $$$z$$$. A connected component is a set of free cells of the matrix such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule. You are asked $$$q$$$ queries about the matrix. Each query is the following: $$$l$$$ $$$r$$$xa0— count the number of connected components of the matrix, consisting of columns from $$$l$$$ to $$$r$$$ of the matrix $$$a$$$, inclusive. Print the answers to all queries. Input The first line contains an integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$)xa0— the number of columns of matrix $$$a$$$. The $$$i$$$-th of the next three lines contains a description of the $$$i$$$-th row of the matrix $$$a$$$xa0— a string, consisting of $$$n$$$ characters. Each character is either $$$1$$$ (denoting a free cell) or $$$0$$$ (denoting a taken cell). The next line contains an integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$)xa0— the number of queries. The $$$j$$$-th of the next $$$q$$$ lines contains two integers $$$l_j$$$ and $$$r_j$$$ ($$$1 le l_j le r_j le n$$$)xa0— the description of the $$$j$$$-th query. Output Print $$$q$$$ integersxa0— the $$$j$$$-th value should be equal to the number of the connected components of the matrix, consisting of columns from $$$l_j$$$ to $$$r_j$$$ of the matrix $$$a$$$, inclusive. Example Input 12 100101011101 110110010110 010001011101 8 1 12 1 1 1 2 9 9 8 11 9 12 11 12 4 6 | 2,500 | true | false | false | true | true | false | true | false | false | false | 2,315 |
79C | After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string _s_, and she tried to remember the string _s_ correctly. However, Ciel feels _n_ strings _b_1,u2009_b_2,u2009... ,u2009_b__n_ are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of _s_. Determine the longest contiguous substring of _s_ that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string _s_. The length of _s_ will be between 1 and 105, inclusive. In the second line there is a single integer _n_ (1u2009≤u2009_n_u2009≤u200910). Next _n_ lines, there is a string _b__i_ (1u2009≤u2009_i_u2009≤u2009_n_). Each length of _b__i_ will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers _len_ and _pos_: the length of the longest contiguous substring of _s_ that does not contain any _b__i_, and the first position of the substring (0-indexed). The position _pos_ must be between 0 and _s_u2009-u2009_len_ inclusive, where _s_ is the length of string _s_. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Input IhaveNoIdea 9 I h a v e N o I d Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii. | 1,800 | false | true | false | true | true | false | false | false | false | false | 9,568 |
46D | Nowadays it is becoming increasingly difficult to park a car in cities successfully. Let's imagine a segment of a street as long as _L_ meters along which a parking lot is located. Drivers should park their cars strictly parallel to the pavement on the right side of the street (remember that in the country the authors of the tasks come from the driving is right side!). Every driver when parking wants to leave for themselves some extra space to move their car freely, that's why a driver is looking for a place where the distance between his car and the one behind his will be no less than _b_ meters and the distance between his car and the one in front of his will be no less than _f_ meters (if there's no car behind then the car can be parked at the parking lot segment edge; the same is true for the case when there're no cars parked in front of the car). Let's introduce an axis of coordinates along the pavement. Let the parking lot begin at point 0 and end at point _L_. The drivers drive in the direction of the coordinates' increasing and look for the earliest place (with the smallest possible coordinate) where they can park the car. In case there's no such place, the driver drives on searching for his perfect peaceful haven. Sometimes some cars leave the street and free some space for parking. Considering that there never are two moving cars on a street at a time write a program that can use the data on the drivers, entering the street hoping to park there and the drivers leaving it, to model the process and determine a parking lot space for each car. Input The first line contains three integers _L_, _b_ и _f_ (10u2009≤u2009_L_u2009≤u2009100000,u20091u2009≤u2009_b_,u2009_f_u2009≤u2009100). The second line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009100) that indicates the number of requests the program has got. Every request is described on a single line and is given by two numbers. The first number represents the request type. If the request type is equal to 1, then in that case the second number indicates the length of a car (in meters) that enters the street looking for a place to park. And if the request type is equal to 2, then the second number identifies the number of such a request (starting with 1) that the car whose arrival to the parking lot was described by a request with this number, leaves the parking lot. It is guaranteed that that car was parked at the moment the request of the 2 type was made. The lengths of cars are integers from 1 to 1000. Output For every request of the 1 type print number -1 on the single line if the corresponding car couldn't find place to park along the street. Otherwise, print a single number equal to the distance between the back of the car in its parked position and the beginning of the parking lot zone. Examples Input 30 1 2 6 1 5 1 4 1 5 2 2 1 5 1 4 Input 30 1 1 6 1 5 1 4 1 5 2 2 1 5 1 4 | 1,800 | false | false | true | false | true | false | false | false | false | false | 9,746 |
1682D | There are $$$n$$$ nodes arranged in a circle numbered from $$$1$$$ to $$$n$$$ in the clockwise order. You are also given a binary string $$$s$$$ of length $$$n$$$. Your task is to construct a tree on the given $$$n$$$ nodes satisfying the two conditions below or report that there such tree does not exist: For each node $$$i$$$ $$$(1 le i le n)$$$, the degree of node is even if $$$s_i = 0$$$ and odd if $$$s_i = 1$$$. No two edges of the tree intersect internally in the circle. The edges are allowed to intersect on the circumference. Note that all edges are drawn as straight line segments. For example, edge $$$(u, v)$$$ in the tree is drawn as a line segment connecting $$$u$$$ and $$$v$$$ on the circle. A tree on $$$n$$$ nodes is a connected graph with $$$n - 1$$$ edges. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 2cdot 10^4)$$$ xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(2 leq n leq 2cdot 10^5)$$$ xa0— the number of nodes. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, if there does not exist a tree that satisfies the given conditions, then output "NO" (without quotes), otherwise output "YES" followed by the description of tree. You can output each letter in any case (for example, "YES", "Yes", "yes", "yEs", "yEs" will be recognized as a positive answer). If there exists a tree, then output $$$n - 1$$$ lines, each containing two integers $$$u$$$ and $$$v$$$ $$$(1 leq u,v leq n, u eq v)$$$ denoting an edge between $$$u$$$ and $$$v$$$ in the tree. If there are multiple possible answers, output any. Example Output YES 2 1 3 4 1 4 NO YES 2 3 1 2 5 6 6 2 3 4 Note In the first test case, the tree looks as follows: In the second test case, there is only one possible tree with an edge between $$$1$$$ and $$$2$$$, and it does not satisfy the degree constraints. In the third test case, The tree on the left satisfies the degree constraints but the edges intersect internally, therefore it is not a valid tree, while the tree on the right is valid. | 2,000 | false | false | true | false | false | true | false | false | false | false | 2,176 |
98E | Shrek and the Donkey (as you can guess, they also live in the far away kingdom) decided to play a card game called YAGame. The rules are very simple: initially Shrek holds _m_ cards and the Donkey holds _n_ cards (the players do not see each other's cards), and one more card lies on the table face down so that both players cannot see it as well. Thus, at the beginning of the game there are overall _m_u2009+u2009_n_u2009+u20091 cards. Besides, the players know which cards the pack of cards consists of and their own cards (but they do not know which card lies on the table and which ones the other player has). The players move in turn and Shrek starts. During a move a player can: Try to guess which card is lying on the table. If he guesses correctly, the game ends and he wins. If his guess is wrong, the game also ends but this time the other player wins. Name any card from the pack. If the other player has such card, he must show it and put it aside (so that this card is no longer used in the game). If the other player doesn't have such card, he says about that. Recently Donkey started taking some yellow pills and winning over Shrek. Now Shrek wants to evaluate his chances to win if he too starts taking the pills. Help Shrek assuming the pills are good in quality and that both players using them start playing in the optimal manner. Input The first line contains space-separated integers _m_ and _n_ (0u2009≤u2009_m_,u2009_n_u2009≤u20091000). Output Print space-separated probabilities that Shrek wins and Donkey wins correspondingly; the absolute error should not exceed 10u2009-u20099. | 2,700 | true | false | false | true | false | false | false | false | false | false | 9,492 |
962A | Polycarp has created his own training plan to prepare for the programming contests. He will train for $$$n$$$ days, all days are numbered from $$$1$$$ to $$$n$$$, beginning from the first. On the $$$i$$$-th day Polycarp will necessarily solve $$$a_i$$$ problems. One evening Polycarp plans to celebrate the equator. He will celebrate it on the first evening of such a day that from the beginning of the training and to this day inclusive he will solve half or more of all the problems. Determine the index of day when Polycarp will celebrate the equator. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 200,000$$$) — the number of days to prepare for the programming contests. The second line contains a sequence $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10,000$$$), where $$$a_i$$$ equals to the number of problems, which Polycarp will solve on the $$$i$$$-th day. Output Print the index of the day when Polycarp will celebrate the equator. Note In the first example Polycarp will celebrate the equator on the evening of the second day, because up to this day (inclusive) he will solve $$$4$$$ out of $$$7$$$ scheduled problems on four days of the training. In the second example Polycarp will celebrate the equator on the evening of the third day, because up to this day (inclusive) he will solve $$$6$$$ out of $$$12$$$ scheduled problems on six days of the training. | 1,300 | false | false | true | false | false | false | false | false | false | false | 5,856 |
1285C | Problem - 1285C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math number theory *1400 No tag edit access → Contest materials ") Editorial") $$$ such that $$$LCM(a, b)$$$ equals $$$X$$$. Both $$$a$$$ and $$$b$$$ should be positive integers. $$$LCM(a, b)$$$ is the smallest positive integer that is divisible by both $$$a$$$ and $$$b$$$. For example, $$$LCM(6, 8) = 24$$$, $$$LCM(4, 12) = 12$$$, $$$LCM(2, 3) = 6$$$. Of course, Fadi immediately knew the answer. Can you be just like Fadi and find any such pair? Input The first and only line contains an integer $$$X$$$ ($$$1 le X le 10^{12}$$$). Output Print two positive integers, $$$a$$$ and $$$b$$$, such that the value of $$$max(a, b)$$$ is minimum possible and $$$LCM(a, b)$$$ equals $$$X$$$. If there are several possible such pairs, you can print any. Examples Input 2 Output 1 2 Input 6 Output 2 3 Input 4 Output 1 4 Input 1 Output 1 1 | 1,400 | true | false | false | false | false | false | true | false | false | false | 4,265 |
1893A | You are given an array $$$b_1, b_2, ldots, b_n$$$. An anonymous informant has told you that the array $$$b$$$ was obtained as follows: initially, there existed an array $$$a_1, a_2, ldots, a_n$$$, after which the following two-component operation was performed $$$k$$$ times: 1. A fixed point$$$^{dagger}$$$ $$$x$$$ of the array $$$a$$$ was chosen. 2. Then, the array $$$a$$$ was cyclically shifted to the left$$$^{ddagger}$$$ exactly $$$x$$$ times. As a result of $$$k$$$ such operations, the array $$$b_1, b_2, ldots, b_n$$$ was obtained. You want to check if the words of the anonymous informant can be true or if they are guaranteed to be false. $$$^{dagger}$$$A number $$$x$$$ is called a fixed point of the array $$$a_1, a_2, ldots, a_n$$$ if $$$1 leq x leq n$$$ and $$$a_x = x$$$. $$$^{ddagger}$$$A cyclic left shift of the array $$$a_1, a_2, ldots, a_n$$$ is the array $$$a_2, ldots, a_n, a_1$$$. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n, k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le k le 10^9$$$)xa0— the length of the array $$$b$$$ and the number of operations performed. The second line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0— the elements of the array $$$b$$$. It is guaranteed that the sum of the values of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "Yes" if the words of the anonymous informant can be true, and "No" if they are guaranteed to be false. Example Input 6 5 3 4 3 3 2 3 3 100 7 2 1 5 5 6 1 1 1 1 1 1000000000 1 8 48 9 10 11 12 13 14 15 8 2 1 1 42 Output Yes Yes No Yes Yes No Note In the first test case, the array $$$a$$$ could be equal to $$$[3, 2, 3, 4, 3]$$$. In the first operation, a fixed point $$$x = 2$$$ was chosen, and after $$$2$$$ left shifts, the array became $$$[3, 4, 3, 3, 2]$$$. In the second operation, a fixed point $$$x = 3$$$ was chosen, and after $$$3$$$ left shifts, the array became $$$[3, 2, 3, 4, 3]$$$. In the third operation, a fixed point $$$x = 3$$$ was chosen again, and after $$$3$$$ left shifts, the array became $$$[4, 3, 3, 2, 3]$$$, which is equal to the array $$$b$$$. In the second test case, the array $$$a$$$ could be equal to $$$[7, 2, 1]$$$. After the operation with a fixed point $$$x = 2$$$, the array became $$$[1, 7, 2]$$$. Then, after the operation with a fixed point $$$x = 1$$$, the array returned to its initial state $$$[7, 2, 1]$$$. These same $$$2$$$ operations (with $$$x = 2$$$, and $$$x = 1$$$) were repeated $$$49$$$ times. So, after $$$100$$$ operations, the array returned to $$$[7, 2, 1]$$$. In the third test case, it can be shown that there is no solution. | 1,400 | false | false | true | false | false | false | true | false | false | true | 951 |
1029A | Problem - 1029A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation strings *1300 No tag edit access → Contest materials Editorial") — the length of the string $$$t$$$ and the number of substrings. The second line of the input contains the string $$$t$$$ consisting of exactly $$$n$$$ lowercase Latin letters. Output Print such string $$$s$$$ of minimum possible length that there are exactly $$$k$$$ substrings of $$$s$$$ equal to $$$t$$$. It is guaranteed that the answer is always unique. Examples Input 3 4 aba Output ababababa Input 3 2 cat Output catcat | 1,300 | false | false | true | false | false | false | false | false | false | false | 5,561 |
883J | The mayor of the Berland city S sees the beauty differently than other city-dwellers. In particular, he does not understand at all, how antique houses can be nice-looking. So the mayor wants to demolish all ancient buildings in the city. The city S is going to host the football championship very soon. In order to make the city beautiful, every month the Berland government provides mayor a money tranche. The money has to be spent on ancient buildings renovation. There are _n_ months before the championship and the _i_-th month tranche equals to _a__i_ burles. The city S has _m_ antique buildings and the renovation cost of the _j_-th building is _b__j_ burles. The mayor has his own plans for spending the money. As he doesn't like antique buildings he wants to demolish as much of them as possible. For the _j_-th building he calculated its demolishing cost _p__j_. The mayor decided to act according to the following plan. Each month he chooses several (possibly zero) of _m_ buildings to demolish in such a way that renovation cost of each of them separately is not greater than the money tranche _a__i_ of this month (_b__j_u2009≤u2009_a__i_)xa0— it will allow to deceive city-dwellers that exactly this building will be renovated. Then the mayor has to demolish all selected buildings during the current month as otherwise the dwellers will realize the deception and the plan will fail. Definitely the total demolishing cost can not exceed amount of money the mayor currently has. The mayor is not obliged to spend all the money on demolishing. If some money is left, the mayor puts it to the bank account and can use it in any subsequent month. Moreover, at any month he may choose not to demolish any buildings at all (in this case all the tranche will remain untouched and will be saved in the bank). Your task is to calculate the maximal number of buildings the mayor can demolish. Input The first line of the input contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100u2009000)xa0— the number of months before the championship and the number of ancient buildings in the city S. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is the tranche of the _i_-th month. The third line contains _m_ integers _b_1,u2009_b_2,u2009...,u2009_b__m_ (1u2009≤u2009_b__j_u2009≤u2009109), where _b__j_ is renovation cost of the _j_-th building. The fourth line contains _m_ integers _p_1,u2009_p_2,u2009...,u2009_p__m_ (1u2009≤u2009_p__j_u2009≤u2009109), where _p__j_ is the demolishing cost of the _j_-th building. Output Output single integerxa0— the maximal number of buildings the mayor can demolish. Examples Input 3 5 5 3 1 5 2 9 1 10 4 2 1 3 10 Input 5 6 6 3 2 4 3 3 6 4 5 4 2 1 4 3 2 5 3 Note In the third example the mayor acts as follows. In the first month he obtains 6 burles tranche and demolishes buildings #2 (renovation cost 6, demolishing cost 4) and #4 (renovation cost 5, demolishing cost 2). He spends all the money on it. After getting the second month tranche of 3 burles, the mayor selects only building #1 (renovation cost 3, demolishing cost 1) for demolishing. As a result, he saves 2 burles for the next months. In the third month he gets 2 burle tranche, but decides not to demolish any buildings at all. As a result, he has 2u2009+u20092u2009=u20094 burles in the bank. This reserve will be spent on the fourth month together with the 4-th tranche for demolishing of houses #3 and #5 (renovation cost is 4 for each, demolishing costs are 3 and 5 correspondingly). After this month his budget is empty. Finally, after getting the last tranche of 3 burles, the mayor demolishes building #6 (renovation cost 2, demolishing cost 3). As it can be seen, he demolished all 6 buildings. | 2,400 | false | true | false | false | false | true | false | false | true | false | 6,218 |
1427H | A prisoner wants to escape from a prison. The prison is represented by the interior of the convex polygon with vertices $$$P_1, P_2, P_3, ldots, P_{n+1}, P_{n+2}, P_{n+3}$$$. It holds $$$P_1=(0,0)$$$, $$$P_{n+1}=(0, h)$$$, $$$P_{n+2}=(-10^{18}, h)$$$ and $$$P_{n+3}=(-10^{18}, 0)$$$. The prison walls $$$P_{n+1}P_{n+2}$$$, $$$P_{n+2}P_{n+3}$$$ and $$$P_{n+3}P_1$$$ are very high and the prisoner is not able to climb them. Hence his only chance is to reach a point on one of the walls $$$P_1P_2, P_2P_3,dots, P_{n}P_{n+1}$$$ and escape from there. On the perimeter of the prison, there are two guards. The prisoner moves at speed $$$1$$$ while the guards move, remaining always on the perimeter of the prison, with speed $$$v$$$. If the prisoner reaches a point of the perimeter where there is a guard, the guard kills the prisoner. If the prisoner reaches a point of the part of the perimeter he is able to climb and there is no guard there, he escapes immediately. Initially the prisoner is at the point $$$(-10^{17}, h/2)$$$ and the guards are at $$$P_1$$$. Find the minimum speed $$$v$$$ such that the guards can guarantee that the prisoner will not escape (assuming that both the prisoner and the guards move optimally). Notes: At any moment, the guards and the prisoner can see each other. The "climbing part" of the escape takes no time. You may assume that both the prisoner and the guards can change direction and velocity instantly and that they both have perfect reflexes (so they can react instantly to whatever the other one is doing). The two guards can plan ahead how to react to the prisoner movements. Input The first line of the input contains $$$n$$$ ($$$1 le n le 50$$$). The following $$$n+1$$$ lines describe $$$P_1, P_2,dots, P_{n+1}$$$. The $$$i$$$-th of such lines contain two integers $$$x_i$$$, $$$y_i$$$ ($$$0le x_i, y_ile 1,000$$$) — the coordinates of $$$P_i=(x_i, y_i)$$$. It is guaranteed that $$$P_1=(0,0)$$$ and $$$x_{n+1}=0$$$. The polygon with vertices $$$P_1,P_2,dots, P_{n+1}, P_{n+2}, P_{n+3}$$$ (where $$$P_{n+2}, P_{n+3}$$$ shall be constructed as described in the statement) is guaranteed to be convex and such that there is no line containing three of its vertices. Output Print a single real number, the minimum speed $$$v$$$ that allows the guards to guarantee that the prisoner will not escape. Your answer will be considered correct if its relative or absolute error does not exceed $$$10^{-6}$$$. | 3,500 | false | false | false | false | false | false | false | true | false | false | 3,526 |
612A | Problem - 612A - Codeforces =============== xa0 ]( "Educational Codeforces Round 4") . Input The first line contains three positive integers _n_,u2009_p_,u2009_q_ (1u2009≤u2009_p_,u2009_q_u2009≤u2009_n_u2009≤u2009100). The second line contains the string _s_ consists of lowercase and uppercase latin letters and digits. Output If it's impossible to split the string _s_ to the strings of length _p_ and _q_ print the only number "-1". Otherwise in the first line print integer _k_ — the number of strings in partition of _s_. Each of the next _k_ lines should contain the strings in partition. Each string should be of the length _p_ or _q_. The string should be in order of their appearing in string _s_ — from left to right. If there are several solutions print any of them. Examples Input 5 2 3 Hello Output 2 He llo Input 10 9 5 Codeforces Output 2 Codef orces Input 6 4 5 Privet Output -1 Input 8 1 1 abacabac Output 8 a b a c a b a c | 1,300 | false | false | true | false | false | false | true | false | false | false | 7,393 |
1236D | Alice got a new doll these days. It can even walk! Alice has built a maze for the doll and wants to test it. The maze is a grid with $$$n$$$ rows and $$$m$$$ columns. There are $$$k$$$ obstacles, the $$$i$$$-th of them is on the cell $$$(x_i, y_i)$$$, which means the cell in the intersection of the $$$x_i$$$-th row and the $$$y_i$$$-th column. However, the doll is clumsy in some ways. It can only walk straight or turn right at most once in the same cell (including the start cell). It cannot get into a cell with an obstacle or get out of the maze. More formally, there exist $$$4$$$ directions, in which the doll can look: 1. The doll looks in the direction along the row from the first cell to the last. While moving looking in this direction the doll will move from the cell $$$(x, y)$$$ into the cell $$$(x, y + 1)$$$; 2. The doll looks in the direction along the column from the first cell to the last. While moving looking in this direction the doll will move from the cell $$$(x, y)$$$ into the cell $$$(x + 1, y)$$$; 3. The doll looks in the direction along the row from the last cell to first. While moving looking in this direction the doll will move from the cell $$$(x, y)$$$ into the cell $$$(x, y - 1)$$$; 4. The doll looks in the direction along the column from the last cell to the first. While moving looking in this direction the doll will move from the cell $$$(x, y)$$$ into the cell $$$(x - 1, y)$$$. . Standing in some cell the doll can move into the cell in the direction it looks or it can turn right once. Turning right once, the doll switches it's direction by the following rules: $$$1 o 2$$$, $$$2 o 3$$$, $$$3 o 4$$$, $$$4 o 1$$$. Standing in one cell, the doll can make at most one turn right. Now Alice is controlling the doll's moves. She puts the doll in of the cell $$$(1, 1)$$$ (the upper-left cell of the maze). Initially, the doll looks to the direction $$$1$$$, so along the row from the first cell to the last. She wants to let the doll walk across all the cells without obstacles exactly once and end in any place. Can it be achieved? Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$, separated by spaces ($$$1 leq n,m leq 10^5, 0 leq k leq 10^5$$$)xa0— the size of the maze and the number of obstacles. Next $$$k$$$ lines describes the obstacles, the $$$i$$$-th line contains two integer numbers $$$x_i$$$ and $$$y_i$$$, separated by spaces ($$$1 leq x_i leq n,1 leq y_i leq m$$$), which describes the position of the $$$i$$$-th obstacle. It is guaranteed that no two obstacles are in the same cell and no obstacle is in cell $$$(1, 1)$$$. Output Print 'Yes' (without quotes) if the doll can walk across all the cells without obstacles exactly once by the rules, described in the statement. If it is impossible to walk across the maze by these rules print 'No' (without quotes). Note Here is the picture of maze described in the first example: In the first example, the doll can walk in this way: The doll is in the cell $$$(1, 1)$$$, looks to the direction $$$1$$$. Move straight; The doll is in the cell $$$(1, 2)$$$, looks to the direction $$$1$$$. Move straight; The doll is in the cell $$$(1, 3)$$$, looks to the direction $$$1$$$. Turn right; The doll is in the cell $$$(1, 3)$$$, looks to the direction $$$2$$$. Move straight; The doll is in the cell $$$(2, 3)$$$, looks to the direction $$$2$$$. Move straight; The doll is in the cell $$$(3, 3)$$$, looks to the direction $$$2$$$. Turn right; The doll is in the cell $$$(3, 3)$$$, looks to the direction $$$3$$$. Move straight; The doll is in the cell $$$(3, 2)$$$, looks to the direction $$$3$$$. Move straight; The doll is in the cell $$$(3, 1)$$$, looks to the direction $$$3$$$. The goal is achieved, all cells of the maze without obstacles passed exactly once. | 2,300 | false | true | true | false | true | false | true | false | false | false | 4,515 |
1307A | The USA Construction Operation (USACO) recently ordered Farmer John to arrange a row of $$$n$$$ haybale piles on the farm. The $$$i$$$-th pile contains $$$a_i$$$ haybales. However, Farmer John has just left for vacation, leaving Bessie all on her own. Every day, Bessie the naughty cow can choose to move one haybale in any pile to an adjacent pile. Formally, in one day she can choose any two indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$) such that $$$i-j=1$$$ and $$$a_i>0$$$ and apply $$$a_i = a_i - 1$$$, $$$a_j = a_j + 1$$$. She may also decide to not do anything on some days because she is lazy. Bessie wants to maximize the number of haybales in pile $$$1$$$ (i.e. to maximize $$$a_1$$$), and she only has $$$d$$$ days to do so before Farmer John returns. Help her find the maximum number of haybales that may be in pile $$$1$$$ if she acts optimally! 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. Next $$$2t$$$ lines contain a description of test cases xa0— two lines per test case. The first line of each test case contains integers $$$n$$$ and $$$d$$$ ($$$1 le n,d le 100$$$) — the number of haybale piles and the number of days, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 100$$$) xa0— the number of haybales in each pile. Output For each test case, output one integer: the maximum number of haybales that may be in pile $$$1$$$ after $$$d$$$ days if Bessie acts optimally. Example Input 3 4 5 1 0 3 2 2 2 100 1 1 8 0 Note In the first test case of the sample, this is one possible way Bessie can end up with $$$3$$$ haybales in pile $$$1$$$: On day one, move a haybale from pile $$$3$$$ to pile $$$2$$$ On day two, move a haybale from pile $$$3$$$ to pile $$$2$$$ On day three, move a haybale from pile $$$2$$$ to pile $$$1$$$ On day four, move a haybale from pile $$$2$$$ to pile $$$1$$$ On day five, do nothing In the second test case of the sample, Bessie can do nothing on the first day and move a haybale from pile $$$2$$$ to pile $$$1$$$ on the second day. | 800 | false | true | true | false | false | false | false | false | false | false | 4,156 |
238D | There is a programming language in which every program is a non-empty sequence of "<" and ">" signs and digits. Let's explain how the interpreter of this programming language works. A program is interpreted using movement of instruction pointer (IP) which consists of two parts. Current character pointer (CP); Direction pointer (DP) which can point left or right; Initially CP points to the leftmost character of the sequence and DP points to the right. We repeat the following steps until the first moment that CP points to somewhere outside the sequence. If CP is pointing to a digit the interpreter prints that digit then CP moves one step according to the direction of DP. After that the value of the printed digit in the sequence decreases by one. If the printed digit was 0 then it cannot be decreased therefore it's erased from the sequence and the length of the sequence decreases by one. If CP is pointing to "<" or ">" then the direction of DP changes to "left" or "right" correspondingly. Then CP moves one step according to DP. If the new character that CP is pointing to is "<" or ">" then the previous character will be erased from the sequence. If at any moment the CP goes outside of the sequence the execution is terminated. It's obvious the every program in this language terminates after some steps. We have a sequence _s_1,u2009_s_2,u2009...,u2009_s__n_ of "<", ">" and digits. You should answer _q_ queries. Each query gives you _l_ and _r_ and asks how many of each digit will be printed if we run the sequence _s__l_,u2009_s__l_u2009+u20091,u2009...,u2009_s__r_ as an independent program in this language. Input The first line of input contains two integers _n_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009105) — represents the length of the sequence _s_ and the number of queries. The second line contains _s_, a sequence of "<", ">" and digits (0..9) written from left to right. Note, that the characters of _s_ are not separated with spaces. The next _q_ lines each contains two integers _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_) — the _i_-th query. Output For each query print 10 space separated integers: _x_0,u2009_x_1,u2009...,u2009_x_9 where _x__i_ equals the number of times the interpreter prints _i_ while running the corresponding program. Print answers to the queries in the order they are given in input. Examples Input 7 4 1>3>22< 1 3 4 7 7 7 1 7 Output 0 1 0 1 0 0 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 3 2 1 0 0 0 0 0 0 | 2,900 | false | false | true | false | true | false | false | false | false | false | 8,881 |
1283E | Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year... $$$n$$$ friends live in a city which can be represented as a number line. The $$$i$$$-th friend lives in a house with an integer coordinate $$$x_i$$$. The $$$i$$$-th friend can come celebrate the New Year to the house with coordinate $$$x_i-1$$$, $$$x_i+1$$$ or stay at $$$x_i$$$. Each friend is allowed to move no more than once. For all friends $$$1 le x_i le n$$$ holds, however, they can come to houses with coordinates $$$0$$$ and $$$n+1$$$ (if their houses are at $$$1$$$ or $$$n$$$, respectively). For example, let the initial positions be $$$x = [1, 2, 4, 4]$$$. The final ones then can be $$$[1, 3, 3, 4]$$$, $$$[0, 2, 3, 3]$$$, $$$[2, 2, 5, 5]$$$, $$$[2, 1, 3, 5]$$$ and so on. The number of occupied houses is the number of distinct positions among the final ones. So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of friends. The second line contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 le x_i le n$$$) — the coordinates of the houses of the friends. Note In the first example friends can go to $$$[2, 2, 3, 3]$$$. So friend $$$1$$$ goes to $$$x_1+1$$$, friend $$$2$$$ stays at his house $$$x_2$$$, friend $$$3$$$ goes to $$$x_3-1$$$ and friend $$$4$$$ goes to $$$x_4-1$$$. $$$[1, 1, 3, 3]$$$, $$$[2, 2, 3, 3]$$$ or $$$[2, 2, 4, 4]$$$ are also all valid options to obtain $$$2$$$ occupied houses. For the maximum number of occupied houses friends can go to $$$[1, 2, 3, 4]$$$ or to $$$[0, 2, 4, 5]$$$, for example. | 1,800 | false | true | false | true | false | false | false | false | false | false | 4,276 |
122B | Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. One day Petya was delivered a string _s_, containing only digits. He needs to find a string that represents a lucky number without leading zeroes, is not empty, is contained in _s_ as a substring the maximum number of times. Among all the strings for which the three conditions given above are fulfilled, Petya only needs the lexicographically minimum one. Find this string for Petya. Input The single line contains a non-empty string _s_ whose length can range from 1 to 50, inclusive. The string only contains digits. The string can contain leading zeroes. Output In the only line print the answer to Petya's problem. If the sought string does not exist, print "-1" (without quotes). Note The lexicographical comparison of strings is performed by the < operator in the modern programming languages. String _x_ is lexicographically less than string _y_ either if _x_ is a prefix of _y_, or exists such _i_ (1u2009≤u2009_i_u2009≤u2009_min_(_x_,u2009_y_)), that _x__i_u2009<u2009_y__i_ and for any _j_ (1u2009≤u2009_j_u2009<u2009_i_) _x__j_u2009=u2009_y__j_. Here _a_ denotes the length of string _a_. In the first sample three conditions are fulfilled for strings "4", "7" and "47". The lexicographically minimum one is "4". In the second sample _s_ has no substrings which are lucky numbers. In the third sample the three conditions are only fulfilled for string "7". | 1,000 | false | false | true | false | false | false | true | false | false | false | 9,390 |
1746B | You have an array $$$a$$$ of size $$$n$$$ consisting only of zeroes and ones. You can do the following operation: choose two indices $$$1 le i , j le n$$$, $$$i e j$$$, add $$$a_{i}$$$ to $$$a_{j}$$$, remove $$$a_{i}$$$ from $$$a$$$. Note that elements of $$$a$$$ can become bigger than $$$1$$$ after performing some operations. Also note that $$$n$$$ becomes $$$1$$$ less after the operation. What is the minimum number of operations needed to make $$$a$$$ non-decreasing, i.xa0e. that each element is not less than the previous element? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 10^5$$$), the size of array $$$a$$$. Next line contains $$$n$$$ integers $$$a_{1}, a_{2}, ldots a_{n}$$$ ($$$a_i$$$ is $$$0$$$ or $$$1$$$), elements of array $$$a$$$. It's guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case print a single integer, minimum number of operations needed to make $$$a$$$ non-decreasing. Example Input 4 8 0 0 1 1 1 1 1 1 5 1 0 0 1 1 2 1 0 11 1 1 0 0 1 0 0 1 1 1 0 Note In the first test case, $$$a$$$ is already non-decreasing, so you don't need to do any operations and the answer is $$$0$$$. In the second test case, you can perform an operation for $$$i = 1$$$ and $$$j = 5$$$, so $$$a$$$ will be equal to $$$[0, 0, 1, 2]$$$ and it becomes non-decreasing. In the third test case, you can perform an operation for $$$i = 2$$$ and $$$j = 1$$$, so $$$a$$$ will be equal to $$$[1]$$$ and it becomes non-decreasing. | 800 | false | true | false | false | false | true | false | false | false | false | 1,809 |
1842F | Problem - 1842F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dfs and similar greedy shortest paths sortings trees *2500 No tag edit access → Contest materials ") xa0— the number of vertices. The following $$$n-1$$$ lines of the input contains $$$2$$$ integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n, u_i eq v_i$$$)xa0— indicating an edge between vertices $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree. Output Output $$$n+1$$$ numbers. The $$$i$$$-th number is the answer of $$$k=i-1$$$. Examples Input 4 1 2 3 2 2 4 Output 0 3 4 5 6 Input 1 Output 0 0 Note Consider the first example. When $$$k=2$$$, Tenzing can paint vertices $$$1$$$ and $$$2$$$ black then the value of edge $$$(1,2)$$$ is 0, and the values of other edges are all equal to $$$2$$$. So the value of that tree is $$$4$$$. | 2,500 | false | true | false | false | false | false | false | false | true | false | 1,234 |
1632E1 | This version of the problem differs from the next one only in the constraint on $$$n$$$. A tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The distance between two vertices is the minimum sum of weights on the path connecting them. You are given a weighted tree with $$$n$$$ vertices, each edge has a weight of $$$1$$$. Denote $$$d(v)$$$ as the distance between vertex $$$1$$$ and vertex $$$v$$$. Let $$$f(x)$$$ be the minimum possible value of $$$maxlimits_{1 leq v leq n} {d(v)}$$$ if you can temporarily add an edge with weight $$$x$$$ between any two vertices $$$a$$$ and $$$b$$$ $$$(1 le a, b le n)$$$. Note that after this operation, the graph is no longer a tree. For each integer $$$x$$$ from $$$1$$$ to $$$n$$$, find $$$f(x)$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 3000$$$). Each of the next $$$n−1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) indicating that there is an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$3000$$$. Output For each test case, print $$$n$$$ integers in a single line, $$$x$$$-th of which is equal to $$$f(x)$$$ for all $$$x$$$ from $$$1$$$ to $$$n$$$. Example Input 3 4 1 2 2 3 1 4 2 1 2 7 1 2 1 3 3 4 3 5 3 6 5 7 Output 1 2 2 2 1 1 2 2 3 3 3 3 3 Note In the first testcase: For $$$x = 1$$$, we can an edge between vertices $$$1$$$ and $$$3$$$, then $$$d(1) = 0$$$ and $$$d(2) = d(3) = d(4) = 1$$$, so $$$f(1) = 1$$$. For $$$x ge 2$$$, no matter which edge we add, $$$d(1) = 0$$$, $$$d(2) = d(4) = 1$$$ and $$$d(3) = 2$$$, so $$$f(x) = 2$$$. | 2,400 | false | false | false | false | true | false | false | true | false | true | 2,454 |
468A | Little X used to play a card game called "24 Game", but recently he has found it too easy. So he invented a new game. Initially you have a sequence of _n_ integers: 1,u20092,u2009...,u2009_n_. In a single step, you can pick two of them, let's denote them _a_ and _b_, erase them from the sequence, and append to the sequence either _a_u2009+u2009_b_, or _a_u2009-u2009_b_, or _a_u2009×u2009_b_. After _n_u2009-u20091 steps there is only one number left. Can you make this number equal to 24? Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105). Output If it's possible, print "YES" in the first line. Otherwise, print "NO" (without the quotes). If there is a way to obtain 24 as the result number, in the following _n_u2009-u20091 lines print the required operations an operation per line. Each operation should be in form: "_a_ _op_ _b_ = _c_". Where _a_ and _b_ are the numbers you've picked at this operation; _op_ is either "+", or "-", or "*"; _c_ is the result of corresponding operation. Note, that the absolute value of _c_ mustn't be greater than 1018. The result of the last operation must be equal to 24. Separate operator sign and equality sign from numbers with spaces. If there are multiple valid answers, you may print any of them. Examples Output YES 8 7 = 56 6 5 = 30 3 - 4 = -1 1 - 2 = -1 30 - -1 = 31 56 - 31 = 25 25 + -1 = 24 | 1,500 | true | true | false | false | false | true | false | false | false | false | 7,967 |
319B | Problem - 319B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures implementation *1900 No tag edit access → Contest materials kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step. You're given the initial arrangement of the psychos in the line. Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment. Look notes to understand the statement more precise. Input The first line of input contains integer _n_ denoting the number of psychos, (1u2009≤u2009_n_u2009≤u2009105). In the second line there will be a list of _n_ space separated distinct integers each in range 1 to _n_, inclusive — ids of the psychos in the line from left to right. Output Print the number of steps, so that the line remains the same afterward. Examples Input 10 10 9 7 8 6 5 3 4 2 1 Output 2 Input 6 1 2 3 4 5 6 Output 0 Note In the first sample line of the psychos transforms as follows: | 1,900 | false | false | true | false | true | false | false | false | false | false | 8,557 |
954F | You are running through a rectangular field. This field can be represented as a matrix with 3 rows and _m_ columns. (_i_,u2009_j_) denotes a cell belonging to _i_-th row and _j_-th column. You start in (2,u20091) and have to end your path in (2,u2009_m_). From the cell (_i_,u2009_j_) you may advance to: (_i_u2009-u20091,u2009_j_u2009+u20091) — only if _i_u2009>u20091, (_i_,u2009_j_u2009+u20091), or (_i_u2009+u20091,u2009_j_u2009+u20091) — only if _i_u2009<u20093. However, there are _n_ obstacles blocking your path. _k_-th obstacle is denoted by three integers _a__k_, _l__k_ and _r__k_, and it forbids entering any cell (_a__k_,u2009_j_) such that _l__k_u2009≤u2009_j_u2009≤u2009_r__k_. You have to calculate the number of different paths from (2,u20091) to (2,u2009_m_), and print it modulo 109u2009+u20097. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009104, 3u2009≤u2009_m_u2009≤u20091018) — the number of obstacles and the number of columns in the matrix, respectively. Then _n_ lines follow, each containing three integers _a__k_, _l__k_ and _r__k_ (1u2009≤u2009_a__k_u2009≤u20093, 2u2009≤u2009_l__k_u2009≤u2009_r__k_u2009≤u2009_m_u2009-u20091) denoting an obstacle blocking every cell (_a__k_,u2009_j_) such that _l__k_u2009≤u2009_j_u2009≤u2009_r__k_. Some cells may be blocked by multiple obstacles. Output Print the number of different paths from (2,u20091) to (2,u2009_m_), taken modulo 109u2009+u20097. If it is impossible to get from (2,u20091) to (2,u2009_m_), then the number of paths is 0. | 2,100 | false | false | false | true | false | false | false | false | true | false | 5,904 |
331C2 | Yet another Armageddon is coming! This time the culprit is the Julya tribe calendar. The beavers in this tribe knew math very well. Smart Beaver, an archaeologist, got a sacred plate with a magic integer on it. The translation from Old Beaverish is as follows: "May the Great Beaver bless you! May your chacres open and may your third eye never turn blind from beholding the Truth! Take the magic number, subtract a digit from it (the digit must occur in the number) and get a new magic number. Repeat this operation until a magic number equals zero. The Earth will stand on Three Beavers for the time, equal to the number of subtractions you perform!" Distinct subtraction sequences can obviously get you different number of operations. But the Smart Beaver is ready to face the worst and is asking you to count the minimum number of operations he needs to reduce the magic number to zero. Input The single line contains the magic integer _n_, 0u2009≤u2009_n_. to get 20 points, you need to solve the problem with constraints: _n_u2009≤u2009106 (subproblem C1); to get 40 points, you need to solve the problem with constraints: _n_u2009≤u20091012 (subproblems C1+C2); to get 100 points, you need to solve the problem with constraints: _n_u2009≤u20091018 (subproblems C1+C2+C3). Output Print a single integer — the minimum number of subtractions that turns the magic number to a zero. Note In the first test sample the minimum number of operations can be reached by the following sequence of subtractions: 24u2009→u200920u2009→u200918u2009→u200910u2009→u20099u2009→u20090 | 2,400 | false | false | false | true | false | false | false | false | false | false | 8,517 |
725D | One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed. You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn't considered in the standings. A contest has just finished. There are _n_ teams, numbered 1 through _n_. The _i_-th team has _t__i_ balloons and weight _w__i_. It's guaranteed that _t__i_ doesn't exceed _w__i_ so nobody floats initially. Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can't give away more balloons than his team initially has. What is the best place Limak can get? Input The first line of the standard input contains one integer _n_ (2u2009≤u2009_n_u2009≤u2009300u2009000)xa0— the number of teams. The _i_-th of _n_ following lines contains two integers _t__i_ and _w__i_ (0u2009≤u2009_t__i_u2009≤u2009_w__i_u2009≤u20091018)xa0— respectively the number of balloons and the weight of the _i_-th team. Limak is a member of the first team. Output Print one integer denoting the best place Limak can get. Examples Input 8 20 1000 32 37 40 1000 45 50 16 16 16 16 14 1000 2 1000 Input 7 4 4 4 4 4 4 4 4 4 4 4 4 5 5 Input 7 14000000003 1000000000000000000 81000000000 88000000000 5000000000 7000000000 15000000000 39000000000 46000000000 51000000000 0 1000000000 0 0 Note In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is: 1. Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place. 2. Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified. 3. Limak gives 1 balloon to each of two teams with 16 balloons initially. 4. Limak has 20u2009-u20096u2009-u20096u2009-u20091u2009-u20091u2009=u20096 balloons. 5. There are three other teams left and their numbers of balloons are 40, 14 and 2. 6. Limak gets the third place because there are two teams with more balloons. In the second sample, Limak has the second place and he can't improve it. In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81u2009000u2009000u2009000, 5u2009000u2009000u2009000 and 46u2009000u2009000u2009000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7). | 1,800 | false | true | false | false | true | false | false | false | false | false | 6,904 |
1323B | You are given an array $$$a$$$ of length $$$n$$$ and array $$$b$$$ of length $$$m$$$ both consisting of only integers $$$0$$$ and $$$1$$$. Consider a matrix $$$c$$$ of size $$$n imes m$$$ formed by following rule: $$$c_{i, j} = a_i cdot b_j$$$ (i.e. $$$a_i$$$ multiplied by $$$b_j$$$). It's easy to see that $$$c$$$ consists of only zeroes and ones too. How many subrectangles of size (area) $$$k$$$ consisting only of ones are there in $$$c$$$? A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers $$$x_1, x_2, y_1, y_2$$$ ($$$1 le x_1 le x_2 le n$$$, $$$1 le y_1 le y_2 le m$$$) a subrectangle $$$c[x_1 dots x_2][y_1 dots y_2]$$$ is an intersection of the rows $$$x_1, x_1+1, x_1+2, dots, x_2$$$ and the columns $$$y_1, y_1+1, y_1+2, dots, y_2$$$. The size (area) of a subrectangle is the total number of cells in it. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq n, m leq 40,000, 1 leq k leq n cdot m$$$), length of array $$$a$$$, length of array $$$b$$$ and required size of subrectangles. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 1$$$), elements of $$$a$$$. The third line contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$0 leq b_i leq 1$$$), elements of $$$b$$$. Output Output single integerxa0— the number of subrectangles of $$$c$$$ with size (area) $$$k$$$ consisting only of ones. Examples Input 3 5 4 1 1 1 1 1 1 1 1 Note In first example matrix $$$c$$$ is: There are $$$4$$$ subrectangles of size $$$2$$$ consisting of only ones in it: In second example matrix $$$c$$$ is: | 1,500 | false | true | true | false | false | false | false | true | false | false | 4,100 |
649B | Statement is not available on English language Есть _n_-подъездный дом, в каждом подъезде по _m_ этажей, и на каждом этаже каждого подъезда ровно _k_ квартир. Таким образом, в доме всего _n_·_m_·_k_ квартир. Они пронумерованы естественным образом от 1 до _n_·_m_·_k_, то есть первая квартира на первом этаже в первом подъезде имеет номер 1, первая квартира на втором этаже первого подъезда имеет номер _k_u2009+u20091 и так далее. Особенность этого дома состоит в том, что он круглый. То есть если обходить его по часовой стрелке, то после подъезда номер 1 следует подъезд номер 2, затем подъезд номер 3 и так далее до подъезда номер _n_. После подъезда номер _n_ снова идёт подъезд номер 1. Эдвард живёт в квартире номер _a_, а Наташаxa0— в квартире номер _b_. Переход на 1 этаж вверх или вниз по лестнице занимает 5 секунд, переход от двери подъезда к двери соседнего подъездаxa0— 15 секунд, а переход в пределах одного этажа одного подъезда происходит мгновенно. Также в каждом подъезде дома есть лифт. Он устроен следующим образом: он всегда приезжает ровно через 10 секунд после вызова, а чтобы переместить пассажира на один этаж вверх или вниз, лифт тратит ровно 1 секунду. Посадка и высадка происходят мгновенно. Помогите Эдварду найти минимальное время, за которое он сможет добраться до квартиры Наташи. Считайте, что Эдвард может выйти из подъезда только с первого этажа соответствующего подъезда (это происходит мгновенно). Если Эдвард стоит перед дверью какого-то подъезда, он может зайти в него и сразу окажется на первом этаже этого подъезда (это также происходит мгновенно). Эдвард может выбирать, в каком направлении идти вокруг дома. Входные данные В первой строке входных данных следуют три числа _n_, _m_, _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u20091000)xa0— количество подъездов в доме, количество этажей в каждом подъезде и количество квартир на каждом этаже каждого подъезда соответственно. Во второй строке входных данных записаны два числа _a_ и _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_·_m_·_k_)xa0— номера квартир, в которых живут Эдвард и Наташа, соответственно. Гарантируется, что эти номера различны. Выходные данные Выведите единственное целое числоxa0— минимальное время (в секундах), за которое Эдвард сможет добраться от своей квартиры до квартиры Наташи. Примеры Входные данные 4 10 5 200 6 Примечание В первом тестовом примере Эдвард находится в 4 подъезде на 10 этаже, а Наташа находится в 1 подъезде на 2 этаже. Поэтому Эдварду выгодно сначала спуститься на лифте на первый этаж (на это он потратит 19 секунд, из которых 10xa0— на ожидание и 9xa0— на поездку на лифте), затем обойти дом против часовой стрелки до подъезда номер 1 (на это он потратит 15 секунд), и наконец подняться по лестнице на этаж номер 2 (на это он потратит 5 секунд). Таким образом, ответ равен 19u2009+u200915u2009+u20095u2009=u200939. Во втором тестовом примере Эдвард живёт в подъезде 2 на этаже 1, а Наташа находится в подъезде 1 на этаже 1. Поэтому Эдварду выгодно просто обойти дом по часовой стрелке до подъезда 1, на это он потратит 15 секунд. | 1,400 | false | false | false | false | false | true | false | false | false | false | 7,221 |
1494C | You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line. You start on a position $$$0$$$. There are $$$n$$$ boxes, the $$$i$$$-th box is on a position $$$a_i$$$. All positions of the boxes are distinct. There are also $$$m$$$ special positions, the $$$j$$$-th position is $$$b_j$$$. All the special positions are also distinct. In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you. You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Then descriptions of $$$t$$$ testcases follow. The first line of each testcase contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$)xa0— the number of boxes and the number of special positions, respectively. The second line of each testcase contains $$$n$$$ distinct integers in the increasing order $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_1 < a_2 < dots < a_n le 10^9$$$; $$$a_i eq 0$$$)xa0— the initial positions of the boxes. The third line of each testcase contains $$$m$$$ distinct integers in the increasing order $$$b_1, b_2, dots, b_m$$$ ($$$-10^9 le b_1 < b_2 < dots < b_m le 10^9$$$; $$$b_i eq 0$$$)xa0— the special positions. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. The sum of $$$m$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase print a single integerxa0— the maximum number of boxes that can be placed on special positions. Example Input 5 5 6 -1 1 5 11 15 -4 -3 -2 6 7 15 2 2 -1 1 -1000000000 1000000000 2 2 -1000000000 1000000000 -1 1 3 5 -1 1 2 -2 -1 1 2 5 2 1 1 2 10 Note In the first testcase you can go $$$5$$$ to the right: the box on position $$$1$$$ gets pushed to position $$$6$$$ and the box on position $$$5$$$ gets pushed to position $$$7$$$. Then you can go $$$6$$$ to the left to end up on position $$$-1$$$ and push a box to $$$-2$$$. At the end, the boxes are on positions $$$[-2, 6, 7, 11, 15]$$$, respectively. Among them positions $$$[-2, 6, 7, 15]$$$ are special, thus, the answer is $$$4$$$. In the second testcase you can push the box from $$$-1$$$ to $$$-10^9$$$, then the box from $$$1$$$ to $$$10^9$$$ and obtain the answer $$$2$$$. The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions. In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer $$$3$$$. In the fifth testcase there are fewer special positions than boxes. You can move either $$$8$$$ or $$$9$$$ to the right to have some box on position $$$10$$$. | 1,900 | false | true | true | true | false | false | false | true | false | false | 3,200 |
802K | Thank you for helping Heidi! It is now the second of April, but she has been summoned by Jenny again. The pranks do not seem to end... In the meantime, Heidi has decided that she does not trust her friends anymore. Not too much, anyway. Her relative lack of trust is manifested as follows: whereas previously she would not be made to visit the same person twice, now she can only be sure that she will not be made to visit the same person more than _k_ times. (In the case of Jenny, this includes her first visit in the beginning. The situation from the easy version corresponds to setting _k_u2009=u20091.) This is not as bad as it looks, since a single ticket for a route between two friends allows Heidi to travel between this pair of friends the whole day (in both directions). In other words, once she pays for travel between a pair of friends, all further travels between that pair are free. How much money will Heidi waste now, in a worst-case scenario? Input The first line contains two space-separated integers – the number of friends _n_ () and the parameter _k_ (1u2009≤u2009_k_u2009≤u2009105). The next _n_u2009-u20091 lines each contain three space-separated integers _u_, _v_ and _c_ (0u2009≤u2009_u_,u2009_v_u2009≤u2009_n_u2009-u20091, 1u2009≤u2009_c_u2009≤u2009104) meaning that _u_ and _v_ are friends and the cost for traveling between _u_ and _v_ is _c_. It is again guaranteed that the social network of the input forms a tree. Output Again, output a single integer – the maximum sum of costs of tickets. Examples Input 9 3 0 1 1 0 2 1 1 3 2 1 4 2 1 5 2 2 6 3 2 7 3 2 8 3 Input 9 5 0 1 1 0 2 1 1 3 2 1 4 2 1 5 2 2 6 3 2 7 3 2 8 3 Input 11 6 1 0 7932 2 1 1952 3 2 2227 4 0 9112 5 4 6067 6 0 6786 7 6 3883 8 4 7137 9 1 2796 10 5 6200 Note In the first example, the worst-case scenario for Heidi is to visit the friends in the following order: 0,u20091,u20095,u20091,u20093,u20091,u20090,u20092,u20096,u20092,u20097,u20092,u20098. Observe that no friend is visited more than 3 times. | 2,100 | false | false | false | true | false | false | false | false | false | false | 6,557 |
1772A | Problem - 1772A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials ") — the number of test cases. Each test case consists of one line containing an expression of the form $$$a{+}b$$$ ($$$0 le a, b le 9$$$, both $$$a$$$ and $$$b$$$ are integers). The integers are not separated from the $$$+$$$ sign. Output For each test case, print one integer — the result of the expression. Example Input 4 4+2 0+0 3+7 8+9 Output 6 0 10 17 | 800 | false | false | true | false | false | false | false | false | false | false | 1,663 |
1734B | There is a pyramid which consists of $$$n$$$ floors. The floors are numbered from top to bottom in increasing order. In the pyramid, the $$$i$$$-th floor consists of $$$i$$$ rooms. Denote the $$$j$$$-th room on the $$$i$$$-th floor as $$$(i,j)$$$. For all positive integers $$$i$$$ and $$$j$$$ such that $$$1 le j le i < n$$$, there are $$$2$$$ one-way staircases which lead from $$$(i,j)$$$ to $$$(i+1,j)$$$ and from $$$(i,j)$$$ to $$$(i+1,j+1)$$$ respectively. In each room you can either put a torch or leave it empty. Define the brightness of a room $$$(i, j)$$$ to be the number of rooms with a torch from which you can reach the room $$$(i, j)$$$ through a non-negative number of staircases. For example, when $$$n=5$$$ and torches are placed in the rooms $$$(1,1)$$$, $$$(2,1)$$$, $$$(3,2)$$$, $$$(4,1)$$$, $$$(4,3)$$$, and $$$(5,3)$$$, the pyramid can be illustrated as follows: In the above picture, rooms with torches are colored in yellow, and empty rooms are white. The blue numbers in the bottom-right corner indicate the brightness of the rooms. The room $$$(4,2)$$$ (the room with a star) has brightness $$$3$$$. In the picture below, the rooms from where you can reach $$$(4,2)$$$ have red border. The brightness is $$$3$$$ since there are three torches among these rooms. The pyramid is called nice if and only if for all floors, all rooms in the floor have the same brightness. Define the brilliance of a nice pyramid to be the sum of brightness over the rooms $$$(1,1)$$$, $$$(2,1)$$$, $$$(3,1)$$$, ..., $$$(n,1)$$$. Find an arrangement of torches in the pyramid, such that the resulting pyramid is nice and its brilliance is maximized. We can show that an answer always exists. If there are multiple answers, output any one of them. Output For each test case, output $$$n$$$ lines, the arrangement of torches in the pyramid. The $$$i$$$-th line should contain $$$i$$$ integers, each separated with a space. The $$$j$$$-th integer on the $$$i$$$-th line should be $$$1$$$ if room $$$(i,j)$$$ has a torch, and $$$0$$$ otherwise. We can show that an answer always exists. If there are multiple answers, output any one of them. Note In the third test case, torches are placed in $$$(1,1)$$$, $$$(2,1)$$$, $$$(2,2)$$$, $$$(3,1)$$$, and $$$(3,3)$$$. The pyramid is nice as rooms on each floor have the same brightness. For example, all rooms on the third floor have brightness $$$3$$$. The brilliance of the pyramid is $$$1+2+3 = 6$$$. It can be shown that no arrangements with $$$n=3$$$ will have a greater brilliance. | 800 | false | false | false | false | false | true | false | false | false | false | 1,885 |
1343B | You are given a positive integer $$$n$$$, it is guaranteed that $$$n$$$ is even (i.e. divisible by $$$2$$$). You want to construct the array $$$a$$$ of length $$$n$$$ such that: The first $$$frac{n}{2}$$$ elements of $$$a$$$ are even (divisible by $$$2$$$); the second $$$frac{n}{2}$$$ elements of $$$a$$$ are odd (not divisible by $$$2$$$); all elements of $$$a$$$ are distinct and positive; the sum of the first half equals to the sum of the second half ($$$sumlimits_{i=1}^{frac{n}{2}} a_i = sumlimits_{i=frac{n}{2} + 1}^{n} a_i$$$). If there are multiple answers, you can print any. It is not guaranteed that the answer exists. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the length of the array. It is guaranteed that that $$$n$$$ is even (i.e. divisible by $$$2$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print the answer — "NO" (without quotes), if there is no suitable answer for the given test case or "YES" in the first line and any suitable array $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) satisfying conditions from the problem statement on the second line. Example Output NO YES 2 4 1 5 NO YES 2 4 6 8 1 3 5 11 NO | 800 | true | false | false | false | false | true | false | false | false | false | 3,992 |
1351A | Problem - 1351A - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *800 No tag edit access → Contest materials ") time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You are given two integers $$$a$$$ and $$$b$$$. Print $$$a+b$$$. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases in the input. Then $$$t$$$ test cases follow. Each test case is given as a line of two integers $$$a$$$ and $$$b$$$ ($$$-1000 le a, b le 1000$$$). Output Print $$$t$$$ integers — the required numbers $$$a+b$$$. Example Input 4 1 5 314 15 -99 99 123 987 Output 6 329 0 1110 | 800 | false | false | true | false | false | false | false | false | false | false | 3,955 |
1446F | You are given an integer $$$k$$$ and $$$n$$$ distinct points with integer coordinates on the Euclidean plane, the $$$i$$$-th point has coordinates $$$(x_i, y_i)$$$. Consider a list of all the $$$frac{n(n - 1)}{2}$$$ pairs of points $$$((x_i, y_i), (x_j, y_j))$$$ ($$$1 le i < j le n$$$). For every such pair, write out the distance from the line through these two points to the origin $$$(0, 0)$$$. Your goal is to calculate the $$$k$$$-th smallest number among these distances. Input The first line contains two integers $$$n$$$, $$$k$$$ ($$$2 le n le 10^5$$$, $$$1 le k le frac{n(n - 1)}{2}$$$). The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$-10^4 le x_i, y_i le 10^4$$$) xa0— the coordinates of the $$$i$$$-th point. It is guaranteed that all given points are pairwise distinct. Output You should output one numberxa0— the $$$k$$$-th smallest distance from the origin. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$frac{a - b}{max{(1, b)}} le 10^{-6}$$$. Example Input 4 3 2 1 -2 -1 0 -1 -2 4 Note There are $$$6$$$ pairs of points: Line $$$1-2$$$ : distance $$$0$$$ from the origin Line $$$1-3$$$ : distance $$$frac{sqrt{2}}{2} approx 0.707106781$$$ from the origin Line $$$1-4$$$ : distance $$$2$$$ from the origin Line $$$2-3$$$ : distance $$$1$$$ from the origin Line $$$2-4$$$ : distance $$$2$$$ from the origin Line $$$3-4$$$ : distance $$$frac{2}{sqrt{29}} approx 0.371390676$$$ from the origin The third smallest distance among those is approximately $$$0.707106781$$$. | 3,200 | false | false | false | false | true | false | false | true | false | false | 3,441 |
1886C | Recall that string $$$a$$$ is lexicographically smaller than string $$$b$$$ if $$$a$$$ is a prefix of $$$b$$$ (and $$$a e b$$$), or there exists an index $$$i$$$ ($$$1 le i le min(a, b)$$$) such that $$$a_i < b_i$$$, and for any index $$$j$$$ ($$$1 le j < i$$$) $$$a_j = b_j$$$. Consider a sequence of strings $$$s_1, s_2, dots, s_n$$$, each consisting of lowercase Latin letters. String $$$s_1$$$ is given explicitly, and all other strings are generated according to the following rule: to obtain the string $$$s_i$$$, a character is removed from string $$$s_{i-1}$$$ in such a way that string $$$s_i$$$ is lexicographically minimal. For example, if $$$s_1 = mathrm{dacb}$$$, then string $$$s_2 = mathrm{acb}$$$, string $$$s_3 = mathrm{ab}$$$, string $$$s_4 = mathrm{a}$$$. After that, we obtain the string $$$S = s_1 + s_2 + dots + s_n$$$ ($$$S$$$ is the concatenation of all strings $$$s_1, s_2, dots, s_n$$$). You need to output the character in position $$$pos$$$ of the string $$$S$$$ (i.u2009e. the character $$$S_{pos}$$$). Input The first line contains one integer $$$t$$$ — the number of test cases ($$$1 le t le 10^4$$$). Each test case consists of two lines. The first line contains the string $$$s_1$$$ ($$$1 le s_1 le 10^6$$$), consisting of lowercase Latin letters. The second line contains the integer $$$pos$$$ ($$$1 le pos le frac{s_1 cdot (s_1 +1)}{2}$$$). You may assume that $$$n$$$ is equal to the length of the given string ($$$n = s_1$$$). Additional constraint on the input: the sum of $$$s_1$$$ over all test cases does not exceed $$$10^6$$$. | 1,600 | false | false | true | false | false | false | false | false | false | false | 975 |
64I | Problem - 64I - Codeforces =============== xa0 or DESC (nonascending order). Rules in the list are separated by a single comma with a single space. You have to sort rows of the table primarily by the first rule, then, in case of a tie sort by the second rule. And so on. If two rows are equal in terms of every rule, then preserve their relative order. You can assume that each element of the table has type "string", so you have to use lexicographic comparison. Input The first line contains column names. The second line contains the list of rules. The rest of the input data contains the table. All the words and column names are separated by single spaces. The number of rows and columns is between 1 and 100, inclusive. Names of columns and elements are strings containing only uppercase and lowercase Latin letters and digits, having the length between 1 and 10, inclusive. Output Print the table after the sorting. Examples Input NAME GROUP AGE GROUP ASC, AGE DESC Alex 412 19 Peter 422 19 Sergey 412 18 Andrey 311 18 Output Andrey 311 18 Alex 412 19 Sergey 412 18 Peter 422 19 | 2,400 | false | false | false | false | false | false | false | false | true | false | 9,647 |
449D | Problem - 449D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks combinatorics dp *2400 No tag edit access → Contest materials a group of size _k_. Jzzhu wonders, how many groups exists such that _a__i_1 & _a__i_2 & ... & _a__i__k_u2009=u20090 (1u2009≤u2009_k_u2009≤u2009_n_)? Help him and print this number modulo 1000000007 (109u2009+u20097). Operation _x_ & _y_ denotes bitwise AND operation of two numbers. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009106). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009106). Output Output a single integer representing the number of required groups modulo 1000000007 (109u2009+u20097). Examples Input 3 2 3 3 Output 0 Input 4 0 1 2 3 Output 10 Input 6 5 2 0 5 2 1 Output 53 | 2,400 | false | false | false | true | false | false | false | false | false | false | 8,041 |
128D | Problem - 128D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags constructive algorithms implementation *2000 No tag edit access → Contest materials . The next line contains those numbers, separated by a space. All numbers are integers and belong to the range from 1 to 109. Output Print the single line "YES" (without the quotes), if Anna could have completed the task correctly using all those numbers (using all of them is necessary). If Anna couldn't have fulfilled the task, no matter how hard she would try, print "NO" (without the quotes). Examples Input 4 1 2 3 2 Output YES Input 6 1 1 2 2 2 3 Output YES Input 6 2 4 1 1 2 2 Output NO | 2,000 | false | false | true | false | false | true | false | false | false | false | 9,367 |
1149E | In Byteland, there are two political parties fighting for seats in the Parliament in the upcoming elections: Wrong Answer Party and Time Limit Exceeded Party. As they want to convince as many citizens as possible to cast their votes on them, they keep promising lower and lower taxes. There are $$$n$$$ cities in Byteland, connected by $$$m$$$ one-way roads. Interestingly enough, the road network has no cycles — it's impossible to start in any city, follow a number of roads, and return to that city. Last year, citizens of the $$$i$$$-th city had to pay $$$h_i$$$ bourles of tax. Parties will now alternately hold the election conventions in various cities. If a party holds a convention in city $$$v$$$, the party needs to decrease the taxes in this city to a non-negative integer amount of bourles. However, at the same time they can arbitrarily modify the taxes in each of the cities that can be reached from $$$v$$$ using a single road. The only condition that must be fulfilled that the tax in each city has to remain a non-negative integer amount of bourles. The first party to hold the convention is Wrong Answer Party. It's predicted that the party to hold the last convention will win the election. Can Wrong Answer Party win regardless of Time Limit Exceeded Party's moves? Input The first line of the input contains two integers $$$n$$$, $$$m$$$ ($$$1 leq n leq 200,000$$$, $$$0 leq m leq 200,000$$$) — the number of cities and roads in Byteland. The next line contains $$$n$$$ space-separated integers $$$h_1, h_2, dots, h_n$$$ ($$$0 leq h_i leq 10^9$$$); $$$h_i$$$ denotes the amount of taxes paid in the $$$i$$$-th city. Each of the following $$$m$$$ lines contains two integers ($$$1 leq u, v leq n$$$, $$$u eq v$$$), and describes a one-way road leading from the city $$$u$$$ to the city $$$v$$$. There will be no cycles in the road network. No two roads will connect the same pair of cities. We can show that the conventions cannot be held indefinitely for any correct test case. Output If Wrong Answer Party can win the election, output WIN in the first line of your output. In this case, you're additionally asked to produce any convention allowing the party to win regardless of the opponent's actions. The second line should contain $$$n$$$ non-negative integers $$$h'_1, h'_2, dots, h'_n$$$ ($$$0 leq h'_i leq 2 cdot 10^{18}$$$) describing the amount of taxes paid in consecutive cities after the convention. If there are multiple answers, output any. We guarantee that if the party has any winning move, there exists a move after which no city has to pay more than $$$2 cdot 10^{18}$$$ bourles. If the party cannot assure their victory, output LOSE in the first and only line of the output. Examples Input 3 3 314 159 265 1 2 1 3 3 2 Input 6 4 2 2 5 5 6 6 1 3 2 4 3 5 4 6 Note In the first example, Wrong Answer Party should hold the convention in the city $$$1$$$. The party will decrease the taxes in this city to $$$1$$$ bourle. As the city $$$2$$$ is directly reachable from $$$1$$$, we can arbitrarily modify the taxes in this city. The party should change the tax to $$$5$$$ bourles. It can be easily proved that Time Limit Exceeded cannot win the election after this move if Wrong Answer Party plays optimally. The second example test presents the situation we created after a single move in the previous test; as it's Wrong Answer Party's move now, the party cannot win. In the third test, we should hold the convention in the first city. This allows us to change the taxes in any city to any desired value; we can for instance decide to set all the taxes to zero, which allows the Wrong Answer Party to win the election immediately. | 3,200 | false | false | false | false | false | false | false | false | false | true | 4,965 |
1680E | You are given a board of size $$$2 imes n$$$ ($$$2$$$ rows, $$$n$$$ columns). Some cells of the board contain chips. The chip is represented as '*', and an empty space is represented as '.'. It is guaranteed that there is at least one chip on the board. In one move, you can choose any chip and move it to any adjacent (by side) cell of the board (if this cell is inside the board). It means that if the chip is in the first row, you can move it left, right or down (but it shouldn't leave the board). Same, if the chip is in the second row, you can move it left, right or up. If the chip moves to the cell with another chip, the chip in the destination cell disappears (i.u2009e. our chip captures it). Your task is to calculate the minimum number of moves required to leave exactly one chip on the board. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the length of the board. The second line of the test case contains the string $$$s_1$$$ consisting of $$$n$$$ characters '*' (chip) and/or '.' (empty cell). The third line of the test case contains the string $$$s_2$$$ consisting of $$$n$$$ characters '*' (chip) and/or '.' (empty cell). Additional constraints on the input: in each test case, there is at least one chip on a board; the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$ ($$$sum n le 2 cdot 10^5$$$). Output For each test case, print one integer — the minimum number of moves required to leave exactly one chip on the board. Example Input 5 1 * . 2 .* ** 3 *.* .*. 4 **.* **.. 5 **... ...** | 2,000 | false | true | false | true | false | false | false | false | false | false | 2,187 |
1771F | Hossam gives you a sequence of integers $$$a_1, , a_2, , dots, , a_n$$$ of length $$$n$$$. Moreover, he will give you $$$q$$$ queries of type $$$(l, , r)$$$. For each query, consider the elements $$$a_l, , a_{l + 1}, , dots, , a_r$$$. Hossam wants to know the smallest number in this sequence, such that it occurs in this sequence an odd number of times. You need to compute the answer for each query before process the next query. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$), the length of the sequence. The second line contains $$$n$$$ integers $$$a_1, , a_2, , dots, , a_n$$$ ($$$1 le a_i le 10^9$$$). The third line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$), the number of queries. Each of the next $$$q$$$ lines contains two integers $$$a$$$ and $$$b$$$ ($$$0 le a, , b le 2 cdot 10^9$$$), the numbers used to encode the queries. Let $$$mathrm{ans}_i$$$ be the answer on the $$$i$$$-th query, and $$$mathrm{ans}_0$$$ be zero. Then $$$$$$l_i = a_i oplus mathrm{ans}_{i - 1},$$$$$$ $$$$$$r_i = b_i oplus mathrm{ans}_{i - 1},$$$$$$ where $$$l_i, , r_i$$$ are parameters of the $$$i$$$-th query and $$$oplus$$$ means the [bitwise exclusive or]( operation. It is guaranteed that $$$1 le l le r le n$$$. Output For each query, print the smallest number that occurs an odd number of times on the given segment of the sequence. If there is no such number, print $$$0$$$. Examples Input 5 1 2 1 2 2 6 1 2 0 2 0 6 0 5 2 2 3 7 Input 10 51 43 69 48 23 52 48 76 19 55 10 1 1 57 57 54 62 20 27 56 56 79 69 16 21 18 30 25 25 62 61 Output 51 55 19 48 76 19 23 19 55 19 Note In the example, $$$$$$l_1 = 1, , r_1 = 2,$$$$$$ $$$$$$l_2 = 1, , r_2 = 3,$$$$$$ $$$$$$l_3 = 2, , r_3 = 4,$$$$$$ $$$$$$l_4 = 1, , r_4 = 4,$$$$$$ $$$$$$l_5 = 2, , r_5 = 2,$$$$$$ $$$$$$l_6 = 1, , r_6 = 5.$$$$$$ | 2,500 | false | false | false | false | true | false | false | true | false | false | 1,664 |
17C | Nick likes strings very much, he likes to rotate them, sort them, rearrange characters within a string... Once he wrote a random string of characters a, b, c on a piece of paper and began to perform the following operations: to take two adjacent characters and replace the second character with the first one, to take two adjacent characters and replace the first character with the second one To understand these actions better, let's take a look at a string «abc». All of the following strings can be obtained by performing one of the described operations on «abc»: «bbc», «abb», «acc». Let's denote the frequency of a character for each of the characters a, b and c as the number of occurrences of this character in the string. For example, for string «abc»: _a_ = 1, _b_ = 1, _c_ = 1, and for string «bbc»: _a_ = 0, _b_ = 2, _c_ = 1. While performing the described operations, Nick sometimes got balanced strings. Let's say that a string is balanced, if the frequencies of each character differ by at most 1. That is u2009-u20091u2009≤u2009_a_u2009-u2009_b_u2009≤u20091, u2009-u20091u2009≤u2009_a_u2009-u2009_c_u2009≤u20091 и u2009-u20091u2009≤u2009_b_u2009-u2009_c_u2009≤u20091. Would you help Nick find the number of different balanced strings that can be obtained by performing the operations described above, perhaps multiple times, on the given string _s_. This number should be calculated modulo 51123987. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009150) — the length of the given string _s_. Next line contains the given string _s_. The initial string can be balanced as well, in this case it should be counted too. The given string _s_ consists only of characters a, b and c. Output Output the only number — the number of different balanced strings that can be obtained by performing the described operations, perhaps multiple times, on the given string _s_, modulo 51123987. Note In the first sample it is possible to get 51 different strings through the described operations, but only 7 of them are balanced: «abca», «bbca», «bcca», «bcaa», «abcc», «abbc», «aabc». In the second sample: «abbc», «aabc», «abcc». In the third sample there is only one balanced string — «ab» itself. | 2,500 | false | false | false | true | false | false | false | false | false | false | 9,908 |
1437B | You are given a string $$$s$$$ of even length $$$n$$$. String $$$s$$$ is binary, in other words, consists only of 0's and 1's. String $$$s$$$ has exactly $$$frac{n}{2}$$$ zeroes and $$$frac{n}{2}$$$ ones ($$$n$$$ is even). In one operation you can reverse any substring of $$$s$$$. A substring of a string is a contiguous subsequence of that string. What is the minimum number of operations you need to make string $$$s$$$ alternating? A string is alternating if $$$s_i eq s_{i + 1}$$$ for all $$$i$$$. There are two types of alternating strings in general: 01010101... or 10101010... Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$; $$$n$$$ is even)xa0— the length of string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ ($$$s_i in$$$ {0, 1}). String $$$s$$$ has exactly $$$frac{n}{2}$$$ zeroes and $$$frac{n}{2}$$$ ones. It's guaranteed that the total sum of $$$n$$$ over test cases doesn't exceed $$$10^5$$$. Output For each test case, print the minimum number of operations to make $$$s$$$ alternating. Example Input 3 2 10 4 0110 8 11101000 Note In the first test case, string 10 is already alternating. In the second test case, we can, for example, reverse the last two elements of $$$s$$$ and get: 0110 $$$ ightarrow$$$ 0101. In the third test case, we can, for example, make the following two operations: 1. 11101000 $$$ ightarrow$$$ 10101100; 2. 10101100 $$$ ightarrow$$$ 10101010. | 1,200 | false | true | false | false | false | true | false | false | false | false | 3,484 |
818F | Ivan is developing his own computer game. Now he tries to create some levels for his game. But firstly for each level he needs to draw a graph representing the structure of the level. Ivan decided that there should be exactly _n__i_ vertices in the graph representing level _i_, and the edges have to be bidirectional. When constructing the graph, Ivan is interested in special edges called bridges. An edge between two vertices _u_ and _v_ is called a bridge if this edge belongs to every path between _u_ and _v_ (and these vertices will belong to different connected components if we delete this edge). For each level Ivan wants to construct a graph where at least half of the edges are bridges. He also wants to maximize the number of edges in each constructed graph. So the task Ivan gave you is: given _q_ numbers _n_1,u2009_n_2,u2009...,u2009_n__q_, for each _i_ tell the maximum number of edges in a graph with _n__i_ vertices, if at least half of the edges are bridges. Note that the graphs cannot contain multiple edges or self-loops. Input The first line of input file contains a positive integer _q_ (1u2009≤u2009_q_u2009≤u2009100u2009000) — the number of graphs Ivan needs to construct. Then _q_ lines follow, _i_-th line contains one positive integer _n__i_ (1u2009≤u2009_n__i_u2009≤u20092·109) — the number of vertices in _i_-th graph. Note that in hacks you have to use _q_u2009=u20091. Output Output _q_ numbers, _i_-th of them must be equal to the maximum number of edges in _i_-th graph. Note In the first example it is possible to construct these graphs: 1. 1u2009-u20092, 1u2009-u20093; 2. 1u2009-u20092, 1u2009-u20093, 2u2009-u20094; 3. 1u2009-u20092, 1u2009-u20093, 2u2009-u20093, 1u2009-u20094, 2u2009-u20095, 3u2009-u20096. | 2,100 | true | false | false | false | false | false | false | true | false | false | 6,482 |
1364A | Ehab loves number theory, but for some reason he hates the number $$$x$$$. Given an array $$$a$$$, find the length of its longest subarray such that the sum of its elements isn't divisible by $$$x$$$, or determine that such subarray doesn't exist. An array $$$a$$$ is a subarray of an array $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains an integer $$$t$$$ $$$(1 le t le 5)$$$xa0— the number of test cases you need to solve. The description of the test cases follows. The first line of each test case contains 2 integers $$$n$$$ and $$$x$$$ ($$$1 le n le 10^5$$$, $$$1 le x le 10^4$$$)xa0— the number of elements in the array $$$a$$$ and the number that Ehab hates. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_{n}$$$ ($$$0 le a_i le 10^4$$$)xa0— the elements of the array $$$a$$$. Output For each testcase, print the length of the longest subarray whose sum isn't divisible by $$$x$$$. If there's no such subarray, print $$$-1$$$. Example Input 3 3 3 1 2 3 3 4 1 2 3 2 2 0 6 Note In the first test case, the subarray $$$[2,3]$$$ has sum of elements $$$5$$$, which isn't divisible by $$$3$$$. In the second test case, the sum of elements of the whole array is $$$6$$$, which isn't divisible by $$$4$$$. In the third test case, all subarrays have an even sum, so the answer is $$$-1$$$. | 1,200 | false | false | false | false | true | false | true | false | false | false | 3,862 |
1043G | Ildar took a band (a thin strip of cloth) and colored it. Formally, the band has $$$n$$$ cells, each of them is colored into one of $$$26$$$ colors, so we can denote each color with one of the lowercase letters of English alphabet. Ildar decided to take some segment of the band $$$[l, r]$$$ ($$$1 le l le r le n$$$) he likes and cut it from the band. So he will create a new band that can be represented as a string $$$t = s_l s_{l+1} ldots s_r$$$. After that Ildar will play the following game: he cuts the band $$$t$$$ into some new bands and counts the number of different bands among them. Formally, Ildar chooses $$$1 le k le t$$$ indexes $$$1 le i_1 < i_2 < ldots < i_k = t$$$ and cuts $$$t$$$ to $$$k$$$ bands-strings $$$t_1 t_2 ldots t_{i_1}, t_{i_1 + 1} ldots t_{i_2}, ldots, {t_{i_{k-1} + 1}} ldots t_{i_k}$$$ and counts the number of different bands among them. He wants to know the minimal possible number of different bands he can get under the constraint that at least one band repeats at least two times. The result of the game is this number. If it is impossible to cut $$$t$$$ in such a way, the result of the game is -1. Unfortunately Ildar hasn't yet decided which segment he likes, but he has $$$q$$$ segments-candidates $$$[l_1, r_1]$$$, $$$[l_2, r_2]$$$, ..., $$$[l_q, r_q]$$$. Your task is to calculate the result of the game for each of them. Input The first line contains one integer $$$n$$$ ($$$1 le n le 200,000$$$)xa0— the length of the band Ildar has. The second line contains a string $$$s$$$ consisting of $$$n$$$ lowercase English lettersxa0— the band Ildar has. The third line contains a single integer $$$q$$$ ($$$1 le q le 200,000$$$)xa0— the number of segments Ildar has chosen as candidates. Each of the next $$$q$$$ lines contains two integer integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) denoting the ends of the $$$i$$$-th segment. Output Output $$$q$$$ lines, where the $$$i$$$-th of them should contain the result of the game on the segment $$$[l_i, r_i]$$$. Example Input 9 abcabcdce 7 1 6 4 7 5 9 6 9 1 9 3 6 4 4 Note Consider the first example. If Ildar chooses the segment $$$[1, 6]$$$, he cuts a string $$$t = abcabc$$$. If he cuts $$$t$$$ into two bands $$$abc$$$ and $$$abc$$$, the band $$$abc$$$ repeats two times and the number of different tapes is $$$1$$$. So, the result of this game is $$$1$$$. If Ildar chooses the segment $$$[4, 7]$$$, he cuts a string $$$t = abcd$$$. It is impossible to cut this band in such a way that there is at least one band repeating at least two times. So, the result of this game is $$$-1$$$. If Ildar chooses the segment $$$[3, 6]$$$, he cuts a string $$$t = cabc$$$. If he cuts $$$t$$$ into three bands $$$c$$$, $$$ab$$$ and $$$c$$$, the band $$$c$$$ repeats two times and the number of different bands is $$$2$$$. So, the result of this game is $$$2$$$. | 3,500 | false | false | false | false | true | false | false | false | false | false | 5,477 |
1295C | You are given two strings $$$s$$$ and $$$t$$$ consisting of lowercase Latin letters. Also you have a string $$$z$$$ which is initially empty. You want string $$$z$$$ to be equal to string $$$t$$$. You can perform the following operation to achieve this: append any subsequence of $$$s$$$ at the end of string $$$z$$$. A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements. For example, if $$$z = ac$$$, $$$s = abcde$$$, you may turn $$$z$$$ into following strings in one operation: 1. $$$z = acace$$$ (if we choose subsequence $$$ace$$$); 2. $$$z = acbcd$$$ (if we choose subsequence $$$bcd$$$); 3. $$$z = acbce$$$ (if we choose subsequence $$$bce$$$). Note that after this operation string $$$s$$$ doesn't change. Calculate the minimum number of such operations to turn string $$$z$$$ into string $$$t$$$. Input The first line contains the integer $$$T$$$ ($$$1 le T le 100$$$) — the number of test cases. The first line of each testcase contains one string $$$s$$$ ($$$1 le s le 10^5$$$) consisting of lowercase Latin letters. The second line of each testcase contains one string $$$t$$$ ($$$1 le t le 10^5$$$) consisting of lowercase Latin letters. It is guaranteed that the total length of all strings $$$s$$$ and $$$t$$$ in the input does not exceed $$$2 cdot 10^5$$$. Output For each testcase, print one integer — the minimum number of operations to turn string $$$z$$$ into string $$$t$$$. If it's impossible print $$$-1$$$. Example Input 3 aabce ace abacaba aax ty yyt | 1,600 | false | true | false | true | false | false | false | false | false | false | 4,221 |
792B | _n_ children are standing in a circle and playing the counting-out game. Children are numbered clockwise from 1 to _n_. In the beginning, the first child is considered the leader. The game is played in _k_ steps. In the _i_-th step the leader counts out _a__i_ people in clockwise order, starting from the next person. The last one to be pointed at by the leader is eliminated, and the next player after him becomes the new leader. For example, if there are children with numbers [8,u200910,u200913,u200914,u200916] currently in the circle, the leader is child 13 and _a__i_u2009=u200912, then counting-out rhyme ends on child 16, who is eliminated. Child 8 becomes the leader. You have to write a program which prints the number of the child to be eliminated on every step. Input The first line contains two integer numbers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_k_u2009≤u2009_n_u2009-u20091). The next line contains _k_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≤u2009_a__i_u2009≤u2009109). Output Print _k_ numbers, the _i_-th one corresponds to the number of child to be eliminated at the _i_-th step. Note Let's consider first example: In the first step child 4 is eliminated, child 5 becomes the leader. In the second step child 2 is eliminated, child 3 becomes the leader. In the third step child 5 is eliminated, child 6 becomes the leader. In the fourth step child 6 is eliminated, child 7 becomes the leader. In the final step child 1 is eliminated, child 3 becomes the leader. | 1,300 | false | false | true | false | false | false | false | false | false | false | 6,612 |
996B | Allen wants to enter a fan zone that occupies a round square and has $$$n$$$ entrances. There already is a queue of $$$a_i$$$ people in front of the $$$i$$$-th entrance. Each entrance allows one person from its queue to enter the fan zone in one minute. Allen uses the following strategy to enter the fan zone: Initially he stands in the end of the queue in front of the first entrance. Each minute, if he is not allowed into the fan zone during the minute (meaning he is not the first in the queue), he leaves the current queue and stands in the end of the queue of the next entrance (or the first entrance if he leaves the last entrance). Determine the entrance through which Allen will finally enter the fan zone. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of entrances. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^9$$$)xa0— the number of people in queues. These numbers do not include Allen. Note In the first example the number of people (not including Allen) changes as follows: $$$[ extbf{2}, 3, 2, 0] o [1, extbf{2}, 1, 0] o [0, 1, extbf{0}, 0]$$$. The number in bold is the queue Alles stands in. We see that he will enter the fan zone through the third entrance. In the second example the number of people (not including Allen) changes as follows: $$$[ extbf{10}, 10] o [9, extbf{9}] o [ extbf{8}, 8] o [7, extbf{7}] o [ extbf{6}, 6] o [5, extbf{5}] o [ extbf{4}, 4] o [3, extbf{3}] o [ extbf{2}, 2] o [1, extbf{1}] o [ extbf{0}, 0]$$$. In the third example the number of people (not including Allen) changes as follows: $$$[ extbf{5}, 2, 6, 5, 7, 4] o [4, extbf{1}, 5, 4, 6, 3] o [3, 0, extbf{4}, 3, 5, 2] o [2, 0, 3, extbf{2}, 4, 1] o [1, 0, 2, 1, extbf{3}, 0] o [0, 0, 1, 0, 2, extbf{0}]$$$. | 1,300 | true | false | false | false | false | false | false | true | false | false | 5,719 |
1848F | Recently, Vika was studying her favorite internet resource - Wikipedia. On the expanses of Wikipedia, she read about an interesting mathematical operation bmod n}$$$. Here $$$x bmod y$$$ denotes the remainder of dividing $$$x$$$ by $$$y$$$. The elements of the array are numbered starting from $$$0$$$. Since it is not enough to perform the above actions once for a complete study, Vika repeats them until the array $$$a$$$ becomes all zeros. Determine how many of the above actions it will take to make all elements of the array $$$a$$$ zero. If this moment never comes, output $$$-1$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2^{20}$$$) - the length of the array $$$a$$$. It is guaranteed that $$$n$$$ can be represented as $$$2^k$$$ for some integer $$$k$$$ ($$$0 le k le 20$$$). The second line contains $$$n$$$ integers $$$a_0, a_1, a_2, dots, a_{n-1}$$$ ($$$0 le a_i le 10^9$$$) - the elements of the array $$$a$$$. Output Output a single number - the minimum number of actions required to make all elements of the array $$$a$$$ zero, or $$$-1$$$ if the array $$$a$$$ will never become zero. Note In the first example, after one operation, the array $$$a$$$ will become equal to $$$[3, 3, 3, 3]$$$. After one more operation, it will become equal to $$$[0, 0, 0, 0]$$$. In the second example, the array $$$a$$$ initially consists only of zeros. In the third example, after one operation, the array $$$a$$$ will become equal to $$$[0]$$$. | 2,400 | true | false | false | true | false | false | false | true | false | false | 1,189 |
1566B | A binary string is a string that consists of characters $$$0$$$ and $$$1$$$. Let $$$operatorname{MEX}$$$ of a binary string be the smallest digit among $$$0$$$, $$$1$$$, or $$$2$$$ that does not occur in the string. For example, $$$operatorname{MEX}$$$ of $$$001011$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ occur in the string at least once, $$$operatorname{MEX}$$$ of $$$1111$$$ is $$$0$$$, because $$$0$$$ and $$$2$$$ do not occur in the string and $$$0 < 2$$$. A binary string $$$s$$$ is given. You should cut it into any number of substrings such that each character is in exactly one substring. It is possible to cut the string into a single substring — the whole string. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. What is the minimal sum of $$$operatorname{MEX}$$$ of all substrings pieces can be? Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Description of the test cases follows. Each test case contains a single binary string $$$s$$$ ($$$1 le s le 10^5$$$). It's guaranteed that the sum of lengths of $$$s$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print a single integerxa0— the minimal sum of $$$operatorname{MEX}$$$ of all substrings that it is possible to get by cutting $$$s$$$ optimally. Example Input 6 01 1111 01100 101 0000 01010 Note In the first test case the minimal sum is $$$operatorname{MEX}(0) + operatorname{MEX}(1) = 1 + 0 = 1$$$. In the second test case the minimal sum is $$$operatorname{MEX}(1111) = 0$$$. In the third test case the minimal sum is $$$operatorname{MEX}(01100) = 2$$$. | 800 | false | true | false | true | false | true | false | false | false | false | 2,812 |
339D | Xenia the beginner programmer has a sequence _a_, consisting of 2_n_ non-negative integers: _a_1,u2009_a_2,u2009...,u2009_a_2_n_. Xenia is currently studying bit operations. To better understand how they work, Xenia decided to calculate some value _v_ for _a_. Namely, it takes several iterations to calculate value _v_. At the first iteration, Xenia writes a new sequence _a_1xa0_or_xa0_a_2,u2009_a_3xa0_or_xa0_a_4,u2009...,u2009_a_2_n_u2009-u20091xa0_or_xa0_a_2_n_, consisting of 2_n_u2009-u20091 elements. In other words, she writes down the bit-wise OR of adjacent elements of sequence _a_. At the second iteration, Xenia writes the bitwise exclusive OR of adjacent elements of the sequence obtained after the first iteration. At the third iteration Xenia writes the bitwise OR of the adjacent elements of the sequence obtained after the second iteration. And so on; the operations of bitwise exclusive OR and bitwise OR alternate. In the end, she obtains a sequence consisting of one element, and that element is _v_. Let's consider an example. Suppose that sequence _a_u2009=u2009(1,u20092,u20093,u20094). Then let's write down all the transformations (1,u20092,u20093,u20094) u2009→u2009 (1xa0_or_xa02u2009=u20093,u20093xa0_or_xa04u2009=u20097) u2009→u2009 (3xa0_xor_xa07u2009=u20094). The result is _v_u2009=u20094. You are given Xenia's initial sequence. But to calculate value _v_ for a given sequence would be too easy, so you are given additional _m_ queries. Each query is a pair of integers _p_,u2009_b_. Query _p_,u2009_b_ means that you need to perform the assignment _a__p_u2009=u2009_b_. After each query, you need to print the new value _v_ for the new sequence _a_. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u200917,u20091u2009≤u2009_m_u2009≤u2009105). The next line contains 2_n_ integers _a_1,u2009_a_2,u2009...,u2009_a_2_n_ (0u2009≤u2009_a__i_u2009<u2009230). Each of the next _m_ lines contains queries. The _i_-th line contains integers _p__i_,u2009_b__i_ (1u2009≤u2009_p__i_u2009≤u20092_n_,u20090u2009≤u2009_b__i_u2009<u2009230) — the _i_-th query. | 1,700 | false | false | false | false | true | false | false | false | false | false | 8,477 |
847H | Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last _n_ minutes and in the _i_-th minute friends will send _a__i_ requests. Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load. Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable. For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp. Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to _n_. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000) — the duration of the load testing. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is the number of requests from friends in the _i_-th minute of the load testing. Note In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests. In the second example it is enough to make one additional request in the third minute, so the answer is 1. In the third example the load already satisfies all conditions described in the statement, so the answer is 0. | 1,600 | false | true | false | false | false | false | false | false | false | false | 6,364 |
1179B | This morning Tolik has understood that while he was sleeping he had invented an incredible problem which will be a perfect fit for Codeforces! But, as a "Discuss tasks" project hasn't been born yet (in English, well), he decides to test a problem and asks his uncle. After a long time thinking, Tolik's uncle hasn't any ideas on how to solve it. But, he doesn't want to tell Tolik about his inability to solve it, so he hasn't found anything better than asking you how to solve this task. In this task you are given a cell field $$$n cdot m$$$, consisting of $$$n$$$ rows and $$$m$$$ columns, where point's coordinates $$$(x, y)$$$ mean it is situated in the $$$x$$$-th row and $$$y$$$-th column, considering numeration from one ($$$1 leq x leq n, 1 leq y leq m$$$). Initially, you stand in the cell $$$(1, 1)$$$. Every move you can jump from cell $$$(x, y)$$$, which you stand in, by any non-zero vector $$$(dx, dy)$$$, thus you will stand in the $$$(x+dx, y+dy)$$$ cell. Obviously, you can't leave the field, but also there is one more important conditionxa0— you're not allowed to use one vector twice. Your task is to visit each cell of the field exactly once (the initial cell is considered as already visited). Tolik's uncle is a very respectful person. Help him to solve this task! Output Print "-1" (without quotes) if it is impossible to visit every cell exactly once. Else print $$$n cdot m$$$ pairs of integers, $$$i$$$-th from them should contain two integers $$$x_i, y_i$$$ ($$$1 leq x_i leq n, 1 leq y_i leq m$$$)xa0— cells of the field in order of visiting, so that all of them are distinct and vectors of jumps between them are distinct too. Notice that the first cell should have $$$(1, 1)$$$ coordinates, according to the statement. Note The vectors from the first example in the order of making jumps are $$$(0, 2), (0, -1), (1, 0), (0, 1), (0, -2)$$$. | 1,800 | false | false | false | false | false | true | false | false | false | false | 4,820 |
732D | Vasiliy has an exam period which will continue for _n_ days. He has to pass exams on _m_ subjects. Subjects are numbered from 1 to _m_. About every day we know exam for which one of _m_ subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number _a__i_xa0— the number of days he should prepare to pass the exam number _i_. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during _a__i_ days for the exam number _i_. He can mix the order of preparation for exams in any way. Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105)xa0— the number of days in the exam period and the number of subjects. The second line contains _n_ integers _d_1,u2009_d_2,u2009...,u2009_d__n_ (0u2009≤u2009_d__i_u2009≤u2009_m_), where _d__i_ is the number of subject, the exam of which can be passed on the day number _i_. If _d__i_ equals 0, it is not allowed to pass any exams on the day number _i_. The third line contains _m_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__m_ (1u2009≤u2009_a__i_u2009≤u2009105), where _a__i_ is the number of days that are needed to prepare before passing the exam on the subject _i_. Output Print one integerxa0— the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1. Examples Input 10 3 0 0 1 2 3 0 2 0 1 2 1 1 4 Note In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it. | 1,700 | false | true | false | false | false | false | false | true | true | false | 6,867 |
1633E | You are given a connected weighted undirected graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. You are asked $$$k$$$ queries about it. Each query consists of a single integer $$$x$$$. For each query, you select a spanning tree in the graph. Let the weights of its edges be $$$w_1, w_2, dots, w_{n-1}$$$. The cost of a spanning tree is $$$sum limits_{i=1}^{n-1} w_i - x$$$ (the sum of absolute differences between the weights and $$$x$$$). The answer to a query is the lowest cost of a spanning tree. The queries are given in a compressed format. The first $$$p$$$ $$$(1 le p le k)$$$ queries $$$q_1, q_2, dots, q_p$$$ are provided explicitly. For queries from $$$p+1$$$ to $$$k$$$, $$$q_j = (q_{j-1} cdot a + b) mod c$$$. Print the xor of answers to all queries. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 50$$$; $$$n - 1 le m le 300$$$)xa0— the number of vertices and the number of edges in the graph. Each of the next $$$m$$$ lines contains a description of an undirected edge: three integers $$$v$$$, $$$u$$$ and $$$w$$$ ($$$1 le v, u le n$$$; $$$v eq u$$$; $$$0 le w le 10^8$$$)xa0— the vertices the edge connects and its weight. Note that there might be multiple edges between a pair of vertices. The edges form a connected graph. The next line contains five integers $$$p, k, a, b$$$ and $$$c$$$ ($$$1 le p le 10^5$$$; $$$p le k le 10^7$$$; $$$0 le a, b le 10^8$$$; $$$1 le c le 10^8$$$)xa0— the number of queries provided explicitly, the total number of queries and parameters to generate the queries. The next line contains $$$p$$$ integers $$$q_1, q_2, dots, q_p$$$ ($$$0 le q_j < c$$$)xa0— the first $$$p$$$ queries. Note The queries in the first example are $$$0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0$$$. The answers are $$$11, 9, 7, 3, 1, 5, 8, 7, 5, 7, 11$$$. The queries in the second example are $$$3, 0, 2, 1, 6, 0, 3, 5, 4, 1$$$. The answers are $$$14, 19, 15, 16, 11, 19, 14, 12, 13, 16$$$. The queries in the third example are $$$75, 0, 0, dots$$$. The answers are $$$50, 150, 150, dots$$$. | 2,400 | true | true | false | false | true | false | false | true | true | true | 2,448 |
877E | Danil decided to earn some money, so he had found a part-time job. The interview have went well, so now he is a light switcher. Danil works in a rooted tree (undirected connected acyclic graph) with _n_ vertices, vertex 1 is the root of the tree. There is a room in each vertex, light can be switched on or off in each room. Danil's duties include switching light in all rooms of the subtree of the vertex. It means that if light is switched on in some room of the subtree, he should switch it off. Otherwise, he should switch it on. Unfortunately (or fortunately), Danil is very lazy. He knows that his boss is not going to personally check the work. Instead, he will send Danil tasks using Workforces personal messages. There are two types of tasks: 1. pow v describes a task to switch lights in the subtree of vertex _v_. 2. get v describes a task to count the number of rooms in the subtree of _v_, in which the light is turned on. Danil should send the answer to his boss using Workforces messages. A subtree of vertex _v_ is a set of vertices for which the shortest path from them to the root passes through _v_. In particular, the vertex _v_ is in the subtree of _v_. Danil is not going to perform his duties. He asks you to write a program, which answers the boss instead of him. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000) — the number of vertices in the tree. The second line contains _n_u2009-u20091 space-separated integers _p_2,u2009_p_3,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009<u2009_i_), where _p__i_ is the ancestor of vertex _i_. The third line contains _n_ space-separated integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (0u2009≤u2009_t__i_u2009≤u20091), where _t__i_ is 1, if the light is turned on in vertex _i_ and 0 otherwise. The fourth line contains a single integer _q_ (1u2009≤u2009_q_u2009≤u2009200u2009000) — the number of tasks. The next _q_ lines are get v or pow v (1u2009≤u2009_v_u2009≤u2009_n_) — the tasks described above. Output For each task get v print the number of rooms in the subtree of _v_, in which the light is turned on. Example Input 4 1 1 1 1 0 0 1 9 get 1 get 2 get 3 get 4 pow 1 get 1 get 2 get 3 get 4 Note The tree before the task pow 1.The tree after the task pow 1. | 2,000 | false | false | false | false | true | false | false | false | false | false | 6,236 |
1999C | As a computer science student, Alex faces a hard challengexa0— showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $$$s$$$ minutes to shower and a day only has $$$m$$$ minutes! He already has $$$n$$$ tasks planned for the day. Task $$$i$$$ is represented as an interval $$$(l_i$$$, $$$r_i)$$$, which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between $$$l_i$$$ and $$$r_i$$$). No two tasks overlap. Given all $$$n$$$ time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least $$$s$$$? In the first test case, Alex can shower for the first $$$3$$$ minutes of the day and not miss any of the tasks. 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 three integers $$$n$$$, $$$s$$$, and $$$m$$$ ($$$1 leq n leq 2 cdot 10^5$$$; $$$1 leq s, m leq 10^9$$$)xa0— the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$0 leq l_i < r_i leq m$$$)xa0— the time interval of the $$$i$$$-th task. No two tasks overlap. Additional constraint on the input: $$$l_i > r_{i-1}$$$ for every $$$i > 1$$$. The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case output "YES" (without quotes) if Alex can take a shower for that given test case, and "NO" (also without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response). Example Input 4 3 3 10 3 5 6 8 9 10 3 3 10 1 2 3 5 6 7 3 3 10 1 2 3 5 6 8 3 4 10 1 2 6 7 8 9 | 800 | false | true | true | false | false | false | false | false | false | false | 269 |
659A | Vasya lives in a round building, whose entrances are numbered sequentially by integers from 1 to _n_. Entrance _n_ and entrance 1 are adjacent. Today Vasya got bored and decided to take a walk in the yard. Vasya lives in entrance _a_ and he decided that during his walk he will move around the house _b_ entrances in the direction of increasing numbers (in this order entrance _n_ should be followed by entrance 1). The negative value of _b_ corresponds to moving _b_ entrances in the order of decreasing numbers (in this order entrance 1 is followed by entrance _n_). If _b_u2009=u20090, then Vasya prefers to walk beside his entrance. Illustration for _n_u2009=u20096, _a_u2009=u20092, _b_u2009=u2009u2009-u20095. Help Vasya to determine the number of the entrance, near which he will be at the end of his walk. Input The single line of the input contains three space-separated integers _n_, _a_ and _b_ (1u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_a_u2009≤u2009_n_,u2009u2009-u2009100u2009≤u2009_b_u2009≤u2009100)xa0— the number of entrances at Vasya's place, the number of his entrance and the length of his walk, respectively. Output Print a single integer _k_ (1u2009≤u2009_k_u2009≤u2009_n_)xa0— the number of the entrance where Vasya will be at the end of his walk. Note The first example is illustrated by the picture in the statements. | 1,000 | true | false | true | false | false | false | false | false | false | false | 7,189 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.