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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
924F | We call a positive integer _x_ a _k_-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to _k_. Each digit should belong to exactly one subset after the split. There are _n_ queries for you. Each query is described with three integers _l_, _r_ and _k_, which mean that you are asked how many integers _x_ between _l_ and _r_ (inclusive) are _k_-beautiful. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20095·104), indicating the number of queries. Each of the next _n_ lines describes a query, containing three integers _l_, _r_ and _k_ (1u2009≤u2009_l_u2009≤u2009_r_u2009≤u20091018, 0u2009≤u2009_k_u2009≤u20099). Output For each query print a single numberxa0— the answer to the query. Examples Input 10 1 100 0 1 100 1 1 100 2 1 100 3 1 100 4 1 100 5 1 100 6 1 100 7 1 100 8 1 100 9 Output 9 28 44 58 70 80 88 94 98 100 Input 10 1 1000 0 1 1000 1 1 1000 2 1 1000 3 1 1000 4 1 1000 5 1 1000 6 1 1000 7 1 1000 8 1 1000 9 Output 135 380 573 721 830 906 955 983 996 1000 Note If 1u2009≤u2009_x_u2009≤u20099, integer _x_ is _k_-beautiful if and only if _x_u2009≤u2009_k_. If 10u2009≤u2009_x_u2009≤u200999, integer _x_u2009=u200910_a_u2009+u2009_b_ is _k_-beautiful if and only if _a_u2009-u2009_b_u2009≤u2009_k_, where _a_ and _b_ are integers between 0 and 9, inclusive. 100 is _k_-beautiful if and only if _k_u2009≥u20091. | 3,200 | false | false | false | true | false | false | false | false | false | false | 6,012 |
794A | A robber has attempted to rob a bank but failed to complete his task. However, he had managed to open all the safes. Oleg the bank client loves money (who doesn't), and decides to take advantage of this failed robbery and steal some money from the safes. There are many safes arranged in a line, where the _i_-th safe from the left is called safe _i_. There are _n_ banknotes left in all the safes in total. The _i_-th banknote is in safe _x__i_. Oleg is now at safe _a_. There are two security guards, one of which guards the safe _b_ such that _b_u2009<u2009_a_, i.e. the first guard is to the left of Oleg. The other guard guards the safe _c_ so that _c_u2009>u2009_a_, i.e. he is to the right of Oleg. The two guards are very lazy, so they do not move. In every second, Oleg can either take all the banknotes from the current safe or move to any of the neighboring safes. However, he cannot visit any safe that is guarded by security guards at any time, becaues he might be charged for stealing. Determine the maximum amount of banknotes Oleg can gather. Input The first line of input contains three space-separated integers, _a_, _b_ and _c_ (1u2009≤u2009_b_u2009<u2009_a_u2009<u2009_c_u2009≤u2009109), denoting the positions of Oleg, the first security guard and the second security guard, respectively. The next line of input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105), denoting the number of banknotes. The next line of input contains _n_ space-separated integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (1u2009≤u2009_x__i_u2009≤u2009109), denoting that the _i_-th banknote is located in the _x__i_-th safe. Note that _x__i_ are not guaranteed to be distinct. Output Output a single integer: the maximum number of banknotes Oleg can take. Examples Input 5 3 7 8 4 7 5 5 3 6 2 8 Note In the first example Oleg can take the banknotes in positions 4, 5, 6 (note that there are 2 banknotes at position 5). Oleg can't take the banknotes in safes 7 and 8 because he can't run into the second security guard. Similarly, Oleg cannot take the banknotes at positions 3 and 2 because he can't run into the first security guard. Thus, he can take a maximum of 4 banknotes. For the second sample, Oleg can't take any banknotes without bumping into any of the security guards. | 800 | false | false | true | false | false | false | true | false | false | false | 6,600 |
1725E | In the country of Dengkleknesia, there are $$$N$$$ factories numbered from $$$1$$$ to $$$N$$$. Factory $$$i$$$ has an electrical coefficient of $$$A_i$$$. There are also $$$N-1$$$ power lines with the $$$j$$$-th power line connecting factory $$$U_j$$$ and factory $$$V_j$$$. It can be guaranteed that each factory in Dengkleknesia is connected to all other factories in Dengkleknesia through one or more power lines. In other words, the collection of factories forms a tree. Each pair of different factories in Dengkleknesia can use one or more existing power lines to transfer electricity to each other. However, each power line needs to be turned on first so that electricity can pass through it. Define $$$f(x, y, z)$$$ as the minimum number of power lines that need to be turned on so that factory $$$x$$$ can make electrical transfers to factory $$$y$$$ and factory $$$z$$$. Also define $$$g(x, y, z)$$$ as the number of distinct prime factors of $$$ ext{GCD}(A_x, A_y, A_z)$$$. To measure the electrical efficiency, you must find the sum of $$$f(x, y, z) imes g(x, y, z)$$$ for all combinations of $$$(x, y, z)$$$ such that $$$1 leq x < y < z leq N$$$. Because the answer can be very large, you just need to output the answer modulo $$$998,244,353$$$. Note: $$$ ext{GCD}(k_1, k_2, k_3)$$$ is the greatest common divisor of $$$k_1$$$, $$$k_2$$$, and $$$k_3$$$, which is the biggest integer that simultaneously divides $$$k_1$$$, $$$k_2$$$, and $$$k_3$$$. Input The first line contains a single integer $$$N$$$ ($$$1 le N le 2 cdot 10^5$$$) — the number of factories in Dengkleknesia. The second line contains $$$N$$$ integers $$$A_1, A_2, dots, A_N$$$ ($$$1 leq A_i leq 2 cdot 10^5$$$) — the electrical coefficients of the factories. The $$$j$$$-th of the next $$$N-1$$$ lines contains two integers $$$U_j$$$ and $$$V_j$$$ ($$$1 le U_j, V_j le N$$$) — a power line that connects cities $$$U_j$$$ and $$$V_j$$$. The collection of factories forms a tree. Output An integer representing the sum of $$$f(x, y, z) imes g(x, y, z)$$$ for all combinations of $$$(x, y, z)$$$ such that $$$1 leq x < y < z leq N$$$, modulo $$$998,244,353$$$ Examples Input 4 6 14 6 6 1 2 2 3 2 4 Note In the first example, the only $$$(x, y, z)$$$ possible is $$$(1, 2, 3)$$$. Because $$$ ext{GCD}(A_1, A_2, A_3) = ext{GCD}(1, 2, 3) = 1$$$ has $$$0$$$ distinct prime factors, therefore $$$f(x, y, z) imes g(x, y, z) = 2 imes 0 = 0$$$. In the second example, all triples $$$(x, y, z)$$$ that satisfy the condition are as follows: $$$(1, 2, 3) ightarrow f(1, 2, 3) imes g(1, 2, 3) = 2 imes 1 = 2$$$ $$$(1, 2, 4) ightarrow f(1, 2, 4) imes g(1, 2, 4) = 2 imes 1 = 2$$$ $$$(1, 3, 4) ightarrow f(1, 3, 4) imes g(1, 3, 4) = 3 imes 2 = 6$$$ $$$(2, 3, 4) ightarrow f(2, 3, 4) imes g(2, 3, 4) = 2 imes 1 = 2$$$ So the electrical efficiency is $$$2 + 2 + 6 + 2 = 12$$$. | 2,500 | true | false | false | true | true | false | false | false | false | false | 1,942 |
706B | Vasiliy likes to rest after a hard work, so you may often meet him in some bar nearby. As all programmers do, he loves the famous drink "Beecola", which can be bought in _n_ different shops in the city. It's known that the price of one bottle in the shop _i_ is equal to _x__i_ coins. Vasiliy plans to buy his favorite drink for _q_ consecutive days. He knows, that on the _i_-th day he will be able to spent _m__i_ coins. Now, for each of the days he want to know in how many different shops he can buy a bottle of "Beecola". Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of shops in the city that sell Vasiliy's favourite drink. The second line contains _n_ integers _x__i_ (1u2009≤u2009_x__i_u2009≤u2009100u2009000)xa0— prices of the bottles of the drink in the _i_-th shop. The third line contains a single integer _q_ (1u2009≤u2009_q_u2009≤u2009100u2009000)xa0— the number of days Vasiliy plans to buy the drink. Then follow _q_ lines each containing one integer _m__i_ (1u2009≤u2009_m__i_u2009≤u2009109)xa0— the number of coins Vasiliy can spent on the _i_-th day. Output Print _q_ integers. The _i_-th of them should be equal to the number of shops where Vasiliy will be able to buy a bottle of the drink on the _i_-th day. Note On the first day, Vasiliy won't be able to buy a drink in any of the shops. On the second day, Vasiliy can buy a drink in the shops 1, 2, 3 and 4. On the third day, Vasiliy can buy a drink only in the shop number 1. Finally, on the last day Vasiliy can buy a drink in any shop. | 1,100 | false | false | true | true | false | false | false | true | false | false | 6,998 |
580D | When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. There were _n_ dishes. Kefa knows that he needs exactly _m_ dishes. But at that, he doesn't want to order the same dish twice to taste as many dishes as possible. Kefa knows that the _i_-th dish gives him _a__i_ units of satisfaction. But some dishes do not go well together and some dishes go very well together. Kefa set to himself _k_ rules of eating food of the following type — if he eats dish _x_ exactly before dish _y_ (there should be no other dishes between _x_ and _y_), then his satisfaction level raises by _c_. Of course, our parrot wants to get some maximal possible satisfaction from going to the restaurant. Help him in this hard task! Input The first line of the input contains three space-separated numbers, _n_, _m_ and _k_ (1u2009≤u2009_m_u2009≤u2009_n_u2009≤u200918, 0u2009≤u2009_k_u2009≤u2009_n_u2009*u2009(_n_u2009-u20091)) — the number of dishes on the menu, the number of portions Kefa needs to eat to get full and the number of eating rules. The second line contains _n_ space-separated numbers _a__i_, (0u2009≤u2009_a__i_u2009≤u2009109) — the satisfaction he gets from the _i_-th dish. Next _k_ lines contain the rules. The _i_-th rule is described by the three numbers _x__i_, _y__i_ and _c__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_, 0u2009≤u2009_c__i_u2009≤u2009109). That means that if you eat dish _x__i_ right before dish _y__i_, then the Kefa's satisfaction increases by _c__i_. It is guaranteed that there are no such pairs of indexes _i_ and _j_ (1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_k_), that _x__i_u2009=u2009_x__j_ and _y__i_u2009=u2009_y__j_. Output In the single line of the output print the maximum satisfaction that Kefa can get from going to the restaurant. Examples Input 4 3 2 1 2 3 4 2 1 5 3 4 2 Note In the first sample it is best to first eat the second dish, then the first one. Then we get one unit of satisfaction for each dish and plus one more for the rule. In the second test the fitting sequences of choice are 4 2 1 or 2 1 4. In both cases we get satisfaction 7 for dishes and also, if we fulfill rule 1, we get an additional satisfaction 5. | 1,800 | false | false | false | true | false | false | false | false | false | false | 7,525 |
638D | A super computer has been built in the Turtle Academy of Sciences. The computer consists of _n_·_m_·_k_ CPUs. The architecture was the paralellepiped of size _n_u2009×u2009_m_u2009×u2009_k_, split into 1u2009×u20091u2009×u20091 cells, each cell contains exactly one CPU. Thus, each CPU can be simultaneously identified as a group of three numbers from the layer number from 1 to _n_, the line number from 1 to _m_ and the column number from 1 to _k_. In the process of the Super Computer's work the CPUs can send each other messages by the famous turtle scheme: CPU (_x_,u2009_y_,u2009_z_) can send messages to CPUs (_x_u2009+u20091,u2009_y_,u2009_z_), (_x_,u2009_y_u2009+u20091,u2009_z_) and (_x_,u2009_y_,u2009_z_u2009+u20091) (of course, if they exist), there is no feedback, that is, CPUs (_x_u2009+u20091,u2009_y_,u2009_z_), (_x_,u2009_y_u2009+u20091,u2009_z_) and (_x_,u2009_y_,u2009_z_u2009+u20091) cannot send messages to CPU (_x_,u2009_y_,u2009_z_). Over time some CPUs broke down and stopped working. Such CPUs cannot send messages, receive messages or serve as intermediates in transmitting messages. We will say that CPU (_a_,u2009_b_,u2009_c_) controls CPU (_d_,u2009_e_,u2009_f_) , if there is a chain of CPUs (_x__i_,u2009_y__i_,u2009_z__i_), such that (_x_1u2009=u2009_a_,u2009_y_1u2009=u2009_b_,u2009_z_1u2009=u2009_c_), (_x__p_u2009=u2009_d_,u2009_y__p_u2009=u2009_e_,u2009_z__p_u2009=u2009_f_) (here and below _p_ is the length of the chain) and the CPU in the chain with number _i_ (_i_u2009<u2009_p_) can send messages to CPU _i_u2009+u20091. Turtles are quite concerned about the denial-proofness of the system of communication between the remaining CPUs. For that they want to know the number of critical CPUs. A CPU (_x_,u2009_y_,u2009_z_) is critical, if turning it off will disrupt some control, that is, if there are two distinctive from (_x_,u2009_y_,u2009_z_) CPUs: (_a_,u2009_b_,u2009_c_) and (_d_,u2009_e_,u2009_f_), such that (_a_,u2009_b_,u2009_c_) controls (_d_,u2009_e_,u2009_f_) before (_x_,u2009_y_,u2009_z_) is turned off and stopped controlling it after the turning off. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u2009100)xa0— the dimensions of the Super Computer. Then _n_ blocks follow, describing the current state of the processes. The blocks correspond to the layers of the Super Computer in the order from 1 to _n_. Each block consists of _m_ lines, _k_ characters in each — the description of a layer in the format of an _m_u2009×u2009_k_ table. Thus, the state of the CPU (_x_,u2009_y_,u2009_z_) is corresponded to the _z_-th character of the _y_-th line of the block number _x_. Character "1" corresponds to a working CPU and character "0" corresponds to a malfunctioning one. The blocks are separated by exactly one empty line. Note In the first sample the whole first layer of CPUs is malfunctional. In the second layer when CPU (2,u20091,u20092) turns off, it disrupts the control by CPU (2,u20091,u20093) over CPU (2,u20091,u20091), and when CPU (2,u20092,u20092) is turned off, it disrupts the control over CPU (2,u20092,u20093) by CPU (2,u20092,u20091). In the second sample all processors except for the corner ones are critical. In the third sample there is not a single processor controlling another processor, so the answer is 0. | 1,800 | false | false | false | false | false | false | true | false | false | true | 7,258 |
1936D | You are given two arrays $$$a$$$ and $$$b$$$ of size $$$n$$$ along with a fixed integer $$$v$$$. An interval $$$$$$. You are given $$$q$$$ queries of two types: "1 i x": assign $$$b_i := x$$$; "2 l r": find the minimum beauty among all good intervals $$$[l_0,r_0]$$$ satisfying $$$l le l_0 le r_0 le r$$$. If there is no suitable good interval, output $$$-1$$$ instead. Please process all queries. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$v$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le v le 10^9$$$). The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). The third line of each testcase contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le 10^9$$$). The fourth line of each testcase contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$). The $$$i$$$-th of the following $$$q$$$ lines contains the description of queries. Each line is of one of two types: "1 i x" ($$$1 le i le n$$$, $$$1 le x le 10^9)$$$; "2 l r" ($$$1 le l le r le n$$$). It is guaranteed that both the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Note In the first test case, $$$a = [2, 1, 3]$$$, $$$b = [2, 2, 3]$$$, and $$$v = 7$$$. The first query is of the second type and has $$$l = 1$$$ and $$$r = 3$$$. The largest interval available is $$$[1, 3]$$$, and its bitwise OR is $$$b_1 mid b_2 mid b_3 = 3$$$ which is less than $$$v$$$. Thus, no good interval exists. The second query asks to change $$$b_2$$$ to $$$5$$$, so $$$b$$$ becomes $$$[2, 5, 3]$$$. The third query is of the second type and has $$$l = 2$$$ and $$$r = 3$$$. There are three possible intervals: $$$[2, 2]$$$, $$$[3, 3]$$$, and $$$[2, 3]$$$. However, $$$b_2 = 5 < v$$$, $$$b_3 = 3 < v$$$. So only the last interval is good: it has $$$b_2 mid b_3 = 7$$$. The answer is thus $$$max(a_2, a_3) = 3$$$. The fourth query is of the second type and has $$$l = 1$$$ and $$$r = 3$$$. There are three good intervals: $$$[1, 2]$$$, $$$[2, 3]$$$, and $$$[1, 3]$$$. Their beauty is $$$2$$$, $$$3$$$, $$$3$$$ correspondingly. The answer is thus $$$2$$$. In the second test case, $$$a = [5, 1, 2, 4]$$$, $$$b = [4, 2, 3, 3]$$$, and $$$v = 5$$$. The first query has $$$l = 1$$$ and $$$r = 4$$$. The only good intervals are: $$$[1, 2]$$$, $$$[1, 3]$$$, $$$[1, 4]$$$. Their beauty is $$$5$$$, $$$5$$$, $$$5$$$ correspondingly. The answer is thus $$$5$$$. | 3,100 | false | true | false | false | true | false | false | true | false | false | 665 |
1851F | Lisa was kidnapped by martians! It okay, because she has watched a lot of TV shows about aliens, so she knows what awaits her. Let's call integer martian if it is a non-negative integer and strictly less than $$$2^k$$$, for example, when $$$k = 12$$$, the numbers $$$51$$$, $$$1960$$$, $$$0$$$ are martian, and the numbers $$$pi$$$, $$$-1$$$, $$$frac{21}{8}$$$, $$$4096$$$ are not. The aliens will give Lisa $$$n$$$ martian numbers $$$a_1, a_2, ldots, a_n$$$. Then they will ask her to name any martian number $$$x$$$. After that, Lisa will select a pair of numbers $$$a_i, a_j$$$ ($$$i eq j$$$) in the given sequence and count $$$(a_i oplus x) & (a_j oplus x)$$$. The operation $$$oplus$$$ means & (23 oplus 17) = (00101_2 oplus 10001_2) & (10111_2 oplus 10001_2) = 10100_2 & 00110_2 = 00100_2 = 4$$$. Lisa is sure that the higher the calculated value, the higher her chances of returning home. Help the girl choose such $$$i, j, x$$$ that maximize the calculated value. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— number of testcases. Each testcase is described by two lines. The first line contains integers $$$n, k$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le k le 30$$$)xa0— the length of the sequence of martian numbers and the value of $$$k$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < 2^k$$$)xa0— a sequence of martian numbers. It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. Output For each testcase, print three integers $$$i, j, x$$$ ($$$1 le i, j le n$$$, $$$i eq j$$$, $$$0 le x < 2^k$$$). The value of $$$(a_i oplus x) & (a_j oplus x)$$$ should be the maximum possible. If there are several solutions, you can print any one. Example Input 10 5 4 3 9 1 4 13 3 1 1 0 1 6 12 144 1580 1024 100 9 13 4 3 7 3 0 4 3 2 0 0 1 2 4 12 2 9 4 6 14 9 4 4 4 5 10 2 2 1 1 0 2 4 11 4 9 4 2 11 10 1 6 9 11 0 5 Output 1 3 14 1 3 0 5 6 4082 2 3 7 1 2 3 1 2 15 4 5 11 1 2 0 1 2 0 2 7 4 Note First testcase: $$$(3 oplus 14) & (1 oplus 14) = (0011_2 oplus 1110_2) & (0001_2 oplus 1110_2) = 1101_2 = 1101_2 & 1111_2 = 1101_2 = 13$$$. Second testcase: $$$(1 oplus 0) & (1 oplus 0) = 1$$$. Third testcase: $$$(9 oplus 4082) & (13 oplus 4082) = 4091$$$. Fourth testcase: $$$(3 oplus 7) & (0 oplus 7) = 4$$$. | 1,800 | true | true | false | false | false | false | false | false | false | false | 1,169 |
626B | Catherine has a deck of _n_ cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions: take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color; take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color. She repeats this process until there is only one card left. What are the possible colors for the final card? Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200)xa0— the total number of cards. The next line contains a string _s_ of length _n_ — the colors of the cards. _s_ contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively. Output Print a single string of up to three charactersxa0— the possible colors of the final card (using the same symbols as the input) in alphabetical order. Note In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card. In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and a red card for a blue card, then exchange the blue card and remaining green card for a red card. In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards. | 1,300 | true | false | false | true | false | true | false | false | false | false | 7,327 |
1017C | Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The only thing Mrs. Smith remembered was that any permutation of $$$n$$$ can be a secret phone number. Only those permutations that minimize secret value might be the phone of her husband. The sequence of $$$n$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once. The secret value of a phone number is defined as the sum of the length of the longest increasing subsequence (LIS) and length of the longest decreasing subsequence (LDS). A subsequence $$$a_{i_1}, a_{i_2}, ldots, a_{i_k}$$$ where $$$1leq i_1 < i_2 < ldots < i_kleq n$$$ is called increasing if $$$a_{i_1} < a_{i_2} < a_{i_3} < ldots < a_{i_k}$$$. If $$$a_{i_1} > a_{i_2} > a_{i_3} > ldots > a_{i_k}$$$, a subsequence is called decreasing. An increasing/decreasing subsequence is called longest if it has maximum length among all increasing/decreasing subsequences. For example, if there is a permutation $$$[6, 4, 1, 7, 2, 3, 5]$$$, LIS of this permutation will be $$$[1, 2, 3, 5]$$$, so the length of LIS is equal to $$$4$$$. LDS can be $$$[6, 4, 1]$$$, $$$[6, 4, 2]$$$, or $$$[6, 4, 3]$$$, so the length of LDS is $$$3$$$. Note, the lengths of LIS and LDS can be different. So please help Mrs. Smith to find a permutation that gives a minimum sum of lengths of LIS and LDS. Note In the first sample, you can build a permutation $$$[3, 4, 1, 2]$$$. LIS is $$$[3, 4]$$$ (or $$$[1, 2]$$$), so the length of LIS is equal to $$$2$$$. LDS can be ony of $$$[3, 1]$$$, $$$[4, 2]$$$, $$$[3, 2]$$$, or $$$[4, 1]$$$. The length of LDS is also equal to $$$2$$$. The sum is equal to $$$4$$$. Note that $$$[3, 4, 1, 2]$$$ is not the only permutation that is valid. In the second sample, you can build a permutation $$$[2, 1]$$$. LIS is $$$[1]$$$ (or $$$[2]$$$), so the length of LIS is equal to $$$1$$$. LDS is $$$[2, 1]$$$, so the length of LDS is equal to $$$2$$$. The sum is equal to $$$3$$$. Note that permutation $$$[1, 2]$$$ is also valid. | 1,600 | false | true | false | false | false | true | false | false | false | false | 5,603 |
251B | Little Petya likes permutations a lot. Recently his mom has presented him permutation _q_1,u2009_q_2,u2009...,u2009_q__n_ of length _n_. A permutation _a_ of length _n_ is a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_), all integers there are distinct. There is only one thing Petya likes more than permutations: playing with little Masha. As it turns out, Masha also has a permutation of length _n_. Petya decided to get the same permutation, whatever the cost may be. For that, he devised a game with the following rules: Before the beginning of the game Petya writes permutation 1,u20092,u2009...,u2009_n_ on the blackboard. After that Petya makes exactly _k_ moves, which are described below. During a move Petya tosses a coin. If the coin shows heads, he performs point 1, if the coin shows tails, he performs point 2. 1. Let's assume that the board contains permutation _p_1,u2009_p_2,u2009...,u2009_p__n_ at the given moment. Then Petya removes the written permutation _p_ from the board and writes another one instead: _p__q_1,u2009_p__q_2,u2009...,u2009_p__q__n_. In other words, Petya applies permutation _q_ (which he has got from his mother) to permutation _p_. 2. All actions are similar to point 1, except that Petya writes permutation _t_ on the board, such that: _t__q__i_u2009=u2009_p__i_ for all _i_ from 1 to _n_. In other words, Petya applies a permutation that is inverse to _q_ to permutation _p_. We know that after the _k_-th move the board contained Masha's permutation _s_1,u2009_s_2,u2009...,u2009_s__n_. Besides, we know that throughout the game process Masha's permutation never occurred on the board before the _k_-th move. Note that the game has exactly _k_ moves, that is, throughout the game the coin was tossed exactly _k_ times. Your task is to determine whether the described situation is possible or else state that Petya was mistaken somewhere. See samples and notes to them for a better understanding. Input The first line contains two integers _n_ and _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009100). The second line contains _n_ space-separated integers _q_1,u2009_q_2,u2009...,u2009_q__n_ (1u2009≤u2009_q__i_u2009≤u2009_n_) — the permutation that Petya's got as a present. The third line contains Masha's permutation _s_, in the similar format. It is guaranteed that the given sequences _q_ and _s_ are correct permutations. Output If the situation that is described in the statement is possible, print "YES" (without the quotes), otherwise print "NO" (without the quotes). Note In the first sample Masha's permutation coincides with the permutation that was written on the board before the beginning of the game. Consequently, that violates the condition that Masha's permutation never occurred on the board before _k_ moves were performed. In the second sample the described situation is possible, in case if after we toss a coin, we get tails. In the third sample the possible coin tossing sequence is: heads-tails-tails. In the fourth sample the possible coin tossing sequence is: heads-heads. | 1,800 | true | false | true | false | false | false | false | false | false | false | 8,830 |
2014H | At such times archery was always the main sport of the day, for the Nottinghamshire yeomen were the best hand at the longbow in all merry England, but this year the Sheriff hesitated... Sheriff of Nottingham has organized a tournament in archery. It's the final round and Robin Hood is playing against Sheriff! There are $$$n$$$ targets in a row numbered from $$$1$$$ to $$$n$$$. When a player shoots target $$$i$$$, their score increases by $$$a_i$$$ and the target $$$i$$$ is destroyed. The game consists of turns and players alternate between whose turn it is. Robin Hood always starts the game, then Sheriff and so on. The game continues until all targets are destroyed. Both players start with score $$$0$$$. At the end of the game, the player with most score wins and the other player loses. If both players have the same score, it's a tie and no one wins or loses. In each turn, the player can shoot any target that wasn't shot before. Both play optimally to get the most score possible. Sheriff of Nottingham has a suspicion that he might lose the game! This cannot happen, you must help Sheriff. Sheriff will pose $$$q$$$ queries, each specifying $$$l$$$ and $$$r$$$. This means that the game would be played only with targets $$$l, l+1, dots, r$$$, as others would be removed by Sheriff before the game starts. For each query $$$l$$$, $$$r$$$, determine whether the Sheriff can not lose the game when only considering the targets $$$l, l+1, dots, r$$$. Input The first line of input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$, $$$q$$$ ($$$1 le n,q le 2cdot10^5$$$)xa0— the number of targets and the queries Sheriff will pose. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^6$$$)xa0— the points for hitting each target. Then follow $$$q$$$ lines, each with two integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$)xa0— the range of the targets that is considered for each query. It is guaranteed that the sum of both $$$n$$$ and $$$q$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output For each query, output "YES", if the Sheriff does not lose the game when only considering the targets $$$l, l+1, dots, r$$$, and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. | 1,900 | false | true | false | false | true | false | false | false | false | false | 164 |
2006A | Iris has a tree rooted at vertex $$$1$$$. Each vertex has a value of $$$mathtt 0$$$ or $$$mathtt 1$$$. Let's consider a leaf of the tree (the vertex $$$1$$$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $$$mathtt{10}$$$ and $$$mathtt{01}$$$ substrings in it. Take the following tree as an example. Green vertices have a value of $$$mathtt 1$$$ while white vertices have a value of $$$mathtt 0$$$. Let's calculate the weight of the leaf $$$5$$$: the formed string is $$$mathtt{10110}$$$. The number of occurrences of substring $$$mathtt{10}$$$ is $$$2$$$, the number of occurrences of substring $$$mathtt{01}$$$ is $$$1$$$, so the difference is $$$2 - 1 = 1$$$. Let's calculate the weight of the leaf $$$6$$$: the formed string is $$$mathtt{101}$$$. The number of occurrences of substring $$$mathtt{10}$$$ is $$$1$$$, the number of occurrences of substring $$$mathtt{01}$$$ is $$$1$$$, so the difference is $$$1 - 1 = 0$$$. The score of a tree is defined as the number of leaves with non-zero weight in the tree. But the values of some vertices haven't been decided and will be given to you as $$$ exttt{?}$$$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $$$ exttt{?}$$$ and changes its value to $$$mathtt{0}$$$ or $$$mathtt{1}$$$, with Iris going first. The game continues until there are no vertices with value $$$mathtt{?}$$$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that. Assuming that both girls play optimally, please determine the final score of the tree. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5cdot 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 10^5$$$)xa0— the number of vertices in the tree. The following $$$n - 1$$$ lines each contain two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n$$$)xa0— denoting an edge between vertices $$$u$$$ and $$$v$$$. It's guaranteed that the given edges form a tree. The last line contains a string $$$s$$$ of length $$$n$$$. The $$$i$$$-th character of $$$s$$$ represents the value of vertex $$$i$$$. It's guaranteed that $$$s$$$ only contains characters $$$mathtt{0}$$$, $$$mathtt{1}$$$ and $$$mathtt{?}$$$. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2cdot 10^5$$$. Output For each test case, output a single integer — the final score of the tree. Example Input 6 4 1 2 1 3 4 1 0101 4 1 2 3 2 2 4 ???0 5 1 2 1 3 2 4 2 5 ?1?01 6 1 2 2 3 3 4 5 3 3 6 ?0???? 5 1 2 1 3 1 4 1 5 11?1? 2 2 1 ?? Note In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf: From vertex $$$1$$$ to vertex $$$2$$$. The string formed by the path is $$$mathtt{01}$$$, so the weight of the leaf is $$$0-1=-1$$$. From vertex $$$1$$$ to vertex $$$3$$$. The string formed by the path is $$$mathtt{00}$$$, so the weight of the leaf is $$$0-0=0$$$. From vertex $$$1$$$ to vertex $$$4$$$. The string formed by the path is $$$mathtt{01}$$$, so the weight of the leaf is $$$0-1=-1$$$. Thus, there are two leaves with non-zero weight, so the score of the tree is $$$2$$$. In the second test case, one of the sequences of optimal choices for the two players can be: Iris chooses to change the value of the vertex $$$3$$$ to $$$mathtt 1$$$. Dora chooses to change the value of the vertex $$$1$$$ to $$$mathtt 0$$$. Iris chooses to change the value of the vertex $$$2$$$ to $$$mathtt 0$$$. The final tree is as follows: The only leaf with non-zero weight is $$$3$$$, so the score of the tree is $$$1$$$. Note that this may not be the only sequence of optimal choices for Iris and Dora. | 1,700 | false | true | false | false | false | true | false | false | false | true | 217 |
869B | Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense, and arise again from the flames and ashes of its predecessorxa0— as is known to many, the phoenix does it like this. The phoenix has a rather long lifespan, and reincarnates itself once every _a_! years. Here _a_! denotes the factorial of integer _a_, that is, _a_!u2009=u20091u2009×u20092u2009×u2009...u2009×u2009_a_. Specifically, 0!u2009=u20091. Koyomi doesn't care much about this, but before he gets into another mess with oddities, he is interested in the number of times the phoenix will reincarnate in a timespan of _b_! years, that is, . Note that when _b_u2009≥u2009_a_ this value is always integer. As the answer can be quite large, it would be enough for Koyomi just to know the last digit of the answer in decimal representation. And you're here to provide Koyomi with this knowledge. Input The first and only line of input contains two space-separated integers _a_ and _b_ (0u2009≤u2009_a_u2009≤u2009_b_u2009≤u20091018). Output Output one line containing a single decimal digitxa0— the last digit of the value that interests Koyomi. Note In the first example, the last digit of is 2; In the second example, the last digit of is 0; In the third example, the last digit of is 2. | 1,100 | true | false | false | false | false | false | false | false | false | false | 6,265 |
436A | The hero of the Cut the Rope game is a little monster named Om Nom. He loves candies. And what a coincidence! He also is the hero of today's problem. One day, Om Nom visited his friend Evan. Evan has _n_ candies of two types (fruit drops and caramel drops), the _i_-th candy hangs at the height of _h__i_ centimeters above the floor of the house, its mass is _m__i_. Om Nom wants to eat as many candies as possible. At the beginning Om Nom can make at most _x_ centimeter high jumps. When Om Nom eats a candy of mass _y_, he gets stronger and the height of his jump increases by _y_ centimeters. What maximum number of candies can Om Nom eat if he never eats two candies of the same type in a row (Om Nom finds it too boring)? Input The first line contains two integers, _n_ and _x_ (1u2009≤u2009_n_,u2009_x_u2009≤u20092000) — the number of sweets Evan has and the initial height of Om Nom's jump. Each of the following _n_ lines contains three integers _t__i_,u2009_h__i_,u2009_m__i_ (0u2009≤u2009_t__i_u2009≤u20091;xa01u2009≤u2009_h__i_,u2009_m__i_u2009≤u20092000) — the type, height and the mass of the _i_-th candy. If number _t__i_ equals 0, then the current candy is a caramel drop, otherwise it is a fruit drop. Output Print a single integer — the maximum number of candies Om Nom can eat. Examples Input 5 3 0 2 4 1 3 1 0 8 3 0 20 10 1 5 5 Note One of the possible ways to eat 4 candies is to eat them in the order: 1, 5, 3, 2. Let's assume the following scenario: 1. Initially, the height of Om Nom's jump equals 3. He can reach candies 1 and 2. Let's assume that he eats candy 1. As the mass of this candy equals 4, the height of his jump will rise to 3u2009+u20094u2009=u20097. 2. Now Om Nom can reach candies 2 and 5. Let's assume that he eats candy 5. Then the height of his jump will be 7u2009+u20095u2009=u200912. 3. At this moment, Om Nom can reach two candies, 2 and 3. He won't eat candy 2 as its type matches the type of the previously eaten candy. Om Nom eats candy 3, the height of his jump is 12u2009+u20093u2009=u200915. 4. Om Nom eats candy 2, the height of his jump is 15u2009+u20091u2009=u200916. He cannot reach candy 4. | 1,500 | false | true | false | false | false | false | false | false | false | false | 8,097 |
1509A | Sayaka Saeki is a member of the student council, which has $$$n$$$ other members (excluding Sayaka). The $$$i$$$-th member has a height of $$$a_i$$$ millimeters. It's the end of the school year and Sayaka wants to take a picture of all other members of the student council. Being the hard-working and perfectionist girl as she is, she wants to arrange all the members in a line such that the amount of photogenic consecutive pairs of members is as large as possible. A pair of two consecutive members $$$u$$$ and $$$v$$$ on a line is considered photogenic if their average height is an integer, i.e. $$$frac{a_u + a_v}{2}$$$ is an integer. Help Sayaka arrange the other members to maximize the number of photogenic consecutive pairs. Input The first line contains a single integer $$$t$$$ ($$$1le tle 500$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2000$$$) xa0— the number of other council members. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$) xa0— the heights of each of the other members in millimeters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output on one line $$$n$$$ integers representing the heights of the other members in the order, which gives the largest number of photogenic consecutive pairs. If there are multiple such orders, output any of them. Example Input 4 3 1 1 2 3 1 1 1 8 10 9 13 15 3 16 9 13 2 18 9 Output 1 1 2 1 1 1 13 9 13 15 3 9 16 10 9 18 Note In the first test case, there is one photogenic pair: $$$(1, 1)$$$ is photogenic, as $$$frac{1+1}{2}=1$$$ is integer, while $$$(1, 2)$$$ isn't, as $$$frac{1+2}{2}=1.5$$$ isn't integer. In the second test case, both pairs are photogenic. | 800 | false | false | false | false | false | true | false | false | false | false | 3,130 |
118A | Problem - 118A - 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 *1000 No tag edit access → Contest materials ") | 1,000 | false | false | true | false | false | false | false | false | false | false | 9,416 |
1611G | Polycarp has a rectangular field of $$$n imes m$$$ cells (the size of the $$$n cdot m$$$ field does not exceed $$$10^6$$$ cells, $$$m ge 2$$$), in each cell of which there can be candy. There are $$$n$$$ rows and $$$m$$$ columns in the field. Let's denote a cell with coordinates $$$x$$$ vertically and $$$y$$$ horizontally by $$$(x, y)$$$. Then the top-left cell will be denoted as $$$(1, 1)$$$, and the bottom-right cell will be denoted as $$$(n, m)$$$. If there is candy in the cell, then the cell is marked with the symbol '1', otherwisexa0— with the symbol '0'. Polycarp made a Robot that can collect candy. The Robot can move from $$$(x, y)$$$ either to $$$(x+1, y+1)$$$, or to $$$(x+1, y-1)$$$. If the Robot is in a cell that contains candy, it takes it. While there is at least one candy on the field, the following procedure is executed: Polycarp puts the Robot in an arbitrary cell on the topmost row of the field. He himself chooses in which cell to place the Robot. It is allowed to put the Robot in the same cell multiple times. The Robot moves across the field and collects candies. He controls the Robot. When the Robot leaves the field, Polycarp takes it. If there are still candies left, Polycarp repeats the procedure. Find the minimum number of times Polycarp needs to put the Robot on the topmost row of the field in order to collect all the candies. It is guaranteed that Polycarp can always collect all the candies. Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of input data sets in the test. Before each input data, there is a blank line in the test. Next is a line that contains integers $$$n$$$ and $$$m$$$ ($$$2 le m$$$, $$$2 le n cdot m le 10^6$$$) — field sizes. This is followed by $$$n$$$ lines, $$$i$$$-th of which describes the $$$i$$$-th line of the field. Each of them is a string of size $$$m$$$ characters: the symbol '1' corresponds to a cell with candy, the symbol '0'xa0— an empty cell. It is guaranteed that the sum of $$$n cdot m$$$ values for all input data sets in the test does not exceed $$$10^6$$$. Output Print $$$t$$$ lines, each line should contain the answer to the corresponding set of input data: the minimum number of times Polycarpus needs to put the Robot on the topmost row of the field in order to collect all the candies. Example Input 4 2 2 00 00 3 3 100 000 101 4 5 01000 00001 00010 10000 3 3 111 111 111 Note In the first set Polycarp may not put the Robot on the field at all, so the answer "0" In the second set, Polycarp will need to place the robot on the field twice. The Robot can collect candies like this: for the first time Polycarp puts the Robot in the cell $$$(1, 1)$$$ and collects candies at the positions $$$(1, 1)$$$ and $$$(3, 3)$$$. The second time Polycarp can again put the Robot in $$$(1, 1)$$$, and then the Robot will move first to $$$(2,2)$$$, then to $$$(3, 1)$$$ and collect the last candy. In the fourth set, you can show that the Robot cannot collect all the candies in three passes. | 2,500 | false | true | false | false | true | false | false | false | false | false | 2,584 |
1829F | A snowflake graph is generated from two integers $$$x$$$ and $$$y$$$, both greater than $$$1$$$, as follows: Start with one central vertex. Connect $$$x$$$ new vertices to this central vertex. Connect $$$y$$$ new vertices to each of these $$$x$$$ vertices. For example, below is a snowflake graph for $$$x=5$$$ and $$$y=3$$$. The snowflake graph above has a central vertex $$$15$$$, then $$$x=5$$$ vertices attached to it ($$$3$$$, $$$6$$$, $$$7$$$, $$$8$$$, and $$$20$$$), and then $$$y=3$$$ vertices attached to each of those. Given a snowflake graph, determine the values of $$$x$$$ and $$$y$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 200$$$; $$$1 leq m leq minleft(1000, frac{n(n-1)}{2} ight)$$$)xa0— the number of vertices and edges in the graph, respectively. The next $$$m$$$ lines each contain two integers each $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n$$$, $$$u eq v$$$)xa0— the numbers of vertices connected by an edge. The graph does not contain multiple edges and self-loops. It is guaranteed that this graph is a snowflake graph for some integers $$$x$$$ and $$$y$$$ both greater than $$$1$$$. Output For each test case, on a separate line output the values of $$$x$$$ and $$$y$$$, in that order, separated by a space. Example Input 3 21 20 21 20 5 20 13 20 1 3 11 3 10 3 4 8 19 8 14 8 9 7 12 7 17 7 18 6 16 6 2 6 6 15 7 15 8 15 20 15 3 15 7 6 1 2 1 3 2 4 2 5 3 6 3 7 9 8 9 3 3 6 6 2 2 1 5 2 2 7 4 3 3 8 Note The first test case is pictured in the statement. Note that the output 3 5 is incorrect, since $$$x$$$ should be output before $$$y$$$. | 1,300 | true | false | false | false | false | false | false | false | false | true | 1,309 |
385E | Our bear's forest has a checkered field. The checkered field is an _n_u2009×u2009_n_ table, the rows are numbered from 1 to _n_ from top to bottom, the columns are numbered from 1 to _n_ from left to right. Let's denote a cell of the field on the intersection of row _x_ and column _y_ by record (_x_,u2009_y_). Each cell of the field contains growing raspberry, at that, the cell (_x_,u2009_y_) of the field contains _x_u2009+u2009_y_ raspberry bushes. The bear came out to walk across the field. At the beginning of the walk his speed is (_dx_,u2009_dy_). Then the bear spends exactly _t_ seconds on the field. Each second the following takes place: Let's suppose that at the current moment the bear is in cell (_x_,u2009_y_). First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry from _k_ bushes, he increases each component of his speed by _k_. In other words, if before eating the _k_ bushes of raspberry his speed was (_dx_,u2009_dy_), then after eating the berry his speed equals (_dx_u2009+u2009_k_,u2009_dy_u2009+u2009_k_). Let's denote the current speed of the bear (_dx_,u2009_dy_) (it was increased after the previous step). Then the bear moves from cell (_x_,u2009_y_) to cell (((_x_u2009+u2009_dx_u2009-u20091)xa0_mod_xa0_n_)u2009+u20091,u2009((_y_u2009+u2009_dy_u2009-u20091)xa0_mod_xa0_n_)u2009+u20091). Then one additional raspberry bush grows in each cell of the field. You task is to predict the bear's actions. Find the cell he ends up in if he starts from cell (_sx_,u2009_sy_). Assume that each bush has infinitely much raspberry and the bear will never eat all of it. Input The first line of the input contains six space-separated integers: _n_, _sx_, _sy_, _dx_, _dy_, _t_ (1u2009≤u2009_n_u2009≤u2009109;xa01u2009≤u2009_sx_,u2009_sy_u2009≤u2009_n_;xa0u2009-u2009100u2009≤u2009_dx_,u2009_dy_u2009≤u2009100;xa00u2009≤u2009_t_u2009≤u20091018). Output Print two integers — the coordinates of the cell the bear will end up in after _t_ seconds. Note Operation _a_xa0_mod_xa0_b_ means taking the remainder after dividing _a_ by _b_. Note that the result of the operation is always non-negative. For example, (u2009-u20091)xa0_mod_xa03u2009=u20092. In the first sample before the first move the speed vector will equal (3,4) and the bear will get to cell (4,1). Before the second move the speed vector will equal (9,10) and he bear will get to cell (3,1). Don't forget that at the second move, the number of berry bushes increased by 1. In the second sample before the first move the speed vector will equal (1,1) and the bear will get to cell (1,1). Before the second move, the speed vector will equal (4,4) and the bear will get to cell (1,1). Don't forget that at the second move, the number of berry bushes increased by 1. | 2,300 | true | false | false | false | false | false | false | false | false | false | 8,287 |
627B | A factory produces thimbles in bulk. Typically, it can produce up to _a_ thimbles a day. However, some of the machinery is defective, so it can currently only produce _b_ thimbles each day. The factory intends to choose a _k_-day period to do maintenance and construction; it cannot produce any thimbles during this time, but will be restored to its full production of _a_ thimbles per day after the _k_ days are complete. Initially, no orders are pending. The factory receives updates of the form _d__i_, _a__i_, indicating that _a__i_ new orders have been placed for the _d__i_-th day. Each order requires a single thimble to be produced on precisely the specified day. The factory may opt to fill as many or as few of the orders in a single batch as it likes. As orders come in, the factory owner would like to know the maximum number of orders he will be able to fill if he starts repairs on a given day _p__i_. Help the owner answer his questions. Input The first line contains five integers _n_, _k_, _a_, _b_, and _q_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009200u2009000, 1u2009≤u2009_b_u2009<u2009_a_u2009≤u200910 000, 1u2009≤u2009_q_u2009≤u2009200u2009000)xa0— the number of days, the length of the repair time, the production rates of the factory, and the number of updates, respectively. The next _q_ lines contain the descriptions of the queries. Each query is of one of the following two forms: 1 _d__i_ _a__i_ (1u2009≤u2009_d__i_u2009≤u2009_n_, 1u2009≤u2009_a__i_u2009≤u200910 000), representing an update of _a__i_ orders on day _d__i_, or 2 _p__i_ (1u2009≤u2009_p__i_u2009≤u2009_n_u2009-u2009_k_u2009+u20091), representing a question: at the moment, how many orders could be filled if the factory decided to commence repairs on day _p__i_? It's guaranteed that the input will contain at least one query of the second type. Output For each query of the second type, print a line containing a single integer — the maximum number of orders that the factory can fill over all _n_ days. Examples Input 5 2 2 1 8 1 1 2 1 5 3 1 2 1 2 2 1 4 2 1 3 2 2 1 2 3 Input 5 4 10 1 6 1 1 5 1 5 5 1 3 2 1 5 2 2 1 2 2 Note Consider the first sample. We produce up to 1 thimble a day currently and will produce up to 2 thimbles a day after repairs. Repairs take 2 days. For the first question, we are able to fill 1 order on day 1, no orders on days 2 and 3 since we are repairing, no orders on day 4 since no thimbles have been ordered for that day, and 2 orders for day 5 since we are limited to our production capacity, for a total of 3 orders filled. For the third question, we are able to fill 1 order on day 1, 1 order on day 2, and 2 orders on day 5, for a total of 4 orders. | 1,700 | false | false | false | false | true | false | false | false | false | false | 7,320 |
68B | Problem - 68B - Codeforces =============== xa0 _k_ percent of it is lost. That is, if _x_ units were transferred from one accumulator to the other, amount of energy in the first one decreased by _x_ units and in other increased by units. Your task is to help Petya find what maximum equal amount of energy can be stored in each accumulator after the transfers. Input First line of the input contains two integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u200910000,u20090u2009≤u2009_k_u2009≤u200999) — number of accumulators and the percent of energy that is lost during transfers. Next line contains _n_ integers _a_1,u2009_a_2,u2009... ,u2009_a__n_ — amounts of energy in the first, second, .., _n_-th accumulator respectively (0u2009≤u2009_a__i_u2009≤u20091000,u20091u2009≤u2009_i_u2009≤u2009_n_). Output Output maximum possible amount of energy that can remain in each of accumulators after the transfers of energy. The absolute or relative error in the answer should not exceed 10u2009-u20096. Examples Input 3 50 4 2 1 Output 2.000000000 Input 2 90 1 11 Output 1.909090909 | 1,600 | false | false | false | false | false | false | false | true | false | false | 9,630 |
978A | Petya has an array $$$a$$$ consisting of $$$n$$$ integers. He wants to remove duplicate (equal) elements. Petya wants to leave only the rightmost entry (occurrence) for each element of the array. The relative order of the remaining unique elements should not be changed. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 50$$$) — the number of elements in Petya's array. The following line contains a sequence $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 1,000$$$) — the Petya's array. Output In the first line print integer $$$x$$$ — the number of elements which will be left in Petya's array after he removed the duplicates. In the second line print $$$x$$$ integers separated with a space — Petya's array after he removed the duplicates. For each unique element only the rightmost entry should be left. Note In the first example you should remove two integers $$$1$$$, which are in the positions $$$1$$$ and $$$4$$$. Also you should remove the integer $$$5$$$, which is in the position $$$2$$$. In the second example you should remove integer $$$2$$$, which is in the position $$$1$$$, and two integers $$$4$$$, which are in the positions $$$2$$$ and $$$4$$$. In the third example you should remove four integers $$$6$$$, which are in the positions $$$1$$$, $$$2$$$, $$$3$$$ and $$$4$$$. | 800 | false | false | true | false | false | false | false | false | false | false | 5,818 |
480E | Petya's been bored at work and he is killing the time by watching the parking lot at the office. The parking lot looks from above like an _n_u2009×u2009_m_ table (a cell of the table corresponds to a single parking spot). Some spots in the parking lot are taken, others are empty. Petya watches cars riding into the parking lot one by one. After a car settles down at the parking spot, Petya amuzes himself by counting what maximum square of empty spots (i.e. a square subtable) can be seen on the parking lot if we look at it from above. Also, he takes notes of the square's size (side length) in his notebook. You task is: given the state of the parking lot at the initial moment of time and the information about where the arriving cars park, restore what Petya wrote in his notebook. It is midday, so nobody leaves the lot. Input The first line contains three integers _n_, _m_ and _k_ — the sizes of the parking lot and the number of arriving cars after Petya started his watch (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u20092000). Each of the following _n_ lines contains _m_ characters 'X' and '.', where 'X' means a taken spot and '.' means an empty spot. Each of the next _k_ lines contains a pair of integers _x__i_, _y__i_ — the number of row and column of the spot the corresponding car takes (1u2009≤u2009_x__i_u2009≤u2009_n_, 1u2009≤u2009_y__i_u2009≤u2009_m_). It is guaranteed that this place was empty. You can assume that a car enters a parking lot only after the previous car successfully finds a spot. Output Print _k_ integers — the length of the side of the maximum square of empty spots after the corresponding car has entered the parking lot. Examples Input 7 8 4 ........ X.....X. ........ ........ .X...... ........ ........ 1 5 6 4 3 5 4 6 | 2,800 | false | false | false | false | true | false | false | false | false | false | 7,910 |
770A | Innokentiy decides to change the password in the social net "Contact!", but he is too lazy to invent a new password by himself. That is why he needs your help. Innokentiy decides that new password should satisfy the following conditions: the length of the password must be equal to _n_, the password should consist only of lowercase Latin letters, the number of distinct symbols in the password must be equal to _k_, any two consecutive symbols in the password must be distinct. Your task is to help Innokentiy and to invent a new password which will satisfy all given conditions. Input The first line contains two positive integers _n_ and _k_ (2u2009≤u2009_n_u2009≤u2009100, 2u2009≤u2009_k_u2009≤u2009_min_(_n_,u200926)) — the length of the password and the number of distinct symbols in it. Pay attention that a desired new password always exists. Output Print any password which satisfies all conditions given by Innokentiy. Note In the first test there is one of the appropriate new passwords — java, because its length is equal to 4 and 3 distinct lowercase letters a, j and v are used in it. In the second test there is one of the appropriate new passwords — python, because its length is equal to 6 and it consists of 6 distinct lowercase letters. In the third test there is one of the appropriate new passwords — phphp, because its length is equal to 5 and 2 distinct lowercase letters p and h are used in it. Pay attention the condition that no two identical symbols are consecutive is correct for all appropriate passwords in tests. | 800 | false | false | true | false | false | false | false | false | false | false | 6,702 |
1218I | For her birthday Alice received an interesting gift from her friends – The Light Square. The Light Square game is played on an $$$N imes N$$$ lightbulbs square board with a magical lightbulb bar of size $$$N imes 1$$$ that has magical properties. At the start of the game some lights on the square board and magical bar are turned on. The goal of the game is to transform the starting light square board pattern into some other pattern using the magical bar without rotating the square board. The magical bar works as follows: It can be placed on any row or column The orientation of the magical lightbulb must be left to right or top to bottom for it to keep its magical properties The entire bar needs to be fully placed on a board The lights of the magical bar never change If the light on the magical bar is the same as the light of the square it is placed on it will switch the light on the square board off, otherwise it will switch the light on The magical bar can be used an infinite number of times Alice has a hard time transforming her square board into the pattern Bob gave her. Can you help her transform the board or let her know it is impossible? If there are multiple solutions print any. Input The first line contains one positive integer number $$$N (1 leq N leq 2000)$$$ representing the size of the square board. The next $$$N$$$ lines are strings of length $$$N$$$ consisting of 1's and 0's representing the initial state of the square board starting from the top row. If the character in a string is 1 it means the light is turned on, otherwise it is off. The next $$$N$$$ lines are strings of length $$$N$$$ consisting of 1's and 0's representing the desired state of the square board starting from the top row that was given to Alice by Bob. The last line is one string of length $$$N$$$ consisting of 1's and 0's representing the pattern of the magical bar in a left to right order. Output Transform the instructions for Alice in order to transform the square board into the pattern Bob gave her. The first line of the output contains an integer number $$$M (0 leq M leq 10^5$$$) representing the number of times Alice will need to apply the magical bar. The next $$$M$$$ lines are of the form "col $$$X$$$" or "row $$$X$$$", where $$$X$$$ is $$$0$$$-based index of the matrix, meaning the magical bar should be applied to either row $$$X$$$ or column $$$X$$$. If there is no solution, print only -1. In case of multiple solutions print any correct one. Examples Input 3 110 011 100 100 011 100 100 Output 3 row 0 col 0 col 1 Note Example 1: It is impossible to transform square board from one format to another Example 2: Magic bar can be applied on first row or column. | 2,100 | false | true | false | false | false | false | false | false | false | false | 4,576 |
253E | Let's consider a network printer that functions like that. It starts working at time 0. In each second it can print one page of a text. At some moments of time the printer receives printing tasks. We know that a printer received _n_ tasks. Let's number the tasks by consecutive integers from 1 to _n_. Then the task number _i_ is characterised by three integers: _t__i_ is the time when the task came, _s__i_ is the task's volume (in pages) and _p__i_ is the task's priority. The priorities of all tasks are distinct. When the printer receives a task, the task goes to the queue and remains there until all pages from this task are printed. The printer chooses a page to print each time when it either stops printing some page or when it is free and receives a new task. Among all tasks that are in the queue at this moment, the printer chooses the task with the highest priority and next second prints an unprinted page from this task. You can assume that a task goes to the queue immediately, that's why if a task has just arrived by time _t_, the printer can already choose it for printing. You are given full information about all tasks except for one: you don't know this task's priority. However, we know the time when the last page from this task was finished printing. Given this information, find the unknown priority value and determine the moments of time when the printer finished printing each task. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u200950000). Next _n_ lines describe the tasks. The _i_-th of these lines contains three integers _t__i_, _s__i_ and _p__i_, separated by single spaces (0u2009≤u2009_t__i_u2009≤u2009109,u20091u2009≤u2009_s__i_,u2009_p__i_u2009≤u2009109). Exactly one task (let's assume that his number is _x_) has number -1 written instead of the priority. All priorities are different. The last line contains integer _T_ — the time when the printer finished printing the last page of task _x_ (1u2009≤u2009_T_u2009≤u20091015). Numbers _t__i_ are not necessarily distinct. The tasks in the input are written in the arbitrary order. Output In the first line print integer _p__x_ — the priority of the task number _x_ (1u2009≤u2009_p__x_u2009≤u2009109, remember that all priorities should be distinct). Then print _n_ integers, the _i_-th of them represents the moment of time when the last page of the task number _i_ finished printing. It is guaranteed that at least one solution exists. If there are multiple solutions, print any of them. Note Let's consider the first test case. Let's assume that the unknown priority equals 4, then the printer's actions for each second are as follows: the beginning of the 1-st second (time 0). The queue has task 2. The printer prints the first page of this task; the beginning of the 2-nd second (time 1). The queue has tasks 2 and 3. The printer prints the first page of task 3; the beginning of the 3-rd second (time 2). The queue has tasks 2 and 3. The printer prints the second page of task 3; the beginning of the 4-th second (time 3). The queue has tasks 2 and 3. The printer prints the third (last) page of task 3. Thus, by the end of the 4-th second this task will have been printed; the beginning of the 5-th second (time 4). The queue has tasks 2 and 1. The printer prints the first page of task 1; the beginning of the 6-th second (time 5). The queue has tasks 2 and 1. The printer prints the second page of task 1; the beginning of the 7-th second (time 6). The queue has tasks 2 and 1. The printer prints the third (last) page of task 1. Thus, by the end of the 7-th second this task will have been printed; the beginning of the 8-th second (time 7). The queue has task 2. The printer prints the second (last) page of task 2. Thus, by the end of the 8-th second this task will have been printed. In the end, task number 1 will have been printed by the end of the 7-th second, as was required. And tasks 2 and 3 are printed by the end of the of the 8-th and the 4-th second correspondingly. | 2,200 | false | false | true | false | true | false | false | true | true | false | 8,820 |
1272C | Recently, Norge found a string $$$s = s_1 s_2 ldots s_n$$$ consisting of $$$n$$$ lowercase Latin letters. As an exercise to improve his typing speed, he decided to type all substrings of the string $$$s$$$. Yes, all $$$frac{n (n + 1)}{2}$$$ of them! A substring of $$$s$$$ is a non-empty string $$$x = s[a ldots b] = s_{a} s_{a + 1} ldots s_{b}$$$ ($$$1 leq a leq b leq n$$$). For example, "auto" and "ton" are substrings of "automaton". Shortly after the start of the exercise, Norge realized that his keyboard was broken, namely, he could use only $$$k$$$ Latin letters $$$c_1, c_2, ldots, c_k$$$ out of $$$26$$$. After that, Norge became interested in how many substrings of the string $$$s$$$ he could still type using his broken keyboard. Help him to find this number. Input The first line contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 2 cdot 10^5$$$, $$$1 leq k leq 26$$$) — the length of the string $$$s$$$ and the number of Latin letters still available on the keyboard. The second line contains the string $$$s$$$ consisting of exactly $$$n$$$ lowercase Latin letters. The third line contains $$$k$$$ space-separated distinct lowercase Latin letters $$$c_1, c_2, ldots, c_k$$$ — the letters still available on the keyboard. Output Print a single number — the number of substrings of $$$s$$$ that can be typed using only available letters $$$c_1, c_2, ldots, c_k$$$. Examples Input 10 3 sadfaasdda f a d Note In the first example Norge can print substrings $$$s[1ldots2]$$$, $$$s[2ldots3]$$$, $$$s[1ldots3]$$$, $$$s[1ldots1]$$$, $$$s[2ldots2]$$$, $$$s[3ldots3]$$$, $$$s[5ldots6]$$$, $$$s[6ldots7]$$$, $$$s[5ldots7]$$$, $$$s[5ldots5]$$$, $$$s[6ldots6]$$$, $$$s[7ldots7]$$$. | 1,200 | false | false | true | true | false | false | false | false | false | false | 4,327 |
652E | Johnny is playing a well-known computer game. The game are in some country, where the player can freely travel, pass quests and gain an experience. In that country there are _n_ islands and _m_ bridges between them, so you can travel from any island to any other. In the middle of some bridges are lying ancient powerful artifacts. Johnny is not interested in artifacts, but he can get some money by selling some artifact. At the start Johnny is in the island _a_ and the artifact-dealer is in the island _b_ (possibly they are on the same island). Johnny wants to find some artifact, come to the dealer and sell it. The only difficulty is that bridges are too old and destroying right after passing over them. Johnnie's character can't swim, fly and teleport, so the problem became too difficult. Note that Johnny can't pass the half of the bridge, collect the artifact and return to the same island. Determine if Johnny can find some artifact and sell it. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u20093·105, 0u2009≤u2009_m_u2009≤u20093·105) — the number of islands and bridges in the game. Each of the next _m_ lines contains the description of the bridge — three integers _x__i_, _y__i_, _z__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_, _x__i_u2009≠u2009_y__i_, 0u2009≤u2009_z__i_u2009≤u20091), where _x__i_ and _y__i_ are the islands connected by the _i_-th bridge, _z__i_ equals to one if that bridge contains an artifact and to zero otherwise. There are no more than one bridge between any pair of islands. It is guaranteed that it's possible to travel between any pair of islands. The last line contains two integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_) — the islands where are Johnny and the artifact-dealer respectively. Output If Johnny can find some artifact and sell it print the only word "YES" (without quotes). Otherwise print the word "NO" (without quotes). Examples Input 6 7 1 2 0 2 3 0 3 1 0 3 4 1 4 5 0 5 6 0 6 4 0 1 6 Input 5 4 1 2 0 2 3 0 3 4 0 2 5 1 1 4 Input 5 6 1 2 0 2 3 0 3 1 0 3 4 0 4 5 1 5 3 0 1 2 | 2,300 | false | false | false | false | false | false | false | false | false | true | 7,206 |
1213A | You are given $$$n$$$ chips on a number line. The $$$i$$$-th chip is placed at the integer coordinate $$$x_i$$$. Some chips can have equal coordinates. You can perform each of the two following types of moves any (possibly, zero) number of times on any chip: Move the chip $$$i$$$ by $$$2$$$ to the left or $$$2$$$ to the right for free (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 2$$$ or with $$$x_i + 2$$$); move the chip $$$i$$$ by $$$1$$$ to the left or $$$1$$$ to the right and pay one coin for this move (i.e. replace the current coordinate $$$x_i$$$ with $$$x_i - 1$$$ or with $$$x_i + 1$$$). Note that it's allowed to move chips to any integer coordinate, including negative and zero. Your task is to find the minimum total number of coins required to move all $$$n$$$ chips to the same coordinate (i.e. all $$$x_i$$$ should be equal after some sequence of moves). Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 100$$$) — the number of chips. The second line of the input contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$1 le x_i le 10^9$$$), where $$$x_i$$$ is the coordinate of the $$$i$$$-th chip. Output Print one integer — the minimum total number of coins required to move all $$$n$$$ chips to the same coordinate. Note In the first example you need to move the first chip by $$$2$$$ to the right and the second chip by $$$1$$$ to the right or move the third chip by $$$2$$$ to the left and the second chip by $$$1$$$ to the left so the answer is $$$1$$$. In the second example you need to move two chips with coordinate $$$3$$$ by $$$1$$$ to the left so the answer is $$$2$$$. | 900 | true | false | false | false | false | false | false | false | false | false | 4,619 |
1205F | Define the beauty of a permutation of numbers from $$$1$$$ to $$$n$$$ $$$(p_1, p_2, dots, p_n)$$$ as number of pairs $$$(L, R)$$$ such that $$$1 le L le R le n$$$ and numbers $$$p_L, p_{L+1}, dots, p_R$$$ are consecutive $$$R-L+1$$$ numbers in some order. For example, the beauty of the permutation $$$(1, 2, 5, 3, 4)$$$ equals $$$9$$$, and segments, corresponding to pairs, are $$$[1]$$$, $$$[2]$$$, $$$[5]$$$, $$$[4]$$$, $$$[3]$$$, $$$[1, 2]$$$, $$$[3, 4]$$$, $$$[5, 3, 4]$$$, $$$[1, 2, 5, 3, 4]$$$. Answer $$$q$$$ independent queries. In each query, you will be given integers $$$n$$$ and $$$k$$$. Determine if there exists a permutation of numbers from $$$1$$$ to $$$n$$$ with beauty equal to $$$k$$$, and if there exists, output one of them. Input The first line contains a single integer $$$q$$$ ($$$1le q le 10,000$$$)xa0— the number of queries. Follow $$$q$$$ lines. Each line contains two integers $$$n$$$, $$$k$$$ ($$$1 le n le 100$$$, $$$1 le k le frac{n(n+1)}{2}$$$)xa0— the length of permutation and needed beauty respectively. Output For a query output "NO", if such a permutation doesn't exist. Otherwise, output "YES", and in the next line output $$$n$$$ numbersxa0— elements of permutation in the right order. Examples Output YES 1 YES 2 4 1 5 3 NO YES 2 3 1 4 5 Note Let's look at the first example. The first query: in $$$(1)$$$ there is only one segment consisting of consecutive numbers — the entire permutation. The second query: in $$$(2, 4, 1, 5, 3)$$$ there are $$$6$$$ such segments: $$$[2]$$$, $$$[4]$$$, $$$[1]$$$, $$$[5]$$$, $$$[3]$$$, $$$[2, 4, 1, 5, 3]$$$. There is no such permutation for the second query. The fourth query: in $$$(2, 3, 1, 4, 5)$$$ there are $$$10$$$ such segments: $$$[2]$$$, $$$[3]$$$, $$$[1]$$$, $$$[4]$$$, $$$[5]$$$, $$$[2, 3]$$$, $$$[2, 3, 1]$$$, $$$[2, 3, 1, 4]$$$, $$$[4, 5]$$$, $$$[2, 3, 1, 4, 5]$$$. | 3,400 | true | false | false | false | false | true | false | false | false | false | 4,664 |
1095F | You are given an undirected graph consisting of $$$n$$$ vertices. A number is written on each vertex; the number on vertex $$$i$$$ is $$$a_i$$$. Initially there are no edges in the graph. You may add some edges to this graph, but you have to pay for them. The cost of adding an edge between vertices $$$x$$$ and $$$y$$$ is $$$a_x + a_y$$$ coins. There are also $$$m$$$ special offers, each of them is denoted by three numbers $$$x$$$, $$$y$$$ and $$$w$$$, and means that you can add an edge connecting vertices $$$x$$$ and $$$y$$$ and pay $$$w$$$ coins for it. You don't have to use special offers: if there is a pair of vertices $$$x$$$ and $$$y$$$ that has a special offer associated with it, you still may connect these two vertices paying $$$a_x + a_y$$$ coins for it. What is the minimum number of coins you have to spend to make the graph connected? Recall that a graph is connected if it's possible to get from any vertex to any other vertex using only the edges belonging to this graph. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$0 le m le 2 cdot 10^5$$$) — the number of vertices in the graph and the number of special offers, respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^{12}$$$) — the numbers written on the vertices. Then $$$m$$$ lines follow, each containing three integers $$$x$$$, $$$y$$$ and $$$w$$$ ($$$1 le x, y le n$$$, $$$1 le w le 10^{12}$$$, $$$x e y$$$) denoting a special offer: you may add an edge connecting vertex $$$x$$$ and vertex $$$y$$$, and this edge will cost $$$w$$$ coins. Output Print one integer — the minimum number of coins you have to pay to make the graph connected. Examples Input 3 2 1 3 3 2 3 5 2 1 1 Input 5 4 1 2 3 4 5 1 2 8 1 3 10 1 4 7 1 5 15 Note In the first example it is possible to connect $$$1$$$ to $$$2$$$ using special offer $$$2$$$, and then $$$1$$$ to $$$3$$$ without using any offers. In next two examples the optimal answer may be achieved without using special offers. | 1,900 | false | true | false | false | false | false | false | false | false | true | 5,232 |
1311D | You are given three integers $$$a le b le c$$$. In one move, you can add $$$+1$$$ or $$$-1$$$ to any of these integers (i.e. increase or decrease any number by one). You can perform such operation any (possibly, zero) number of times, you can even perform this operation several times with one number. Note that you cannot make non-positive numbers using such operations. You have to perform the minimum number of such operations in order to obtain three integers $$$A le B le C$$$ such that $$$B$$$ is divisible by $$$A$$$ and $$$C$$$ is divisible by $$$B$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. The next $$$t$$$ lines describe test cases. Each test case is given on a separate line as three space-separated integers $$$a, b$$$ and $$$c$$$ ($$$1 le a le b le c le 10^4$$$). Output For each test case, print the answer. In the first line print $$$res$$$ — the minimum number of operations you have to perform to obtain three integers $$$A le B le C$$$ such that $$$B$$$ is divisible by $$$A$$$ and $$$C$$$ is divisible by $$$B$$$. On the second line print any suitable triple $$$A, B$$$ and $$$C$$$. Example Input 8 1 2 3 123 321 456 5 10 15 15 18 21 100 100 101 1 22 29 3 19 38 6 30 46 Output 1 1 1 3 102 114 228 456 4 4 8 16 6 18 18 18 1 100 100 100 7 1 22 22 2 1 19 38 8 6 24 48 | 2,000 | true | false | false | false | false | false | true | false | false | false | 4,140 |
1184E3 | Problem - 1184E3 - 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 dsu graphs trees *2400 No tag edit access → Contest materials time limit per test 10 seconds memory limit per test 256 megabytes input standard input output standard output With your help, Heidi has prepared a plan of trap placement and defence. Yet suddenly, the Doctor popped out of the TARDIS and told her that he had spied on the Daleks' preparations, and there is more of them than ever. Desperate times require desperate measures, so Heidi is going to risk meeting with the Daleks and she will consider placing a trap along any Corridor. This means she needs your help again in calculating $$$E_{max}(c)$$$ – the largest $$$e le 10^9$$$ such that if we changed the energy requirement of $$$c$$$ to $$$e$$$, then the Daleks might use $$$c$$$ in their invasion – but this time for all Time Corridors. Input First line: number $$$n$$$ of destinations, number $$$m$$$ of corridors ($$$2 leq n leq 10^5$$$, $$$n - 1 leq m leq 10^6$$$). The next $$$m$$$ lines: destinations $$$a$$$, $$$b$$$ and energy $$$e$$$ ($$$1 leq a, b leq n$$$, $$$a eq b$$$, $$$0 leq e leq 10^9$$$). No pair $$${a, b}$$$ will repeat. The graph is guaranteed to be connected. It is not guaranteed that all energy requirements $$$e$$$ are distinct, or that the minimum spanning tree is unique. Output Output $$$m$$$ lines, each containing one integer: $$$E_{max}(c_i)$$$ for the $$$i$$$-th Corridor $$$c_i$$$ from the input. Example Input 3 3 1 2 8 2 3 3 3 1 4 Output 4 8 8 | 2,400 | false | false | false | false | true | false | false | false | false | true | 4,781 |
1898A | Milica has a string $$$s$$$ of length $$$n$$$, consisting only of characters A and B. She wants to modify $$$s$$$ so it contains exactly $$$k$$$ instances of B. In one operation, she can do the following: Select an integer $$$i$$$ ($$$1 leq i leq n$$$) and a character $$$c$$$ ($$$c$$$ is equal to either A or B). Then, replace each of the first $$$i$$$ characters of string $$$s$$$ (that is, characters $$$s_1, s_2, ldots, s_i$$$) with $$$c$$$. Milica does not want to perform too many operations in order not to waste too much time on them. She asks you to find the minimum number of operations required to modify $$$s$$$ so it contains exactly $$$k$$$ instances of B. She also wants you to find these operations (that is, integer $$$i$$$ and character $$$c$$$ selected in each operation). Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 500$$$). The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$3 leq n leq 100$$$, $$$0 leq k leq n$$$)xa0— the length of the string $$$s$$$ and the number of characters B Milica wants to appear in $$$s$$$ in the end. The second line of each test case contains the string $$$s$$$ of length $$$n$$$, consisting only of characters A and B. Output For each test case, in the first line output a single integer $$$m$$$xa0— the minimum number of operations Milica should perform. In the $$$j$$$-th of the next $$$m$$$ lines output an integer $$$i$$$ ($$$1 le i le n$$$) and a character $$$c$$$ ($$$c$$$ is 'A' or 'B')xa0— the parameters of the $$$j$$$-th operation as described in the statement. If there are multiple solutions with the minimum possible number of operations, output any of them. Example Input 5 5 2 AAABB 5 3 AABAB 5 0 BBBBB 3 0 BAA 10 3 BBBABBBBAB Output 0 1 1 B 1 5 A 1 2 A 1 6 A Note In the first test case, there are already $$$2$$$ characters B in $$$s$$$, so Milica does not have to perform any operations. In the second test case, the only way to achieve $$$3$$$ characters B in $$$s$$$ in one operation is to replace the first character of $$$s$$$ by B on the first operation: AABAB $$$ ightarrow$$$ BABAB. In the third test case, the only way to achieve $$$0$$$ characters B in $$$s$$$ in one operation is to replace the first $$$5$$$ characters of $$$s$$$ by A on the first operation: BBBBB $$$ ightarrow$$$ AAAAA. In the fourth test case, one of the ways to achieve $$$0$$$ characters B in $$$s$$$ in one operation is to replace the first $$$2$$$ characters of $$$s$$$ by A on the first operation: BAA $$$ ightarrow$$$ AAA. Note that "1 A" and "3 A" are also correct one-operation solutions. | 800 | false | false | true | false | false | false | true | false | false | false | 928 |
798E | Mike has discovered a new way to encode permutations. If he has a permutation _P_u2009=u2009[_p_1,u2009_p_2,u2009...,u2009_p__n_], he will encode it in the following way: Denote by _A_u2009=u2009[_a_1,u2009_a_2,u2009...,u2009_a__n_] a sequence of length _n_ which will represent the code of the permutation. For each _i_ from 1 to _n_ sequentially, he will choose the smallest unmarked _j_ (1u2009≤u2009_j_u2009≤u2009_n_) such that _p__i_u2009<u2009_p__j_ and will assign to _a__i_ the number _j_ (in other words he performs _a__i_u2009=u2009_j_) and will mark _j_. If there is no such _j_, he'll assign to _a__i_ the number u2009-u20091 (he performs _a__i_u2009=u2009u2009-u20091). Mike forgot his original permutation but he remembers its code. Your task is simple: find any permutation such that its code is the same as the code of Mike's original permutation. You may assume that there will always be at least one valid permutation. Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009500u2009000) — length of permutation. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_ or _a__i_u2009=u2009u2009-u20091) — the code of Mike's permutation. You may assume that all positive values from _A_ are different. Output In first and only line print _n_ numbers _p_1,u2009_p_2,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_) — a permutation _P_ which has the same code as the given one. Note that numbers in permutation are distinct. Examples Input 8 2 -1 4 -1 6 -1 8 -1 Note For the permutation from the first example: _i_u2009=u20091, the smallest _j_ is 2 because _p_2u2009=u20096u2009>u2009_p_1u2009=u20092. _i_u2009=u20092, there is no _j_ because _p_2u2009=u20096 is the greatest element in the permutation. _i_u2009=u20093, the smallest _j_ is 1 because _p_1u2009=u20092u2009>u2009_p_3u2009=u20091. _i_u2009=u20094, the smallest _j_ is 5 (2 was already marked) because _p_5u2009=u20095u2009>u2009_p_4u2009=u20094. _i_u2009=u20095, there is no _j_ because 2 is already marked. _i_u2009=u20096, the smallest _j_ is 4 because _p_4u2009=u20094u2009>u2009_p_6u2009=u20093. | 3,000 | false | false | false | false | true | true | false | false | true | true | 6,577 |
150B | Problem - 150B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dfs and similar graphs math *1600 No tag edit access → Contest materials . Be careful and don't miss a string or two! Let us remind you that a string is a palindrome if it can be read the same way in either direction, from the left to the right and from the right to the left. Input The first and only line contains three integers: _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_,u2009_k_u2009≤u20092000). Output Print a single integer — the number of strings of the described type modulo 1000000007 (109u2009+u20097). Examples Input 1 1 1 Output 1 Input 5 2 4 Output 2 Note In the first sample only one string is valid: "a" (let's denote the only letter of our alphabet as "a"). In the second sample (if we denote the alphabet letters as "a" and "b") the following strings are valid: "aaaaa" and "bbbbb". | 1,600 | true | false | false | false | false | false | false | false | false | true | 9,275 |
1582F2 | This is a harder version of the problem with bigger constraints. Korney Korneevich dag up an array $$$a$$$ of length $$$n$$$. Korney Korneevich has recently read about the operation elements. A sequence $$$s_1, s_2, ldots , s_m$$$ is called increasing if $$$s_1 < s_2 < ldots < s_m$$$. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 5000$$$) — the elements of the array $$$a$$$. Output In the first line print a single integer $$$k$$$ — the number of found $$$x$$$ values. In the second line print $$$k$$$ integers in increasing order $$$x_1, x_2, ldots x_k$$$ ($$$0 le x_1 < ldots < x_k$$$) — found $$$x$$$ values. Examples Output 12 0 1 2 3 4 5 6 7 10 11 12 13 Note In the first test case: To get value $$$x = 0$$$ it is possible to choose and empty subsequence To get value $$$x = 2$$$ it is possible to choose a subsequence $$$[2]$$$ To get value $$$x = 4$$$ it is possible to choose a subsequence $$$[4]$$$ To get value $$$x = 6$$$ it is possible to choose a subsequence $$$[2, 4]$$$ | 2,400 | false | true | false | true | false | false | true | true | false | false | 2,719 |
1329D | Dreamoon likes strings. Today he created a game about strings: String $$$s_1, s_2, ldots, s_n$$$ is beautiful if and only if for each $$$1 le i < n, s_i e s_{i+1}$$$. Initially, Dreamoon has a string $$$a$$$. In each step Dreamoon can choose a beautiful substring of $$$a$$$ and remove it. Then he should concatenate the remaining characters (in the same order). Dreamoon wants to use the smallest number of steps to make $$$a$$$ empty. Please help Dreamoon, and print any sequence of the smallest number of steps to make $$$a$$$ empty. Input The first line contains an integer $$$t$$$ ($$$1 leq t leq 200,000$$$), denoting the number of test cases in the input. For each test case, there's one line with a non-empty string of lowercase Latin letters $$$a$$$. The total sum of lengths of strings in all test cases is at most $$$200,000$$$. Output For each test case, in the first line, you should print $$$m$$$: the smallest number of steps to make $$$a$$$ empty. Each of the following $$$m$$$ lines should contain two integers $$$l_i, r_i$$$ ($$$1 leq l_i leq r_i leq a$$$), denoting, that the $$$i$$$-th step is removing the characters from index $$$l_i$$$ to $$$r_i$$$ in the current string. (indices are numbered starting from $$$1$$$). Note that after the deletion of the substring, indices of remaining characters may change, and $$$r_i$$$ should be at most the current length of $$$a$$$. If there are several possible solutions, you can print any. Example Input 4 aabbcc aaabbb aaa abacad Output 3 3 3 2 4 1 2 3 3 4 2 3 1 2 3 1 1 1 1 1 1 1 1 6 | 3,100 | false | false | false | false | true | true | false | false | false | false | 4,062 |
1566G | You are given an undirected weighted graph, consisting of $$$n$$$ vertices and $$$m$$$ edges. Some queries happen with this graph: Delete an existing edge from the graph. Add a non-existing edge to the graph. At the beginning and after each query, you should find four different vertices $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ such that there exists a path between $$$a$$$ and $$$b$$$, there exists a path between $$$c$$$ and $$$d$$$, and the sum of lengths of two shortest paths from $$$a$$$ to $$$b$$$ and from $$$c$$$ to $$$d$$$ is minimal. The answer to the query is the sum of the lengths of these two shortest paths. The length of the path is equal to the sum of weights of edges in this path. Input The first line contains two integers $$$n$$$ and $$$m$$$ $$$(4 le n, m le 10^5)$$$xa0— the number of vertices and edges in the graph respectively. Each of the next $$$m$$$ lines contain three integers $$$v$$$, $$$u$$$, $$$w$$$ ($$$1 le v, u le n, v eq u$$$, $$$1 le w le 10^9$$$)xa0— this triple means that there is an edge between vertices $$$v$$$ and $$$u$$$ with weight $$$w$$$. The next line contains a single integer $$$q$$$ $$$(0 le q le 10^5)$$$xa0— the number of queries. The next $$$q$$$ lines contain the queries of two types: $$$0$$$ $$$v$$$ $$$u$$$xa0— this query means deleting an edge between $$$v$$$ and $$$u$$$ $$$(1 le v, u le n, v eq u)$$$. It is guaranteed that such edge exists in the graph. $$$1$$$ $$$v$$$ $$$u$$$ $$$w$$$xa0— this query means adding an edge between vertices $$$v$$$ and $$$u$$$ with weight $$$w$$$ ($$$1 le v, u le n, v eq u$$$, $$$1 le w le 10^9$$$). It is guaranteed that there was no such edge in the graph. It is guaranteed that the initial graph does not contain multiple edges. At the beginning and after each query, the graph doesn't need to be connected. It is guaranteed that at each moment the number of edges will be at least $$$4$$$. It can be proven, that at each moment there exist some four vertices $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ such that there exists a path between vertices $$$a$$$ and $$$b$$$, and there exists a path between vertices $$$c$$$ and $$$d$$$. Output Print $$$q + 1$$$ integersxa0— the minimal sum of lengths of shortest paths between chosen pairs of vertices before the queries and after each of them. Example Input 6 6 1 3 6 4 3 1 1 4 1 2 6 4 2 4 2 5 4 3 4 1 2 5 2 0 1 4 0 3 4 1 6 1 3 Note Before the queries you can choose vertices $$$(a, b) = (3, 2)$$$ and $$$(c, d) = (1, 4)$$$. The sum of lengths of two shortest paths is $$$3 + 1 = 4$$$. After the first query you can choose vertices $$$(a, b) = (2, 5)$$$ and $$$(c, d) = (1, 4)$$$. The sum of lengths of two shortest paths is $$$2 + 1 = 3$$$. After the second query you can choose vertices $$$(a, b) = (3, 4)$$$ and $$$(c, d) = (2, 5)$$$. The sum of lengths of two shortest paths is $$$1 + 2 = 3$$$. After the third query, you can choose vertices $$$(a, b) = (2, 6)$$$ and $$$(c, d) = (4, 5)$$$. The sum of lengths of two shortest paths is $$$4 + 3 = 7$$$. After the last query you can choose vertices $$$(a, b) = (1, 6)$$$ and $$$(c, d) = (2, 5)$$$. The sum of lengths of two shortest paths is $$$3 + 2 = 5$$$. | 3,100 | false | true | true | false | true | true | false | false | false | true | 2,806 |
519E | A and B are preparing themselves for programming contests. The University where A and B study is a set of rooms connected by corridors. Overall, the University has _n_ rooms connected by _n_u2009-u20091 corridors so that you can get from any room to any other one by moving along the corridors. The rooms are numbered from 1 to _n_. Every day А and B write contests in some rooms of their university, and after each contest they gather together in the same room and discuss problems. A and B want the distance from the rooms where problems are discussed to the rooms where contests are written to be equal. The distance between two rooms is the number of edges on the shortest path between them. As they write contests in new rooms every day, they asked you to help them find the number of possible rooms to discuss problems for each of the following _m_ days. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of rooms in the University. The next _n_u2009-u20091 lines describe the corridors. The _i_-th of these lines (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091) contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_), showing that the _i_-th corridor connects rooms _a__i_ and _b__i_. The next line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of queries. Next _m_ lines describe the queries. The _j_-th of these lines (1u2009≤u2009_j_u2009≤u2009_m_) contains two integers _x__j_ and _y__j_ (1u2009≤u2009_x__j_,u2009_y__j_u2009≤u2009_n_) that means that on the _j_-th day A will write the contest in the room _x__j_, B will write in the room _y__j_. Output In the _i_-th (1u2009≤u2009_i_u2009≤u2009_m_) line print the number of rooms that are equidistant from the rooms where A and B write contest on the _i_-th day. Note in the first sample there is only one room at the same distance from rooms number 2 and 3 — room number 1. | 2,100 | false | false | false | true | true | false | false | true | false | false | 7,758 |
1624F | This problem is interactive. We decided to play a game with you and guess the number $$$x$$$ ($$$1 le x < n$$$), where you know the number $$$n$$$. You can make queries like this: + c: this command assigns $$$x = x + c$$$ ($$$1 le c < n$$$) and then returns you the value $$$lfloorfrac{x}{n} floor$$$ ($$$x$$$ divide by $$$n$$$ and round down). You win if you guess the current number with no more than $$$10$$$ queries. Interaction The interaction begins by reading an integer $$$n$$$ ($$$2 < n le 1000$$$), which is written in the input data on its own line. Then you can make no more than $$$10$$$ queries. To make a query, print on a separate line: + c: this command will assign $$$x = x + c$$$ ($$$1 le c < n$$$) and then print $$$lfloorfrac{x}{n} floor$$$ (divide $$$x$$$ by $$$n$$$ and round down) on a separate line. Print the answer, like the queries, on a separate line. The answer doesn't count in number of queries. To output it, use the following format: ! x: the current value of $$$x$$$. After that, your program should exit. You have to use a flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java — System.out.flush(), in Pascal — flush(output) and in Python — sys.stdout.flush(). Note that the interactor is not responsive. To make a hack, use the following format: a single line must contain two numbers $$$x$$$ and $$$n$$$, separated by a space. Note In the first sample initially $$$x = 2$$$. After the first query $$$x = 3$$$, $$$lfloorfrac{x}{n} floor = 1$$$. In the second sample also initially $$$x = 2$$$. After the first query $$$x = 3$$$, $$$lfloorfrac{x}{n} floor = 0$$$. After the second query $$$x = 4$$$, $$$lfloorfrac{x}{n} floor = 0$$$. After the third query $$$x=5$$$, $$$lfloorfrac{x}{n} floor = 1$$$. | 2,000 | false | false | false | false | false | true | false | true | false | false | 2,495 |
618G | Your friend recently gave you some slimes for your birthday. You have a very large amount of slimes with value 1 and 2, and you decide to invent a game using these slimes. You initialize a row with _n_ empty spaces. You also choose a number _p_ to be used in the game. Then, you will perform the following steps while the last space is empty. 1. With probability , you will choose a slime with value 1, and with probability , you will choose a slime with value 2. You place the chosen slime on the last space of the board. 2. You will push the slime to the left as far as possible. If it encounters another slime, and they have the same value _v_, you will merge the slimes together to create a single slime with value _v_u2009+u20091. This continues on until the slime reaches the end of the board, or encounters a slime with a different value than itself. You have played the game a few times, but have gotten bored of it. You are now wondering, what is the expected sum of all values of the slimes on the board after you finish the game. Input The first line of the input will contain two integers _n_,u2009_p_ (1u2009≤u2009_n_u2009≤u2009109,u20091u2009≤u2009_p_u2009<u2009109). Output Print the expected sum of all slimes on the board after the game finishes. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20094. Namely, let's assume that your answer is _a_ and the answer of the jury is _b_. The checker program will consider your answer correct, if . Examples Output 269.825611298854770 Note In the first sample, we have a board with two squares, and there is a 0.5 probability of a 1 appearing and a 0.5 probability of a 2 appearing. Our final board states can be 1 2 with probability 0.25, 2 1 with probability 0.375, 3 2 with probability 0.1875, 3 1 with probability 0.1875. The expected value is thus (1u2009+u20092)·0.25u2009+u2009(2u2009+u20091)·0.375u2009+u2009(3u2009+u20092)·0.1875u2009+u2009(3u2009+u20091)·0.1875u2009=u20093.5625. | 3,300 | true | false | false | true | false | false | false | false | false | false | 7,358 |
1539A | There are $$$n$$$ people participating in some contest, they start participating in $$$x$$$ minutes intervals. That means the first participant starts at time $$$0$$$, the second participant starts at time $$$x$$$, the thirdxa0— at time $$$2 cdot x$$$, and so on. Duration of contest is $$$t$$$ minutes for each participant, so the first participant finishes the contest at time $$$t$$$, the secondxa0— at time $$$t + x$$$, and so on. When a participant finishes the contest, their dissatisfaction equals to the number of participants that started the contest (or starting it now), but haven't yet finished it. Determine the sum of dissatisfaction of all participants. Input The first line contains a single integer $$$k$$$ ($$$1 le k le 1000$$$)xa0— the number of test cases. Each of the next $$$k$$$ lines contains three integers $$$n$$$, $$$x$$$, $$$t$$$ ($$$1 le n, x, t le 2 cdot 10^9$$$)xa0— the number of participants, the start interval and the contest duration. Output Print $$$k$$$ lines, in the $$$i$$$-th line print the total dissatisfaction of participants in the $$$i$$$-th test case. Example Input 4 4 2 5 3 1 2 3 3 10 2000000000 1 2000000000 Output 5 3 3 1999999999000000000 Note In the first example the first participant starts at $$$0$$$ and finishes at time $$$5$$$. By that time the second and the third participants start, so the dissatisfaction of the first participant is $$$2$$$. The second participant starts at time $$$2$$$ and finishes at time $$$7$$$. By that time the third the fourth participants start, so the dissatisfaction of the second participant is $$$2$$$. The third participant starts at $$$4$$$ and finishes at $$$9$$$. By that time the fourth participant starts, so the dissatisfaction of the third participant is $$$1$$$. The fourth participant starts at $$$6$$$ and finishes at $$$11$$$. By time $$$11$$$ everyone finishes the contest, so the dissatisfaction of the fourth participant is $$$0$$$. In the second example the first participant starts at $$$0$$$ and finishes at time $$$2$$$. By that time the second participants starts, and the third starts at exactly time $$$2$$$. So the dissatisfaction of the first participant is $$$2$$$. The second participant starts at time $$$1$$$ and finishes at time $$$3$$$. At that time the third participant is solving the contest. | 1,000 | true | true | false | false | false | false | false | false | false | false | 2,948 |
1108C | You have a garland consisting of $$$n$$$ lamps. Each lamp is colored red, green or blue. The color of the $$$i$$$-th lamp is $$$s_i$$$ ('R', 'G' and 'B' — colors of lamps in the garland). You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice. A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is $$$t$$$, then for each $$$i, j$$$ such that $$$t_i = t_j$$$ should be satisfied $$$i-j~ mod~ 3 = 0$$$. The value $$$x$$$ means absolute value of $$$x$$$, the operation $$$x~ mod~ y$$$ means remainder of $$$x$$$ when divided by $$$y$$$. For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG". Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them. Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of lamps. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ characters 'R', 'G' and 'B' — colors of lamps in the garland. Output In the first line of the output print one integer $$$r$$$ — the minimum number of recolors needed to obtain a nice garland from the given one. In the second line of the output print one string $$$t$$$ of length $$$n$$$ — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them. | 1,300 | true | true | false | false | false | false | true | false | false | false | 5,160 |
377B | Soon there will be held the world's largest programming contest, but the testing system still has _m_ bugs. The contest organizer, a well-known university, has no choice but to attract university students to fix all the bugs. The university has _n_ students able to perform such work. The students realize that they are the only hope of the organizers, so they don't want to work for free: the _i_-th student wants to get _c__i_ 'passes' in his subjects (regardless of the volume of his work). Bugs, like students, are not the same: every bug is characterized by complexity _a__j_, and every student has the level of his abilities _b__i_. Student _i_ can fix a bug _j_ only if the level of his abilities is not less than the complexity of the bug: _b__i_u2009≥u2009_a__j_, and he does it in one day. Otherwise, the bug will have to be fixed by another student. Of course, no student can work on a few bugs in one day. All bugs are not dependent on each other, so they can be corrected in any order, and different students can work simultaneously. The university wants to fix all the bugs as quickly as possible, but giving the students the total of not more than _s_ passes. Determine which students to use for that and come up with the schedule of work saying which student should fix which bug. Input The first line contains three space-separated integers: _n_, _m_ and _s_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105, 0u2009≤u2009_s_u2009≤u2009109)xa0— the number of students, the number of bugs in the system and the maximum number of passes the university is ready to give the students. The next line contains _m_ space-separated integers _a_1, _a_2,xa0..., _a__m_ (1u2009≤u2009_a__i_u2009≤u2009109)xa0— the bugs' complexities. The next line contains _n_ space-separated integers _b_1, _b_2,xa0..., _b__n_ (1u2009≤u2009_b__i_u2009≤u2009109)xa0— the levels of the students' abilities. The next line contains _n_ space-separated integers _c_1, _c_2,xa0..., _c__n_ (0u2009≤u2009_c__i_u2009≤u2009109)xa0— the numbers of the passes the students want to get for their help. Output If the university can't correct all bugs print "NO". Otherwise, on the first line print "YES", and on the next line print _m_ space-separated integers: the _i_-th of these numbers should equal the number of the student who corrects the _i_-th bug in the optimal answer. The bugs should be corrected as quickly as possible (you must spend the minimum number of days), and the total given passes mustn't exceed _s_. If there are multiple optimal answers, you can output any of them. Examples Input 3 4 9 1 3 1 2 2 1 3 4 3 6 Input 3 4 10 2 3 1 2 2 1 3 4 3 6 Input 3 4 9 2 3 1 2 2 1 3 4 3 6 Input 3 4 5 1 3 1 2 2 1 3 5 3 6 Note Consider the first sample. The third student (with level 3) must fix the 2nd and 4th bugs (complexities 3 and 2 correspondingly) and the second student (with level 1) must fix the 1st and 3rd bugs (their complexity also equals 1). Fixing each bug takes one day for each student, so it takes 2 days to fix all bugs (the students can work in parallel). The second student wants 3 passes for his assistance, the third student wants 6 passes. It meets the university's capabilities as it is ready to give at most 9 passes. | 1,900 | false | true | false | false | true | false | false | true | true | false | 8,323 |
77A | The year of 2012 is coming... According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil. The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: _a_ for Mephisto, _b_ for Diablo and _c_ for Baal. Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a of experience, rounded down, where _x_ will be the amount of experience for the killed megaboss and _y_ — the number of people in the team. Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum. It is known that some heroes like others. But if hero _p_ likes hero _q_, this does not mean that the hero _q_ likes hero _p_. No hero likes himself. The total amount of liking in teams is the amount of ordered pairs (_p_,u2009_q_), such that heroes _p_ and _q_ are in the same group, and hero _p_ likes hero _q_ (but it is not important if hero _q_ likes hero _p_). In case of heroes _p_ and _q_ likes each other and they are in the same group, this pair should be counted twice, as (_p_,u2009_q_) and (_q_,u2009_p_). A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team. It is guaranteed that every hero is able to destroy any megaboss alone. Input The first line contains a single non-negative integer _n_ (0u2009≤u2009_n_u2009≤u200942) — amount of liking between the heroes. Next _n_ lines describe liking in the form "p likes q", meaning that the hero p likes the hero q (p u2009≠u2009 q). Every liking is described in the input exactly once, no hero likes himself. In the last line are given three integers _a_, _b_ and _c_ (1u2009≤u2009_a_,u2009_b_,u2009_c_u2009≤u20092·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal. In all the pretests, except for examples from the statement, the following condition is satisfied: _a_u2009=u2009_b_u2009=u2009_c_. Output Print two integers — the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team). When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking. | 1,400 | false | false | true | false | false | false | true | false | false | false | 9,580 |
1935B | In the Master's Assistance Center, Nyam-Nyam was given a homework assignment in informatics. There is an array $$$a$$$ of length $$$n$$$, and you want to divide it into $$$k > 1$$$ subsegments$$$^{dagger}$$$ in such a way that the $$$operatorname{MEX} ^{ddagger}$$$ on each subsegment is equal to the same integer. Help Nyam-Nyam find any suitable division, or determine that it does not exist. $$$^{dagger}$$$A division of an array into $$$k$$$ subsegments is defined as $$$k$$$ pairs of integers $$$(l_1, r_1), (l_2, r_2), ldots, (l_k, r_k)$$$ such that $$$l_i le r_i$$$ and for each $$$1 le j le k - 1$$$, $$$l_{j + 1} = r_j + 1$$$, and also $$$l_1 = 1$$$ and $$$r_k = n$$$. These pairs represent the subsegments themselves. $$$^{ddagger}operatorname{MEX}$$$ of an array is the smallest non-negative integer that does not belong to the array. For example: $$$operatorname{MEX}$$$ of the array $$$[2, 2, 1]$$$ is $$$0$$$, because $$$0$$$ does not belong to the array. $$$operatorname{MEX}$$$ of the array $$$[3, 1, 0, 1]$$$ is $$$2$$$, because $$$0$$$ and $$$1$$$ belong to the array, but $$$2$$$ does not. $$$operatorname{MEX}$$$ of the array $$$[0, 3, 1, 2]$$$ is $$$4$$$, because $$$0$$$, $$$1$$$, $$$2$$$, and $$$3$$$ belong to the array, but $$$4$$$ does not. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^5$$$) — the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i < n$$$) — the elements of the array $$$a$$$. 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 integer $$$-1$$$ if a suitable division does not exist. Otherwise, on the first line, output an integer $$$k$$$ ($$$2 le k le n$$$) — the number of subsegments in the division. Then output $$$k$$$ lines — the division into subsegments. The $$$i$$$-th line should contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$) — the boundaries of the $$$i$$$-th subsegment. The following conditions must be satisfied: For all $$$1 le j le k - 1$$$, $$$l_{j + 1} = r_j + 1$$$; $$$l_1 = 1$$$, $$$r_k = n$$$. If there are multiple possible solutions, output any of them. Example Input 5 2 0 0 5 0 1 2 3 4 8 0 1 7 1 0 1 0 3 3 2 2 2 4 0 1 2 0 Output 2 1 1 2 2 -1 3 1 3 4 5 6 8 3 1 1 2 2 3 3 -1 Note In the first test case, the array $$$a$$$ can be divided into $$$2$$$ subsegments with boundaries $$$[1, 1]$$$ and $$$[2, 2]$$$: $$$operatorname{MEX}$$$ of the first subsegment $$$[0]$$$ is $$$1$$$, as $$$0$$$ belongs to the subsegment, but $$$1$$$ does not. $$$operatorname{MEX}$$$ of the second subsegment $$$[0]$$$ is $$$1$$$, as $$$0$$$ belongs to the subsegment, but $$$1$$$ does not. In the second test case, it can be proven that the required division does not exist. In the third test case, the array $$$a$$$ can be divided into $$$3$$$ subsegments with boundaries $$$[1, 3]$$$, $$$[4, 5]$$$, $$$[6, 8]$$$: $$$operatorname{MEX}$$$ of the first subsegment $$$[0, 1, 7]$$$ is $$$2$$$, as $$$0$$$ and $$$1$$$ belong to the subsegment, but $$$2$$$ does not. $$$operatorname{MEX}$$$ of the second subsegment $$$[1, 0]$$$ is $$$2$$$, as $$$0$$$ and $$$1$$$ belong to the subsegment, but $$$2$$$ does not. $$$operatorname{MEX}$$$ of the third subsegment $$$[1, 0, 3]$$$ is $$$2$$$, as $$$0$$$ and $$$1$$$ belong to the subsegment, but $$$2$$$ does not. | 1,200 | false | false | false | false | false | true | false | false | false | false | 673 |
519B | A and B are preparing themselves for programming contests. B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code. Initially, the compiler displayed _n_ compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake. However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change. Can you help B find out exactly what two errors he corrected? Input The first line of the input contains integer _n_ (3u2009≤u2009_n_u2009≤u2009105) — the initial number of compilation errors. The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the errors the compiler displayed for the first time. The third line contains _n_u2009-u20091 space-separated integers _b_1,u2009_b_2,u2009...,u2009_b__n_u2009-u20091 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one. The fourth line contains _n_u2009-u20092 space-separated integers _с_1,u2009_с_2,u2009...,u2009_с__n_u2009-u20092 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one. Output Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively. Examples Input 5 1 5 8 123 7 123 7 5 1 5 1 7 Input 6 1 4 3 3 5 7 3 7 5 4 3 4 3 7 5 Note In the first test sample B first corrects the error number 8, then the error number 123. In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step. | 1,100 | false | false | true | false | true | false | false | false | true | false | 7,761 |
1580A | CQXYM found a rectangle $$$A$$$ of size $$$n imes m$$$. There are $$$n$$$ rows and $$$m$$$ columns of blocks. Each block of the rectangle is an obsidian block or empty. CQXYM can change an obsidian block to an empty block or an empty block to an obsidian block in one operation. A rectangle $$$M$$$ size of $$$a imes b$$$ is called a portal if and only if it satisfies the following conditions: $$$a geq 5,b geq 4$$$. For all $$$1 < x < a$$$, blocks $$$M_{x,1}$$$ and $$$M_{x,b}$$$ are obsidian blocks. For all $$$1 < x < b$$$, blocks $$$M_{1,x}$$$ and $$$M_{a,x}$$$ are obsidian blocks. For all $$$1<x<a,1<y<b$$$, block $$$M_{x,y}$$$ is an empty block. $$$M_{1, 1}, M_{1, b}, M_{a, 1}, M_{a, b}$$$ can be any type. Note that the there must be $$$a$$$ rows and $$$b$$$ columns, not $$$b$$$ rows and $$$a$$$ columns. Note that corners can be any type CQXYM wants to know the minimum number of operations he needs to make at least one sub-rectangle a portal. Input The first line contains an integer $$$t$$$ ($$$t geq 1$$$), which is the number of test cases. For each test case, the first line contains two integers $$$n$$$ and $$$m$$$ ($$$5 le n le 400$$$, $$$4 le m le 400$$$). Then $$$n$$$ lines follow, each line contains $$$m$$$ characters $$$0$$$ or $$$1$$$. If the $$$j$$$-th character of $$$i$$$-th line is $$$0$$$, block $$$A_{i,j}$$$ is an empty block. Otherwise, block $$$A_{i,j}$$$ is an obsidian block. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$400$$$. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$400$$$. Output Output $$$t$$$ answers, and each answer in a line. Examples Input 1 5 4 1000 0000 0110 0000 0001 Input 1 9 9 001010001 101110100 000010011 100000001 101010101 110001111 000001111 111100000 000110000 Note In the first test case, the final portal is like this: 1110 1001 1001 1001 0111 | 1,700 | false | true | true | true | true | false | true | false | false | false | 2,733 |
372C | A festival will be held in a town's main street. There are _n_ sections in the main street. The sections are numbered 1 through _n_ from left to right. The distance between each adjacent sections is 1. In the festival _m_ fireworks will be launched. The _i_-th (1u2009≤u2009_i_u2009≤u2009_m_) launching is on time _t__i_ at section _a__i_. If you are at section _x_ (1u2009≤u2009_x_u2009≤u2009_n_) at the time of _i_-th launching, you'll gain happiness value _b__i_u2009-u2009_a__i_u2009-u2009_x_ (note that the happiness value might be a negative value). You can move up to _d_ length units in a unit time interval, but it's prohibited to go out of the main street. Also you can be in an arbitrary section at initial time moment (time equals to 1), and want to maximize the sum of happiness that can be gained from watching fireworks. Find the maximum total happiness. Note that two or more fireworks can be launched at the same time. Input The first line contains three integers _n_, _m_, _d_ (1u2009≤u2009_n_u2009≤u2009150000;xa01u2009≤u2009_m_u2009≤u2009300;xa01u2009≤u2009_d_u2009≤u2009_n_). Each of the next _m_ lines contains integers _a__i_, _b__i_, _t__i_ (1u2009≤u2009_a__i_u2009≤u2009_n_;xa01u2009≤u2009_b__i_u2009≤u2009109;xa01u2009≤u2009_t__i_u2009≤u2009109). The _i_-th line contains description of the _i_-th launching. It is guaranteed that the condition _t__i_u2009≤u2009_t__i_u2009+u20091 (1u2009≤u2009_i_u2009<u2009_m_) will be satisfied. Output Print a single integer — the maximum sum of happiness that you can gain from watching all the fireworks. Please, do not write the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 50 3 1 49 1 1 26 1 4 6 1 10 Input 10 2 1 1 1000 4 9 1000 4 | 2,100 | true | false | false | true | true | false | false | false | false | false | 8,341 |
1423J | Problem - 1423J - Codeforces =============== xa0 ]( "Mirror of Bubble Cup 13 Finals on Codeforces") =m$$$? Help Jerry Mao solve the long standing problem! Input The first line contains a single integer $$$t$$$ $$$(1 leq t leq 5cdot 10^5)$$$ - number of test cases. On next line there are $$$t$$$ numbers, $$$m_i$$$ $$$(1 leq m_i leq 10^{18})$$$ - meaning that in case $$$i$$$ you should solve for number $$$m_i$$$. Output For each test case $$$i$$$, print the answer on separate lines: number of polynomials $$$P$$$ as described in statement such that $$$P(2)=m_i$$$, modulo $$$10^9 + 7$$$. Example Input 2 2 4 Output 2 4 Note In first case, for $$$m=2$$$, polynomials that satisfy the constraint are $$$x$$$ and $$$2$$$. In second case, for $$$m=4$$$, polynomials that satisfy the constraint are $$$x^2$$$, $$$x + 2$$$, $$$2x$$$ and $$$4$$$. | 2,400 | true | false | false | true | false | true | false | false | false | false | 3,554 |
175C | Vasya plays the Geometry Horse. The game goal is to destroy geometric figures of the game world. A certain number of points is given for destroying each figure depending on the figure type and the current factor value. There are _n_ types of geometric figures. The number of figures of type _k__i_ and figure cost _c__i_ is known for each figure type. A player gets _c__i_·_f_ points for destroying one figure of type _i_, where _f_ is the current factor. The factor value can be an integer number from 1 to _t_u2009+u20091, inclusive. At the beginning of the game the factor value is equal to 1. The factor is set to _i_u2009+u20091 after destruction of _p__i_ (1u2009≤u2009_i_u2009≤u2009_t_) figures, so the (_p__i_u2009+u20091)-th figure to be destroyed is considered with factor equal to _i_u2009+u20091. Your task is to determine the maximum number of points Vasya can get after he destroys all figures. Take into account that Vasya is so tough that he can destroy figures in any order chosen by him. Input The first line contains the only integer number _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of figure types. Each of the following _n_ lines contains two integer numbers _k__i_ and _c__i_ (1u2009≤u2009_k__i_u2009≤u2009109,u20090u2009≤u2009_c__i_u2009≤u20091000), separated with space — the number of figures of the _i_-th type and the cost of one _i_-type figure, correspondingly. The next line contains the only integer number _t_ (1u2009≤u2009_t_u2009≤u2009100) — the number that describe the factor's changes. The next line contains _t_ integer numbers _p__i_ (1u2009≤u2009_p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__t_u2009≤u20091012), separated with spaces. Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specificator. Output Print the only number — the maximum number of points Vasya can get. Note In the first example Vasya destroys three figures first and gets 3·1·10u2009=u200930 points. Then the factor will become equal to 2 and after destroying the last two figures Vasya will get 2·2·10u2009=u200940 points. As a result Vasya will get 70 points. In the second example all 8 figures will be destroyed with factor 1, so Vasya will get (3·8u2009+u20095·10)·1u2009=u200974 points. | 1,600 | false | true | true | false | false | false | false | false | true | false | 9,163 |
1746C | You are given a permutation $$$a$$$ of size $$$n$$$ and you should perform $$$n$$$ operations on it. In the $$$i$$$-th operation, you can choose a non-empty suffix of $$$a$$$ and increase all of its elements by $$$i$$$. How can we perform the operations to minimize the number of inversions in the final array? Note that you can perform operations on the same suffix any number of times you want. A permutation of size $$$n$$$ is an array of size $$$n$$$ such that each integer from $$$1$$$ to $$$n$$$ occurs exactly once in this array. A suffix is several consecutive elements of an array that include the last element of the array. An inversion in an array $$$a$$$ is a pair of indices $$$(i, j)$$$ such that $$$i > j$$$ and $$$a_{i} < a_{j}$$$. 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 a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the size of the array. The second line contains $$$n$$$ distinct integers $$$a_{1}, a_{2}, dots, a_{n}$$$ ($$$1 le a_i le n$$$), the initial permutation $$$a$$$. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print $$$n$$$ integers $$$x_{1}, x_{2}, ldots, x_{n}$$$ ($$$1 le x_{i} le n$$$ for each $$$1 le i le n$$$) indicating that the $$$i$$$-th operation must be applied to the suffix starting at index $$$x_{i}$$$. If there are multiple answers, print any of them. Note In the first test case one of the optimal solutions is to increase the whole array on each operation (that is, choose the suffix starting at index $$$1$$$). The final array $$$[11, 12, 13, 14]$$$ contains $$$0$$$ inversions. In the second test case, $$$a$$$ will be equal to $$$[2, 4, 3, 5, 6]$$$, $$$[2, 4, 3, 7, 8]$$$, $$$[2, 4, 6, 10, 11]$$$, $$$[2, 8, 10, 14, 15]$$$ and $$$[7, 13, 15, 19, 20]$$$ after the first, second, third, fourth, and fifth operations, respectively. So the final array $$$a$$$ has zero inversions. | 1,300 | true | true | true | false | false | true | false | false | false | false | 1,808 |
241F | The Old City is a rectangular city represented as an _m_u2009×u2009_n_ grid of blocks. This city contains many buildings, straight two-way streets and junctions. Each junction and each building is exactly one block. All the streets have width of one block and are either vertical or horizontal. There is a junction on both sides of each street. We call two blocks adjacent if and only if they share a common side. No two blocks of different streets are adjacent and no two junctions are adjacent. There is an annual festival and as a part of it, The Old Peykan follows a special path in the city. This path starts from a block in a street, continues with many junctions and ends in a block of some street. For each street block, we know how much time it takes for the Old Peykan to go from this block to an adjacent block. Also the Old Peykan can go from each junction to its adjacent street blocks in one minute. Of course Old Peykan can't go to building blocks. We know the initial position of the Old Peykan and the sequence of junctions that it passes to reach its destination. After passing all the junctions and reaching the destination, it will stay there forever. Your task is to find out where will the Old Peykan be _k_ minutes after it starts moving. Consider that The Old Peykan always follows the shortest path that passes through the given sequence of junctions and reaches the destination. Note that the Old Peykan may visit some blocks more than once. Input The first line of input contains three integers _m_, _n_ and _k_ (3u2009≤u2009_m_,u2009_n_u2009≤u2009100,u20091u2009≤u2009_k_u2009≤u2009100000). Next _m_ lines are representing the city's map. Each of them containts _n_ characters, each character is a block: Character "#" represents a building. Digits "1", "2", ..., "9" represent a block of an street and this digit means the number of minutes it takes for the Old Peykan to pass this block. Characters "a", "b", ..., "z" means that this block is a junction and this character is it's name. All the junction names are unique. Consider that all blocks have the coordinates: the _j_-th in the _i_-th line have coordinates (_i_,u2009_j_) (1u2009≤u2009_i_u2009≤u2009_m_,u20091u2009≤u2009_j_u2009≤u2009_n_). The (_m_u2009+u20092)th line contains two integers _r__s_ and _c__s_ (1u2009≤u2009_r__s_u2009≤u2009_m_,u20091u2009≤u2009_c__s_u2009≤u2009_n_), string _s_ and another two integers _r__e_ and _c__e_ (1u2009≤u2009_r__e_u2009≤u2009_m_,u20091u2009≤u2009_c__e_u2009≤u2009_n_). The path starts from block (_r__s_,u2009_c__s_), continues through junctions in the order that is specified by _s_ and will end in block (_r__e_,u2009_c__e_). Length of _s_ is between 1 and 1000. It's guaranteed that string _s_ denotes a correct path from the start position to the end position and string _s_ doesn't contain two consecutive equal letters. Also start position (_r__s_,u2009_c__s_) and the end position (_r__e_,u2009_c__e_) are street blocks. Output In a single line print two integers _r__f_ and _c__f_ — (_r__f_,u2009_c__f_) being the position of the Old Peykan after exactly _k_ minutes. Examples Input 3 10 12 ########## #z1a1111b# ########## 2 3 ab 2 8 Input 10 3 5 ### #w# #1# #a# #1# #1# #1# #1# #b# ### 3 2 abababababababab 6 2 Input 3 10 6 ########## #z1a1311b# ########## 2 3 ab 2 8 | 2,300 | false | false | true | false | false | false | true | false | false | false | 8,870 |
126E | Doctor prescribed medicine to his patient. The medicine is represented by pills. Each pill consists of a shell and healing powder. The shell consists of two halves; each half has one of four colors — blue, red, white or yellow. The doctor wants to put 28 pills in a rectangular box 7u2009×u20098 in size. Besides, each pill occupies exactly two neighboring cells and any cell contains exactly one half of a pill. Thus, the result is a four colored picture 7u2009×u20098 in size. The doctor thinks that a patient will recover sooner if the picture made by the pills will be special. Unfortunately, putting the pills in the box so as to get the required picture is not a very easy task. That's why doctor asks you to help. Doctor has some amount of pills of each of 10 painting types. They all contain the same medicine, that's why it doesn't matter which 28 of them will be stored inside the box. Place the pills in the box so that the required picture was formed. If it is impossible to place the pills in the required manner, then place them so that the number of matching colors in all 56 cells in the final arrangement and the doctor's picture were maximum. Input First 7 lines contain the doctor's picture. Each line contains 8 characters, each character can be "B", "R", "W" and "Y" that stands for blue, red, white and yellow colors correspondingly. Next four lines contain 10 numbers that stand for, correspondingly, the number of pills painted: "BY" "BW" "BR" "BB" "RY" "RW" "RR" "WY" "WW" "YY" Those numbers lie within range from 0 to 28 inclusively. It is guaranteed that the total number of pills in no less than 28. Output Print on the first line the maximal number cells for which the colors match. Then print 13 lines each containing 15 characters — the pills' position in the optimal arrangement. The intersections of odd lines and odd columns should contain characters "B", "R", "W" and "Y". All other positions should contain characters ".", "-" and "". Use "-" and "" to show which halves belong to one pill. See the samples for more clarification. If there are several possible solutions, print any of them. Examples Input WWWBBWWW WWWBBWWW YYWBBWWW YYWBBWRR YYWBBWRR YYWBBWRR YYWBBWRR 0 0 0 8 0 1 5 1 10 5 Output 53 W.W.W.B.B.W.W.W ....... W.W.W.B.B.W.W.W ............... Y.Y.W.B.B.W.W-W ......... Y.Y.W.B.B.W.R.R ............. Y.Y.W.B.B.R.R.R ......... Y.Y.W.B.B.W.R.R ............. Y-Y.B-B.B-B.R.R Input WWWWWWWW WBYWRBBY BRYRBWYY WWBRYWBB BWWRWBYW RBWRBWYY WWWWWWWW 0 0 0 1 0 0 1 0 1 25 Output 15 W.Y.Y-Y.Y-Y.Y-Y ............. W.Y.Y.Y.Y.B-B.Y ........... Y-Y.Y.Y.Y.Y-Y.Y ............... Y.Y.Y.R.Y.Y.Y-Y ......... Y.Y.Y.R.Y.Y.Y.Y ............. Y-Y.Y.Y-Y.Y.Y.Y ............. Y-Y.Y.Y-Y.Y.Y-Y | 2,900 | false | false | false | false | false | false | true | false | false | false | 9,373 |
1781A | Vika's house has a room in a shape of a rectangular parallelepiped (also known as a rectangular cuboid). Its floor is a rectangle of size $$$w imes d$$$, and the ceiling is right above at the constant height of $$$h$$$. Let's introduce a coordinate system on the floor so that its corners are at points $$$(0, 0)$$$, $$$(w, 0)$$$, $$$(w, d)$$$, and $$$(0, d)$$$. A laptop is standing on the floor at point $$$(a, b)$$$. A projector is hanging on the ceiling right above point $$$(f, g)$$$. Vika wants to connect the laptop and the projector with a cable in such a way that the cable always goes along the walls, ceiling, or floor (i.xa0e. does not go inside the cuboid). Additionally, the cable should always run parallel to one of the cuboid's edges (i.xa0e. it can not go diagonally). What is the minimum length of a cable that can connect the laptop to the projector? Illustration for the first test case. One of the optimal ways to put the cable is shown in green. 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 three integers $$$w$$$, $$$d$$$, and $$$h$$$ ($$$2 le w, d, h le 1000$$$)xa0— the size of the room. The second line contains four integers $$$a$$$, $$$b$$$, $$$f$$$, $$$g$$$ ($$$0 < a, f < w$$$; $$$0 < b, g < d$$$): the laptop is located on the floor at point $$$(a, b)$$$, while the projector is hanging on the ceiling right above point $$$(f, g)$$$. Output For each test case, print a single integerxa0— the minimum length of the cable connecting the laptop and the projector that runs only along the walls, floor, and ceiling parallel to cuboid's edges. Example Input 5 55 20 29 23 10 18 3 20 10 5 1 5 2 5 15 15 4 7 13 10 10 2 1000 2 1 1 1 999 10 4 10 7 1 2 1 Note The picture in the statement illustrates the first test case. | 800 | true | false | false | false | false | false | false | false | false | false | 1,588 |
9E | Hexadecimal likes drawing. She has drawn many graphs already, both directed and not. Recently she has started to work on a still-life «interesting graph and apples». An undirected graph is called interesting, if each of its vertices belongs to one cycle only — a funny ring — and does not belong to any other cycles. A funny ring is a cycle that goes through all the vertices just once. Moreover, loops are funny rings too. She has already drawn the apples and some of the graph edges. But now it is not clear, how to connect the rest of the vertices to get an interesting graph as a result. The answer should contain the minimal amount of added edges. And furthermore, the answer should be the lexicographically smallest one. The set of edges (_x_1,u2009_y_1),u2009(_x_2,u2009_y_2),u2009...,u2009(_x__n_,u2009_y__n_), where _x__i_u2009≤u2009_y__i_, is lexicographically smaller than the set (_u_1,u2009_v_1),u2009(_u_2,u2009_v_2),u2009...,u2009(_u__n_,u2009_v__n_), where _u__i_u2009≤u2009_v__i_, provided that the sequence of integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2,u2009...,u2009_x__n_,u2009_y__n_ is lexicographically smaller than the sequence _u_1,u2009_v_1,u2009_u_2,u2009_v_2,u2009...,u2009_u__n_,u2009_v__n_. If you do not cope, Hexadecimal will eat you. ...eat you alive. Input The first line of the input data contains a pair of integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u200950, 0u2009≤u2009_m_u2009≤u20092500) — the amount of vertices and edges respectively. The following lines contain pairs of numbers _x__i_ and _y__i_ (1u2009≤u2009_x__i_, _y__i_u2009≤u2009_n_) — the vertices that are already connected by edges. The initial graph may contain multiple edges and loops. Output In the first line output «YES» or «NO»: if it is possible or not to construct an interesting graph. If the answer is «YES», in the second line output _k_ — the amount of edges that should be added to the initial graph. Finally, output _k_ lines: pairs of vertices _x__j_ and _y__j_, between which edges should be drawn. The result may contain multiple edges and loops. _k_ can be equal to zero. | 2,300 | false | false | false | false | false | false | false | false | false | true | 9,946 |
1151F | A girl named Sonya is studying in the scientific lyceum of the Kingdom of Kremland. The teacher of computer science (Sonya's favorite subject!) invented a task for her. Given an array $$$a$$$ of length $$$n$$$, consisting only of the numbers $$$0$$$ and $$$1$$$, and the number $$$k$$$. Exactly $$$k$$$ times the following happens: Two numbers $$$i$$$ and $$$j$$$ are chosen equiprobable such that ($$$1 leq i < j leq n$$$). The numbers in the $$$i$$$ and $$$j$$$ positions are swapped. Sonya's task is to find the probability that after all the operations are completed, the $$$a$$$ array will be sorted in non-decreasing order. She turned to you for help. Help Sonya solve this problem. It can be shown that the desired probability is either $$$0$$$ or it can be represented as $$$dfrac{P}{Q}$$$, where $$$P$$$ and $$$Q$$$ are coprime integers and $$$Q otequiv 0~pmod {10^9+7}$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 100, 1 leq k leq 10^9$$$)xa0— the length of the array $$$a$$$ and the number of operations. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 1$$$)xa0— the description of the array $$$a$$$. Output If the desired probability is $$$0$$$, print $$$0$$$, otherwise print the value $$$P cdot Q^{-1}$$$ $$$pmod {10^9+7}$$$, where $$$P$$$ and $$$Q$$$ are defined above. Note In the first example, all possible variants of the final array $$$a$$$, after applying exactly two operations: $$$(0, 1, 0)$$$, $$$(0, 0, 1)$$$, $$$(1, 0, 0)$$$, $$$(1, 0, 0)$$$, $$$(0, 1, 0)$$$, $$$(0, 0, 1)$$$, $$$(0, 0, 1)$$$, $$$(1, 0, 0)$$$, $$$(0, 1, 0)$$$. Therefore, the answer is $$$dfrac{3}{9}=dfrac{1}{3}$$$. In the second example, the array will not be sorted in non-decreasing order after one operation, therefore the answer is $$$0$$$. | 2,300 | false | false | false | true | false | false | false | false | false | false | 4,957 |
1906E | You are currently researching the Merge Sort algorithm. Merge Sort is a sorting algorithm that is based on the principle of Divide and Conquer. It works by dividing an array into two subarrays of equal length, sorting each subarrays, then merging the sorted subarrays back together to form the final sorted array. You are particularly interested in the merging routine. Common merge implementation will combine two subarrays by iteratively comparing their first elements, and move the smaller one to a new merged array. More precisely, the merge algorithm can be presented by the following pseudocode. Merge(A[1..N], B[1..N]): C = [] i = 1 j = 1 while i <= N AND j <= N: if A[i] < B[j]: append A[i] to C i = i + 1 else: append B[j] to C j = j + 1 while i <= N: append A[i] to C i = i + 1 while j <= N: append B[j] to C j = j + 1 return C During your research, you are keen to understand the behaviour of the merge algorithm when arrays $$$A$$$ and $$$B$$$ are not necessarily sorted. For example, if $$$A = [3, 1, 6]$$$ and $$$B = [4, 5, 2]$$$, then $$$ ext{Merge}(A, B) = [3, 1, 4, 5, 2, 6]$$$. To further increase the understanding of the merge algorithm, you decided to work on the following problem. You are given an array $$$C$$$ of length $$$2 cdot N$$$ such that it is a permutation of $$$1$$$ to $$$2 cdot N$$$. Construct any two arrays $$$A$$$ and $$$B$$$ of the same length $$$N$$$, such that $$$ ext{Merge}(A, B) = C$$$, or determine if it is impossible to do so. Input The first line consists of an integer $$$N$$$ ($$$1 leq N leq 1000$$$). The following line consists of $$$2 cdot N$$$ integers $$$C_i$$$. The array $$$C$$$ is a permutation of $$$1$$$ to $$$2 cdot N$$$. Output If it is impossible to construct two arrays $$$A$$$ and $$$B$$$ of length $$$N$$$ such that $$$ ext{Merge}(A, B) = C$$$, then output -1. Otherwise, output the arrays $$$A$$$ and $$$B$$$ in two lines. The first line consists of $$$N$$$ integers $$$A_i$$$. The second line consists of $$$N$$$ integers $$$B_i$$$. If there are several possible answers, output any of them. Note Explanation for the sample input/output #1 The solution $$$A = [3, 1, 4]$$$ and $$$B = [5, 2, 6]$$$ is also correct. Explanation for the sample input/output #2 The solution $$$A = [1, 2, 3, 4]$$$ and $$$B = [5, 6, 7, 8]$$$ is also correct. | 1,900 | false | false | false | true | false | true | false | false | false | false | 873 |
894E | Ralph is going to collect mushrooms in the Mushroom Forest. There are _m_ directed paths connecting _n_ trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the _i_-th time, there regrow _i_ mushrooms less than there was before this pass. That is, if there is initially _x_ mushrooms on a path, then Ralph will collect _x_ mushrooms for the first time, _x_u2009-u20091 mushrooms the second time, _x_u2009-u20091u2009-u20092 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0. For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it). Ralph decided to start from the tree _s_. How many mushrooms can he collect using only described paths? Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009106, 0u2009≤u2009_m_u2009≤u2009106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively. Each of the following _m_ lines contains three integers _x_, _y_ and _w_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_, 0u2009≤u2009_w_u2009≤u2009108), denoting a path that leads from tree _x_ to tree _y_ with _w_ mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees. The last line contains a single integer _s_ (1u2009≤u2009_s_u2009≤u2009_n_)xa0— the starting position of Ralph. Output Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route. Note In the first sample Ralph can pass three times on the circle and collect 4u2009+u20094u2009+u20093u2009+u20093u2009+u20091u2009+u20091u2009=u200916 mushrooms. After that there will be no mushrooms for Ralph to collect. In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3. | 2,100 | false | false | false | true | false | false | false | false | false | true | 6,171 |
855F | Nagini, being a horcrux You-know-who created with the murder of Bertha Jorkins, has accumulated its army of snakes and is launching an attack on Hogwarts school. Hogwarts' entrance can be imagined as a straight line (x-axis) from 1 to 105. Nagini is launching various snakes at the Hogwarts entrance. Each snake lands parallel to the entrance, covering a segment at a distance _k_ from _x_u2009=u2009_l_ to _x_u2009=u2009_r_. Formally, each snake can be imagined as being a line segment between points (_l_,u2009_k_) and (_r_,u2009_k_). Note that _k_ can be both positive and negative, but not 0. Let, at some _x_-coordinate _x_u2009=u2009_i_, there be snakes at point (_i_,u2009_y_1) and point (_i_,u2009_y_2), such that _y_1u2009>u20090 and _y_2u2009<u20090. Then, if for any point (_i_,u2009_y_3) containing a snake such that _y_3u2009>u20090, _y_1u2009≤u2009_y_3 holds and for any point (_i_,u2009_y_4) containing a snake such that _y_4u2009<u20090, _y_2u2009≤u2009_y_4 holds, then the danger value at coordinate _x_u2009=u2009_i_ is _y_1u2009+u2009_y_2. If no such _y_1 and _y_2 exist, danger value is 0. Harry wants to calculate the danger value of various segments of the Hogwarts entrance. Danger value for a segment [_l_,u2009_r_) of the entrance can be calculated by taking the sum of danger values for each integer _x_-coordinate present in the segment. Formally, you have to implement two types of queries: 1 l r k: a snake is added parallel to entrance from _x_u2009=u2009_l_ to _x_u2009=u2009_r_ at y-coordinate _y_u2009=u2009_k_ (_l_ inclusive, _r_ exclusive). 2 l r: you have to calculate the danger value of segment _l_ to _r_ (_l_ inclusive, _r_ exclusive). Input First line of input contains a single integer _q_ (1u2009≤u2009_q_u2009≤u20095·104) denoting the number of queries. Next _q_ lines each describe a query. Each query description first contains the query type _type__i_ (1u2009≤u2009_type__i_u2009≤u20092). This is followed by further description of the query. In case of the type being 1, it is followed by integers _l__i_,u2009_r__i_ and _k__i_ (, u2009-u2009109u2009≤u2009_k__i_u2009≤u2009109, _k_u2009≠u20090). Otherwise, it just contains two integers, _l__i_ and _r__i_ (1u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009105). Note In the first sample case, the danger value for _x_-coordinates 1 is 0 as there is no _y_2 satisfying the above condition for _x_u2009=u20091. Danger values for _x_-coordinates 2 and 3 is 10u2009+u2009u2009-u20097u2009=u200917. Danger values for _x_-coordinates 4 to 9 is again 0 as there is no _y_2 satisfying the above condition for these coordinates. Thus, total danger value is 17u2009+u200917u2009=u200934. | 3,100 | false | false | false | false | true | false | false | true | false | false | 6,322 |
1868D | A pseudotree is a connected graph which has exactly one cycle and no self-loops. Note that a pseudotree may contain multiple-edges. It can be shown that a pseudotree with $$$n$$$ vertices always contains $$$n$$$ edges. After deleting all edges on the cycle in the pseudotree, a forest$$$^{dagger}$$$ will be formed. It can be shown that each tree in the forest will contain exactly one vertex which is on cycle before removing the edges. If all trees in the forest have the same depth$$$^{ddagger}$$$ when picking the vertex on cycle as root, we call the original pseudotree flower-like. Our friend sszcdjr, had a flower-like pseudotree with $$$n$$$ vertices and $$$n$$$ edges. However, he forgot all the edges in the pseudotree. Fortunately, he still remembers the degrees of vertices. Specifically, the degree of the $$$i$$$-th vertex is $$$d_i$$$. You have to help sszcdjr construct a possible flower-like pseudotree with $$$n$$$ vertices, where the degree of the $$$i$$$-th vertex is exactly $$$d_i$$$, or tell him that it is impossible. $$$^{dagger}$$$ A forest is a graph in which all connectivity components are trees. A connected graph without cycles and self-loops is called a tree. $$$^{ddagger}$$$ The depth of a tree with a root is the maximum distance from the root to the vertex of this tree. Input The first line of the input contains a single integer $$$t$$$ ($$$1leq tleq 10^5$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2leq nleq 10^6$$$) — the number of vertices. The second line of each test case contains $$$n$$$ integers $$$d_1,d_2,ldots,d_n$$$ ($$$1leq d_ileq n$$$) — the degree of each vertex. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, if there exist a possible flower-like pseudotree: Print "Yes" (without quotes) in the first line. Then, output $$$n$$$ lines, in each line print two integers $$$u_i$$$ and $$$v_i$$$ — the two vertices that the $$$i$$$-th edge connects. If there are multiple answers, you may output any of them. Otherwise, print "No" (without quotes) in the only line of output. You can output the first line of each test case in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 6 3 2 2 2 4 1 2 3 4 7 4 3 3 1 1 1 1 6 1 1 2 2 3 3 10 1 1 5 2 1 1 1 1 1 6 9 1 1 3 1 1 4 1 1 5 Output Yes 1 2 2 3 3 1 No Yes 1 2 2 3 3 1 1 4 1 5 2 6 3 7 Yes 5 6 6 5 1 3 2 4 3 5 4 6 No Yes 3 6 6 9 9 3 1 3 2 6 4 6 5 9 7 9 8 9 Note In the first test case, the only possible flower-like psuedotree is: After deleting all edges on the cycle in the pseudotree, each tree has depth $$$0$$$. In the second test case, it can be proven that there's no such flower-like psuedotree. In the third test case, one of the possible flower-like psuedotrees is: | 3,000 | false | true | true | false | false | true | false | false | false | true | 1,064 |
630G | Because of budget cuts one IT company established new non-financial reward system instead of bonuses. Two kinds of actions are rewarded: fixing critical bugs and suggesting new interesting features. A man who fixed a critical bug gets "I fixed a critical bug" pennant on his table. A man who suggested a new interesting feature gets "I suggested a new feature" pennant on his table. Because of the limited budget of the new reward system only 5 "I fixed a critical bug" pennants and 3 "I suggested a new feature" pennants were bought. In order to use these pennants for a long time they were made challenge ones. When a man fixes a new critical bug one of the earlier awarded "I fixed a critical bug" pennants is passed on to his table. When a man suggests a new interesting feature one of the earlier awarded "I suggested a new feature" pennants is passed on to his table. One man can have several pennants of one type and of course he can have pennants of both types on his table. There are _n_ tables in the IT company. Find the number of ways to place the pennants on these tables given that each pennant is situated on one of the tables and each table is big enough to contain any number of pennants. Input The only line of the input contains one integer _n_ (1u2009≤u2009_n_u2009≤u2009500) — the number of tables in the IT company. Output Output one integer — the amount of ways to place the pennants on _n_ tables. | 1,600 | true | false | false | false | false | false | false | false | false | false | 7,298 |
486A | u2009=u2009u2009-u20091u2009+u20092u2009-u20093u2009+u2009..u2009+u2009(u2009-u20091)_n__n_ Your task is to calculate _f_(_n_) for a given integer _n_. Input The single line contains the positive integer _n_ (1u2009≤u2009_n_u2009≤u20091015). Output Print _f_(_n_) in a single line. Note _f_(4)u2009=u2009u2009-u20091u2009+u20092u2009-u20093u2009+u20094u2009=u20092 _f_(5)u2009=u2009u2009-u20091u2009+u20092u2009-u20093u2009+u20094u2009-u20095u2009=u2009u2009-u20093 | 800 | true | false | true | false | false | false | false | false | false | false | 7,895 |
1000B | Recently, you bought a brand new smart lamp with programming features. At first, you set up a schedule to the lamp. Every day it will turn power on at moment $$$0$$$ and turn power off at moment $$$M$$$. Moreover, the lamp allows you to set a program of switching its state (states are "lights on" and "lights off"). Unfortunately, some program is already installed into the lamp. The lamp allows only good programs. Good program can be represented as a non-empty array $$$a$$$, where $$$0 < a_1 < a_2 < dots < a_{a} < M$$$. All $$$a_i$$$ must be integers. Of course, preinstalled program is a good program. The lamp follows program $$$a$$$ in next manner: at moment $$$0$$$ turns power and light on. Then at moment $$$a_i$$$ the lamp flips its state to opposite (if it was lit, it turns off, and vice versa). The state of the lamp flips instantly: for example, if you turn the light off at moment $$$1$$$ and then do nothing, the total time when the lamp is lit will be $$$1$$$. Finally, at moment $$$M$$$ the lamp is turning its power off regardless of its state. Since you are not among those people who read instructions, and you don't understand the language it's written in, you realize (after some testing) the only possible way to alter the preinstalled program. You can insert at most one element into the program $$$a$$$, so it still should be a good program after alteration. Insertion can be done between any pair of consecutive elements of $$$a$$$, or even at the begining or at the end of $$$a$$$. Find such a way to alter the program that the total time when the lamp is lit is maximum possible. Maybe you should leave program untouched. If the lamp is lit from $$$x$$$ till moment $$$y$$$, then its lit for $$$y - x$$$ units of time. Segments of time when the lamp is lit are summed up. Input First line contains two space separated integers $$$n$$$ and $$$M$$$ ($$$1 le n le 10^5$$$, $$$2 le M le 10^9$$$) — the length of program $$$a$$$ and the moment when power turns off. Second line contains $$$n$$$ space separated integers $$$a_1, a_2, dots, a_n$$$ ($$$0 < a_1 < a_2 < dots < a_n < M$$$) — initially installed program $$$a$$$. Note In the first example, one of possible optimal solutions is to insert value $$$x = 3$$$ before $$$a_1$$$, so program will be $$$[3, 4, 6, 7]$$$ and time of lamp being lit equals $$$(3 - 0) + (6 - 4) + (10 - 7) = 8$$$. Other possible solution is to insert $$$x = 5$$$ in appropriate place. In the second example, there is only one optimal solution: to insert $$$x = 2$$$ between $$$a_1$$$ and $$$a_2$$$. Program will become $$$[1, 2, 10]$$$, and answer will be $$$(1 - 0) + (10 - 2) = 9$$$. In the third example, optimal answer is to leave program untouched, so answer will be $$$(3 - 0) + (7 - 4) = 6$$$. | 1,500 | false | true | false | false | false | false | false | false | false | false | 5,704 |
1388A | Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint's task. Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let's define a positive integer $$$x$$$ as nearly prime if it can be represented as $$$p cdot q$$$, where $$$1 < p < q$$$ and $$$p$$$ and $$$q$$$ are prime numbers. For example, integers $$$6$$$ and $$$10$$$ are nearly primes (since $$$2 cdot 3 = 6$$$ and $$$2 cdot 5 = 10$$$), but integers $$$1$$$, $$$3$$$, $$$4$$$, $$$16$$$, $$$17$$$ or $$$44$$$ are not. Captain Flint guessed an integer $$$n$$$ and asked you: can you represent it as the sum of $$$4$$$ different positive integers where at least $$$3$$$ of them should be nearly prime. Uncle Bogdan easily solved the task and joined the crew. Can you do the same? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Next $$$t$$$ lines contain test casesxa0— one per line. The first and only line of each test case contains the single integer $$$n$$$ $$$(1 le n le 2 cdot 10^5)$$$xa0— the number Flint guessed. Note In the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime. In the third test case, $$$n=31=2 cdot 7 + 2 cdot 5 + 2 cdot 3 + 1$$$: integers $$$14$$$, $$$10$$$, $$$6$$$ are nearly prime. In the fourth test case, $$$n=36=5 + 2 cdot 3 + 2 cdot 5 + 3 cdot 5$$$: integers $$$6$$$, $$$10$$$, $$$15$$$ are nearly prime. In the fifth test case, $$$n=44=2 cdot 3 + 7 + 2 cdot 5 + 3 cdot 7$$$: integers $$$6$$$, $$$10$$$, $$$21$$$ are nearly prime. In the sixth test case, $$$n=100=2 + 2 cdot 5 + 3 cdot 11 + 5 cdot 11$$$: integers $$$10$$$, $$$33$$$, $$$55$$$ are nearly prime. In the seventh test case, $$$n=258=2 cdot 5 + 3 cdot 7 + 13 cdot 17 + 2 cdot 3$$$: integers $$$10$$$, $$$21$$$, $$$221$$$, $$$6$$$ are nearly prime. | 800 | true | true | false | false | false | false | true | false | false | false | 3,733 |
1178G | You are given a rooted tree on $$$n$$$ vertices. The vertices are numbered from $$$1$$$ to $$$n$$$; the root is the vertex number $$$1$$$. Each vertex has two integers associated with it: $$$a_i$$$ and $$$b_i$$$. We denote the set of all ancestors of $$$v$$$ (including $$$v$$$ itself) by $$$R(v)$$$. The awesomeness of a vertex $$$v$$$ is defined as $$$$$$left sum_{w in R(v)} a_w ight cdot leftsum_{w in R(v)} b_w ight,$$$$$$ where $$$x$$$ denotes the absolute value of $$$x$$$. Process $$$q$$$ queries of one of the following forms: 1 v xxa0— increase $$$a_v$$$ by a positive integer $$$x$$$. 2 vxa0— report the maximum awesomeness in the subtree of vertex $$$v$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 leq n leq 2cdot 10^5, 1 leq q leq 10^5$$$)xa0— the number of vertices in the tree and the number of queries, respectively. The second line contains $$$n - 1$$$ integers $$$p_2, p_3, dots, p_n$$$ ($$$1 leq p_i < i$$$), where $$$p_i$$$ means that there is an edge between vertices $$$i$$$ and $$$p_i$$$. The third line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-5000 leq a_i leq 5000$$$), the initial values of $$$a_i$$$ for each vertex. The fourth line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$-5000 leq b_i leq 5000$$$), the values of $$$b_i$$$ for each vertex. Each of the next $$$q$$$ lines describes a query. It has one of the following forms: 1 v xu2009 ($$$1 leq v leq n$$$, $$$1leq x leq 5000$$$). 2 vu2009 ($$$1 leq v leq n$$$). Output For each query of the second type, print a single line with the maximum awesomeness in the respective subtree. Example Input 5 6 1 1 2 2 10 -3 -7 -3 -10 10 3 9 3 6 2 1 2 2 1 2 6 2 1 1 2 5 2 1 Note The initial awesomeness of the vertices is $$$[100, 91, 57, 64, 57]$$$. The most awesome vertex in the subtree of vertex $$$1$$$ (the first query) is $$$1$$$, and the most awesome vertex in the subtree of vertex $$$2$$$ (the second query) is $$$2$$$. After the first update (the third query), the awesomeness changes to $$$[100, 169, 57, 160, 57]$$$ and thus the most awesome vertex in the whole tree (the fourth query) is now $$$2$$$. After the second update (the fifth query), the awesomeness becomes $$$[100, 234, 57, 240, 152]$$$, hence the most awesome vertex (the sixth query) is now $$$4$$$. | 3,000 | false | false | false | false | true | false | false | false | false | false | 4,823 |
191E | It is dark times in Berland. Berlyand opposition, funded from a neighboring state, has organized a demonstration in Berland capital Bertown. Through the work of intelligence we know that the demonstrations are planned to last for _k_ days. Fortunately, Berland has a special police unit, which can save the country. It has exactly _n_ soldiers numbered from 1 to _n_. Berland general, the commander of the detachment, must schedule the detachment's work in these difficult _k_ days. In each of these days, the general must send a certain number of police officers to disperse riots. Since the detachment is large and the general is not very smart, he can only select a set of all soldiers numbered from _l_ to _r_, inclusive, where _l_ and _r_ are selected arbitrarily. Now the general has exactly two problems. First, he cannot send the same group twice — then soldiers get bored and they rebel. Second, not all soldiers are equally reliable. Every soldier has a reliability of _a__i_. The reliability of the detachment is counted as the sum of reliabilities of soldiers in it. The reliability of a single soldier can be negative, then when you include him in the detachment, he will only spoil things. The general is distinguished by his great greed and shortsightedness, so each day he sends to the dissolution the most reliable group of soldiers possible (that is, of all the groups that have not been sent yet). The Berland Government has decided to know what would be the minimum reliability of the detachment, sent to disperse the demonstrations during these _k_ days. The general himself can not cope with such a difficult task. Help him to not embarrass himself in front of his superiors! Input The first line contains two integers _n_ and _k_ — the number of soldiers in the detachment and the number of times somebody goes on duty. The second line contains _n_ space-separated integers _a__i_, their absolute value doesn't exceed 109 — the soldiers' reliabilities. Please do not use the %lld specifier to read or write 64-bit integers in С++, it is preferred to use cin, cout streams of the %I64d specifier. | 2,200 | false | false | false | false | true | false | false | true | false | false | 9,073 |
2003E2 | This is a hard version of this problem. The differences between the versions are the constraint on $$$m$$$ and $$$r_i < l_{i + 1}$$$ holds for each $$$i$$$ from $$$1$$$ to $$$m - 1$$$ in the easy version. You can make hacks only if both versions of the problem are solved. Turtle gives you $$$m$$$ intervals $$$[l_1, r_1], [l_2, r_2], ldots, [l_m, r_m]$$$. He thinks that a permutation $$$p$$$ is interesting if there exists an integer $$$k_i$$$ for every interval ($$$l_i le k_i < r_i$$$), and if he lets $$$a_i = maxlimits_{j = l_i}^{k_i} p_j, b_i = minlimits_{j = k_i + 1}^{r_i} p_j$$$ for every integer $$$i$$$ from $$$1$$$ to $$$m$$$, the following condition holds: $$$$$$maxlimits_{i = 1}^m a_i < minlimits_{i = 1}^m b_i$$$$$$ Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $$$n$$$, or tell him if there is no interesting permutation. An inversion of a permutation $$$p$$$ is a pair of integers $$$(i, j)$$$ ($$$1 le i < j le n$$$) such that $$$p_i > p_j$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$2 le n le 5 cdot 10^3, 0 le m le 5 cdot 10^3$$$) — the length of the permutation and the number of intervals. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$l_i, r_i$$$ ($$$1 le l_i < r_i le n$$$) — the $$$i$$$-th interval. Note that there may exist identical intervals (i.e., there may exist two different indices $$$i, j$$$ such that $$$l_i = l_j$$$ and $$$r_i = r_j$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^3$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$5 cdot 10^3$$$. Output For each test case, if there is no interesting permutation, output a single integer $$$-1$$$. Otherwise, output a single integer — the maximum number of inversions. Note In the third test case, the interesting permutation with the maximum number of inversions is $$$[5, 2, 4, 3, 1]$$$. In the fourth test case, the interesting permutation with the maximum number of inversions is $$$[4, 3, 8, 7, 6, 2, 1, 5]$$$. In this case, we can let $$$[k_1, k_2, k_3] = [2, 2, 7]$$$. In the fifth and the sixth test case, it can be proven that there is no interesting permutation. In the seventh test case, the interesting permutation with the maximum number of inversions is $$$[4, 7, 6, 3, 2, 1, 5]$$$. In this case, we can let $$$[k_1, k_2, k_3, k_4] = [1, 6, 1, 6]$$$. In the eighth test case, the interesting permutation with the maximum number of inversions is $$$[4, 7, 3, 6, 2, 5, 1]$$$. | 2,700 | true | true | false | true | true | false | true | false | false | false | 233 |
1167B | This is an interactive problem. Remember to flush your output while communicating with the testing program. You may use fflush(stdout) in C++, system.out.flush() in Java, stdout.flush() in Python or flush(output) in Pascal to flush the output. If you use some other programming language, consult its documentation. You may also refer to the guide on interactive problems: [ jury guessed some array $$$a$$$ consisting of $$$6$$$ integers. There are $$$6$$$ special numbers — $$$4$$$, $$$8$$$, $$$15$$$, $$$16$$$, $$$23$$$, $$$42$$$ — and each of these numbers occurs in $$$a$$$ exactly once (so, $$$a$$$ is some permutation of these numbers). You don't know anything about their order, but you are allowed to ask up to $$$4$$$ queries. In each query, you may choose two indices $$$i$$$ and $$$j$$$ ($$$1 le i, j le 6$$$, $$$i$$$ and $$$j$$$ are not necessarily distinct), and you will get the value of $$$a_i cdot a_j$$$ in return. Can you guess the array $$$a$$$? The array $$$a$$$ is fixed beforehand in each test, the interaction program doesn't try to adapt to your queries. Interaction Before submitting the answer, you may ask up to $$$4$$$ queries. To ask a query, print one line in the following format: $$$?$$$ $$$i$$$ $$$j$$$, where $$$i$$$ and $$$j$$$ should be two integers such that $$$1 le i, j le 6$$$. The line should be ended with a line break character. After submitting a query, flush the output and read the answer to your query — one line containing one integer $$$a_i cdot a_j$$$. If you submit an incorrect query (or ask more than $$$4$$$ queries), the answer to it will be one string 0. After receiving such an answer, your program should terminate immediately — otherwise you may receive verdict "Runtime error", "Time limit exceeded" or some other verdict instead of "Wrong answer". To give the answer, your program should print one line $$$!$$$ $$$a_1$$$ $$$a_2$$$ $$$a_3$$$ $$$a_4$$$ $$$a_5$$$ $$$a_6$$$ with a line break in the end. After that, it should flush the output and terminate gracefully. Note If you want to submit a hack for this problem, your test should contain exactly six space-separated integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_6$$$. Each of $$$6$$$ special numbers should occur exactly once in the test. The test should be ended with a line break character. | 1,400 | true | false | false | false | false | false | true | false | false | false | 4,882 |
1818A | In a debate club with $$$n$$$ members, including yourself (member $$$1$$$), there are $$$k$$$ opinions to be discussed in sequence. During each discussion, members express their agreement or disagreement with the opinion. Let's define $$$Y$$$ as the number of members who agree and $$$N$$$ as the number of members who disagree. After each discussion, members leave the club based on the following criteria: If more members agree than disagree ($$$Y > N$$$), all members who disagreed leave the club. If more members disagree than agree ($$$Y < N$$$), all members who agreed leave the club. If there is a tie ($$$Y = N$$$), all members leave the club. As the club president, your goal is to stay in the club and maximize the number of members remaining after the meeting. You have access to each member's stance on all $$$k$$$ opinions before the meeting starts, and you can expel any number of members (excluding yourself) before the meeting begins. Determine the maximum number of members, including yourself, who can remain in the club after the meeting. You don't need to provide the specific expulsion strategy but only the maximum number of members that can stay. Ensure that you remain in the club after the meeting as well. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains two positive integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 100$$$) — the number of members and the number of discussions. The $$$i$$$-th of the following $$$n$$$ lines contains a string $$$t_i$$$ of length $$$k$$$. The $$$j$$$-th character in the string $$$t_i$$$ indicates whether the $$$i$$$-th member agrees or disagrees with the $$$j$$$-th opinion if they are present during that discussion. A "+" symbol means the member agrees, while a "-" symbol means the member disagrees. It is guaranteed that the sum of $$$n cdot k$$$ over all test cases does not exceed $$$5 cdot 10^4$$$. Output For each test case, output the maximum number of members, including yourself, who can remain in the club after the meeting. Example Input 5 2 2 ++ +- 1 3 +-+ 4 1 + - - + 5 4 ++++ +--+ ++-+ +-++ ++++ 4 2 ++ -- -- -+ Note For convenience, we will analyze the examples based on who actually attended the meeting (i.xa0e. was not expelled) rather than who was expelled. Example 1: Only the first member could have attended the meeting, otherwise both members would have left after the second opinion is discussed. Example 2: There is only a single member that attends the meeting and stays till the end. Example 3: The club has $$$4$$$ members and only one opinion will be discussed during the meeting. Let's analyze the possible outcomes based on the participants in the meeting: If only the first member attends, they'll be the only one left after the meeting. If the first member attends with the second or third member, they will be a tie in the discussion, making them both leave. If the first member attends with the second and third members, the first member will be in the minority and will leave after the discussion, which contradicts the statement. If the first and fourth members attend, they will agree during the discussion and both remain till the end. If the first, second, and fourth members attend, the second member will be in the minority during the discussion, and only the first and fourth members will remain at the end. The same happens if the second member is replaced by the third member. If all four members attend, there will be a tie during the discussion, making everyone leave. The maximum number of members remaining after the meeting is $$$2$$$. Example 4: The club has $$$5$$$ members and $$$4$$$ opinions will be discussed during the meeting. One way to achieve the maximum number of members is if only the first, third, and fifth members attend the meeting. In this case, they all agree during the first two discussions, after which the third member is in the minority during the third discussion. Then, the first and fifth members agree in the last discussion, and those two members stay till the end of the meeting. Example 5: The club has $$$4$$$ members and $$$2$$$ opinions will be discussed. If the first three members attend the meeting, the first member will be in the minority during the first discussion and will leave the club. After that, the second and third members will both disagree with the second opinion, and they both will stay till the end of the meeting. In this way, there will be 2 members left after the meeting, but it is an invalid outcome, as it forces the first member to leave. Therefore, the maximum number of 1 member is achieved if only the first member attends the meeting. | 800 | false | true | true | false | false | false | false | false | false | false | 1,367 |
1466A | Argus was charged with guarding Io, which is not an ordinary cow. Io is quite an explorer, and she wanders off rather frequently, making Argus' life stressful. So the cowherd decided to construct an enclosed pasture for Io. There are $$$n$$$ trees growing along the river, where Argus tends Io. For this problem, the river can be viewed as the $$$OX$$$ axis of the Cartesian coordinate system, and the $$$n$$$ trees as points with the $$$y$$$-coordinate equal $$$0$$$. There is also another tree growing in the point $$$(0, 1)$$$. Argus will tie a rope around three of the trees, creating a triangular pasture. Its exact shape doesn't matter to Io, but its area is crucial to her. There may be many ways for Argus to arrange the fence, but only the ones which result in different areas of the pasture are interesting for Io. Calculate the number of different areas that her pasture may have. Note that the pasture must have nonzero area. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow, each one is described in two lines. In the first line of each test case there is a single integer $$$n$$$ ($$$1 leq n leq 50$$$) denoting the number of trees growing along the river. Next line contains $$$n$$$ distinct integers $$$x_1 < x_2 < ldots < x_{n - 1} < x_n$$$ ($$$1 leq x_i leq 50$$$), the $$$x$$$-coordinates of trees growing along the river. Output In a single line output an integer, the number of different nonzero areas that triangles with trees as vertices may have. Example Input 8 4 1 2 4 5 3 1 3 5 3 2 6 8 2 1 2 1 50 5 3 4 5 6 8 3 1 25 26 6 1 2 4 8 16 32 Note In the first test case, we have $$$6$$$ non-degenerate triangles with the following areas: $$$0.5$$$, $$$0.5$$$, $$$1$$$, $$$1.5$$$, $$$1.5$$$ and $$$2$$$. The pasture can have $$$4$$$ different areas, and thus $$$4$$$ is the answer. In the second test case, we have $$$3$$$ non-degenerate triangles with the following areas: $$$1$$$, $$$1$$$ and $$$2$$$. The pasture can have $$$2$$$ different areas, so $$$2$$$ is the answer. The following two drawings present the situation in the second test case. The blue triangles in the first drawing have area $$$1$$$. The red triangle in the second drawing has area $$$2$$$. | 800 | true | false | false | false | false | false | true | false | false | false | 3,366 |
1572B | You are given a sequence $$$a$$$ of length $$$n$$$ consisting of $$$0$$$s and $$$1$$$s. You can perform the following operation on this sequence: Pick an index $$$i$$$ from $$$1$$$ to $$$n-2$$$ (inclusive). Change all of $$$a_{i}$$$, $$$a_{i+1}$$$, $$$a_{i+2}$$$ to $$$a_{i} oplus a_{i+1} oplus a_{i+2}$$$ simultaneously, where $$$oplus$$$ denotes the . The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 2cdot10^5$$$) — the length of $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$) — elements of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, do the following: if there is no way of making all the elements of $$$a$$$ equal to $$$0$$$ after performing the above operation some number of times, print "NO". otherwise, in the first line print "YES", in the second line print $$$k$$$ ($$$0 le k le n$$$) — the number of operations that you want to perform on $$$a$$$, and in the third line print a sequence $$$b_1, b_2, dots, b_k$$$ ($$$1 le b_i le n - 2$$$) — the indices on which the operation should be applied. If there are multiple solutions, you may print any. Note In the first example, the sequence contains only $$$0$$$s so we don't need to change anything. In the second example, we can transform $$$[1, 1, 1, 1, 0]$$$ to $$$[1, 1, 0, 0, 0]$$$ and then to $$$[0, 0, 0, 0, 0]$$$ by performing the operation on the third element of $$$a$$$ and then on the first element of $$$a$$$. In the third example, no matter whether we first perform the operation on the first or on the second element of $$$a$$$ we will get $$$[1, 1, 1, 1]$$$, which cannot be transformed to $$$[0, 0, 0, 0]$$$. | 2,500 | false | true | false | false | false | true | true | false | false | false | 2,781 |
1368B | # Codeforces Subsequences Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes Karl likes Codeforces and subsequences. He wants to find a string of lowercase English letters that contains at least k subsequences codeforces . Out of all possible strings, Karl wants to find a shortest one. Formally, a codeforces subsequence of a string s is a subset of ten characters of s that read codeforces from left to right. For example, codeforces contains codeforces a single time, while codeforcesisawesome contains codeforces four times: codeforces isawesome , codeforce si sawesome , codeforce sisawe some , codeforc esisaw es ome .Help Karl find any shortest string that contains at least k codeforces subsequences. # Input The only line contains a single integer k (1 ≤ k ≤ 10 16 ). # Output Print a shortest string of lowercase English letters that contains at least k codeforces subsequences. If there are several such strings, print any of them. # Examples standard input standard output 1 codeforces 3 codeforcesss Page 1 of 1 | 1,500 | true | true | false | false | false | true | true | false | false | false | 3,835 |
2036F | Alice has been giving gifts to Bob for many years, and she knows that what he enjoys the most is performing $$$. Therefore, this year for his birthday, she gifted him a super-powerful "XORificator 3000", the latest model. Bob was very pleased with the gift, as it allowed him to instantly compute the XOR of all interesting integers in any range from $$$l$$$ to $$$r$$$, inclusive. After all, what else does a person need for happiness? Unfortunately, the device was so powerful that at one point it performed XOR with itself and disappeared. Bob was very upset, and to cheer him up, Alice asked you to write your version of the "XORificator". Input The first line of input contains a single integer $$$t$$$ $$$(1 leq t leq 10^4)$$$ — the number of XOR queries on the segment. The following $$$t$$$ lines contain the queries, each consisting of the integers $$$l$$$, $$$r$$$, $$$i$$$, $$$k$$$ $$$(1 leq l leq r leq 10^{18}$$$, $$$0 leq i leq 30$$$, $$$0 leq k < 2^i)$$$. Output For each query, output a single integer — the XOR of all integers $$$x$$$ in the range $$$[l, r]$$$ such that $$$x otequiv k mod 2^i$$$. Example Input 6 1 3 1 0 2 28 3 7 15 43 1 0 57 2007 1 0 1010 1993 2 2 1 1000000000 30 1543 Output 2 2 13 0 4 1000000519 Note In the first query, the interesting integers in the range $$$[1, 3]$$$ are $$$1$$$ and $$$3$$$, so the answer will be $$$1 oplus 3 = 2$$$. | 1,900 | false | false | false | true | false | false | false | false | false | false | 47 |
982B | In the Bus of Characters there are $$$n$$$ rows of seat, each having $$$2$$$ seats. The width of both seats in the $$$i$$$-th row is $$$w_i$$$ centimeters. All integers $$$w_i$$$ are distinct. Initially the bus is empty. On each of $$$2n$$$ stops one passenger enters the bus. There are two types of passengers: an introvert always chooses a row where both seats are empty. Among these rows he chooses the one with the smallest seats width and takes one of the seats in it; an extrovert always chooses a row where exactly one seat is occupied (by an introvert). Among these rows he chooses the one with the largest seats width and takes the vacant place in it. You are given the seats width in each row and the order the passengers enter the bus. Determine which row each passenger will take. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 200,000$$$) — the number of rows in the bus. The second line contains the sequence of integers $$$w_1, w_2, dots, w_n$$$ ($$$1 le w_i le 10^{9}$$$), where $$$w_i$$$ is the width of each of the seats in the $$$i$$$-th row. It is guaranteed that all $$$w_i$$$ are distinct. The third line contains a string of length $$$2n$$$, consisting of digits '0' and '1' — the description of the order the passengers enter the bus. If the $$$j$$$-th character is '0', then the passenger that enters the bus on the $$$j$$$-th stop is an introvert. If the $$$j$$$-th character is '1', the the passenger that enters the bus on the $$$j$$$-th stop is an extrovert. It is guaranteed that the number of extroverts equals the number of introverts (i.xa0e. both numbers equal $$$n$$$), and for each extrovert there always is a suitable row. Output Print $$$2n$$$ integers — the rows the passengers will take. The order of passengers should be the same as in input. Examples Input 6 10 8 9 11 13 5 010010011101 Output 6 6 2 3 3 1 4 4 1 2 5 5 Note In the first example the first passenger (introvert) chooses the row $$$2$$$, because it has the seats with smallest width. The second passenger (introvert) chooses the row $$$1$$$, because it is the only empty row now. The third passenger (extrovert) chooses the row $$$1$$$, because it has exactly one occupied seat and the seat width is the largest among such rows. The fourth passenger (extrovert) chooses the row $$$2$$$, because it is the only row with an empty place. | 1,300 | false | true | true | false | true | false | false | false | false | false | 5,791 |
1332F | Eric is the teacher of graph theory class. Today, Eric teaches independent set and edge-induced subgraph. Given a graph $$$G=(V,E)$$$, an independent set is a subset of vertices $$$V' subset V$$$ such that for every pair $$$u,v in V'$$$, $$$(u,v) ot in E$$$ (i.e. no edge in $$$E$$$ connects two vertices from $$$V'$$$). An edge-induced subgraph consists of a subset of edges $$$E' subset E$$$ and all the vertices in the original graph that are incident on at least one edge in the subgraph. Given $$$E' subset E$$$, denote $$$G[E']$$$ the edge-induced subgraph such that $$$E'$$$ is the edge set of the subgraph. Here is an illustration of those definitions: In order to help his students get familiar with those definitions, he leaves the following problem as an exercise: Given a tree $$$G=(V,E)$$$, calculate the sum of $$$w(H)$$$ over all except null edge-induced subgraph $$$H$$$ of $$$G$$$, where $$$w(H)$$$ is the number of independent sets in $$$H$$$. Formally, calculate $$$sum limits_{emptyset ot= E' subset E} w(G[E'])$$$. Show Eric that you are smarter than his students by providing the correct answer as quickly as possible. Note that the answer might be large, you should output the answer modulo $$$998,244,353$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$), representing the number of vertices of the graph $$$G$$$. Each of the following $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$, $$$u ot= v$$$), describing edges of the given tree. It is guaranteed that the given edges form a tree. Output Output one integer, representing the desired value modulo $$$998,244,353$$$. | 2,500 | false | false | false | true | false | false | false | false | false | false | 4,045 |
1374C | You are given a bracket sequence $$$s$$$ of length $$$n$$$, where $$$n$$$ is even (divisible by two). The string $$$s$$$ consists of $$$frac{n}{2}$$$ opening brackets '(' and $$$frac{n}{2}$$$ closing brackets ')'. In one move, you can choose exactly one bracket and move it to the beginning of the string or to the end of the string (i.e. you choose some index $$$i$$$, remove the $$$i$$$-th character of $$$s$$$ and insert it before or after all remaining characters of $$$s$$$). Your task is to find the minimum number of moves required to obtain regular bracket sequence from $$$s$$$. It can be proved that the answer always exists under the given constraints. Recall what the regular bracket sequence is: "()" is regular bracket sequence; if $$$s$$$ is regular bracket sequence then "(" + $$$s$$$ + ")" is regular bracket sequence; if $$$s$$$ and $$$t$$$ are regular bracket sequences then $$$s$$$ + $$$t$$$ is regular bracket sequence. For example, "()()", "(())()", "(())" and "()" are regular bracket sequences, but ")(", "()(" and ")))" are not. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 2000$$$) — the number of test cases. Then $$$t$$$ test cases follow. The first line of the test case contains one integer $$$n$$$ ($$$2 le n le 50$$$) — the length of $$$s$$$. It is guaranteed that $$$n$$$ is even. The second line of the test case containg the string $$$s$$$ consisting of $$$frac{n}{2}$$$ opening and $$$frac{n}{2}$$$ closing brackets. Output For each test case, print the answer — the minimum number of moves required to obtain regular bracket sequence from $$$s$$$. It can be proved that the answer always exists under the given constraints. Example Input 4 2 )( 4 ()() 8 ())()()( 10 )))((((()) Note In the first test case of the example, it is sufficient to move the first bracket to the end of the string. In the third test case of the example, it is sufficient to move the last bracket to the beginning of the string. In the fourth test case of the example, we can choose last three openning brackets, move them to the beginning of the string and obtain "((()))(())". | 1,000 | false | true | false | false | false | false | false | false | false | false | 3,792 |
492C | Vanya wants to pass _n_ exams and get the academic scholarship. He will get the scholarship if the average grade mark for all the exams is at least _avg_. The exam grade cannot exceed _r_. Vanya has passed the exams and got grade _a__i_ for the _i_-th exam. To increase the grade for the _i_-th exam by 1 point, Vanya must write _b__i_ essays. He can raise the exam grade multiple times. What is the minimum number of essays that Vanya needs to write to get scholarship? Input The first line contains three integers _n_, _r_, _avg_ (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_r_u2009≤u2009109, 1u2009≤u2009_avg_u2009≤u2009_min_(_r_,u2009106))xa0— the number of exams, the maximum grade and the required grade point average, respectively. Each of the following _n_ lines contains space-separated integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_u2009≤u2009_r_, 1u2009≤u2009_b__i_u2009≤u2009106). Output In the first line print the minimum number of essays. Examples Input 5 5 4 5 2 4 7 3 1 3 2 2 5 Note In the first sample Vanya can write 2 essays for the 3rd exam to raise his grade by 2 points and 2 essays for the 4th exam to raise his grade by 1 point. In the second sample, Vanya doesn't need to write any essays as his general point average already is above average. | 1,400 | false | true | false | false | false | false | false | false | true | false | 7,866 |
1779E | This is an interactive problem. Anya has gathered $$$n$$$ chess experts numbered from $$$1$$$ to $$$n$$$ for which the following properties hold: For any pair of players one of the players wins every game against the other (and no draws ever occur); Transitivity does not necessarily holdxa0— it might happen that $$$A$$$ always beats $$$B$$$, $$$B$$$ always beats $$$C$$$ and $$$C$$$ always beats $$$A$$$. Anya does not know, for each pair, who is the player who beats the other. To organize a tournament, Anya hosts $$$n-1$$$ games. In each game, she chooses two players. One of them wins and stays, while the other one is disqualified. After all the games are hosted only one player will remain. A player is said to be a candidate master if they can win a tournament (notice that the winner of a tournament may depend on the players selected by Anya in the $$$n-1$$$ games). Since Anya is a curious girl, she is interested in finding the candidate masters. Unfortunately, she does not have much time. To speed up the process, she will organize up to $$$2n$$$ simuls (short for "simultaneous exhibition", in which one player plays against many). In one simul, Anya chooses exactly one player who will play against some (at least one) of the other players. The chosen player wins all games they would win in a regular game, and the same holds for losses. After the simul finishes, Anya is only told the total number of games won by the chosen player (but not which ones). Nobody is disqualified during a simul. Can you help Anya host simuls and determine the candidate masters? The winning players in each pair could be changed between the simuls, but only in a way that preserves the results of all previous simuls. These changes may depend on your queries. Interaction Firstly, the jury sends one integer $$$n$$$ ($$$3 leq n leq 250$$$) which should be readxa0— the number of players. After that, your program may ask queries or report an answer. To ask a query, print "? $$$i ; s_1 s_2 ldots s_n$$$" (without quotes), where $$$i$$$ is the index of the player who will play against some of the other players in the simul. $$$s$$$ is a binary string that denotes the players they play against. $$$i$$$ plays against every player $$$j$$$ for which $$$s_j = 1$$$ holds (and $$$s_j = 1$$$ should hold for at least one $$$1 leq j leq n$$$). Please note that $$$s_i = 0$$$ must hold since a player cannot play against themselves, otherwise, the query is considered to be incorrect. After this, you should read an integerxa0— the number of games player $$$i$$$ has won. When you have identified the answer, you must print "! $$$c_1 c_2 ldots c_n$$$" (without quotes) and terminate your program. $$$c$$$ is a binary string which represents the candidate masters. Player $$$i$$$ is a candidate master if $$$c_i=1$$$ holds, otherwise, they are not. If you ask more than $$$2n$$$ queries or if one of the queries is malformed, the interaction terminates immediately and your program receives verdict Wrong Answer. After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks are disabled in this problem. Examples Output ? 1 010 ? 2 001 ? 3 100 ! 111 Output ? 5 10110 ? 2 10111 ? 1 01111 ! 10000 Note In the first example, the first query describes a simul in which player $$$1$$$ plays against player $$$2$$$ (and no one else). The answer to the query is $$$1$$$, meaning that player $$$1$$$ won the only game they played. We can conclude that $$$1$$$ beats $$$2$$$. Similarly, the second query tells us that $$$2$$$ beats $$$3$$$ and the third query tells us that $$$3$$$ beats $$$1$$$. All players are candidate masters in this case as Player $$$1$$$ can win the tournament if $$$2$$$ and $$$3$$$ play first. $$$3$$$ loses and leaves, while $$$2$$$ stays. $$$1$$$ then plays against $$$2$$$ and wins; Other players can win in the same fashion. In the second example, the third query describes a simul in which player $$$1$$$ plays against every other player. The answer to the query is $$$4$$$, meaning that they won every game they played. It can be concluded that player $$$1$$$ also beats every other player. They can never lose, hence they are the only player who can remain at the end of every possible tournament, and the only possible candidate master. | 2,400 | false | true | false | false | false | true | false | false | true | true | 1,598 |
1165B | Polycarp wants to train before another programming competition. During the first day of his training he should solve exactly $$$1$$$ problem, during the second day — exactly $$$2$$$ problems, during the third day — exactly $$$3$$$ problems, and so on. During the $$$k$$$-th day he should solve $$$k$$$ problems. Polycarp has a list of $$$n$$$ contests, the $$$i$$$-th contest consists of $$$a_i$$$ problems. During each day Polycarp has to choose exactly one of the contests he didn't solve yet and solve it. He solves exactly $$$k$$$ problems from this contest. Other problems are discarded from it. If there are no contests consisting of at least $$$k$$$ problems that Polycarp didn't solve yet during the $$$k$$$-th day, then Polycarp stops his training. How many days Polycarp can train if he chooses the contests optimally? Input The first line of the input contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of contests. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$) — the number of problems in the $$$i$$$-th contest. Output Print one integer — the maximum number of days Polycarp can train if he chooses the contests optimally. | 1,000 | false | true | false | false | true | false | false | false | true | false | 4,895 |
36C | Problem - 36C - Codeforces =============== xa0 and Codeforces Beta Round #36") ") ") ") ") ") . You will be given the order in which Petya washes the bowls. Determine the height of the construction, i.e. the distance from the bottom of the lowest bowl to the top of the highest one. Input The first input line contains integer _n_ (1u2009≤u2009_n_u2009≤u20093000). Each of the following _n_ lines contains 3 integers _h_, _r_ and _R_ (1u2009≤u2009_h_u2009≤u200910000,u20091u2009≤u2009_r_u2009<u2009_R_u2009≤u200910000). They are the height of a bowl, the radius of its bottom and the radius of its top. The plates are given in the order Petya puts them on the table. Output Output the height of the plate pile accurate to at least 10u2009-u20096. Examples Input 2 40 10 50 60 20 30 Output 70.00000000 Input 3 50 30 80 35 25 70 40 10 90 Output 55.00000000 | 2,200 | false | false | true | false | false | false | false | false | false | false | 9,816 |
1402B | The government of Treeland wants to build a new road network. There are $$$2N$$$ cities in Treeland. The unfinished plan of the road network already contains $$$N$$$ road segments, each of which connects two cities with a straight line. No two road segments have a common point (including their endpoints). Your task is to determine $$$N-1$$$ additional road segments satisfying the following conditions: 1. Every new road segment must connect two cities with a straight line. 2. If two segments (new or old) have a common point, then this point must be an endpoint of both segments. 3. The road network connects all cities: for each pair of cities there is a path consisting of segments that connects the two cities. Input The first line of the standard input contains $$$N$$$ ($$${2 leq N leq 10^5}$$$) – the number of existing road segments. Each of the following $$$N$$$ lines contains four integers: $$$x_1,y_1, x_2, y_2$$$, where $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ are the coordinates of the endpoints of the segment $$$(-10^7 leq x_i,y_ileq 10^7)$$$. Output The standard output must contain $$$N-1$$$ lines, each of them containing four integers, $$$x_1, y_1, x_2, y_2$$$, where $$$(x_1,y_1)$$$ and $$$(x_2, y_2)$$$ are the coordinates of the cities that are the endpoints of a new road segment. If there are multiple solutions, your program may output any of them. Scoring $$$ begin{array}{ccl} hline ext{Subtask} & ext{Points} & ext{Constraints} hline 1 & 0 & ext{samples} hline 2 & 15 & ext{all input segments are vertical.} hline 3 & 15 & ext{each pair of input segments are parallel.} hline 4 & 15 & ext{each input segment is either horizontal or vertical.} hline 5 & 15 & N le 10,000 hline 6 & 40 & ext{no additional constraints} hline end{array} $$$ Example Input 5 1 3 3 6 5 1 5 3 3 3 6 5 2 1 4 1 2 3 4 2 Output 2 1 1 3 2 3 2 1 3 3 2 3 5 1 4 2 Note | 2,900 | false | false | false | false | false | false | false | false | true | false | 3,658 |
1714D | You are given some text $$$t$$$ and a set of $$$n$$$ strings $$$s_1, s_2, dots, s_n$$$. In one step, you can choose any occurrence of any string $$$s_i$$$ in the text $$$t$$$ and color the corresponding characters of the text in red. For example, if $$$t= exttt{bababa}$$$ and $$$s_1= exttt{ba}$$$, $$$s_2= exttt{aba}$$$, you can get $$$t=color{red}{ exttt{ba}} exttt{baba}$$$, $$$t= exttt{b}color{red}{ exttt{aba}} exttt{ba}$$$ or $$$t= exttt{bab}color{red}{ exttt{aba}}$$$ in one step. You want to color all the letters of the text $$$t$$$ in red. When you color a letter in red again, it stays red. In the example above, three steps are enough: Let's color $$$t[2 dots 4]=s_2= exttt{aba}$$$ in red, we get $$$t= exttt{b}color{red}{ exttt{aba}} exttt{ba}$$$; Let's color $$$t[1 dots 2]=s_1= exttt{ba}$$$ in red, we get $$$t=color{red}{ exttt{baba}} exttt{ba}$$$; Let's color $$$t[4 dots 6]=s_2= exttt{aba}$$$ in red, we get $$$t=color{red}{ exttt{bababa}}$$$. Each string $$$s_i$$$ can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily. Determine the minimum number of steps needed to color all letters $$$t$$$ in red and how to do it. If it is impossible to color all letters of the text $$$t$$$ in red, output -1. Input The first line of the input contains an integer $$$q$$$ ($$$1 le q le 100$$$)xa0—the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case contains the text $$$t$$$ ($$$1 le t le 100$$$), consisting only of lowercase Latin letters, where $$$t$$$ is the length of the text $$$t$$$. The second line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10$$$) — the number of strings in the set. This is followed by $$$n$$$ lines, each containing a string $$$s_i$$$ ($$$1 le s_i le 10$$$) consisting only of lowercase Latin letters, where $$$s_i$$$xa0— the length of string $$$s_i$$$. Output For each test case, print the answer on a separate line. If it is impossible to color all the letters of the text in red, print a single line containing the number -1. Otherwise, on the first line, print the number $$$m$$$ — the minimum number of steps it will take to turn all the letters $$$t$$$ red. Then in the next $$$m$$$ lines print pairs of indices: $$$w_j$$$ and $$$p_j$$$ ($$$1 le j le m$$$), which denote that the string with index $$$w_j$$$ was used as a substring to cover the occurrences starting in the text $$$t$$$ from position $$$p_j$$$. The pairs can be output in any order. If there are several answers, output any of them. Example Input 6 bababa 2 ba aba caba 2 bac acab abacabaca 3 aba bac aca baca 3 a c b codeforces 4 def code efo forces aaaabbbbcccceeee 4 eeee cccc aaaa bbbb Output 3 2 2 1 1 2 4 -1 4 1 1 2 6 3 3 3 7 4 3 1 1 2 2 3 1 4 2 4 5 2 1 4 3 1 4 5 2 9 1 13 Note The first test case is explained in the problem statement. In the second test case, it is impossible to color all the letters of the text in red. | 1,600 | false | true | false | true | true | false | true | false | false | false | 1,997 |
144E | The secondary diagonal of a square matrix is a diagonal going from the top right to the bottom left corner. Let's define an _n_-degree staircase as a square matrix _n_u2009×u2009_n_ containing no squares above the secondary diagonal (the picture below shows a 5-degree staircase). The squares of the _n_-degree staircase contain _m_ sportsmen. A sportsman needs one second to move to a side-neighboring square of the staircase. Before the beginning of the competition each sportsman must choose one of the shortest ways to the secondary diagonal. After the starting whistle the competition begins and all sportsmen start moving along the chosen paths. When a sportsman reaches a cell of the secondary diagonal, he stops and moves no more. The competition ends when all sportsmen reach the secondary diagonal. The competition is considered successful if during it no two sportsmen were present in the same square simultaneously. Any square belonging to the secondary diagonal also cannot contain more than one sportsman. If a sportsman at the given moment of time leaves a square and another sportsman comes to it, then they are not considered to occupy the same square simultaneously. Note that other extreme cases (for example, two sportsmen moving towards each other) are impossible as the chosen ways are the shortest ones. You are given positions of _m_ sportsmen on the staircase. Your task is to choose among them the maximum number of sportsmen for who the competition can be successful, that is, so that there existed such choice of shortest ways for the sportsmen at which no two sportsmen find themselves in the same square simultaneously. All other sportsmen that are not chosen will be removed from the staircase before the competition starts. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105). Then _m_ lines contain coordinates of sportsmen on the staircase as pairs of integers _r__i_,u2009_c__i_ (1u2009≤u2009_r__i_,u2009_c__i_u2009≤u2009_n_, _n_u2009-u2009_c__i_u2009<u2009_r__i_), where _r__i_ is the number of the staircase row, _c__i_ is the number of the staircase column (to understand the principle of numbering rows and columns see the explanatory pictures). No two sportsmen stand on the same square of the staircase. Output In the first line print the number of the chosen sportsmen. In the second line print the numbers of chosen sportsmen in any order, separating the numbers with spaces. If there are several answers, you are permitted to print any of them. The sportsmen are numbered starting from one in the order in which they are given in the input data. Note A note to the first sample. The picture shows a three-degree staircase. The arrows show the shortest paths that the sportsmen choose. | 2,200 | false | true | false | false | true | false | false | false | false | false | 9,296 |
1703C | Luca has a cypher made up of a sequence of $$$n$$$ wheels, each with a digit $$$a_i$$$ written on it. On the $$$i$$$-th wheel, he made $$$b_i$$$ moves. Each move is one of two types: up move (denoted by $$$ exttt{U}$$$): it increases the $$$i$$$-th digit by $$$1$$$. After applying the up move on $$$9$$$, it becomes $$$0$$$. down move (denoted by $$$ exttt{D}$$$): it decreases the $$$i$$$-th digit by $$$1$$$. After applying the down move on $$$0$$$, it becomes $$$9$$$. Example for $$$n=4$$$. The current sequence is 0 0 0 0. Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the number of wheels. The second line contains $$$n$$$ integers $$$a_i$$$ ($$$0 leq a_i leq 9$$$)xa0— the digit shown on the $$$i$$$-th wheel after all moves have been performed. Then $$$n$$$ lines follow, the $$$i$$$-th of which contains the integer $$$b_i$$$ ($$$1 leq b_i leq 10$$$) and $$$b_i$$$ characters that are either $$$ exttt{U}$$$ or $$$ exttt{D}$$$xa0— the number of moves performed on the $$$i$$$-th wheel, and the moves performed. $$$ exttt{U}$$$ and $$$ exttt{D}$$$ represent an up move and a down move respectively. Output For each test case, output $$$n$$$ space-separated digits xa0— the initial sequence of the cypher. Example Input 3 3 9 3 1 3 DDD 4 UDUU 2 DU 2 0 9 9 DDDDDDDDD 9 UUUUUUUUU 5 0 5 9 8 3 10 UUUUUUUUUU 3 UUD 8 UUDUUDDD 10 UUDUUDUDDU 4 UUUU Output 2 1 1 9 0 0 4 9 6 9 Note In the first test case, we can prove that initial sequence was $$$[2,1,1]$$$. In that case, the following moves were performed: On the first wheel: $$$2 xrightarrow[ exttt{D}]{} 1 xrightarrow[ exttt{D}]{} 0 xrightarrow[ exttt{D}]{} 9$$$. On the second wheel: $$$1 xrightarrow[ exttt{U}]{} 2 xrightarrow[ exttt{D}]{} 1 xrightarrow[ exttt{U}]{} 2 xrightarrow[ exttt{U}]{} 3$$$. On the third wheel: $$$1 xrightarrow[ exttt{D}]{} 0 xrightarrow[ exttt{U}]{} 1$$$. The final sequence was $$$[9,3,1]$$$, which matches the input. | 800 | false | false | true | false | false | false | true | false | false | false | 2,060 |
28E | How horrible! The empire of galactic chickens tries to conquer a beautiful city "Z", they have built a huge incubator that produces millions of chicken soldiers a day, and fenced it around. The huge incubator looks like a polygon on the plane _Oxy_ with _n_ vertices. Naturally, DravDe can't keep still, he wants to destroy the chicken empire. For sure, he will start with the incubator. DravDe is strictly outside the incubator's territory in point _A_(_x__a_,u2009_y__a_), and wants to get inside and kill all the chickens working there. But it takes a lot of doing! The problem is that recently DravDe went roller skating and has broken both his legs. He will get to the incubator's territory in his jet airplane LEVAP-41. LEVAP-41 flies at speed _V_(_x__v_,u2009_y__v_,u2009_z__v_). DravDe can get on the plane in point _A_, fly for some time, and then air drop himself. DravDe is very heavy, that's why he falls vertically at speed _F__down_, but in each point of his free fall DravDe can open his parachute, and from that moment he starts to fall at the wind speed _U_(_x__u_,u2009_y__u_,u2009_z__u_) until he lands. Unfortunately, DravDe isn't good at mathematics. Would you help poor world's saviour find such an air dropping plan, that allows him to land on the incubator's territory? If the answer is not unique, DravDe wants to find the plan with the minimum time of his flight on the plane. If the answers are still multiple, he wants to find the one with the minimum time of his free fall before opening his parachute Input The first line contains the number _n_ (3u2009≤u2009_n_u2009≤u2009104) — the amount of vertices of the fence. Then there follow _n_ lines containing the coordinates of these vertices (two integer numbers _x__i_,u2009_y__i_) in clockwise or counter-clockwise order. It's guaranteed, that the fence does not contain self-intersections. The following four lines contain coordinates of point _A_(_x__a_,u2009_y__a_), speeds _V_(_x__v_,u2009_y__v_,u2009_z__v_), _F__down_ and speed _U_(_x__u_,u2009_y__u_,u2009_z__u_). All the input numbers are integer. All the coordinates don't exceed 104 in absolute value. It's guaranteed, that _z__v_u2009>u20090 and _F__down_,u2009_z__u_u2009<u20090, and point _A_ is strictly outside the incubator's territory. Output In the first line output two numbers _t_1,u2009_t_2 such, that if DravDe air drops at time _t_1 (counting from the beginning of the flight), he lands on the incubator's territory (landing on the border is regarder as landing on the territory). If DravDe doesn't open his parachute, the second number should be equal to the duration of DravDe's falling down. If it's impossible for DravDe to get to the incubator's territory, output -1 -1. If the answer is not unique, output the answer with the minimum _t_1. If the answers are still multiple, output the answer with the minimum _t_2. Your answer must have an absolute or relative error less than 10u2009-u20096. | 2,800 | true | false | false | false | false | false | false | false | false | false | 9,854 |
1779C | Baltic, a famous chess player who is also a mathematician, has an array $$$a_1,a_2, ldots, a_n$$$, and he can perform the following operation several (possibly $$$0$$$) times: Choose some index $$$i$$$ ($$$1 leq i leq n$$$); multiply $$$a_i$$$ with $$$-1$$$, that is, set $$$a_i := -a_i$$$. Baltic's favorite number is $$$m$$$, and he wants $$$a_1 + a_2 + cdots + a_m$$$ to be the smallest of all non-empty prefix sums. More formally, for each $$$k = 1,2,ldots, n$$$ it should hold that $$$$$$a_1 + a_2 + cdots + a_k geq a_1 + a_2 + cdots + a_m.$$$$$$ Please note that multiple smallest prefix sums may exist and that it is only required that $$$a_1 + a_2 + cdots + a_m$$$ is one of them. Help Baltic find the minimum number of operations required to make $$$a_1 + a_2 + cdots + a_m$$$ the least of all prefix sums. It can be shown that a valid sequence of operations always exists. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 10,000$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq m leq n leq 2cdot 10^5$$$)xa0— the size of Baltic's array and his favorite number. The second line contains $$$n$$$ integers $$$a_1,a_2, ldots, a_n$$$ ($$$-10^9 leq a_i leq 10^9$$$)xa0— the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Note In the first example, we perform the operation $$$a_4 := -a_4$$$. The array becomes $$$[-1,-2,-3,4]$$$ and the prefix sums, $$$[a_1, a_1+a_2, a_1+a_2+a_3, a_1+a_2+a_3+a_4]$$$, are equal to $$$[-1,-3,-6,-2]$$$. Thus $$$a_1 + a_2 + a_3=-6$$$ is the smallest of all prefix sums. In the second example, we perform the operation $$$a_3 := -a_3$$$. The array becomes $$$[1,2,-3,4]$$$ with prefix sums equal to $$$[1,3,0,4]$$$. In the third and fourth examples, $$$a_1 + a_2 + cdots + a_m$$$ is already the smallest of the prefix sumsxa0— no operation needs to be performed. In the fifth example, a valid sequence of operations is: $$$a_3 := -a_3$$$, $$$a_2 := -a_2$$$, $$$a_5 := -a_5$$$. The array becomes $$$[-2,-3,5,-5,20]$$$ and its prefix sums are $$$[-2,-5,0,-5,15]$$$. Note that $$$a_1+a_2=-5$$$ and $$$a_1+a_2+a_3+a_4=-5$$$ are both the smallest of the prefix sums (and this is a valid solution). | 1,600 | false | true | false | false | true | false | false | false | false | false | 1,600 |
1927A | You have a horizontal strip of $$$n$$$ cells. Each cell is either white or black. You can choose a continuous segment of cells once and paint them all white. After this action, all the black cells in this segment will become white, and the white ones will remain white. What is the minimum length of the segment that needs to be painted white in order for all $$$n$$$ cells to become white? Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10$$$)xa0— the length of the strip. The second line of each test case contains a string $$$s$$$, consisting of $$$n$$$ characters, each of which is either 'W' or 'B'. The symbol 'W' denotes a white cell, and 'B'xa0— a black one. It is guaranteed that at least one cell of the given strip is black. Output For each test case, output a single numberxa0— the minimum length of a continuous segment of cells that needs to be painted white in order for the entire strip to become white. Example Input 8 6 WBBWBW 1 B 2 WB 3 BBW 4 BWWB 6 BWBWWB 6 WWBBWB 9 WBWBWWWBW Note In the first test case of the example for the strip "WBBWBW", the minimum length of the segment to be repainted white is $$$4$$$. It is necessary to repaint to white the segment from the $$$2$$$-nd to the $$$5$$$-th cell (the cells are numbered from $$$1$$$ from left to right). | 800 | false | true | false | false | false | false | false | false | false | false | 730 |
278A | The circle line of the Berland subway has _n_ stations. We know the distances between all pairs of neighboring stations: _d_1 is the distance between the 1-st and the 2-nd station; _d_2 is the distance between the 2-nd and the 3-rd station;... _d__n_u2009-u20091 is the distance between the _n_u2009-u20091-th and the _n_-th station; _d__n_ is the distance between the _n_-th and the 1-st station. The trains go along the circle line in both directions. Find the shortest distance between stations with numbers _s_ and _t_. Input The first line contains integer _n_ (3u2009≤u2009_n_u2009≤u2009100) — the number of stations on the circle line. The second line contains _n_ integers _d_1,u2009_d_2,u2009...,u2009_d__n_ (1u2009≤u2009_d__i_u2009≤u2009100) — the distances between pairs of neighboring stations. The third line contains two integers _s_ and _t_ (1u2009≤u2009_s_,u2009_t_u2009≤u2009_n_) — the numbers of stations, between which you need to find the shortest distance. These numbers can be the same. The numbers in the lines are separated by single spaces. Output Print a single number — the length of the shortest path between stations number _s_ and _t_. Note In the first sample the length of path 1u2009→u20092u2009→u20093 equals 5, the length of path 1u2009→u20094u2009→u20093 equals 13. In the second sample the length of path 4u2009→u20091 is 100, the length of path 4u2009→u20093u2009→u20092u2009→u20091 is 15. In the third sample the length of path 3u2009→u20091 is 1, the length of path 3u2009→u20092u2009→u20091 is 2. In the fourth sample the numbers of stations are the same, so the shortest distance equals 0. | 800 | false | false | true | false | false | false | false | false | false | false | 8,722 |
865D | Problem - 865D - 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 data structures greedy *2400 No tag edit access → Contest materials , the number of days. Following this is a line with exactly _N_ integers _p_1,u2009_p_2,u2009...,u2009_p__N_ (1u2009≤u2009_p__i_u2009≤u2009106). The price of one share of stock on the _i_-th day is given by _p__i_. Output Print the maximum amount of money you can end up with at the end of _N_ days. Examples Input 9 10 5 4 7 9 12 6 2 10 Output 20 Input 20 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 Output 41 Note In the first example, buy a share at 5, buy another at 4, sell one at 9 and another at 12. Then buy at 2 and sell at 10. The total profit is u2009-u20095u2009-u20094u2009+u20099u2009+u200912u2009-u20092u2009+u200910u2009=u200920. | 2,400 | false | true | false | false | true | true | false | false | false | false | 6,278 |
1887E | Alice suggested Bob to play a game. Bob didn't like this idea, but he couldn't refuse Alice, so he asked you to write a program that would play instead of him. The game starts with Alice taking out a grid sheet of size $$$n imes n$$$, the cells of which are initially not colored. After that she colors some $$$2n$$$ cells with colors $$$1,2,ldots, 2n$$$, respectively, and informs Bob about these cells. In one move, Bob can point to a cell that has not been colored yet and ask Alice to color that cell. Alice colors that cell with one of the $$$2n$$$ colors of her choice, informing Bob of the chosen color. Bob can make no more than $$$10$$$ moves, after which he needs to find a good set of four cells. A set of four cells is considered good if the following conditions are met: All the cells in the set are colored; No two cells in the set are colored with the same color; The centers of the cells form a rectangle with sides parallel to the grid lines. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 200$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$3 le n le 1000$$$) — the size of the grid. The $$$i$$$-th of the following $$$2n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$) — the coordinates of the cell colored with the $$$i$$$-th color. It is guaranteed that all coordinates $$$(x_i, y_i)$$$ are pairwise distinct for the same test case. After reading the input data for each test case, continue the interaction as described below. Interaction You can make no more than $$$10$$$ moves. To make a move, output "? $$$x$$$ $$$y$$$" ($$$1 leq x,y leq n$$$). In response to this, the jury's program will output a single integer from $$$1$$$ to $$$2n$$$xa0— the color of cell $$$(x,y)$$$. After all moves (it is not necessary to make all $$$10$$$ moves and it is not necessary to make at least one move), output a string in the format "! $$$x_1$$$ $$$x_2$$$ $$$y_1$$$ $$$y_2$$$" ($$$1 leq x_1, x_2, y_1, y_2 leq n, x_1 e x_2, y_1 e y_2$$$). If cells $$$(x_1, y_1)$$$, $$$(x_1, y_2)$$$, $$$(x_2, y_1)$$$, $$$(x_2, y_2)$$$ are colored with different colors, the jury's program will output "OK". After that, proceed to the next test case or terminate the program if there are no more test cases left. Otherwise, if any of the specified cells are colored with the same color or one of the cells is not colored yet, the jury's program will output "ERROR". In this case, your program should immediately terminate its execution to receive the Wrong Answer verdict. Otherwise, you may receive an arbitrary verdict. After each move or answer, do not forget to output the end of line and flush the output. Otherwise, you will get the Idleness limit exceeded verdict. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Note that the interactor is adaptive, meaning that the color Alice will use to color the cells during Bob's moves is not fixed in advance. Hacks You cannot use hacks in this problem. Example Input 2 3 1 2 1 3 2 1 2 3 3 1 3 2 1 OK 3 1 1 1 2 1 3 2 1 2 2 2 3 OK Output ? 1 1 ! 1 2 1 3 ! 1 2 1 2 Note In the first test case: In the second test case, cells with coordinates $$$(1, 1), (1, 2), (2, 1), (2, 2)$$$ are initially colored by Alice in different colors. | 3,100 | false | false | false | false | false | true | false | true | false | true | 968 |
1045E | On the surface of a newly discovered planet, which we model by a plane, explorers found remains of two different civilizations in various locations. They would like to learn more about those civilizations and to explore the area they need to build roads between some of locations. But as always, there are some restrictions: 1. Every two locations of the same civilization are connected by a unique path of roads 2. No two locations from different civilizations may have road between them (explorers don't want to accidentally mix civilizations they are currently exploring) 3. Roads must be straight line segments 4. Since intersections are expensive to build, they don't want any two roads to intersect (that is, only common point for any two roads may be at some of locations) Obviously all locations are different points in the plane, but explorers found out one more interesting information that may help you – no three locations lie on the same line! Help explorers and find a solution for their problem, or report it is impossible. Input In the first line, integer $$$n$$$ $$$(1 leq n leq 10^3)$$$ - the number of locations discovered. In next $$$n$$$ lines, three integers $$$x, y, c$$$ $$$(0 leq x, y leq 10^4, c in {0, 1})$$$ - coordinates of the location and number of civilization it belongs to. Output In first line print number of roads that should be built. In the following lines print all pairs of locations (their $$$0$$$-based indices) that should be connected with a road. If it is not possible to build roads such that all restrictions are met, print "Impossible". You should not print the quotation marks. Example Input 5 0 0 1 1 0 0 0 1 0 1 1 1 3 2 0 | 3,200 | false | false | false | false | false | true | false | false | false | false | 5,466 |
1559D2 | This is the hard version of the problem. The only difference between the two versions is the constraint on $$$n$$$. You can make hacks only if all versions of the problem are solved. A forest is an undirected graph without cycles (not necessarily connected). Mocha and Diana are friends in Zhijiang, both of them have a forest with nodes numbered from $$$1$$$ to $$$n$$$, and they would like to add edges to their forests such that: After adding edges, both of their graphs are still forests. They add the same edges. That is, if an edge $$$(u, v)$$$ is added to Mocha's forest, then an edge $$$(u, v)$$$ is added to Diana's forest, and vice versa. Mocha and Diana want to know the maximum number of edges they can add, and which edges to add. Input The first line contains three integers $$$n$$$, $$$m_1$$$ and $$$m_2$$$ ($$$1 le n le 10^5$$$, $$$0 le m_1, m_2 < n$$$) — the number of nodes and the number of initial edges in Mocha's forest and Diana's forest. Each of the next $$$m_1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) — the edges in Mocha's forest. Each of the next $$$m_2$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) — the edges in Diana's forest. Output The first line contains only one integer $$$h$$$, the maximum number of edges Mocha and Diana can add. Each of the next $$$h$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) — the edge you add each time. If there are multiple correct answers, you can print any one of them. Examples Input 3 2 2 1 2 2 3 1 2 1 3 Input 5 3 2 5 4 2 1 4 3 4 3 1 4 Output 5 5 2 2 3 3 4 4 7 6 8 Note In the first example, we cannot add any edge. In the second example, the initial forests are as follows. We can add an edge $$$(2, 4)$$$. | 2,500 | false | true | false | false | false | true | true | false | false | true | 2,832 |
1455G | Polycarp is editing a complicated computer program. First, variable $$$x$$$ is declared and assigned to $$$0$$$. Then there are instructions of two types: 1. set $$$y$$$ $$$v$$$xa0— assign $$$x$$$ a value $$$y$$$ or spend $$$v$$$ burles to remove that instruction (thus, not reassign $$$x$$$); 2. if $$$y$$$ $$$dots$$$ end blockxa0— execute instructions inside the if block if the value of $$$x$$$ is $$$y$$$ and ignore the block otherwise. if blocks can contain set instructions and other if blocks inside them. However, when the value of $$$x$$$ gets assigned to $$$s$$$, the computer breaks and immediately catches fire. Polycarp wants to prevent that from happening and spend as few burles as possible. What is the minimum amount of burles he can spend on removing set instructions to never assign $$$x$$$ to $$$s$$$? Input The first line contains two integers $$$n$$$ and $$$s$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le s le 2 cdot 10^5$$$)xa0— the number of lines in the program and the forbidden value of $$$x$$$. The following $$$n$$$ lines describe the program. Each line is one of three types: 1. set $$$y$$$ $$$v$$$ $$$(0 le y le 2 cdot 10^5, 1 le v le 10^9)$$$; 2. if $$$y$$$ $$$(0 le y le 2 cdot 10^5)$$$; 3. end. Each if instruction is matched by an end instruction. Each end instruction has an if instruction to match. Output Print a single integerxa0— the minimum amount of burles Polycarp can spend on removing set instructions to never assign $$$x$$$ to $$$s$$$. Examples Input 5 1 set 1 10 set 2 15 if 2 set 1 7 end Input 7 2 set 3 4 if 3 set 10 4 set 2 7 set 10 1 end set 4 2 Input 9 200 if 0 set 5 5 if 5 set 100 13 end if 100 set 200 1 end end | 2,900 | false | false | false | true | true | false | false | false | false | false | 3,396 |
1516A | Given an array $$$a$$$ of length $$$n$$$, you can do at most $$$k$$$ operations of the following type on it: choose $$$2$$$ different elements in the array, add $$$1$$$ to the first, and subtract $$$1$$$ from the second. However, all the elements of $$$a$$$ have to remain non-negative after this operation. What is lexicographically the smallest array you can obtain? An array $$$x$$$ is xa0– the number of test cases you need to solve. The first line of each test case contains $$$2$$$ integers $$$n$$$ and $$$k$$$ ($$$2 le n le 100$$$, $$$1 le k le 10000$$$)xa0— the number of elements in the array and the maximum number of operations you can make. The second line contains $$$n$$$ space-separated integers $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_{n}$$$ ($$$0 le a_i le 100$$$)xa0— the elements of the array $$$a$$$. Output For each test case, print the lexicographically smallest array you can obtain after at most $$$k$$$ operations. Example Input 2 3 1 3 1 4 2 10 1 0 Note In the second test case, we start by subtracting $$$1$$$ from the first element and adding $$$1$$$ to the second. Then, we can't get any lexicographically smaller arrays, because we can't make any of the elements negative. | 800 | false | true | false | false | false | false | false | false | false | false | 3,082 |
1571G | A squad of $$$n$$$ warriors is defending a castle from a dragon attack. There are $$$m$$$ barricades between the castle and the dragon. The warriors are numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th warrior knows $$$k_i$$$ attacks: the $$$j$$$-th of them deals $$$a_{i,j}$$$ damage to the dragon and can only be applied if there are exactly $$$b_{i,j}$$$ barricades between the castle and the dragon. The warriors make turns one after another, starting from warrior $$$1$$$. After warrior $$$n$$$ makes his turn, the total damage to the dragon is calculated. The $$$i$$$-th warrior performs exactly one of three possible moves in his turn: 1. destroys one barricade (if there are any left); 2. deploys one of his $$$k_i$$$ attacks; 3. skips a turn. The total damage is the sum of damages dealt by the warriors who chose to deploy their attacks in their turn. What is the maximum total damage the warriors can deal to the dragon? Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 3 cdot 10^5$$$)xa0— the number of warriors and the initial number of barricades. Then the descriptions of attacks for each warrior follow. The $$$i$$$-th description consists of three lines. The first line contains a single integer $$$k_i$$$ ($$$1 le k_i le m + 1$$$)xa0— the number of attacks the $$$i$$$-th warrior knows. The second line contains $$$k_i$$$ integers $$$a_{i,1}, a_{i,2}, dots, a_{i,k_i}$$$ ($$$1 le a_{i,j} le 10^9$$$)xa0— the damage of each attack. The third line contains $$$k_i$$$ integers $$$b_{i,1}, b_{i,2}, dots, b_{i,k_i}$$$ ($$$0 le b_{i,j} le m$$$)xa0— the required number of barricades for each attack. $$$b_{i,j}$$$ for the $$$i$$$-th warrior are pairwise distinct. The attacks are listed in the increasing order of the barricades requirement, so $$$b_{i,1} < b_{i,2} < dots < b_{i,k_i}$$$. The sum of $$$k_i$$$ over all warriors doesn't exceed $$$3 cdot 10^5$$$. Output Print a single integerxa0— the maximum total damage the warriors can deal to the dragon. Examples Input 2 4 1 2 4 2 10 5 3 4 Input 3 3 1 1 0 1 20 2 1 100 3 Input 2 1 2 10 10 0 1 2 30 20 0 1 Note In the first example, the optimal choice is the following: warrior $$$1$$$ destroys a barricade, now there are $$$3$$$ barricades left; warrior $$$2$$$ deploys his first attack (he can do it because $$$b_{1,1}=3$$$, which is the current number of barricades). The total damage is $$$10$$$. If the first warrior used his attack or skipped his turn, then the second warrior would only be able to deploy his second attack. Thus, the total damage would be $$$2+5=7$$$ or $$$5$$$. In the second example, the first warrior skips his move, the second warrior skips his move and the third warrior deploys his only attack. In the third example, two equivalent options are: both warriors deploy their second attacks; the first warrior destroys one barricade and the second warrior deploys his first attack. Both options yield $$$30$$$ total damage. | 2,500 | false | false | false | true | true | false | false | false | false | false | 2,786 |
1082G | Petya has a simple graph (that is, a graph without loops or multiple edges) consisting of $$$n$$$ vertices and $$$m$$$ edges. The weight of the $$$i$$$-th vertex is $$$a_i$$$. The weight of the $$$i$$$-th edge is $$$w_i$$$. A subgraph of a graph is some set of the graph vertices and some set of the graph edges. The set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices. The weight of a subgraph is the sum of the weights of its edges, minus the sum of the weights of its vertices. You need to find the maximum weight of subgraph of given graph. The given graph does not contain loops and multiple edges. Input The first line contains two numbers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^3, 0 le m le 10^3$$$) - the number of vertices and edges in the graph, respectively. The next line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) - the weights of the vertices of the graph. The following $$$m$$$ lines contain edges: the $$$i$$$-e edge is defined by a triple of integers $$$v_i, u_i, w_i$$$ ($$$1 le v_i, u_i le n, 1 le w_i le 10^9, v_i eq u_i$$$). This triple means that between the vertices $$$v_i$$$ and $$$u_i$$$ there is an edge of weight $$$w_i$$$. It is guaranteed that the graph does not contain loops and multiple edges. Output Print one integer — the maximum weight of the subgraph of the given graph. Examples Input 4 5 1 5 2 2 1 3 4 1 4 4 3 4 5 3 2 2 4 2 2 Input 3 3 9 7 8 1 2 1 2 3 2 1 3 3 Note In the first test example, the optimal subgraph consists of the vertices $$${1, 3, 4}$$$ and has weight $$$4 + 4 + 5 - (1 + 2 + 2) = 8$$$. In the second test case, the optimal subgraph is empty. | 2,400 | false | false | false | false | false | false | false | false | false | true | 5,309 |
1091B | Bob is a pirate looking for the greatest treasure the world has ever seen. The treasure is located at the point $$$T$$$, which coordinates to be found out. Bob travelled around the world and collected clues of the treasure location at $$$n$$$ obelisks. These clues were in an ancient language, and he has only decrypted them at home. Since he does not know which clue belongs to which obelisk, finding the treasure might pose a challenge. Can you help him? As everyone knows, the world is a two-dimensional plane. The $$$i$$$-th obelisk is at integer coordinates $$$(x_i, y_i)$$$. The $$$j$$$-th clue consists of $$$2$$$ integers $$$(a_j, b_j)$$$ and belongs to the obelisk $$$p_j$$$, where $$$p$$$ is some (unknown) permutation on $$$n$$$ elements. It means that the treasure is located at $$$T=(x_{p_j} + a_j, y_{p_j} + b_j)$$$. This point $$$T$$$ is the same for all clues. In other words, each clue belongs to exactly one of the obelisks, and each obelisk has exactly one clue that belongs to it. A clue represents the vector from the obelisk to the treasure. The clues must be distributed among the obelisks in such a way that they all point to the same position of the treasure. Your task is to find the coordinates of the treasure. If there are multiple solutions, you may print any of them. Note that you don't need to find the permutation. Permutations are used only in order to explain the problem. Input The first line contains an integer $$$n$$$xa0($$$1 leq n leq 1000$$$)xa0— the number of obelisks, that is also equal to the number of clues. Each of the next $$$n$$$ lines contains two integers $$$x_i$$$, $$$y_i$$$xa0($$$-10^6 leq x_i, y_i leq 10^6$$$)xa0— the coordinates of the $$$i$$$-th obelisk. All coordinates are distinct, that is $$$x_i eq x_j$$$ or $$$y_i eq y_j$$$ will be satisfied for every $$$(i, j)$$$ such that $$$i eq j$$$. Each of the next $$$n$$$ lines contains two integers $$$a_i$$$, $$$b_i$$$xa0($$$-2 cdot 10^6 leq a_i, b_i leq 2 cdot 10^6$$$)xa0— the direction of the $$$i$$$-th clue. All coordinates are distinct, that is $$$a_i eq a_j$$$ or $$$b_i eq b_j$$$ will be satisfied for every $$$(i, j)$$$ such that $$$i eq j$$$. It is guaranteed that there exists a permutation $$$p$$$, such that for all $$$i,j$$$ it holds $$$left(x_{p_i} + a_i, y_{p_i} + b_i ight) = left(x_{p_j} + a_j, y_{p_j} + b_j ight)$$$. Output Output a single line containing two integers $$$T_x, T_y$$$xa0— the coordinates of the treasure. If there are multiple answers, you may print any of them. Examples Input 2 2 5 -6 4 7 -2 -1 -3 Input 4 2 2 8 2 -7 0 -2 6 1 -14 16 -12 11 -18 7 -14 Note As $$$n = 2$$$, we can consider all permutations on two elements. If $$$p = [1, 2]$$$, then the obelisk $$$(2, 5)$$$ holds the clue $$$(7, -2)$$$, which means that the treasure is hidden at $$$(9, 3)$$$. The second obelisk $$$(-6, 4)$$$ would give the clue $$$(-1,-3)$$$ and the treasure at $$$(-7, 1)$$$. However, both obelisks must give the same location, hence this is clearly not the correct permutation. If the hidden permutation is $$$[2, 1]$$$, then the first clue belongs to the second obelisk and the second clue belongs to the first obelisk. Hence $$$(-6, 4) + (7, -2) = (2,5) + (-1,-3) = (1, 2)$$$, so $$$T = (1,2)$$$ is the location of the treasure. In the second sample, the hidden permutation is $$$[2, 3, 4, 1]$$$. | 1,200 | false | true | true | false | false | true | true | false | false | false | 5,258 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.