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
130A
Problem - 130A - Codeforces =============== xa0 . Output Output the _n_-th hexagonal number. Examples Input 2 Output 6 Input 5 Output 45
900
false
false
true
false
false
false
false
false
false
false
9,363
493C
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows that each successful throw has value of either 2 or 3 points. A throw is worth 2 points if the distance it was made from doesn't exceed some value of _d_ meters, and a throw is worth 3 points if the distance is larger than _d_ meters, where _d_ is some non-negative integer. Vasya would like the advantage of the points scored by the first team (the points of the first team minus the points of the second team) to be maximum. For that he can mentally choose the value of _d_. Help him to do that. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — the number of throws of the first team. Then follow _n_ integer numbers — the distances of throws _a__i_ (1u2009≤u2009_a__i_u2009≤u20092·109). Then follows number _m_ (1u2009≤u2009_m_u2009≤u20092·105) — the number of the throws of the second team. Then follow _m_ integer numbers — the distances of throws of _b__i_ (1u2009≤u2009_b__i_u2009≤u20092·109). Output Print two numbers in the format a:b — the score that is possible considering the problem conditions where the result of subtraction _a_u2009-u2009_b_ is maximum. If there are several such scores, find the one in which number _a_ is maximum. Examples Input 5 6 7 8 9 10 5 1 2 3 4 5
1,600
false
false
true
false
true
false
true
true
true
false
7,861
1210F1
This is an easier version of the problem. In this version, $$$n le 6$$$. Marek is working hard on creating strong testcases to his new algorithmic problem. You want to know what it is? Nah, we're not telling you. However, we can tell you how he generates the testcases. Marek chooses an integer $$$n$$$ and $$$n^2$$$ integers $$$p_{ij}$$$ ($$$1 le i le n$$$, $$$1 le j le n$$$). He then generates a random bipartite graph with $$$2n$$$ vertices. There are $$$n$$$ vertices on the left side: $$$ell_1, ell_2, dots, ell_n$$$, and $$$n$$$ vertices on the right side: $$$r_1, r_2, dots, r_n$$$. For each $$$i$$$ and $$$j$$$, he puts an edge between vertices $$$ell_i$$$ and $$$r_j$$$ with probability $$$p_{ij}$$$ percent. It turns out that the tests will be strong only if a perfect matching exists in the generated graph. What is the probability that this will occur? It can be shown that this value can be represented as $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are coprime integers and $$$Q otequiv 0 pmod{10^9+7}$$$. Let $$$Q^{-1}$$$ be an integer for which $$$Q cdot Q^{-1} equiv 1 pmod{10^9+7}$$$. Print the value of $$$P cdot Q^{-1}$$$ modulo $$$10^9+7$$$. Input The first line of the input contains a single integer $$$n$$$ ($$$mathbf{1 le n le 6}$$$). The following $$$n$$$ lines describe the probabilities of each edge appearing in the graph. The $$$i$$$-th of the lines contains $$$n$$$ integers $$$p_{i1}, p_{i2}, dots, p_{in}$$$ ($$$0 le p_{ij} le 100$$$); $$$p_{ij}$$$ denotes the probability, in percent, of an edge appearing between $$$ell_i$$$ and $$$r_j$$$. Output Print a single integer — the probability that the perfect matching exists in the bipartite graph, written as $$$P cdot Q^{-1} pmod{10^9+7}$$$ for $$$P$$$, $$$Q$$$ defined above. Note In the first sample test, each of the $$$16$$$ graphs below is equally probable. Out of these, $$$7$$$ have a perfect matching: Therefore, the probability is equal to $$$frac{7}{16}$$$. As $$$16 cdot 562,500,004 = 1 pmod{10^9+7}$$$, the answer to the testcase is $$$7 cdot 562,500,004 mod{(10^9+7)} = 937,500,007$$$.
3,100
false
false
false
false
false
false
true
false
false
false
4,631
721A
Recently Adaltik discovered japanese crosswords. Japanese crossword is a picture, represented as a table sized _a_u2009×u2009_b_ squares, and each square is colored white or black. There are integers to the left of the rows and to the top of the columns, encrypting the corresponding row or column. The number of integers represents how many groups of black squares there are in corresponding row or column, and the integers themselves represents the number of consecutive black squares in corresponding group (you can find more detailed explanation in Wikipedia [ decided that the general case of japanese crossword is too complicated and drew a row consisting of _n_ squares (e.g. japanese crossword sized 1u2009×u2009_n_), which he wants to encrypt in the same way as in japanese crossword. The example of encrypting of a single row of japanese crossword. Help Adaltik find the numbers encrypting the row he drew. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100)xa0— the length of the row. The second line of the input contains a single string consisting of _n_ characters 'B' or 'W', ('B' corresponds to black square, 'W'xa0— to white square in the row that Adaltik drew). Output The first line should contain a single integer _k_xa0— the number of integers encrypting the row, e.g. the number of groups of black squares in the row. The second line should contain _k_ integers, encrypting the row, e.g. corresponding to sizes of groups of consecutive black squares in the order from left to right. Note The last sample case correspond to the picture in the statement.
800
false
false
true
false
false
false
false
false
false
false
6,931
1761D
Problem - 1761D - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics math *2100 No tag edit access → Contest materials ") Editorial") $$$ be the number of carries of $$$x+y$$$ in binary (i.xa0e. $$$f(x,y)=g(x)+g(y)-g(x+y)$$$, where $$$g(x)$$$ is the number of ones in the binary representation of $$$x$$$). Given two integers $$$n$$$ and $$$k$$$, find the number of ordered pairs $$$(a,b)$$$ such that $$$0 leq a,b < 2^n$$$, and $$$f(a,b)$$$ equals $$$k$$$. Note that for $$$a e b$$$, $$$(a,b)$$$ and $$$(b,a)$$$ are considered as two different pairs. As this number may be large, output it modulo $$$10^9+7$$$. Input The only line of each test contains two integers $$$n$$$ and $$$k$$$ ($$$0leq k<nleq 10^6$$$). Output Output a single integer xa0— the answer modulo $$$10^9+7$$$. Examples Input 3 1 Output 15 Input 3 0 Output 27 Input 998 244 Output 573035660 Note Here are some examples for understanding carries: $$$$$$ begin{aligned} &begin{array}{r} 1_{ }1_{ }1 + _{1}1_{ }0_{ }0 hline 1_{ }0_{ }1_{ }1 end{array} &begin{array}{r} 1_{ }0_{ }1 + _{ }0_{ }0_{1}1 hline 0_{ }1_{ }1_{ }0 end{array} & &begin{array}{r} 1_{ }0_{ }1 + _{1}0_{1}1_{1}1 hline 1_{ }0_{ }0_{ }0 end{array} end{aligned} $$$$$$ So $$$f(7,4)=1$$$, $$$f(5,1)=1$$$ and $$$f(5,3)=3$$$. In the first test case, all the pairs meeting the constraints are $$$(1,1),(1,5),(2,2),(2,3),(3,2),(4,4),(4,5),(4,6),(4,7),(5,1),(5,4),(5,6),(6,4),(6,5),(7,4)$$$.
2,100
true
false
false
false
false
false
false
false
false
false
1,745
366C
Dima, Inna and Seryozha have gathered in a room. That's right, someone's got to go. To cheer Seryozha up and inspire him to have a walk, Inna decided to cook something. Dima and Seryozha have _n_ fruits in the fridge. Each fruit has two parameters: the taste and the number of calories. Inna decided to make a fruit salad, so she wants to take some fruits from the fridge for it. Inna follows a certain principle as she chooses the fruits: the total taste to the total calories ratio of the chosen fruits must equal _k_. In other words, , where _a__j_ is the taste of the _j_-th chosen fruit and _b__j_ is its calories. Inna hasn't chosen the fruits yet, she is thinking: what is the maximum taste of the chosen fruits if she strictly follows her principle? Help Inna solve this culinary problem — now the happiness of a young couple is in your hands! Inna loves Dima very much so she wants to make the salad from at least one fruit. Input The first line of the input contains two integers _n_, _k_ (1u2009≤u2009_n_u2009≤u2009100,u20091u2009≤u2009_k_u2009≤u200910). The second line of the input contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100) — the fruits' tastes. The third line of the input contains _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u2009100) — the fruits' calories. Fruit number _i_ has taste _a__i_ and calories _b__i_. Output If there is no way Inna can choose the fruits for the salad, print in the single line number -1. Otherwise, print a single integer — the maximum possible sum of the taste values of the chosen fruits. Examples Input 5 3 4 4 4 4 4 2 2 2 2 2 Note In the first test sample we can get the total taste of the fruits equal to 18 if we choose fruit number 1 and fruit number 2, then the total calories will equal 9. The condition fulfills, that's exactly what Inna wants. In the second test sample we cannot choose the fruits so as to follow Inna's principle.
1,900
false
false
false
true
false
false
false
false
false
false
8,368
263D
You've got a undirected graph _G_, consisting of _n_ nodes. We will consider the nodes of the graph indexed by integers from 1 to _n_. We know that each node of graph _G_ is connected by edges with at least _k_ other nodes of this graph. Your task is to find in the given graph a simple cycle of length of at least _k_u2009+u20091. A simple cycle of length _d_ (_d_u2009>u20091) in graph _G_ is a sequence of distinct graph nodes _v_1,u2009_v_2,u2009...,u2009_v__d_ such, that nodes _v_1 and _v__d_ are connected by an edge of the graph, also for any integer _i_ (1u2009≤u2009_i_u2009<u2009_d_) nodes _v__i_ and _v__i_u2009+u20091 are connected by an edge of the graph. Input The first line contains three integers _n_, _m_, _k_ (3u2009≤u2009_n_,u2009_m_u2009≤u2009105;xa02u2009≤u2009_k_u2009≤u2009_n_u2009-u20091) — the number of the nodes of the graph, the number of the graph's edges and the lower limit on the degree of the graph node. Next _m_ lines contain pairs of integers. The _i_-th line contains integers _a__i_, _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa0_a__i_u2009≠u2009_b__i_) — the indexes of the graph nodes that are connected by the _i_-th edge. It is guaranteed that the given graph doesn't contain any multiple edges or self-loops. It is guaranteed that each node of the graph is connected by the edges with at least _k_ other nodes of the graph. Output In the first line print integer _r_ (_r_u2009≥u2009_k_u2009+u20091) — the length of the found cycle. In the next line print _r_ distinct integers _v_1,u2009_v_2,u2009...,u2009_v__r_ (1u2009≤u2009_v__i_u2009≤u2009_n_) — the found simple cycle. It is guaranteed that the answer exists. If there are multiple correct answers, you are allowed to print any of them. Examples Input 4 6 3 4 3 1 2 1 3 1 4 2 3 2 4
1,800
false
false
false
false
false
false
false
false
false
true
8,780
1945B
One of the days of the hike coincided with a holiday, so in the evening at the camp, it was decided to arrange a festive fireworks display. For this purpose, the organizers of the hike bought two installations for launching fireworks and a huge number of shells for launching. Both installations are turned on simultaneously. The first installation launches fireworks every $$$a$$$ minutes (i.e., after $$$a, 2 cdot a, 3 cdot a, dots$$$ minutes after launch). The second installation launches fireworks every $$$b$$$ minutes (i.e., after $$$b, 2 cdot b, 3 cdot b, dots$$$ minutes after launch). Each firework is visible in the sky for $$$m + 1$$$ minutes after launch, i.e., if a firework was launched after $$$x$$$ minutes after the installations were turned on, it will be visible every minute from $$$x$$$ to $$$x + m$$$, inclusive. If one firework was launched $$$m$$$ minutes after another, both fireworks will be visible for one minute. What is the maximum number of fireworks that could be seen in the sky at the same time? Input Each test consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Then follow the descriptions of the test cases. The first and only line of each test case contains integers $$$a$$$, $$$b$$$, $$$m$$$ ($$$1 le a, b, m le 10^{18}$$$)xa0— the frequency of launching for the first installation, the second installation, and the time the firework is visible in the sky. Note In the first set of input data, the fireworks are visible in the sky for $$$5$$$ minutes. Since the first installation launches fireworks every $$$6$$$ minutes, and the second one every $$$7$$$ minutes, two fireworks launched from the same installation will not be visible in the sky at the same time. At the same time, after $$$7$$$ minutes from the start of the holiday, one firework from the first and one from the second camp will be visible. Thus, it is possible to see no more than $$$2$$$ fireworks simultaneously. In the third set of input data, $$$17$$$ fireworks will be visible after $$$112$$$ minutes: $$$9$$$ fireworks launched from the first installation at times [$$$56, 63, 70, 77, 84, 91, 98, 105, 112$$$]; $$$8$$$ fireworks launched from the second installation at times [$$$56, 64, 72, 80, 88, 96, 104, 112$$$].
900
true
false
false
false
false
false
false
false
false
false
612
1466C
After his wife's tragic death, Eurydice, Orpheus descended to the realm of death to see her. Reaching its gates was uneasy, but passing through them proved to be even more challenging. Mostly because of Cerberus, the three-headed hound of Hades. Orpheus, a famous poet, and musician plans to calm Cerberus with his poetry and safely walk past him. He created a very peculiar poem for Cerberus. It consists only of lowercase English letters. We call a poem's substring a palindrome if and only if it reads the same backwards and forwards. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deleting several (possibly zero or all) characters from the beginning and several (possibly zero or all) characters from the end. Unfortunately, Cerberus dislikes palindromes of length greater than $$$1$$$. For example in the poem abaa the hound of Hades wouldn't like substrings aba and aa. Orpheus can only calm Cerberus if the hound likes his poetry. That's why he wants to change his poem so that it does not contain any palindrome substrings of length greater than $$$1$$$. Orpheus can modify the poem by replacing a letter at any position with any lowercase English letter. He can use this operation arbitrarily many times (possibly zero). Since there can be many palindromes in his poem, he may have to make some corrections. But how many, exactly? Given the poem, determine the minimal number of letters that have to be changed so that the poem does not contain any palindromes of length greater than $$$1$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 10^5$$$) denoting the number of test cases, then $$$t$$$ test cases follow. The first and only line of each test case contains a non-empty string of lowercase English letters, Orpheus' poem. The sum of the length of Orpheus' poems in all test cases will not exceed $$$10^5$$$. Output You should output $$$t$$$ lines, $$$i$$$-th line should contain a single integer, answer to the $$$i$$$-th test case. Note In the first test case, we can replace the third character with c and obtain a palindrome-less poem bacba. In the second test case, we can replace the third character with d and obtain a palindrome-less poem abdac. In the third test case, the initial poem already doesn't contain any palindromes, so Orpheus doesn't need to change anything there.
1,300
false
true
false
true
false
false
false
false
false
false
3,364
1651F
Monocarp is playing a tower defense game. A level in the game can be represented as an OX axis, where each lattice point from $$$1$$$ to $$$n$$$ contains a tower in it. The tower in the $$$i$$$-th point has $$$c_i$$$ mana capacity and $$$r_i$$$ mana regeneration rate. In the beginning, before the $$$0$$$-th second, each tower has full mana. If, at the end of some second, the $$$i$$$-th tower has $$$x$$$ mana, then it becomes $$$mathit{min}(x + r_i, c_i)$$$ mana for the next second. There are $$$q$$$ monsters spawning on a level. The $$$j$$$-th monster spawns at point $$$1$$$ at the beginning of $$$t_j$$$-th second, and it has $$$h_j$$$ health. Every monster is moving $$$1$$$ point per second in the direction of increasing coordinate. When a monster passes the tower, the tower deals $$$mathit{min}(H, M)$$$ damage to it, where $$$H$$$ is the current health of the monster and $$$M$$$ is the current mana amount of the tower. This amount gets subtracted from both monster's health and tower's mana. Unfortunately, sometimes some monsters can pass all $$$n$$$ towers and remain alive. Monocarp wants to know what will be the total health of the monsters after they pass all towers. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the number of towers. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$c_i$$$ and $$$r_i$$$ ($$$1 le r_i le c_i le 10^9$$$)xa0— the mana capacity and the mana regeneration rate of the $$$i$$$-th tower. The next line contains a single integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0— the number of monsters. The $$$j$$$-th of the next $$$q$$$ lines contains two integers $$$t_j$$$ and $$$h_j$$$ ($$$0 le t_j le 2 cdot 10^5$$$; $$$1 le h_j le 10^{12}$$$)xa0— the time the $$$j$$$-th monster spawns and its health. The monsters are listed in the increasing order of their spawn time, so $$$t_j < t_{j+1}$$$ for all $$$1 le j le q-1$$$.
3,000
false
false
false
false
true
false
true
true
false
false
2,363
1735C
There was a string $$$s$$$ which was supposed to be encrypted. For this reason, all $$$26$$$ lowercase English letters were arranged in a circle in some order, afterwards, each letter in $$$s$$$ was replaced with the one that follows in clockwise order, in that way the string $$$t$$$ was obtained. You are given a string $$$t$$$. Determine the lexicographically smallest string $$$s$$$ that could be a prototype of the given string $$$t$$$. A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ of the same length if and only if: in the first position where $$$a$$$ and $$$b$$$ differ, the string $$$a$$$ has a letter, that appears earlier in the alphabet than the corresponding letter in $$$b$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 3 cdot 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$) — the length of the string $$$t$$$. The next line contains the string $$$t$$$ of the length $$$n$$$, containing lowercase English letters. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, output a single line containing the lexicographically smallest string $$$s$$$ which could be a prototype of $$$t$$$. Example Input 5 1 a 2 ba 10 codeforces 26 abcdefghijklmnopqrstuvwxyz 26 abcdefghijklmnopqrstuvwxzy Output b ac abcdebfadg bcdefghijklmnopqrstuvwxyza bcdefghijklmnopqrstuvwxyaz Note In the first test case, we couldn't have the string "a", since the letter a would transit to itself. Lexicographically the second string "b" is suitable as an answer. In the second test case, the string "aa" is not suitable, since a would transit to itself. "ab" is not suitable, since the circle would be closed with $$$2$$$ letters, but it must contain all $$$26$$$. The next string "ac" is suitable. Below you can see the schemes for the first three test cases. The non-involved letters are skipped, they can be arbitrary placed in the gaps.
1,400
false
true
true
false
false
false
false
false
false
true
1,878
1841C
No, not "random" numbers. Ranom digits are denoted by uppercase Latin letters from A to E. Moreover, the value of the letter A is $$$1$$$, B is $$$10$$$, C is $$$100$$$, D is $$$1000$$$, E is $$$10000$$$. A Ranom number is a sequence of Ranom digits. The value of the Ranom number is calculated as follows: the values of all digits are summed up, but some digits are taken with negative signs: a digit is taken with negative sign if there is a digit with a strictly greater value to the right of it (not necessarily immediately after it); otherwise, that digit is taken with a positive sign. For example, the value of the Ranom number DAAABDCA is $$$1000 - 1 - 1 - 1 - 10 + 1000 + 100 + 1 = 2088$$$. You are given a Ranom number. You can change no more than one digit in it. Calculate the maximum possible value of the resulting number. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The only line of each test case contains a string $$$s$$$ ($$$1 le s le 2 cdot 10^5$$$) consisting of uppercase Latin letters from A to E — the Ranom number you are given. The sum of the string lengths over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0— the maximum possible value of the number, if you can change no more than one digit in it. Example Input 4 DAAABDCA AB ABCDEEDCBA DDDDAAADDABECD Output 11088 10010 31000 15886 Note In the first example, you can get EAAABDCA with the value $$$10000-1-1-1-10+1000+100+1=11088$$$. In the second example, you can get EB with the value $$$10000+10=10010$$$.
1,800
true
true
false
true
false
false
true
false
false
false
1,243
344B
Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule. A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number — the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number. Mike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible. Input The single line of the input contains three space-separated integers _a_, _b_ and _c_ (1u2009≤u2009_a_,u2009_b_,u2009_c_u2009≤u2009106) — the valence numbers of the given atoms. Output If such a molecule can be built, print three space-separated integers — the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print "Impossible" (without the quotes). Note The first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case. The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms. The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself. The configuration in the fourth figure is impossible as each atom must have at least one atomic bond.
1,200
true
false
false
false
false
false
true
false
false
true
8,457
1202D
The subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. You are given an integer $$$n$$$. You have to find a sequence $$$s$$$ consisting of digits $$${1, 3, 7}$$$ such that it has exactly $$$n$$$ subsequences equal to $$$1337$$$. For example, sequence $$$337133377$$$ has $$$6$$$ subsequences equal to $$$1337$$$: 1. $$$337underline{1}3underline{3}underline{3}7underline{7}$$$ (you can remove the second and fifth characters); 2. $$$337underline{1}underline{3}3underline{3}7underline{7}$$$ (you can remove the third and fifth characters); 3. $$$337underline{1}underline{3}underline{3}37underline{7}$$$ (you can remove the fourth and fifth characters); 4. $$$337underline{1}3underline{3}underline{3}underline{7}7$$$ (you can remove the second and sixth characters); 5. $$$337underline{1}underline{3}3underline{3}underline{7}7$$$ (you can remove the third and sixth characters); 6. $$$337underline{1}underline{3}underline{3}3underline{7}7$$$ (you can remove the fourth and sixth characters). Note that the length of the sequence $$$s$$$ must not exceed $$$10^5$$$. You have to answer $$$t$$$ independent queries. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10$$$) — the number of queries. Next $$$t$$$ lines contains a description of queries: the $$$i$$$-th line contains one integer $$$n_i$$$ ($$$1 le n_i le 10^9$$$). Output For the $$$i$$$-th query print one string $$$s_i$$$ ($$$1 le s_i le 10^5$$$) consisting of digits $$${1, 3, 7}$$$. String $$$s_i$$$ must have exactly $$$n_i$$$ subsequences $$$1337$$$. If there are multiple such strings, print any of them.
1,900
true
false
false
false
false
true
false
false
false
false
4,686
1976C
Monocarp is opening his own IT company. He wants to hire $$$n$$$ programmers and $$$m$$$ testers. There are $$$n+m+1$$$ candidates, numbered from $$$1$$$ to $$$n+m+1$$$ in chronological order of their arriving time. The $$$i$$$-th candidate has programming skill $$$a_i$$$ and testing skill $$$b_i$$$ (a person's programming skill is different from their testing skill). The skill of the team is the sum of the programming skills of all candidates hired as programmers, and the sum of the testing skills of all candidates hired as testers. When a candidate arrives to interview, Monocarp tries to assign them to the most suitable position for them (if their programming skill is higher, then he hires them as a programmer, otherwise as a tester). If all slots for that position are filled, Monocarp assigns them to the other position. Your task is, for each candidate, calculate the skill of the team if everyone except them comes to interview. Note that it means that exactly $$$n+m$$$ candidates will arrive, so all $$$n+m$$$ positions in the company will be filled. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each test case consists of three lines: the first line contains two integers $$$n$$$ and $$$m$$$ ($$$0 le n, m le 2 cdot 10^5$$$; $$$2 le n + m + 1 le 2 cdot 10^5$$$)xa0— the number of programmers and the number of testers Monocarp wants to hire, respectively; the second line contains $$$n + m + 1$$$ integers $$$a_1, a_2, dots, a_{n+m+1}$$$ ($$$1 le a_i le 10^9$$$), where $$$a_i$$$ is the programming skill of the $$$i$$$-th candidate; the third line contains $$$n + m + 1$$$ integers $$$b_1, b_2, dots, b_{n+m+1}$$$ ($$$1 le b_i le 10^9$$$; $$$b_i e a_i$$$), where $$$b_i$$$ is the testing skill of the $$$i$$$-th candidate. Additional constraint on the input: the sum of $$$(n + m + 1)$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print $$$n + m + 1$$$ integers, where the $$$i$$$-th integer should be equal to the skill of the team if everyone except the $$$i$$$-th candidate comes to interview. Example Input 4 1 0 2 1 1 2 0 2 4 5 5 5 4 1 1 2 2 1 5 4 5 2 3 1 3 1 4 3 3 4 1 5 5 4 5 2 Output 1 2 5 6 9 8 11 11 12 13 13 13 12 15 Note Let's consider the third test case of the example: if the $$$1$$$-st candidate does not arrive, the $$$2$$$-nd candidate gets hired as a tester, the $$$3$$$-rd candidate gets hired as a programmer, the $$$4$$$-th candidate gets hired as a tester. The total skill of the team will be $$$2 + 5 + 1 = 8$$$; if the $$$2$$$-nd candidate does not arrive, the $$$1$$$-st candidate gets hired as a tester, the $$$3$$$-rd candidate gets hired as a programmer, the $$$4$$$-th candidate gets hired as a tester. The total skill of the team will be $$$5 + 5 + 1 = 11$$$; if the $$$3$$$-rd candidate does not arrive, the $$$1$$$-st candidate gets hired as a tester, the $$$2$$$-nd candidate gets hired as a tester, the $$$4$$$-th candidate gets hired as a programmer. The total skill of the team will be $$$5 + 2 + 4 = 11$$$; if the $$$4$$$-th candidate does not arrive, the $$$1$$$-st candidate gets hired as a tester, the $$$2$$$-nd candidate gets hired as a tester, the $$$3$$$-rd candidate gets hired as a programmer. The total skill of the team will be $$$5 + 2 + 5 = 12$$$.
1,600
false
true
true
true
false
false
false
true
false
false
431
1482D
Arkady has a playlist that initially consists of $$$n$$$ songs, numerated from $$$1$$$ to $$$n$$$ in the order they appear in the playlist. Arkady starts listening to the songs in the playlist one by one, starting from song $$$1$$$. The playlist is cycled, i.xa0e. after listening to the last song, Arkady will continue listening from the beginning. Each song has a genre $$$a_i$$$, which is a positive integer. Let Arkady finish listening to a song with genre $$$y$$$, and the genre of the next-to-last listened song be $$$x$$$. If $$$operatorname{gcd}(x, y) = 1$$$, he deletes the last listened song (with genre $$$y$$$) from the playlist. After that he continues listening normally, skipping the deleted songs, and forgetting about songs he listened to before. In other words, after he deletes a song, he can't delete the next song immediately. Here $$$operatorname{gcd}(x, y)$$$ denotes the = 1$$$) $$$ o$$$ [5, 2, 10, 15] $$$ o$$$ [5, 2, 10, 15] $$$ o$$$ [5, 2, 10, 15] $$$ o$$$ [5, 2, 10, 15] $$$ o$$$ [5, 2, 10, 15] $$$ o$$$ [5, 10, 15] (because $$$operatorname{gcd}(5, 2) = 1$$$) $$$ o$$$ [5, 10, 15] $$$ o$$$ [5, 10, 15] $$$ o$$$ ... The bold numbers represent the two last played songs. Note that after a song is deleted, Arkady forgets that he listened to that and the previous songs. Given the initial playlist, please determine which songs are eventually deleted and the order these songs are deleted. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10,000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the number of songs. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the genres of the songs. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print a single line. First, print a single integer $$$k$$$xa0— the number of deleted songs. After that print $$$k$$$ distinct integers: deleted songs in the order of their deletion. Example Input 5 5 5 9 2 10 15 6 1 2 4 2 4 2 2 1 2 1 1 1 2 Output 2 2 3 2 2 1 2 2 1 1 1 0 Note Explanation of the first test case is given in the statement. In the second test case, the playlist is converted as follows: [1, 2, 4, 2, 4, 2] $$$ o$$$ [1, 2, 4, 2, 4, 2] $$$ o$$$ [1, 4, 2, 4, 2] (because $$$operatorname{gcd}(1, 2) = 1$$$) $$$ o$$$ [1, 4, 2, 4, 2] $$$ o$$$ [1, 4, 2, 4, 2] $$$ o$$$ [1, 4, 2, 4, 2] $$$ o$$$ [1, 4, 2, 4, 2] $$$ o$$$ [1, 4, 2, 4, 2] $$$ o$$$ [4, 2, 4, 2] (because $$$operatorname{gcd}(2, 1) = 1$$$) $$$ o$$$ [4, 2, 4, 2] $$$ o$$$ ... In the third test case, the playlist is converted as follows: [1, 2] $$$ o$$$ [1, 2] $$$ o$$$ [1] (because $$$operatorname{gcd}(1, 2) = 1$$$) $$$ o$$$ [1] $$$ o$$$ [1] (Arkady listened to the same song twice in a row) $$$ o$$$ [] (because $$$operatorname{gcd}(1, 1) = 1$$$). The fourth test case is same as the third after deletion of the second song. In the fifth test case, the same song is listened to over and over again, but since $$$operatorname{gcd}(2, 2) e 1$$$, it is not deleted.
1,900
false
false
true
false
true
false
false
false
false
false
3,264
183B
The Zoo in the Grid Kingdom is represented by an infinite grid. The Zoo has _n_ observation binoculars located at the _OX_ axis. For each _i_ between 1 and _n_, inclusive, there exists a single binocular located at the point with coordinates (_i_,u20090). There are _m_ flamingos in the Zoo, located at points with positive coordinates. The flamingos are currently sleeping and you can assume that they don't move. In order to get a good view over the flamingos, each of the binoculars can be independently rotated to face any angle (not necessarily integer). Then, the binocular can be used to observe all flamingos that is located at the straight line passing through the binocular at the angle it is set. In other words, you can assign each binocular a direction corresponding to any straight line passing through the binocular, and the binocular will be able to see all flamingos located on that line. Today, some kids from the prestigious Codeforces kindergarten went on a Field Study to the Zoo. Their teacher would like to set each binocular an angle to maximize the number of flamingos that can be seen by the binocular. The teacher is very interested in the sum of these values over all binoculars. Please help him find this sum. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009106,u20091u2009≤u2009_m_u2009≤u2009250), denoting the number of binoculars and the number of flamingos, respectively. Then _m_ lines follow, the _i_-th line will contain two space-separated integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009109), which means that the _i_-th flamingo is located at point (_x__i_,u2009_y__i_). All flamingos will be located at distinct points. Output Print a single integer denoting the maximum total number of flamingos that can be seen by all the binoculars. Note This picture shows the answer to the example test case.
1,700
false
false
false
false
false
false
true
false
false
false
9,108
1057C
There are $$$n$$$ candy boxes in front of Tania. The boxes are arranged in a row from left to right, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th box contains $$$r_i$$$ candies, candies have the color $$$c_i$$$ (the color can take one of three values u200bu200b— red, green, or blue). All candies inside a single box have the same color (and it is equal to $$$c_i$$$). Initially, Tanya is next to the box number $$$s$$$. Tanya can move to the neighbor box (that is, with a number that differs by one) or eat candies in the current box. Tanya eats candies instantly, but the movement takes one second. If Tanya eats candies from the box, then the box itself remains in place, but there is no more candies in it. In other words, Tanya always eats all the candies from the box and candies in the boxes are not refilled. It is known that Tanya cannot eat candies of the same color one after another (that is, the colors of candies in two consecutive boxes from which she eats candies are always different). In addition, Tanya's appetite is constantly growing, so in each next box from which she eats candies, there should be strictly more candies than in the previous one. Note that for the first box from which Tanya will eat candies, there are no restrictions on the color and number of candies. Tanya wants to eat at least $$$k$$$ candies. What is the minimum number of seconds she will need? Remember that she eats candies instantly, and time is spent only on movements. Input The first line contains three integers $$$n$$$, $$$s$$$ and $$$k$$$ ($$$1 le n le 50$$$, $$$1 le s le n$$$, $$$1 le k le 2000$$$) — number of the boxes, initial position of Tanya and lower bound on number of candies to eat. The following line contains $$$n$$$ integers $$$r_i$$$ ($$$1 le r_i le 50$$$) — numbers of candies in the boxes. The third line contains sequence of $$$n$$$ letters 'R', 'G' and 'B', meaning the colors of candies in the correspondent boxes ('R' for red, 'G' for green, 'B' for blue). Recall that each box contains candies of only one color. The third line contains no spaces. Note The sequence of actions of Tanya for the first example: move from the box $$$3$$$ to the box $$$2$$$; eat candies from the box $$$2$$$; move from the box $$$2$$$ to the box $$$3$$$; eat candy from the box $$$3$$$; move from the box $$$3$$$ to the box $$$4$$$; move from the box $$$4$$$ to the box $$$5$$$; eat candies from the box $$$5$$$. Since Tanya eats candy instantly, the required time is four seconds.
2,000
false
false
false
true
false
false
false
false
false
false
5,422
1719B
A new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — $$$n$$$ and $$$k$$$, where $$$n$$$ is even. Next, he takes all the integers from $$$1$$$ to $$$n$$$, and splits them all into pairs $$$(a, b)$$$ (each integer must be in exactly one pair) so that for each pair the integer $$$(a + k) cdot b$$$ is divisible by $$$4$$$ (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible. Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers $$$n$$$ and $$$k$$$. Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The following is a description of the input data sets. The single line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 2 cdot 10^5$$$, $$$0 leq k leq 10^9$$$, $$$n$$$ is even) — the number of integers and the number being added $$$k$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, first output the string "YES" if there is a split into pairs, and "NO" if there is none. If there is a split, then in the following $$$frac{n}{2}$$$ lines output pairs of the split, in each line print $$$2$$$ numbers — first the integer $$$a$$$, then the integer $$$b$$$. Example Output YES 1 2 3 4 NO YES 3 4 7 8 11 12 2 1 6 5 10 9 YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Note In the first test case, splitting into pairs $$$(1, 2)$$$ and $$$(3, 4)$$$ is suitable, same as splitting into $$$(1, 4)$$$ and $$$(3, 2)$$$. In the second test case, $$$(1 + 0) cdot 2 = 1 cdot (2 + 0) = 2$$$ is not divisible by $$$4$$$, so there is no partition.
800
true
false
false
false
false
true
false
false
false
false
1,967
1913E
You are given a matrix $$$a$$$, consisting of $$$n$$$ rows by $$$m$$$ columns. Each element of the matrix is equal to $$$0$$$ or $$$1$$$. You can perform the following operation any number of times (possibly zero): choose an element of the matrix and replace it with either $$$0$$$ or $$$1$$$. You are also given two arrays $$$A$$$ and $$$B$$$ (of length $$$n$$$ and $$$m$$$ respectively). After you perform the operations, the matrix should satisfy the following conditions: 1. the number of ones in the $$$i$$$-th row of the matrix should be exactly $$$A_i$$$ for every $$$i in [1, n]$$$. 2. the number of ones in the $$$j$$$-th column of the matrix should be exactly $$$B_j$$$ for every $$$j in [1, m]$$$. Calculate the minimum number of operations you have to perform. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 50$$$). Then $$$n$$$ lines follow. The $$$i$$$-th of them contains $$$m$$$ integers $$$a_{i,1}, a_{i,2}, dots, a_{i,m}$$$ ($$$0 le a_{i,j} le 1$$$). The next line contains $$$n$$$ integers $$$A_1, A_2, dots, A_n$$$ ($$$0le A_ile m$$$). The next line contains $$$m$$$ integers $$$B_1, B_2, dots, B_m$$$ ($$$0le B_ile n$$$). Output Print one integer — the minimum number of operations you have to perform, or -1 if it is impossible. Examples Input 3 3 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 Input 3 3 1 1 1 1 1 1 1 1 1 3 2 1 1 2 3
2,400
false
false
false
false
false
false
false
false
false
true
821
1799H
You are given a tree with $$$n$$$ vertices. A hero $$$k$$$ times do the following operation: Choose some edge. Remove it. Take one of the two remaining parts and delete it. Write the number of vertices in the remaining part. You are given an initial tree and the a sequence of written numbers. Find the number of ways to make operations such that the written numbers are equal to the given numbers. Due to the answer can be big, find it by modulo $$$998,244,353$$$. Two ways are considered different, if on some operation edge or remaining part are selected differently. Input The first line contains a single integer $$$n$$$ ($$$2 leq n leq 5000$$$) — the number of vertices. Each of the next $$$n-1$$$ lines contains two integers $$$s$$$, $$$f$$$ ($$$1 leq s, f leq n$$$, $$$s eq f$$$) — description of edge $$$(s, f)$$$. Next line contains a single integer $$$k$$$ ($$$1 leq k leq min{(6, n - 1)}$$$) — the number of operations. Next line contains $$$k$$$ integers $$$s_1, s_2, ldots, s_k$$$ ($$$n > s_1 > s_2 > ldots > s_k geq 1$$$) — written numbers. Output Print a single integer — the answer to the problem by modulo $$$998,244,353$$$. Examples Input 7 2 1 3 2 4 1 5 3 6 4 7 4 2 4 2 Input 7 1 2 1 3 1 4 2 5 3 6 4 7 1 2 Input 7 1 2 1 3 1 4 2 5 3 6 4 7 4 6 5 2 1 Input 8 1 2 2 3 3 4 3 5 3 6 3 7 3 8 2 7 4 Note In the first test there are four possible ways to make operations: Remove the edge $$$(1, 2)$$$ and delete vertex $$$1$$$. Remove the edge $$$(2, 3)$$$ and delete vertex $$$2$$$. Remove the edge $$$(1, 2)$$$ and delete vertex $$$1$$$. Remove the edge $$$(3, 2)$$$ and delete vertex $$$3$$$. Remove the edge $$$(3, 2)$$$ and delete vertex $$$3$$$. Remove the edge $$$(1, 2)$$$ and delete vertex $$$1$$$. Remove the edge $$$(3, 2)$$$ and delete vertex $$$3$$$. Remove the edge $$$(2, 1)$$$ and delete vertex $$$2$$$. In the second test there are two possible ways to make operations: Remove the edge $$$(4, 1)$$$ and delete the part with vertex $$$4$$$. Remove the edge $$$(2, 3)$$$ and delete the part with vertex $$$2$$$. Remove the edge $$$(4, 1)$$$ and delete the part with vertex $$$4$$$. Remove the edge $$$(3, 2)$$$ and delete the part with vertex $$$3$$$.
3,200
false
false
false
true
false
false
false
false
false
false
1,476
1027D
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about $$$80%$$$ of applicants are girls and majority of them are going to live in the university dormitory for the next $$$4$$$ (hopefully) years. The dormitory consists of $$$n$$$ rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number $$$i$$$ costs $$$c_i$$$ burles. Rooms are numbered from $$$1$$$ to $$$n$$$. Mouse doesn't sit in place all the time, it constantly runs. If it is in room $$$i$$$ in second $$$t$$$ then it will run to room $$$a_i$$$ in second $$$t + 1$$$ without visiting any other rooms inbetween ($$$i = a_i$$$ means that mouse won't leave room $$$i$$$). It's second $$$0$$$ in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap. That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from $$$1$$$ to $$$n$$$ at second $$$0$$$. What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from? Input The first line contains as single integers $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of rooms in the dormitory. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^4$$$) — $$$c_i$$$ is the cost of setting the trap in room number $$$i$$$. The third line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$) — $$$a_i$$$ is the room the mouse will run to the next second after being in room $$$i$$$. Output Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from. Examples Input 5 1 2 3 2 10 1 3 4 3 3 Input 7 1 1 1 1 1 1 1 2 2 2 3 6 7 6 Note In the first example it is enough to set mouse trap in rooms $$$1$$$ and $$$4$$$. If mouse starts in room $$$1$$$ then it gets caught immideately. If mouse starts in any other room then it eventually comes to room $$$4$$$. In the second example it is enough to set mouse trap in room $$$2$$$. If mouse starts in room $$$2$$$ then it gets caught immideately. If mouse starts in any other room then it runs to room $$$2$$$ in second $$$1$$$. Here are the paths of the mouse from different starts from the third example: $$$1 ightarrow 2 ightarrow 2 ightarrow dots$$$; $$$2 ightarrow 2 ightarrow dots$$$; $$$3 ightarrow 2 ightarrow 2 ightarrow dots$$$; $$$4 ightarrow 3 ightarrow 2 ightarrow 2 ightarrow dots$$$; $$$5 ightarrow 6 ightarrow 7 ightarrow 6 ightarrow dots$$$; $$$6 ightarrow 7 ightarrow 6 ightarrow dots$$$; $$$7 ightarrow 6 ightarrow 7 ightarrow dots$$$; So it's enough to set traps in rooms $$$2$$$ and $$$6$$$.
1,700
false
false
false
false
false
false
false
false
false
true
5,573
1834F
Recently, Polycarp was given an unusual typewriter as a gift! Unfortunately, the typewriter was defective and had a rather strange design. The typewriter consists of $$$n$$$ cells numbered from left to right from $$$1$$$ to $$$n$$$, and a carriage that moves over them. The typewriter cells contain $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$, and each cell $$$i$$$ initially contains the integer $$$p_i$$$. Before all actions, the carriage is at cell number $$$1$$$ and there is nothing in its buffer storage. The cell on which the carriage is located is called the current cell. The carriage can perform five types of operations: Take the integer from the current cell, if it is not empty, and put it in the carriage buffer, if it is empty (this buffer can contain no more than one integer). Put the integer from the carriage buffer, if it is not empty, into the current cell, if it is empty. Swap the number in the carriage buffer with the number in the current cell, if both the buffer and the cell contain integers. Move the carriage from the current cell $$$i$$$ to cell $$$i + 1$$$ (if $$$i < n$$$), while the integer in the buffer is preserved. Reset the carriage, i.e. move it to cell number $$$1$$$, while the integer in the buffer is preserved. Polycarp was very interested in this typewriter, so he asks you to help him understand it and will ask you $$$q$$$ queries of three types: 1. Perform a cyclic shift of the sequence $$$p$$$ to the left by $$$k_j$$$. 2. Perform a cyclic shift of the sequence $$$p$$$ to the right by $$$k_j$$$. 3. Reverse the sequence $$$p$$$. Before and after each query, Polycarp wants to know what minimum number of carriage resets is needed for the current sequence in order to distribute the numbers to their cells (so that the number $$$i$$$ ends up in cell number $$$i$$$). Note that Polycarp only wants to know the minimum number of carriage resets required to arrange the numbers in their places, but he does not actually distribute them. Help Polycarp find the answers to his queries! Input The first line contains a single integer $$$n$$$ ($$$1 le n le 4 cdot 10^5$$$)xa0— the number of cells. The second line contains $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$)xa0— the initial arrangement of integers in the cells. The third line contains a single integer $$$q$$$ ($$$0 le q le 4 cdot 10^5$$$)xa0— the number of queries. Each of the next $$$q$$$ lines describes a query from Polycarp: The $$$j$$$-th line, at first, contains the integer $$$t_j$$$ ($$$1 le t_j le 3$$$) xa0— the type of query. If the query is of type $$$t_j = 1$$$ or $$$t_j = 2$$$, then the integer $$$k_j$$$ ($$$1 le k_j le n$$$) xa0— the length of the shift xa0— follows in the same line. Output Output $$$q + 1$$$ numbersxa0— the minimum number of carriage resets required before and after each of Polycarp's queries. Examples Input 5 3 1 2 5 4 5 1 3 3 2 3 1 4 3 Note In the first example, the answer is $$$1$$$. You can understand how the carriage works using this example. In the second example, the sequences for which the answer needs to be calculated look like this: 1. Before all queries: $$$1 2 3$$$xa0— the answer is $$$0$$$. 2. After shifting to the right by $$$1$$$: $$$3 1 2$$$xa0— the answer is $$$2$$$. 3. After reversing the sequence: $$$2 1 3$$$xa0— the answer is $$$1$$$. In the third example, the sequences before and after each query look like this: 1. $$$3 1 2 5 4$$$xa0— the answer is $$$3$$$. 2. $$$5 4 3 1 2$$$xa0— the answer is $$$2$$$. 3. $$$2 1 3 4 5$$$xa0— the answer is $$$1$$$. 4. $$$3 4 5 2 1$$$xa0— the answer is $$$2$$$. 5. $$$1 3 4 5 2$$$xa0— the answer is $$$1$$$. 6. $$$2 5 4 3 1$$$xa0— the answer is $$$2$$$.
2,500
true
false
false
false
false
false
true
false
false
false
1,279
1658F
The cuteness of a binary string is the number of $$$ exttt{1}$$$s divided by the length of the string. For example, the cuteness of $$$ exttt{01101}$$$ is $$$frac{3}{5}$$$. Juju has a binary string $$$s$$$ of length $$$n$$$. She wants to choose some non-intersecting subsegments of $$$s$$$ such that their concatenation has length $$$m$$$ and it has the same cuteness as the string $$$s$$$. More specifically, she wants to find two arrays $$$l$$$ and $$$r$$$ of equal length $$$k$$$ such that $$$1 leq l_1 leq r_1 < l_2 leq r_2 < ldots < l_k leq r_k leq n$$$, and also: $$$sumlimits_{i=1}^k (r_i - l_i + 1) = m$$$; The cuteness of $$$s[l_1,r_1]+s[l_2,r_2]+ldots+s[l_k,r_k]$$$ is equal to the cuteness of $$$s$$$, where $$$s[x, y]$$$ denotes the subsegment $$$s_x s_{x+1} ldots s_y$$$, and $$$+$$$ denotes string concatenation. Juju does not like splitting the string into many parts, so she also wants to minimize the value of $$$k$$$. Find the minimum value of $$$k$$$ such that there exist $$$l$$$ and $$$r$$$ that satisfy the constraints above or determine that it is impossible to find such $$$l$$$ and $$$r$$$ for any $$$k$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$) — the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq m leq n leq 2 cdot 10^5$$$). The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, if there is no valid pair of $$$l$$$ and $$$r$$$, print $$$-1$$$. Otherwise, print $$$k + 1$$$ lines. In the first line, print a number $$$k$$$ ($$$1 leq k leq m$$$) — the minimum number of subsegments required. Then print $$$k$$$ lines, the $$$i$$$-th should contain $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i leq r_i leq n$$$) — the range of the $$$i$$$-th subsegment. Note that you should output the subsegments such that the inequality $$$l_1 leq r_1 < l_2 leq r_2 < ldots < l_k leq r_k$$$ is true. Example Input 4 4 2 0011 8 6 11000011 4 3 0101 5 5 11111 Output 1 2 3 2 2 3 5 8 -1 1 1 5 Note In the first example, the cuteness of $$$ exttt{0011}$$$ is the same as the cuteness of $$$ exttt{01}$$$. In the second example, the cuteness of $$$ exttt{11000011}$$$ is $$$frac{1}{2}$$$ and there is no subsegment of size $$$6$$$ with the same cuteness. So we must use $$$2$$$ disjoint subsegments $$$ exttt{10}$$$ and $$$ exttt{0011}$$$. In the third example, there are $$$8$$$ ways to split the string such that $$$sumlimits_{i=1}^k (r_i - l_i + 1) = 3$$$ but none of them has the same cuteness as $$$ exttt{0101}$$$. In the last example, we don't have to split the string.
2,700
true
true
false
false
false
true
true
false
false
false
2,333
1680B
There is a field divided into $$$n$$$ rows and $$$m$$$ columns. Some cells are empty (denoted as E), other cells contain robots (denoted as R). You can send a command to all robots at the same time. The command can be of one of the four types: move up; move right; move down; move left. When you send a command, all robots at the same time attempt to take one step in the direction you picked. If a robot tries to move outside the field, it explodes; otherwise, every robot moves to an adjacent cell in the chosen direction. You can send as many commands as you want (possibly, zero), in any order. Your goal is to make at least one robot reach the upper left corner of the field. Can you do this without forcing any of the robots to explode? Input The first line contains one integer $$$t$$$ ($$$1 le t le 5000$$$)xa0— the number of test cases. Each test case starts with a line containing two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 5$$$)xa0— the number of rows and the number of columns, respectively. Then $$$n$$$ lines follow; each of them contains a string of $$$m$$$ characters. Each character is either E (empty cell} or R (robot). Additional constraint on the input: in each test case, there is at least one robot on the field. Output If it is possible to make at least one robot reach the upper left corner of the field so that no robot explodes, print YES. Otherwise, print NO. Example Input 6 1 3 ERR 2 2 ER RE 2 2 ER ER 1 1 R 4 3 EEE EEE ERR EER 3 3 EEE EER REE Output YES NO YES YES YES NO Note Explanations for test cases of the example: 1. in the first test case, it is enough to send a command to move left. 2. in the second test case, if you try to send any command, at least one robot explodes. 3. in the third test case, it is enough to send a command to move left. 4. in the fourth test case, there is already a robot in the upper left corner. 5. in the fifth test case, the sequence "move up, move left, move up" leads one robot to the upper left corner; 6. in the sixth test case, if you try to move any robot to the upper left corner, at least one other robot explodes.
800
false
false
true
false
false
false
false
false
false
false
2,190
1499G
You are given a bipartite graph consisting of $$$n_1$$$ vertices in the first part, $$$n_2$$$ vertices in the second part, and $$$m$$$ edges, numbered from $$$1$$$ to $$$m$$$. You have to color each edge into one of two colors, red and blue. You have to minimize the following value: $$$sum limits_{v in V} r(v) - b(v)$$$, where $$$V$$$ is the set of vertices of the graph, $$$r(v)$$$ is the number of red edges incident to $$$v$$$, and $$$b(v)$$$ is the number of blue edges incident to $$$v$$$. Sounds classical and easy, right? Well, you have to process $$$q$$$ queries of the following format: $$$1$$$ $$$v_1$$$ $$$v_2$$$ — add a new edge connecting the vertex $$$v_1$$$ of the first part with the vertex $$$v_2$$$ of the second part. This edge gets a new index as follows: the first added edge gets the index $$$m + 1$$$, the second — $$$m + 2$$$, and so on. After adding the edge, you have to print the hash of the current optimal coloring (if there are multiple optimal colorings, print the hash of any of them). Actually, this hash won't be verified, you may print any number as the answer to this query, but you may be asked to produce the coloring having this hash; $$$2$$$ — print the optimal coloring of the graph with the same hash you printed while processing the previous query. The query of this type will only be asked after a query of type $$$1$$$, and there will be at most $$$10$$$ queries of this type. If there are multiple optimal colorings corresponding to this hash, print any of them. Note that if an edge was red or blue in some coloring, it may change its color in next colorings. The hash of the coloring is calculated as follows: let $$$R$$$ be the set of indices of red edges, then the hash is $$$(sum limits_{i in R} 2^i) bmod 998244353$$$. Note that you should solve the problem in online mode. It means that you can't read the whole input at once. You can read each query only after writing the answer for the last query. Use functions fflush in C++ and BufferedWriter.flush in Java languages after each writing in your program. Input The first line contains three integers $$$n_1$$$, $$$n_2$$$ and $$$m$$$ ($$$1 le n_1, n_2, m le 2 cdot 10^5$$$). Then $$$m$$$ lines follow, the $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i le n_1$$$; $$$1 le y_i le n_2$$$) meaning that the $$$i$$$-th edge connects the vertex $$$x_i$$$ from the first part and the vertex $$$y_i$$$ from the second part. The next line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$)xa0— the number of queries you have to process. The next $$$q$$$ lines contain the queries in the format introduced in the statement. Additional constraints on the input: at any moment, the graph won't contain any multiple edges; the queries of type $$$2$$$ are only asked if the previous query had type $$$1$$$; there are at most $$$10$$$ queries of type $$$2$$$. Output To answer a query of type $$$1$$$, print one integerxa0— the hash of the optimal coloring. To answer a query of type $$$2$$$, print one line. It should begin with the integer $$$k$$$xa0— the number of red edges. Then, $$$k$$$ distinct integer should followxa0— the indices of red edges in your coloring, in any order. Each index should correspond to an existing edge, and the hash of the coloring you produce should be equal to the hash you printed as the answer to the previous query. If there are multiple answers to a query, you may print any of them. Example Input 3 4 2 1 2 3 4 10 1 1 3 1 2 3 2 1 3 3 2 1 2 4 2 1 2 1 1 1 1 2 Output 8 8 1 3 40 2 3 5 104 3 5 6 3 104 360 4 5 6 3 8
3,100
false
false
false
false
true
false
false
false
false
true
3,169
1445B
There is a famous olympiad, which has more than a hundred participants. The Olympiad consists of two stages: the elimination stage, and the final stage. At least a hundred participants will advance to the final stage. The elimination stage in turn consists of two contests. A result of the elimination stage is the total score in two contests, but, unfortunately, the jury lost the final standings and has only standings for the first and for the second contest separately. In each contest, the participants are ranked by their point score in non-increasing order. When two participants have a tie (earned the same score), they are ranked by their passport number (in accordance with local regulations, all passport numbers are distinct). In the first contest, the participant on the 100-th place scored $$$a$$$ points. Also, the jury checked all participants from the 1-st to the 100-th place (inclusive) in the first contest and found out that all of them have at least $$$b$$$ points in the second contest. Similarly, for the second contest, the participant on the 100-th place has $$$c$$$ points. And the jury checked that all the participants from the 1-st to the 100-th place (inclusive) have at least $$$d$$$ points in the first contest. After two contests, all participants are ranked by their total score in two contests in non-increasing order. When participants have the same total score, tie-breaking with passport numbers is used. The cutoff score to qualify to the final stage is the total score of the participant on the 100-th place. Given integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$, please help the jury determine the smallest possible value of the cutoff score. Input You need to process $$$t$$$ test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 3025$$$)xa0— the number of test cases. Then descriptions of $$$t$$$ test cases follow. The first line of each test case contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$0 le a,,b,,c,,d le 9$$$; $$$d leq a$$$; $$$b leq c$$$). One can show that for any test case satisfying the constraints above, there is at least one olympiad scenario possible. Note For the first test case, consider the following olympiad scenario: there are $$$101$$$ participants in the elimination stage, each having $$$1$$$ point for the first contest and $$$2$$$ points for the second contest. Hence the total score of the participant on the 100-th place is $$$3$$$. For the second test case, consider the following olympiad scenario: there are $$$50$$$ participants with points $$$5$$$ and $$$9$$$ for the first and second contest respectively; $$$50$$$ participants with points $$$4$$$ and $$$8$$$ for the first and second contest respectively; and $$$50$$$ participants with points $$$2$$$ and $$$9$$$ for the first and second contest respectively. Hence the total point score of the participant on the 100-th place is $$$12$$$.
900
true
true
false
false
false
false
false
false
false
false
3,448
44C
School holidays come in Berland. The holidays are going to continue for _n_ days. The students of school №_N_ are having the time of their lives and the IT teacher Marina Sergeyevna, who has spent all the summer busy checking the BSE (Berland State Examination) results, has finally taken a vacation break! Some people are in charge of the daily watering of flowers in shifts according to the schedule. However when Marina Sergeyevna was making the schedule, she was so tired from work and so lost in dreams of the oncoming vacation that she perhaps made several mistakes. In fact, it is possible that according to the schedule, on some days during the holidays the flowers will not be watered or will be watered multiple times. Help Marina Sergeyevna to find a mistake. Input The first input line contains two numbers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100) — the number of days in Berland holidays and the number of people in charge of the watering respectively. The next _m_ lines contain the description of the duty schedule. Each line contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_u2009≤u2009_b__i_u2009≤u2009_n_), meaning that the _i_-th person in charge should water the flowers from the _a__i_-th to the _b__i_-th day inclusively, once a day. The duty shifts are described sequentially, i.e. _b__i_u2009≤u2009_a__i_u2009+u20091 for all _i_ from 1 to _n_u2009-u20091 inclusively. Output Print "OK" (without quotes), if the schedule does not contain mistakes. Otherwise you have to find the minimal number of a day when the flowers will not be watered or will be watered multiple times, and output two integers — the day number and the number of times the flowers will be watered that day. Examples Input 10 5 1 2 3 3 4 6 7 7 8 10 Input 10 5 1 2 2 3 4 5 7 8 9 10 Input 10 5 1 2 3 3 5 7 7 7 7 10 Note Keep in mind that in the second sample the mistake occurs not only on the second day, but also on the sixth day, when nobody waters the flowers. However, you have to print the second day, i.e. the day with the minimal number.
1,300
false
false
true
false
false
false
false
false
false
false
9,767
1527C
The weight of a sequence is defined as the number of unordered pairs of indexes $$$(i,j)$$$ (here $$$i lt j$$$) with same value ($$$a_{i} = a_{j}$$$). For example, the weight of sequence $$$a = [1, 1, 2, 2, 1]$$$ is $$$4$$$. The set of unordered pairs of indexes with same value are $$$(1, 2)$$$, $$$(1, 5)$$$, $$$(2, 5)$$$, and $$$(3, 4)$$$. You are given a sequence $$$a$$$ of $$$n$$$ integers. Print the sum of the weight of all subsegments of $$$a$$$. A sequence $$$b$$$ is a subsegment of a sequence $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print a single integerxa0— the sum of the weight of all subsegments of $$$a$$$. Example Input 2 4 1 2 1 1 4 1 2 3 4 Note In test case $$$1$$$, all possible subsegments of sequence $$$[1, 2, 1, 1]$$$ having size more than $$$1$$$ are: 1. $$$[1, 2]$$$ having $$$0$$$ valid unordered pairs; 2. $$$[2, 1]$$$ having $$$0$$$ valid unordered pairs; 3. $$$[1, 1]$$$ having $$$1$$$ valid unordered pair; 4. $$$[1, 2, 1]$$$ having $$$1$$$ valid unordered pairs; 5. $$$[2, 1, 1]$$$ having $$$1$$$ valid unordered pair; 6. $$$[1, 2, 1, 1]$$$ having $$$3$$$ valid unordered pairs.Answer is $$$6$$$. In test case $$$2$$$, all elements of the sequence are distinct. So, there is no valid unordered pair with the same value for any subarray. Answer is $$$0$$$.
1,600
true
false
true
false
false
false
false
false
false
false
3,026
1398C
You are given an array $$$a_1, a_2, dots , a_n$$$ consisting of integers from $$$0$$$ to $$$9$$$. A subarray $$$a_l, a_{l+1}, a_{l+2}, dots , a_{r-1}, a_r$$$ is good if the sum of elements of this subarray is equal to the length of this subarray ($$$sumlimits_{i=l}^{r} a_i = r - l + 1$$$). For example, if $$$a = [1, 2, 0]$$$, then there are $$$3$$$ good subarrays: $$$a_{1 dots 1} = [1], a_{2 dots 3} = [2, 0]$$$ and $$$a_{1 dots 3} = [1, 2, 0]$$$. Calculate the number of good subarrays of the array $$$a$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains a string consisting of $$$n$$$ decimal digits, where the $$$i$$$-th digit is equal to the value of $$$a_i$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print one integer — the number of good subarrays of the array $$$a$$$. Example Input 3 3 120 5 11011 6 600005 Note The first test case is considered in the statement. In the second test case, there are $$$6$$$ good subarrays: $$$a_{1 dots 1}$$$, $$$a_{2 dots 2}$$$, $$$a_{1 dots 2}$$$, $$$a_{4 dots 4}$$$, $$$a_{5 dots 5}$$$ and $$$a_{4 dots 5}$$$. In the third test case there is only one good subarray: $$$a_{2 dots 6}$$$.
1,600
true
false
false
true
true
false
false
false
false
false
3,684
316B1
In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another. (Cultural note: standing in huge and disorganized queues for hours is a native tradition in Russia, dating back to the Soviet period. Queues can resemble crowds rather than lines. Not to get lost in such a queue, a person should follow a strict survival technique: you approach the queue and ask who the last person is, somebody answers and you join the crowd. Now you're the last person in the queue till somebody else shows up. You keep an eye on the one who was last before you as he is your only chance to get to your destination) I'm sure many people have had the problem when a stranger asks who the last person in the queue is and even dares to hint that he will be the last in the queue and then bolts away to some unknown destination. These are the representatives of the modern world, in which the ratio of lack of time is so great that they do not even watch foreign top-rated TV series. Such people often create problems in queues, because the newcomer does not see the last person in the queue and takes a place after the "virtual" link in this chain, wondering where this legendary figure has left. The Smart Beaver has been ill and he's made an appointment with a therapist. The doctor told the Beaver the sad news in a nutshell: it is necessary to do an electrocardiogram. The next day the Smart Beaver got up early, put on the famous TV series on download (three hours till the download's complete), clenched his teeth and bravely went to join a queue to the electrocardiogram room, which is notorious for the biggest queues at the clinic. Having stood for about three hours in the queue, the Smart Beaver realized that many beavers had not seen who was supposed to stand in the queue before them and there was a huge mess. He came up to each beaver in the ECG room queue and asked who should be in front of him in the queue. If the beaver did not know his correct position in the queue, then it might be his turn to go get an ECG, or maybe he should wait for a long, long time... As you've guessed, the Smart Beaver was in a hurry home, so he gave you all the necessary information for you to help him to determine what his number in the queue can be. Input The first line contains two integers _n_ (1u2009≤u2009_n_u2009≤u2009103) and _x_ (1u2009≤u2009_x_u2009≤u2009_n_) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to _n_. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009_n_) — the number of the beaver followed by the _i_-th beaver. If _a__i_u2009=u20090, then the _i_-th beaver doesn't know who is should be in front of him. It is guaranteed that values _a__i_ are correct. That is there is no cycles in the dependencies. And any beaver is followed by at most one beaver in the queue. The input limits for scoring 30 points are (subproblem B1): It is guaranteed that the number of zero elements _a__i_ doesn't exceed 20. The input limits for scoring 100 points are (subproblems B1+B2): The number of zero elements _a__i_ is arbitrary.
1,500
false
false
false
false
false
false
true
false
false
false
8,581
28B
Problem - 28B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dfs and similar dsu graphs *1600 No tag edit access → Contest materials ") ") ") ") . Also each cell determined it's favourite number. On it's move _i_-th cell can exchange it's value with the value of some other _j_-th cell, if _i_u2009-u2009_j_u2009=u2009_d__i_, where _d__i_ is a favourite number of _i_-th cell. Cells make moves in any order, the number of moves is unlimited. The favourite number of each cell will be given to you. You will also be given a permutation of numbers from 1 to _n_. You are to determine whether the game could move to this state. Input The first line contains positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of cells in the array. The second line contains _n_ distinct integers from 1 to _n_ — permutation. The last line contains _n_ integers from 1 to _n_ — favourite numbers of the cells. Output If the given state is reachable in the described game, output YES, otherwise NO. Examples Input 5 5 4 3 2 1 1 1 1 1 1 Output YES Input 7 4 3 5 1 2 7 6 4 6 6 1 6 6 1 Output NO Input 7 4 2 5 1 3 7 6 4 6 6 1 6 6 1 Output YES
1,600
false
false
false
false
false
false
false
false
false
true
9,857
370E
At school Vasya got an impressive list of summer reading books. Unlike other modern schoolchildren, Vasya loves reading, so he read some book each day of the summer. As Vasya was reading books, he was making notes in the Reader's Diary. Each day he wrote the orderal number of the book he was reading. The books in the list are numbered starting from 1 and Vasya was reading them in the order they go in the list. Vasya never reads a new book until he finishes reading the previous one. Unfortunately, Vasya wasn't accurate and some days he forgot to note the number of the book and the notes for those days remained empty. As Vasya knows that the literature teacher will want to check the Reader's Diary, so he needs to restore the lost records. Help him do it and fill all the blanks. Vasya is sure that he spends at least two and at most five days for each book. Vasya finished reading all the books he had started. Assume that the reading list contained many books. So many, in fact, that it is impossible to read all of them in a summer. If there are multiple valid ways to restore the diary records, Vasya prefers the one that shows the maximum number of read books. Input The first line contains integer _n_ — the number of summer days (2u2009≤u2009_n_u2009≤u20092·105). The second line contains _n_ integers _a_1,u2009_a_2,u2009... _a__n_ — the records in the diary in the order they were written (0u2009≤u2009_a__i_u2009≤u2009105). If Vasya forgot to write the number of the book on the _i_-th day, then _a__i_ equals 0. Output If it is impossible to correctly fill the blanks in the diary (the diary may contain mistakes initially), print "-1". Otherwise, print in the first line the maximum number of books Vasya could have read in the summer if we stick to the diary. In the second line print _n_ integers — the diary with correctly inserted records. If there are multiple optimal solutions, you can print any of them.
2,500
false
true
false
true
false
false
false
false
false
false
8,349
1909H
You are given a permutation $$$p_1, p_2, dots, p_n$$$ of $$$[1, 2, dots, n]$$$. You can perform the following operation some (possibly $$$0$$$) times: choose a subarray $$$[l, r]$$$ of even length; swap $$$a_l$$$, $$$a_{l+1}$$$; swap $$$a_{l+2}$$$, $$$a_{l+3}$$$ (if $$$l+3 leq r$$$); $$$dots$$$ swap $$$a_{r-1}$$$, $$$a_r$$$. Sort the permutation in at most $$$10^6$$$ operations. You do not need to minimize the number of operations. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$)xa0— the length of the permutation. The second line contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$, the $$$p_i$$$ are distinct)xa0— the permutation before performing the operations. Output Output your operations in the following format. The first line should contain an integer $$$k$$$ ($$$0 le k le 10^6$$$)xa0— the number of operations. The next $$$k$$$ lines represent the $$$k$$$ operations in order. Each of these $$$k$$$ lines should contain two integers $$$l$$$ and $$$r$$$ ($$$1 leq l < r leq n$$$, $$$r-l+1$$$ must be even)xa0— the corresponding operation consists in choosing the subarray $$$[l, r]$$$ and swapping its elements according to the problem statement. After all the operations, $$$a_i = i$$$ must be true for each $$$i$$$ ($$$1 leq i leq n$$$). Examples Output 5 1 4 1 2 2 5 1 4 4 5 Input 10 6 4 2 3 8 10 9 1 5 7 Output 15 1 8 6 9 1 8 3 10 1 10 1 10 1 6 6 9 6 9 2 7 9 10 5 10 1 6 2 9 1 10 Note In the first test: At the beginning, $$$p = [2, 5, 4, 1, 3]$$$. In the first operation, you can choose $$$[l, r] = [1, 4]$$$. Then, $$$(a_1, a_2)$$$ are swapped and $$$(a_3, a_4)$$$ are swapped. The new permutation is $$$p = [5, 2, 1, 4, 3]$$$. In the second operation, you can choose $$$[l, r] = [1, 2]$$$. Then, $$$(a_1, a_2)$$$ are swapped. The new permutation is $$$p = [2, 5, 1, 4, 3]$$$. In the third operation, you can choose $$$[l, r] = [2, 5]$$$. Then, $$$(a_2, a_3)$$$ are swapped and $$$(a_4, a_5)$$$ are swapped. The new permutation is $$$p = [2, 1, 5, 3, 4]$$$. In the fourth operation, you can choose $$$[l, r] = [1, 4]$$$. Then, $$$(a_1, a_2)$$$ are swapped and $$$(a_3, a_4)$$$ are swapped. The new permutation is $$$p = [1, 2, 3, 5, 4]$$$. In the fifth operation, you can choose $$$[l, r] = [4, 5]$$$. Then, $$$(a_4, a_5)$$$ are swapped. The new permutation is $$$p = [1, 2, 3, 4, 5]$$$, which is sorted. In the second test, the permutation is already sorted, so you do not need to perform any operation.
3,500
false
false
false
false
true
true
false
false
false
false
849
1702C
Along the railroad there are stations indexed from $$$1$$$ to $$$10^9$$$. An express train always travels along a route consisting of $$$n$$$ stations with indices $$$u_1, u_2, dots, u_n$$$, where ($$$1 le u_i le 10^9$$$). The train travels along the route from left to right. It starts at station $$$u_1$$$, then stops at station $$$u_2$$$, then at $$$u_3$$$, and so on. Station $$$u_n$$$xa0— the terminus. It is possible that the train will visit the same station more than once. That is, there may be duplicates among the values $$$u_1, u_2, dots, u_n$$$. You are given $$$k$$$ queries, each containing two different integers $$$a_j$$$ and $$$b_j$$$ ($$$1 le a_j, b_j le 10^9$$$). For each query, determine whether it is possible to travel by train from the station with index $$$a_j$$$ to the station with index $$$b_j$$$. For example, let the train route consist of $$$6$$$ of stations with indices [$$$3, 7, 1, 5, 1, 4$$$] and give $$$3$$$ of the following queries: $$$a_1 = 3$$$, $$$b_1 = 5$$$It is possible to travel from station $$$3$$$ to station $$$5$$$ by taking a section of the route consisting of stations [$$$3, 7, 1, 5$$$]. Answer: YES. $$$a_2 = 1$$$, $$$b_2 = 7$$$You cannot travel from station $$$1$$$ to station $$$7$$$ because the train cannot travel in the opposite direction. Answer: NO. $$$a_3 = 3$$$, $$$b_3 = 10$$$It is not possible to travel from station $$$3$$$ to station $$$10$$$ because station $$$10$$$ is not part of the train's route. Answer: NO. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0—the number of test cases in the test. The descriptions of the test cases follow. The first line of each test case is empty. The second line of each test case contains two integers: $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5, 1 le k le 2 cdot 10^5$$$)xa0—the number of stations the train route consists of and the number of queries. The third line of each test case contains exactly $$$n$$$ integers $$$u_1, u_2, dots, u_n$$$ ($$$1 le u_i le 10^9$$$). The values $$$u_1, u_2, dots, u_n$$$ are not necessarily different. The following $$$k$$$ lines contain two different integers $$$a_j$$$ and $$$b_j$$$ ($$$1 le a_j, b_j le 10^9$$$) describing the query with index $$$j$$$. It is guaranteed that the sum of $$$n$$$ values over all test cases in the test does not exceed $$$2 cdot 10^5$$$. Similarly, it is guaranteed that the sum of $$$k$$$ values over all test cases in the test also does not exceed $$$2 cdot 10^5$$$ Output For each test case, output on a separate line: YES, if you can travel by train from the station with index $$$a_j$$$ to the station with index $$$b_j$$$ NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). Example Input 3 6 3 3 7 1 5 1 4 3 5 1 7 3 10 3 3 1 2 1 2 1 1 2 4 5 7 5 2 1 1 1 2 4 4 1 3 1 4 2 1 4 1 1 2 Output YES NO NO YES YES NO NO YES YES NO YES Note The first test case is explained in the problem statement.
1,100
false
true
false
false
true
false
false
false
false
false
2,068
1988E
For an array $$$[a_1,a_2,ldots,a_n]$$$ of length $$$n$$$, define $$$f(a)$$$ as the sum of the minimum element over all subsegments. That is, $$$$$$f(a)=sum_{l=1}^nsum_{r=l}^n min_{lle ile r}a_i.$$$$$$ A permutation is a sequence of integers from $$$1$$$ to $$$n$$$ of length $$$n$$$ containing each number exactly once. You are given a permutation $$$[a_1,a_2,ldots,a_n]$$$. For each $$$i$$$, solve the following problem independently: Erase $$$a_i$$$ from $$$a$$$, concatenating the remaining parts, resulting in $$$b = [a_1,a_2,ldots,a_{i-1},;a_{i+1},ldots,a_{n}]$$$. Calculate $$$f(b)$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^5$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1le nle 5cdot 10^5$$$). The second line of each test case contains $$$n$$$ distinct integers $$$a_1,ldots,a_n$$$ ($$$1le a_ile n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, print one line containing $$$n$$$ integers. The $$$i$$$-th integer should be the answer when erasing $$$a_i$$$. Example Input 4 1 1 3 3 1 2 5 4 2 1 5 3 8 8 1 4 6 7 3 5 2 Output 0 4 7 5 19 21 27 17 19 79 100 72 68 67 80 73 80 Note In the second test case, $$$a=[3,1,2]$$$. When removing $$$a_1$$$, $$$b=[1,2]$$$. $$$f(b)=1+2+min{1,2}=4$$$. When removing $$$a_2$$$, $$$b=[3,2]$$$. $$$f(b)=3+2+min{3,2}=7$$$. When removing $$$a_3$$$, $$$b=[3,1]$$$. $$$f(b)=3+1+min{3,1}=5$$$.
2,300
false
false
true
false
true
false
true
true
false
false
343
1503E
Problem - 1503E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp math *3100 No tag edit access → Contest materials . Output Output a single integer — the number of stupid colorings modulo $$$998244353$$$. Examples Input 2 2 Output 2 Input 4 3 Output 294 Input 2020 2021 Output 50657649 Note In the first test case, these are the only two stupid $$$2 imes 2$$$ colorings.
3,100
true
false
false
true
false
false
false
false
false
false
3,156
56B
Vasya collects coins: he has exactly one coin for every year from 1 to _n_. Naturally, Vasya keeps all the coins in his collection in the order in which they were released. Once Vasya's younger brother made a change — he took all the coins whose release year dated from _l_ to _r_ inclusively and put them in the reverse order. That is, he took a certain segment [_l_,u2009_r_] and reversed it. At that the segment's endpoints did not coincide. For example, if _n_u2009=u20098, then initially Vasya's coins were kept in the order 1 2 3 4 5 6 7 8. If Vasya's younger brother chose the segment [2,u20096], then after the reversal the coin order will change to 1 6 5 4 3 2 7 8. Vasya suspects that someone else could have spoilt the permutation after his brother. Help him to find that out. Check if the given permutation can be obtained from the permutation 1 2 ... _n_ using exactly one segment reversal. If it is possible, find the segment itself. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20091000) which is the number of coins in Vasya's collection. The second line contains space-separated _n_ integers which are the spoilt sequence of coins. It is guaranteed that the given sequence is a permutation, i.e. it contains only integers from 1 to _n_, and every number is used exactly 1 time. Output If it is impossible to obtain the given permutation from the original one in exactly one action, print 0 0. Otherwise, print two numbers _l_ _r_ (1u2009≤u2009_l_u2009<u2009_r_u2009≤u2009_n_) which are the endpoints of the segment that needs to be reversed to obtain from permutation 1 2 ... _n_ the given one.
1,300
false
false
true
false
false
false
false
false
false
false
9,694
67D
Professor Phunsuk Wangdu has performed some experiments on rays. The setup for _n_ rays is as follows. There is a rectangular box having exactly _n_ holes on the opposite faces. All rays enter from the holes of the first side and exit from the holes of the other side of the box. Exactly one ray can enter or exit from each hole. The holes are in a straight line. Professor Wangdu is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. Can we determine the number of rays in the largest of such groups?". Professor Wangdu now is in trouble and knowing your intellect he asks you to help him. Input The first line contains _n_ (1u2009≤u2009_n_u2009≤u2009106), the number of rays. The second line contains _n_ distinct integers. The _i_-th integer _x__i_ (1u2009≤u2009_x__i_u2009≤u2009_n_) shows that the _x__i_-th ray enters from the _i_-th hole. Similarly, third line contains _n_ distinct integers. The _i_-th integer _y__i_ (1u2009≤u2009_y__i_u2009≤u2009_n_) shows that the _y__i_-th ray exits from the _i_-th hole. All rays are numbered from 1 to _n_. Output Output contains the only integer which is the number of rays in the largest group of rays all of which intersect each other. Note For the first test case, the figure is shown above. The output of the first test case is 3, since the rays number 1, 4 and 3 are the ones which are intersected by each other one i.e. 1 is intersected by 4 and 3, 3 is intersected by 4 and 1, and 4 is intersected by 1 and 3. Hence every ray in this group is intersected by each other one. There does not exist any group containing more than 3 rays satisfying the above-mentioned constraint.
1,900
false
false
false
true
true
false
false
true
false
false
9,633
1497C2
Problem - 1497C2 - 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 math *1600 No tag edit access → Contest materials time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It is the hard version of the problem. The only difference is that in this version $$$3 le k le n$$$. You are given a positive integer $$$n$$$. Find $$$k$$$ positive integers $$$a_1, a_2, ldots, a_k$$$, such that: $$$a_1 + a_2 + ldots + a_k = n$$$ $$$LCM(a_1, a_2, ldots, a_k) le frac{n}{2}$$$ Here $$$LCM$$$ is the $$$ xa0— the number of test cases. The only line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$3 le n le 10^9$$$, $$$3 le k le n$$$). It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case print $$$k$$$ positive integers $$$a_1, a_2, ldots, a_k$$$, for which all conditions are satisfied. Example Input 2 6 4 9 5 Output 1 2 2 1 1 3 3 1 1
1,600
true
false
false
false
false
true
false
false
false
false
3,185
1204C
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $$$n$$$ vertexes and a path in it (that path is not necessary simple) given by a sequence $$$p_1, p_2, ldots, p_m$$$ of $$$m$$$ vertexes; for each $$$1 leq i < m$$$ there is an arc from $$$p_i$$$ to $$$p_{i+1}$$$. Define the sequence $$$v_1, v_2, ldots, v_k$$$ of $$$k$$$ vertexes as good, if $$$v$$$ is a subsequence of $$$p$$$, $$$v_1 = p_1$$$, $$$v_k = p_m$$$, and $$$p$$$ is one of the shortest paths passing through the vertexes $$$v_1$$$, $$$ldots$$$, $$$v_k$$$ in that order. A sequence $$$a$$$ is a subsequence of a sequence $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $$$p$$$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100$$$)xa0— the number of vertexes in a graph. The next $$$n$$$ lines define the graph by an adjacency matrix: the $$$j$$$-th character in the $$$i$$$-st line is equal to $$$1$$$ if there is an arc from vertex $$$i$$$ to the vertex $$$j$$$ else it is equal to $$$0$$$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $$$m$$$ ($$$2 le m le 10^6$$$)xa0— the number of vertexes in the path. The next line contains $$$m$$$ integers $$$p_1, p_2, ldots, p_m$$$ ($$$1 le p_i le n$$$)xa0— the sequence of vertexes in the path. It is guaranteed that for any $$$1 leq i < m$$$ there is an arc from $$$p_i$$$ to $$$p_{i+1}$$$. Output In the first line output a single integer $$$k$$$ ($$$2 leq k leq m$$$)xa0— the length of the shortest good subsequence. In the second line output $$$k$$$ integers $$$v_1$$$, $$$ldots$$$, $$$v_k$$$ ($$$1 leq v_i leq n$$$)xa0— the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. Examples Input 4 0110 0010 0001 1000 4 1 2 3 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Note Below you can see the graph from the first example: The given path is passing through vertexes $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$. The sequence $$$1-2-4$$$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $$$1$$$, $$$2$$$ and $$$4$$$ in that order is $$$1-2-3-4$$$. Note that subsequences $$$1-4$$$ and $$$1-3-4$$$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $$$1-3-4$$$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $$$1-2-4$$$ and $$$1-3-4$$$ are the shortest paths passing through the vertexes $$$1$$$ and $$$4$$$.
1,700
false
true
false
true
false
false
false
false
false
true
4,673
975B
Mancala is a game famous in the Middle East. It is played on a board that consists of 14 holes. Initially, each hole has $$$a_i$$$ stones. When a player makes a move, he chooses a hole which contains a positive number of stones. He takes all the stones inside it and then redistributes these stones one by one in the next holes in a counter-clockwise direction. Note that the counter-clockwise order means if the player takes the stones from hole $$$i$$$, he will put one stone in the $$$(i+1)$$$-th hole, then in the $$$(i+2)$$$-th, etc. If he puts a stone in the $$$14$$$-th hole, the next one will be put in the first hole. After the move, the player collects all the stones from holes that contain even number of stones. The number of stones collected by player is the score, according to Resli. Resli is a famous Mancala player. He wants to know the maximum score he can obtain after one move. Input The only line contains 14 integers $$$a_1, a_2, ldots, a_{14}$$$ ($$$0 leq a_i leq 10^9$$$)xa0— the number of stones in each hole. It is guaranteed that for any $$$i$$$ ($$$1leq i leq 14$$$) $$$a_i$$$ is either zero or odd, and there is at least one stone in the board. Output Output one integer, the maximum possible score after one move. Examples Input 0 1 1 0 0 0 0 0 0 7 0 0 0 0 Input 5 1 1 1 1 0 0 0 0 0 0 0 0 0 Note In the first test case the board after the move from the hole with $$$7$$$ stones will look like 1 2 2 0 0 0 0 0 0 0 1 1 1 1. Then the player collects the even numbers and ends up with a score equal to $$$4$$$.
1,100
false
false
true
false
false
false
true
false
false
false
5,834
1849B
Monocarp is playing yet another computer game. And yet again, his character is killing some monsters. There are $$$n$$$ monsters, numbered from $$$1$$$ to $$$n$$$, and the $$$i$$$-th of them has $$$a_i$$$ health points initially. Monocarp's character has an ability that deals $$$k$$$ damage to the monster with the highest current health. If there are several of them, the one with the smaller index is chosen. If a monster's health becomes less than or equal to $$$0$$$ after Monocarp uses his ability, then it dies. Monocarp uses his ability until all monsters die. Your task is to determine the order in which monsters will die. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 3 cdot 10^5$$$; $$$1 le k le 10^9$$$)xa0— the number of monsters and the damage which Monocarp's ability deals. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0— the initial health points of monsters. The sum of $$$n$$$ over all test cases doesn't exceed $$$3 cdot 10^5$$$. Note In the first example, the health points change as follows: $$$[1, 2, underline{3}] ightarrow [1, underline{2}, 1] ightarrow [underline{1}, 0, 1] ightarrow [-1, 0, underline{1}] ightarrow [-1, 0, -1]$$$. The monster that is going to take damage the next time Monocarp uses his ability is underlined. In the second example, the health points change as follows: $$$[underline{1}, 1] ightarrow [-2, underline{1}] ightarrow [-2, -2]$$$. In the third example, the health points change as follows: $$$[2, underline{8}, 3, 5] ightarrow [2, underline{5}, 3, 5] ightarrow [2, 2, 3, underline{5}] ightarrow [2, 2, underline{3}, 2] ightarrow [underline{2}, 2, 0, 2] ightarrow [-1, underline{2}, 0, 2] ightarrow [-1, -1, 0, underline{2}] ightarrow [-1, -1, 0, -1]$$$.
1,000
true
true
false
false
false
false
false
false
true
false
1,187
1031C
In a galaxy far, far away Lesha the student has just got to know that he has an exam in two days. As always, he hasn't attended any single class during the previous year, so he decided to spend the remaining time wisely. Lesha knows that today he can study for at most $$$a$$$ hours, and he will have $$$b$$$ hours to study tomorrow. Note that it is possible that on his planet there are more hours in a day than on Earth. Lesha knows that the quality of his knowledge will only depend on the number of lecture notes he will read. He has access to an infinite number of notes that are enumerated with positive integers, but he knows that he can read the first note in one hour, the second note in two hours and so on. In other words, Lesha can read the note with number $$$k$$$ in $$$k$$$ hours. Lesha can read the notes in arbitrary order, however, he can't start reading a note in the first day and finish its reading in the second day. Thus, the student has to fully read several lecture notes today, spending at most $$$a$$$ hours in total, and fully read several lecture notes tomorrow, spending at most $$$b$$$ hours in total. What is the maximum number of notes Lesha can read in the remaining time? Which notes should he read in the first day, and whichxa0— in the second? Input The only line of input contains two integers $$$a$$$ and $$$b$$$ ($$$0 leq a, b leq 10^{9}$$$)xa0— the number of hours Lesha has today and the number of hours Lesha has tomorrow. Output In the first line print a single integer $$$n$$$ ($$$0 leq n leq a$$$)xa0— the number of lecture notes Lesha has to read in the first day. In the second line print $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq a$$$), the sum of all $$$p_i$$$ should not exceed $$$a$$$. In the third line print a single integer $$$m$$$ ($$$0 leq m leq b$$$)xa0— the number of lecture notes Lesha has to read in the second day. In the fourth line print $$$m$$$ distinct integers $$$q_1, q_2, ldots, q_m$$$ ($$$1 leq q_i leq b$$$), the sum of all $$$q_i$$$ should not exceed $$$b$$$. All integers $$$p_i$$$ and $$$q_i$$$ should be distinct. The sum $$$n + m$$$ should be largest possible. Note In the first example Lesha can read the third note in $$$3$$$ hours in the first day, and the first and the second notes in one and two hours correspondingly in the second day, spending $$$3$$$ hours as well. Note that Lesha can make it the other way round, reading the first and the second notes in the first day and the third note in the second day. In the second example Lesha should read the third and the sixth notes in the first day, spending $$$9$$$ hours in total. In the second day Lesha should read the first, second fourth and fifth notes, spending $$$12$$$ hours in total.
1,600
false
true
false
false
false
false
false
false
false
false
5,546
546B
Problem - 546B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force greedy implementation sortings *1200 No tag edit access → Contest materials ") editorial") . Next line consists of _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009_n_), which stand for coolness factor of each badge. Output Output single integer — minimum amount of coins the colonel has to pay. Examples Input 4 1 3 1 4 Output 1 Input 5 1 2 3 2 5 Output 2 Note In first sample test we can increase factor of first badge by 1. In second sample test we can increase factors of the second and the third badge by 1.
1,200
false
true
true
false
false
false
true
false
true
false
7,656
1666L
Leslie and Leon entered a labyrinth. The labyrinth consists of $$$n$$$ halls and $$$m$$$ one-way passages betweenxa0them. The halls are numbered from $$$1$$$ to $$$n$$$. Leslie and Leon start their journey in the hall $$$s$$$. Right away, they quarrel and decide to explore thexa0labyrinth separately. However, they want to meet again at the end of their journey. To help Leslie and Leon, your task is to find two different paths from the given hall $$$s$$$ to some other hall $$$t$$$, such that these two paths do not share halls other than the staring hall $$$s$$$ and the ending hall $$$t$$$. Thexa0hallxa0$$$t$$$ has not been determined yet, so you can choose any of the labyrinth's halls as $$$t$$$ except $$$s$$$. Leslie's and Leon's paths do not have to be the shortest ones, but their paths must be simple, visiting any hall at most once. Also, they cannot visit any common halls except $$$s$$$ and $$$t$$$ during their journey, even at different times. Input The first line contains three integers $$$n$$$, $$$m$$$, and $$$s$$$, where $$$n$$$xa0($$$2 le n le 2 cdot 10^5$$$) is the number of vertices, $$$m$$$xa0($$$0 le m le 2 cdot 10^5$$$) is the number of edges in the labyrinth, and $$$s$$$xa0($$$1 le s le n$$$) is the starting hall. Then $$$m$$$ lines with descriptions of passages follow. Each description contains two integers $$$u_i$$$,xa0$$$v_i$$$ ($$$1 le u_i, v_i le n$$$; $$$u_i eq v_i$$$), denoting a passage from the hall $$$u_i$$$ to the hall $$$v_i$$$. The passages are one-way. Each tuple $$$(u_i, v_i)$$$ is present in the input at most once. The labyrinth can contain cycles and is not necessarily connected in any way. Output If it is possible to find the desired two paths, output "Possible", otherwise output "Impossible". If the answer exists, output two path descriptions. Each description occupies two lines. The first line of the description contains an integer $$$h$$$ ($$$2 le h le n$$$)xa0— the number of halls in a path, and the second line contains distinct integers $$$w_1, w_2, dots, w_h$$$ ($$$w_1 = s$$$; $$$1 le w_j le n$$$; $$$w_h = t$$$)xa0— the halls in the path in the order of passing. Both paths must end at the same vertex $$$t$$$. The paths must be different, and all intermediate halls in these paths must be distinct. Examples Input 5 5 1 1 2 2 3 1 4 4 3 3 5 Output Possible 3 1 2 3 3 1 4 3 Input 5 5 1 1 2 2 3 3 4 2 5 5 4
1,800
false
false
false
false
false
false
false
false
false
true
2,274
1657B
You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i le B$$$ for all $$$i$$$ and $$$sumlimits_{i=0}^{n}{a_i}$$$ is maximum possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le B, x, y le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print one integerxa0— the maximum possible $$$sumlimits_{i=0}^{n}{a_i}$$$. Example Input 3 5 100 1 30 7 1000000000 1000000000 1000000000 4 1 7 3 Note In the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$. In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$. In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$.
800
false
true
false
false
false
false
false
false
false
false
2,344
1819F
You have long dreamed of working in a large IT company and finally got a job there. You have studied all existing modern technologies for a long time and are ready to apply all your knowledge in practice. But then you sit down at your desk and see a sheet of paper with the company's motto printed in large letters: abcdabcdabcdabcd.... The company's motto contains four main principles— a (Willi), b (Nilli), c (Crack), d (Release). Therefore, you consider strings of length $$$n$$$ consisting of these four Latin letters. Unordered pairs of letters "ab", "bc", "cd", and "da" in this motto are adjacent, so we will call such pairs of symbols good. So, if you are given a string $$$s$$$ of length $$$n$$$, and it is known that the unordered pair of symbols $$${ x, y }$$$ is good, then you can perform one of the following operations on the string: if $$$s_n = x$$$, then you are allowed to replace this symbol with $$$y$$$, if there exists $$$1 le i < n$$$ such that $$$s_i = x$$$ and $$$s_{i+1} = ldots = s_n = y$$$, then you are allowed to replace the $$$i$$$-th symbol of the string with $$$y$$$, and all subsequent symbols with $$$x$$$. For example, the string bacdd can be replaced with one of the strings bacda, bacdc, or badcc, and the string aac can be replaced with aab or aad. A non-empty sequence of operations for the string $$$s$$$ will be called correct if the following two conditions are met: 1. after performing all operations, the string becomes $$$s$$$ again, 2. no string, except for $$$s$$$, will occur more than once during the operations. At the same time, the string $$$s$$$ can occur exactly twice - before the start of the operations and after performing all operations. Now we are ready to move on to the problem statement! You have a set of strings that is initially empty. Then, each of $$$q$$$ queries adds another string $$$t_i$$$ to the set, or removes the string $$$t_i$$$ from the set. After each query, you need to output the minimum and maximum size of a correct sequence of operations in which each word occurs at least once. The choice of the initial string $$$s$$$ is up to you. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$1 le n le 20$$$, $$$1 le q le 100,000$$$)xa0— the length of the strings under consideration and the number of queries to modify the set of strings. Each of the next $$$q$$$ lines contains a string $$$t_i$$$ ($$$lvert t_i vert = n$$$). All strings consist of characters "a", "b", "c" and "d". If the string $$$t_i$$$ was not in the set before the query, it is added to the set, otherwise it is removed from the set. Output For each of the $$$q$$$ queries, output two integers: the minimum and maximum size of a correct sequence of operations in which each word from the set appears at least once. If there is no sequence of operations that satisfies the condition of the problem, output a single number $$$-1$$$.
3,500
false
false
false
true
true
false
false
false
false
false
1,360
1872G
Given an array $$$a$$$ of $$$n$$$ positive integers. You need to perform the following operation exactly once: Choose $$$2$$$ integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$) and replace the subarray $$$a[l ldots r]$$$ with the single element: the product of all elements in the subarray $$$(a_l cdot ldots cdot a_r)$$$. For example, if an operation with parameters $$$l = 2, r = 4$$$ is applied to the array $$$[5, 4, 3, 2, 1]$$$, the array will turn into $$$[5, 24, 1]$$$. Your task is to maximize the sum of the array after applying this operation. Find the optimal subarray to apply this operation. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. This is followed by the description of the test cases. The first line of each test case contains a single number $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of the values of $$$n$$$ for all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$2$$$ integers $$$l$$$ and $$$r$$$ ($$$1 le l le r le n$$$)xa0— the boundaries of the subarray to be replaced with the product. If there are multiple solutions, output any of them. Example Input 9 4 1 3 1 3 4 1 1 2 3 5 1 1 1 1 1 5 10 1 10 1 10 1 1 2 2 2 3 2 1 2 4 2 1 1 3 6 2 1 2 1 1 3 Output 2 4 3 4 1 1 1 5 1 1 1 2 2 2 4 4 1 6 Note In the first test case, after applying the operation with parameters $$$l = 2, r = 4$$$, the array $$$[1, 3, 1, 3]$$$ turns into $$$[1, 9]$$$, with a sum equal to $$$10$$$. It is easy to see that by replacing any other segment with a product, the sum will be less than $$$10$$$. In the second test case, after applying the operation with parameters $$$l = 3, r = 4$$$, the array $$$[1, 1, 2, 3]$$$ turns into $$$[1, 1, 6]$$$, with a sum equal to $$$8$$$. It is easy to see that by replacing any other segment with a product, the sum will be less than $$$8$$$. In the third test case, it will be optimal to choose any operation with $$$l = r$$$, then the sum of the array will remain $$$5$$$, and when applying any other operation, the sum of the array will decrease.
2,000
true
true
false
false
false
false
true
false
false
false
1,045
761D
Dasha logged into the system and began to solve problems. One of them is as follows: Given two sequences _a_ and _b_ of length _n_ each you need to write a sequence _c_ of length _n_, the _i_-th element of which is calculated as follows: _c__i_u2009=u2009_b__i_u2009-u2009_a__i_. About sequences _a_ and _b_ we know that their elements are in the range from _l_ to _r_. More formally, elements satisfy the following conditions: _l_u2009≤u2009_a__i_u2009≤u2009_r_ and _l_u2009≤u2009_b__i_u2009≤u2009_r_. About sequence _c_ we know that all its elements are distinct. Dasha wrote a solution to that problem quickly, but checking her work on the standard test was not so easy. Due to an error in the test system only the sequence _a_ and the compressed sequence of the sequence _c_ were known from that test. Let's give the definition to a compressed sequence. A compressed sequence of sequence _c_ of length _n_ is a sequence _p_ of length _n_, so that _p__i_ equals to the number of integers which are less than or equal to _c__i_ in the sequence _c_. For example, for the sequence _c_u2009=u2009[250,u2009200,u2009300,u2009100,u200950] the compressed sequence will be _p_u2009=u2009[4,u20093,u20095,u20092,u20091]. Pay attention that in _c_ all integers are distinct. Consequently, the compressed sequence contains all integers from 1 to _n_ inclusively. Help Dasha to find any sequence _b_ for which the calculated compressed sequence of sequence _c_ is correct. Input The first line contains three integers _n_, _l_, _r_ (1u2009≤u2009_n_u2009≤u2009105,u20091u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009109) — the length of the sequence and boundaries of the segment where the elements of sequences _a_ and _b_ are. The next line contains _n_ integers _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_ (_l_u2009≤u2009_a__i_u2009≤u2009_r_) — the elements of the sequence _a_. The next line contains _n_ distinct integers _p_1,u2009u2009_p_2,u2009u2009...,u2009u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_) — the compressed sequence of the sequence _c_. Output If there is no the suitable sequence _b_, then in the only line print "-1". Otherwise, in the only line print _n_ integers — the elements of any suitable sequence _b_. Examples Input 5 1 5 1 1 1 1 1 3 1 5 4 2 Input 6 1 5 1 1 1 1 1 1 2 3 5 4 1 6 Note Sequence _b_ which was found in the second sample is suitable, because calculated sequence _c_u2009=u2009[2u2009-u20093,u20092u2009-u20094,u20092u2009-u20098,u20099u2009-u20099]u2009=u2009[u2009-u20091,u2009u2009-u20092,u2009u2009-u20096,u20090] (note that _c__i_u2009=u2009_b__i_u2009-u2009_a__i_) has compressed sequence equals to _p_u2009=u2009[3,u20092,u20091,u20094].
1,700
false
true
false
false
false
true
true
true
true
false
6,746
1169B
Toad Ivan has $$$m$$$ pairs of integers, each integer is between $$$1$$$ and $$$n$$$, inclusive. The pairs are $$$(a_1, b_1), (a_2, b_2), ldots, (a_m, b_m)$$$. He asks you to check if there exist two integers $$$x$$$ and $$$y$$$ ($$$1 leq x < y leq n$$$) such that in each given pair at least one integer is equal to $$$x$$$ or $$$y$$$. Input The first line contains two space-separated integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 300,000$$$, $$$1 leq m leq 300,000$$$)xa0— the upper bound on the values of integers in the pairs, and the number of given pairs. The next $$$m$$$ lines contain two integers each, the $$$i$$$-th of them contains two space-separated integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i, b_i leq n, a_i eq b_i$$$)xa0— the integers in the $$$i$$$-th pair. Output Output "YES" if there exist two integers $$$x$$$ and $$$y$$$ ($$$1 leq x < y leq n$$$) such that in each given pair at least one integer is equal to $$$x$$$ or $$$y$$$. Otherwise, print "NO". You can print each letter in any case (upper or lower). Examples Input 4 6 1 2 1 3 1 4 2 3 2 4 3 4 Input 300000 5 1 2 1 2 1 2 1 2 1 2 Note In the first example, you can't choose any $$$x$$$, $$$y$$$ because for each such pair you can find a given pair where both numbers are different from chosen integers. In the second example, you can choose $$$x=2$$$ and $$$y=4$$$. In the third example, you can choose $$$x=1$$$ and $$$y=2$$$.
1,500
false
false
true
false
false
false
false
false
false
true
4,870
820A
Mister B once received a gift: it was a book about aliens, which he started read immediately. This book had _c_ pages. At first day Mister B read _v_0 pages, but after that he started to speed up. Every day, starting from the second, he read _a_ pages more than on the previous day (at first day he read _v_0 pages, at secondxa0— _v_0u2009+u2009_a_ pages, at thirdxa0— _v_0u2009+u20092_a_ pages, and so on). But Mister B is just a human, so he physically wasn't able to read more than _v_1 pages per day. Also, to refresh his memory, every day, starting from the second, Mister B had to reread last _l_ pages he read on the previous day. Mister B finished the book when he read the last page for the first time. Help Mister B to calculate how many days he needed to finish the book. Input First and only line contains five space-separated integers: _c_, _v_0, _v_1, _a_ and _l_ (1u2009≤u2009_c_u2009≤u20091000, 0u2009≤u2009_l_u2009<u2009_v_0u2009≤u2009_v_1u2009≤u20091000, 0u2009≤u2009_a_u2009≤u20091000) — the length of the book in pages, the initial reading speed, the maximum reading speed, the acceleration in reading speed and the number of pages for rereading. Output Print one integer — the number of days Mister B needed to finish the book. Note In the first sample test the book contains 5 pages, so Mister B read it right at the first day. In the second sample test at first day Mister B read pages number 1u2009-u20094, at second dayxa0— 4u2009-u200911, at third dayxa0— 11u2009-u200912 and finished the book. In third sample test every day Mister B read 1 page of the book, so he finished in 15 days.
900
false
false
true
false
false
false
false
false
false
false
6,475
1592C
Bakry faced a problem, but since he's lazy to solve it, he asks for your help. You are given a tree of $$$n$$$ nodes, the $$$i$$$-th node has value $$$a_i$$$ assigned to it for each $$$i$$$ from $$$1$$$ to $$$n$$$. As a reminder, a tree on $$$n$$$ nodes is a connected graph with $$$n-1$$$ edges. You want to delete at least $$$1$$$, but at most $$$k-1$$$ edges from the tree, so that the following condition would hold: For every connected component calculate the $$$. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ $$$(2 leq k leq n leq 10^5)$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ..., a_n$$$ $$$(1 leq a_i leq 10^9)$$$. The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$, $$$u_i eq v_i$$$), which means that there's an edge between nodes $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given graph is a tree. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, you should output a single string. If you can delete the edges according to the conditions written above, output "YES" (without quotes). Otherwise, output "NO" (without quotes). You can print each letter of "YES" and "NO" in any case (upper or lower). Example Input 5 2 2 1 3 1 2 5 5 3 3 3 3 3 1 2 2 3 1 4 4 5 5 2 1 7 2 3 5 1 2 2 3 1 4 4 5 5 3 1 6 4 1 2 1 2 2 3 1 4 4 5 3 3 1 7 4 1 2 2 3 Note It can be shown that the objection is not achievable for first, third, and fifth test cases. In the second test case, you can just remove all the edges. There will be $$$5$$$ connected components, each containing only one node with value $$$3$$$, so the bitwise XORs will be $$$3$$$ for all of them. In the fourth test case, this is the tree: . You can remove an edge $$$(4,5)$$$ The bitwise XOR of the first component will be, $$$a_1 oplus a_2 oplus a_3 oplus a_4 = 1 oplus 6 oplus 4 oplus 1 = 2$$$ (where $$$oplus$$$ denotes the bitwise XOR). The bitwise XOR of the second component will be, $$$a_5 = 2$$$.
1,700
false
false
false
true
false
true
false
false
false
true
2,691
887B
Absent-minded Masha got set of _n_ cubes for her birthday. At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural _x_ such she can make using her new cubes all integers from 1 to _x_. To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number. The number can't contain leading zeros. It's not required to use all cubes to build a number. Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations. Input In first line integer _n_ is given (1u2009≤u2009_n_u2009≤u20093)xa0— the number of cubes, Masha got for her birthday. Each of next _n_ lines contains 6 integers _a__i__j_ (0u2009≤u2009_a__i__j_u2009≤u20099)xa0— number on _j_-th face of _i_-th cube. Output Print single integerxa0— maximum number _x_ such Masha can make any integers from 1 to _x_ using her cubes or 0 if Masha can't make even 1. Examples Input 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 Input 3 0 1 3 5 6 8 1 2 4 5 7 8 2 3 4 6 7 9 Note In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.
1,300
false
false
true
false
false
false
true
false
false
false
6,201
1438E
Yurii is sure he can do everything. Can he solve this task, though? He has an array $$$a$$$ consisting of $$$n$$$ positive integers. Let's call a subarray $$$a elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer $$$n$$$ ($$$3 leq n leq 2cdot 10^5$$$)xa0— the length of $$$a$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 leq a_i lt 2^{30}$$$)xa0— elements of $$$a$$$. Output Output a single integerxa0— the number of good subarrays. Examples Input 10 997230370 58052053 240970544 715275815 250707702 156801523 44100666 64791577 43523002 480196854 Note There are $$$6$$$ good subarrays in the example: $$$[3,1,2]$$$ (twice) because $$$(3 oplus 2) = 1$$$; $$$[1,2,3]$$$ (twice) because $$$(1 oplus 3) = 2$$$; $$$[2,3,1]$$$ because $$$(2 oplus 1) = 3$$$; $$$[3,1,2,3,1,2,3,15]$$$ because $$$(3 oplus 15) = (1+2+3+1+2+3)$$$.
2,500
false
false
false
false
false
true
true
true
false
false
3,474
1873A
There are three cards with letters $$$ exttt{a}$$$, $$$ exttt{b}$$$, $$$ exttt{c}$$$ placed in a row in some order. You can do the following operation at most once: Pick two cards, and swap them. Is it possible that the row becomes $$$ exttt{abc}$$$ after the operation? Output "YES" if it is possible, and "NO" otherwise. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 6$$$)xa0— the number of test cases. The only line of each test case contains a single string consisting of each of the three characters $$$ exttt{a}$$$, $$$ exttt{b}$$$, and $$$ exttt{c}$$$ exactly once, representing the cards. Output For each test case, output "YES" if you can make the row $$$ exttt{abc}$$$ with at most one operation, or "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Output YES YES YES NO NO YES Note In the first test case, we don't need to do any operations, since the row is already $$$ exttt{abc}$$$. In the second test case, we can swap $$$ exttt{c}$$$ and $$$ exttt{b}$$$: $$$ exttt{acb} o exttt{abc}$$$. In the third test case, we can swap $$$ exttt{b}$$$ and $$$ exttt{a}$$$: $$$ exttt{bac} o exttt{abc}$$$. In the fourth test case, it is impossible to make $$$ exttt{abc}$$$ using at most one operation.
800
false
false
true
false
false
false
true
false
false
false
1,044
237D
You've got a undirected tree _s_, consisting of _n_ nodes. Your task is to build an optimal T-decomposition for it. Let's define a T-decomposition as follows. Let's denote the set of all nodes _s_ as _v_. Let's consider an undirected tree _t_, whose nodes are some non-empty subsets of _v_, we'll call them _x__i_ . The tree _t_ is a T-decomposition of _s_, if the following conditions holds: 1. the union of all _x__i_ equals _v_; 2. for any edge (_a_,u2009_b_) of tree _s_ exists the tree node _t_, containing both _a_ and _b_; 3. if the nodes of the tree _t_ _x__i_ and _x__j_ contain the node _a_ of the tree _s_, then all nodes of the tree _t_, lying on the path from _x__i_ to _x__j_ also contain node _a_. So this condition is equivalent to the following: all nodes of the tree _t_, that contain node _a_ of the tree _s_, form a connected subtree of tree _t_. There are obviously many distinct trees _t_, that are T-decompositions of the tree _s_. For example, a T-decomposition is a tree that consists of a single node, equal to set _v_. Let's define the cardinality of node _x__i_ as the number of nodes in tree _s_, containing in the node. Let's choose the node with the maximum cardinality in _t_. Let's assume that its cardinality equals _w_. Then the weight of T-decomposition _t_ is value _w_. The optimal T-decomposition is the one with the minimum weight. Your task is to find the optimal T-decomposition of the given tree _s_ that has the minimum number of nodes. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009105), that denotes the number of nodes in tree _s_. Each of the following _n_u2009-u20091 lines contains two space-separated integers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_;xa0_a__i_u2009≠u2009_b__i_), denoting that the nodes of tree _s_ with indices _a__i_ and _b__i_ are connected by an edge. Consider the nodes of tree _s_ indexed from 1 to _n_. It is guaranteed that _s_ is a tree. Output In the first line print a single integer _m_ that denotes the number of nodes in the required T-decomposition. Then print _m_ lines, containing descriptions of the T-decomposition nodes. In the _i_-th (1u2009≤u2009_i_u2009≤u2009_m_) of them print the description of node _x__i_ of the T-decomposition. The description of each node _x__i_ should start from an integer _k__i_, that represents the number of nodes of the initial tree _s_, that are contained in the node _x__i_. Then you should print _k__i_ distinct space-separated integers — the numbers of nodes from _s_, contained in _x__i_, in arbitrary order. Then print _m_u2009-u20091 lines, each consisting two integers _p__i_,u2009_q__i_ (1u2009≤u2009_p__i_,u2009_q__i_u2009≤u2009_m_;xa0_p__i_u2009≠u2009_q__i_). The pair of integers _p__i_,u2009_q__i_ means there is an edge between nodes _x__p__i_ and _x__q__i_ of T-decomposition. The printed T-decomposition should be the optimal T-decomposition for the given tree _s_ and have the minimum possible number of nodes among all optimal T-decompositions. If there are multiple optimal T-decompositions with the minimum number of nodes, print any of them. Examples Output 3 2 2 1 2 3 1 2 4 1 1 2 2 3
2,000
false
true
false
false
false
false
false
false
false
true
8,886
1218F
Alan decided to get in shape for the summer, so he created a precise workout plan to follow. His plan is to go to a different gym every day during the next N days and lift $$$X[i]$$$ grams on day $$$i$$$. In order to improve his workout performance at the gym, he can buy exactly one pre-workout drink at the gym he is currently in and it will improve his performance by $$$A$$$ grams permanently and immediately. In different gyms these pre-workout drinks can cost different amounts $$$C[i]$$$ because of the taste and the gym's location but its permanent workout gains are the same. Before the first day of starting his workout plan, Alan knows he can lift a maximum of $$$K$$$ grams. Help Alan spend a minimum total amount of money in order to reach his workout plan. If there is no way for him to complete his workout plan successfully output $$$-1$$$. Input The first one contains two integer numbers, integers $$$N$$$ $$$(1 leq N leq 10^5)$$$ and $$$K$$$ $$$(1 leq K leq 10^5)$$$ – representing number of days in the workout plan and how many grams he can lift before starting his workout plan respectively. The second line contains $$$N$$$ integer numbers $$$X[i]$$$ $$$(1 leq X[i] leq 10^9)$$$ separated by a single space representing how many grams Alan wants to lift on day $$$i$$$. The third line contains one integer number $$$A$$$ $$$(1 leq A leq 10^9)$$$ representing permanent performance gains from a single drink. The last line contains $$$N$$$ integer numbers $$$C[i]$$$ $$$(1 leq C[i] leq 10^9)$$$ , representing cost of performance booster drink in the gym he visits on day $$$i$$$. Output One integer number representing minimal money spent to finish his workout plan. If he cannot finish his workout plan, output -1. Examples Input 5 10000 10000 30000 30000 40000 20000 20000 5 2 8 3 6 Input 5 10000 10000 40000 30000 30000 20000 10000 5 2 8 3 6 Note First example: After buying drinks on days 2 and 4 Alan can finish his workout plan. Second example: Alan cannot lift 40000 grams on day 2.
1,500
false
true
false
false
true
false
false
false
false
false
4,579
730J
Nick has _n_ bottles of soda left after his birthday. Each bottle is described by two values: remaining amount of soda _a__i_ and bottle volume _b__i_ (_a__i_u2009≤u2009_b__i_). Nick has decided to pour all remaining soda into minimal number of bottles, moreover he has to do it as soon as possible. Nick spends _x_ seconds to pour _x_ units of soda from one bottle to another. Nick asks you to help him to determine _k_ — the minimal number of bottles to store all remaining soda and _t_ — the minimal time to pour soda into _k_ bottles. A bottle can't store more soda than its volume. All remaining soda should be saved. Input The first line contains positive integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of bottles. The second line contains _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009100), where _a__i_ is the amount of soda remaining in the _i_-th bottle. The third line contains _n_ positive integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u2009100), where _b__i_ is the volume of the _i_-th bottle. It is guaranteed that _a__i_u2009≤u2009_b__i_ for any _i_. Output The only line should contain two integers _k_ and _t_, where _k_ is the minimal number of bottles that can store all the soda and _t_ is the minimal time to pour the soda into _k_ bottles. Examples Input 5 10 30 5 6 24 10 41 7 8 24 Note In the first example Nick can pour soda from the first bottle to the second bottle. It will take 3 seconds. After it the second bottle will contain 3u2009+u20093u2009=u20096 units of soda. Then he can pour soda from the fourth bottle to the second bottle and to the third bottle: one unit to the second and two units to the third. It will take 1u2009+u20092u2009=u20093 seconds. So, all the soda will be in two bottles and he will spend 3u2009+u20093u2009=u20096 seconds to do it.
1,900
false
false
false
true
false
false
false
false
false
false
6,879
404D
Game "Minesweeper 1D" is played on a line of squares, the line's height is 1 square, the line's width is _n_ squares. Some of the squares contain bombs. If a square doesn't contain a bomb, then it contains a number from 0 to 2 — the total number of bombs in adjacent squares. For example, the correct field to play looks like that: 001*2***101*. The cells that are marked with "*" contain bombs. Note that on the correct field the numbers represent the number of bombs in adjacent cells. For example, field 2is not correct, because cell with value 2 must have two adjacent cells with bombs. Valera wants to make a correct field to play "Minesweeper 1D". He has already painted a squared field with width of _n_ cells, put several bombs on the field and wrote numbers into some cells. Now he wonders how many ways to fill the remaining cells with bombs and numbers are there if we should get a correct field in the end. Input The first line contains sequence of characters without spaces _s_1_s_2... _s__n_ (1u2009≤u2009_n_u2009≤u2009106), containing only characters "*", "?" and digits "0", "1" or "2". If character _s__i_ equals "*", then the _i_-th cell of the field contains a bomb. If character _s__i_ equals "?", then Valera hasn't yet decided what to put in the _i_-th cell. Character _s__i_, that is equal to a digit, represents the digit written in the _i_-th square. Output Print a single integer — the number of ways Valera can fill the empty cells and get a correct field. As the answer can be rather large, print it modulo 1000000007 (109u2009+u20097). Note In the first test sample you can get the following correct fields: 001**1, 001***, 001*2*, 001*10.
1,900
false
false
true
true
false
false
false
false
false
false
8,207
476E
Problem - 476E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dp strings *2200 No tag edit access → Contest materials . The second line of the input contains the string _p_ (1u2009≤u2009_p_u2009≤u2009500). Both strings will only consist of lower case English letters. Output Print _s_u2009+u20091 space-separated integers in a single line representing the for all _x_ from 0 to _s_. Examples Input aaaaa aa Output 2 2 1 1 0 0 Input axbaxxb ab Output 0 1 1 2 1 1 0 0 Note For the first sample, the corresponding optimal values of _s_' after removal 0 through _s_u2009=u20095 characters from _s_ are {"aaaaa", "aaaa", "aaa", "aa", "a", ""}. For the second sample, possible corresponding optimal values of _s_' are {"axbaxxb", "abaxxb", "axbab", "abab", "aba", "ab", "a", ""}.
2,200
false
false
false
true
false
false
false
false
false
false
7,924
903C
Problem - 903C - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags greedy *1200 No tag edit access → Contest materials , _i_-th box is a cube with side length _a__i_. Mishka can put a box _i_ into another box _j_ if the following conditions are met: _i_-th box is not put into another box; _j_-th box doesn't contain any other boxes; box _i_ is smaller than box _j_ (_a__i_u2009<u2009_a__j_). Mishka can put boxes into each other an arbitrary number of times. He wants to minimize the number of visible boxes. A box is called visible iff it is not put into some another box. Help Mishka to determine the minimum possible number of visible boxes! Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u20095000) — the number of boxes Mishka has got. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109), where _a__i_ is the side length of _i_-th box. Output Print the minimum possible number of visible boxes. Examples Input 3 1 2 3 Output 1 Input 4 4 2 4 3 Output 2 Note In the first example it is possible to put box 1 into box 2, and 2 into 3. In the second example Mishka can put box 2 into box 3, and box 4 into box 1.
1,200
false
true
false
false
false
false
false
false
false
false
6,132
1082C
A multi-subject competition is coming! The competition has $$$m$$$ different subjects participants can choose from. That's why Alex (the coach) should form a competition delegation among his students. He has $$$n$$$ candidates. For the $$$i$$$-th person he knows subject $$$s_i$$$ the candidate specializes in and $$$r_i$$$ — a skill level in his specialization (this level can be negative!). The rules of the competition require each delegation to choose some subset of subjects they will participate in. The only restriction is that the number of students from the team participating in each of the chosen subjects should be the same. Alex decided that each candidate would participate only in the subject he specializes in. Now Alex wonders whom he has to choose to maximize the total sum of skill levels of all delegates, or just skip the competition this year if every valid non-empty delegation has negative sum. (Of course, Alex doesn't have any spare money so each delegate he chooses must participate in the competition). Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5$$$, $$$1 le m le 10^5$$$) — the number of candidates and the number of subjects. The next $$$n$$$ lines contains two integers per line: $$$s_i$$$ and $$$r_i$$$ ($$$1 le s_i le m$$$, $$$-10^4 le r_i le 10^4$$$) — the subject of specialization and the skill level of the $$$i$$$-th candidate. Output Print the single integer — the maximum total sum of skills of delegates who form a valid delegation (according to rules above) or $$$0$$$ if every valid non-empty delegation has negative sum. Examples Input 6 3 2 6 3 6 2 5 3 5 1 9 3 1 Input 5 3 2 6 3 6 2 5 3 5 1 11 Input 5 2 1 -1 1 -5 2 -1 2 -1 1 -10 Note In the first example it's optimal to choose candidates $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, so two of them specialize in the $$$2$$$-nd subject and other two in the $$$3$$$-rd. The total sum is $$$6 + 6 + 5 + 5 = 22$$$. In the second example it's optimal to choose candidates $$$1$$$, $$$2$$$ and $$$5$$$. One person in each subject and the total sum is $$$6 + 6 + 11 = 23$$$. In the third example it's impossible to obtain a non-negative sum.
1,600
false
true
false
false
false
false
false
false
true
false
5,313
494E
During the last 24 hours Hamed and Malek spent all their time playing "Sharti". Now they are too exhausted to finish the last round. So they asked you for help to determine the winner of this round. "Sharti" is played on a _n_u2009×u2009_n_ board with some of cells colored white and others colored black. The rows of the board are numbered from top to bottom using number 1 to _n_. Also the columns of the board are numbered from left to right using numbers 1 to _n_. The cell located at the intersection of _i_-th row and _j_-th column is denoted by (_i_,u2009_j_). The players alternatively take turns. In each turn the player must choose a square with side-length at most _k_ with its lower-right cell painted white. Then the colors of all the cells in this square are inversed (white cells become black and vice-versa). The player who cannot perform a move in his turn loses. You know Hamed and Malek are very clever and they would have played their best moves at each turn. Knowing this and the fact that Hamed takes the first turn, given the initial board as described in the input, you must determine which one of them will be the winner. Input In this problem the initial board is specified as a set of _m_ rectangles. All cells that lie inside at least one of these rectangles are colored white and the rest are colored black. In the first line of input three space-spereated integers _n_,u2009_m_,u2009_k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009109, 1u2009≤u2009_m_u2009≤u20095·104) follow, denoting size of the board, number of rectangles and maximum size of the turn square during the game, respectively. In _i_-th line of the next _m_ lines four space-seperated integers _a__i_,u2009_b__i_,u2009_c__i_,u2009_d__i_ (1u2009≤u2009_a__i_u2009≤u2009_c__i_u2009≤u2009_n_, 1u2009≤u2009_b__i_u2009≤u2009_d__i_u2009≤u2009_n_) are given meaning that _i_-th rectangle determining the initial board is a rectangle with upper-left cell at (_a__i_,u2009_b__i_) and lower-right cell at (_c__i_,u2009_d__i_). Output If Hamed wins, print "Hamed", otherwise print "Malek" (without the quotes). Examples Input 12 5 7 3 4 5 6 1 2 1 2 4 5 9 9 8 6 12 10 12 4 12 4
3,200
false
false
false
false
true
false
false
false
false
false
7,854
1819E
This is an interactive problem. As is well known, the city "E" has never had its roads repaired in its a thousand and a half years old history. And only recently the city administration repaired some of them. It is known that in total in the city "E" there are $$$n$$$ intersections and $$$m$$$ roads, which can be used in both directions, numbered with integers from $$$1$$$ to $$$m$$$. The $$$i$$$-th road connects intersections with numbers $$$a_i$$$ and $$$b_i$$$. Among all $$$m$$$ roads, some subset of the roads has been repaired, but you do not know which one. The only information you could get from the city's road services is that you can get from any intersection to any other intersection by driving only on the roads that have been repaired. You are a young entrepreneur, and decided to organize a delivery service of fresh raw meat in the city "E" (in this city such meat is called "steaks", it is very popular among the locals). You have already recruited a staff of couriers, but the couriers are willing to travel only on repaired roads. Now you have to find out which roads have already been repaired. The city administration has given you the city for a period of time, so you can make different queries of one of three types: 1. Block the road with the number $$$x$$$. In this case, movement on the road for couriers will be forbidden. Initially all roads are unblocked. 2. Unblock the road with the number $$$x$$$. In this case, couriers will be able to move on the road $$$x$$$ if it is repaired. 3. Try to deliver the order to the intersection with the number $$$y$$$. In this case, one of your couriers will start moving from intersection with number $$$s$$$ you don't know and deliver the order to intersection with number $$$y$$$ if there is a path on unblocked repaired roads from intersection $$$s$$$ to intersection $$$y$$$. It is guaranteed that intersection $$$s$$$ will be chosen beforehand. Unfortunately, the city is placed at your complete disposal for a short period of time, so you can make no more than $$$100 cdot m$$$ requests. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1,000$$$) — the number of test cases. The description of test cases follows. The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2,000$$$, $$$n - 1 le m le 2,000$$$)xa0—the number of intersections and roads in the city "E". Each of the following $$$m$$$ lines describes one road. The $$$i$$$-th of these lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le n$$$)xa0— the ends of the $$$i$$$-th road. It is guaranteed that no road connects the city to itself, while it is possible that there are several roads between a pair of different intersections. It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases does not exceed $$$2,000$$$. Interaction Once you have read the description of the test case, you can make queries. Queries can be of three types: 1. "- $$$x$$$" ($$$1 le x le m$$$). In this case the road with the number $$$x$$$ is blocked if it has not already been blocked. 2. "+ $$$x$$$" ($$$1 le x le m$$$). In this case the road with the number $$$x$$$ is unblocked. Note that road $$$x$$$ must be blocked beforehand. All roads are initially unblocked. 3. "? $$$y$$$" ($$$1 le y le n$$$). In this case the jury program chooses some city $$$s$$$. If you can get from town $$$s$$$ to town $$$y$$$ by unblocked repaired roads, the jury program will output $$$1$$$, otherwise the jury program will output $$$0$$$. Note that city $$$s$$$ will be selected before getting information about city $$$y$$$, but your previous requests may be taken into account when selecting city $$$s$$$. In total, you can make no more than $$$100 cdot m$$$ queries for each set of input data. After you have found all repaired roads, output "! $$$c_1, c_2, c_3, ldots, c_m$$$", where $$$c_i$$$ is $$$1$$$ if road $$$i$$$ is repaired, and $$$0$$$ if road is not repaired. This output will not count in the total number of queries. The jury program will output $$$1$$$ if your answer is correct, and $$$0$$$ if the answer is not correct. If you received $$$0$$$, your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get any verdict, because the program will continue reading from the closed stream. If you read $$$1$$$, move on to the next test case, or terminate the program if there is none. Note that you do not have to unblock all roads before outputting the answer. It is guaranteed that all repaired roads are fixed initially and will not be changed by the jury program depending on queries. After outputting a query or the answer 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 You can't do hacks on this problem. Example Input 2 2 2 1 2 2 1 1 0 1 1 3 3 1 2 2 3 3 1 1 1 1 0 1 1 1 1 Output - 1 ? 1 ? 2 - 2 + 1 ? 1 ! 1 0 - 1 ? 2 ? 1 - 2 ? 3 ? 3 + 1 ? 3 ? 2 ? 1 ! 1 1 1 Note In the first test case, road $$$1$$$ was repaired, while road $$$2$$$ was not. For the first delivery request, intersection $$$1$$$ was selected as $$$s$$$, and the path from intersection $$$1$$$ to $$$1$$$ exists. For the second delivery request, intersection $$$1$$$ was selected as $$$s$$$. Since the only repaired road was blocked, there was no path between intersections $$$1$$$ and $$$2$$$. For the third delivery request, intersection $$$2$$$ was selected as $$$s$$$, the path between intersections $$$2$$$ and $$$1$$$ exists along road $$$1$$$, which is repaired and unblocked. In the second test case, intersections $$$1$$$, $$$3$$$, $$$1$$$, $$$2$$$, $$$2$$$, $$$3$$$, $$$1$$$ were selected as starting intersections for delivery requests.
3,200
true
false
false
false
false
false
false
false
false
false
1,361
843C
You are given a tree with _n_ vertices and you are allowed to perform no more than 2_n_ transformations on it. Transformation is defined by three vertices _x_,u2009_y_,u2009_y_' and consists of deleting edge (_x_,u2009_y_) and adding edge (_x_,u2009_y_'). Transformation _x_,u2009_y_,u2009_y_' could be performed if all the following conditions are satisfied: 1. There is an edge (_x_,u2009_y_) in the current tree. 2. After the transformation the graph remains a tree. 3. After the deletion of edge (_x_,u2009_y_) the tree would consist of two connected components. Let's denote the set of nodes in the component containing vertex _x_ by _V__x_, and the set of nodes in the component containing vertex _y_ by _V__y_. Then condition _V__x_u2009>u2009_V__y_ should be satisfied, i.e. the size of the component with _x_ should be strictly larger than the size of the component with _y_. You should minimize the sum of squared distances between all pairs of vertices in a tree, which you could get after no more than 2_n_ transformations and output any sequence of transformations leading initial tree to such state. Note that you don't need to minimize the number of operations. It is necessary to minimize only the sum of the squared distances. Input The first line of input contains integer _n_ (1u2009≤u2009_n_u2009≤u20092·105) — number of vertices in tree. The next _n_u2009-u20091 lines of input contains integers _a_ and _b_ (1u2009≤u2009_a_,u2009_b_u2009≤u2009_n_,u2009_a_u2009≠u2009_b_) — the descriptions of edges. It is guaranteed that the given edges form a tree. Output In the first line output integer _k_ (0u2009≤u2009_k_u2009≤u20092_n_) — the number of transformations from your example, minimizing sum of squared distances between all pairs of vertices. In each of the next _k_ lines output three integers _x_,u2009_y_,u2009_y_' — indices of vertices from the corresponding transformation. Transformations with _y_u2009=u2009_y_' are allowed (even though they don't change tree) if transformation conditions are satisfied. If there are several possible answers, print any of them. Note This is a picture for the second sample. Added edges are dark, deleted edges are dotted.
2,600
true
false
false
false
false
true
false
false
false
true
6,389
769C
The Robot is in a rectangular maze of size _n_u2009×u2009_m_. Each cell of the maze is either empty or occupied by an obstacle. The Robot can move between neighboring cells on the side left (the symbol "L"), right (the symbol "R"), up (the symbol "U") or down (the symbol "D"). The Robot can move to the cell only if it is empty. Initially, the Robot is in the empty cell. Your task is to find lexicographically minimal Robot's cycle with length exactly _k_, which begins and ends in the cell where the Robot was initially. It is allowed to the Robot to visit any cell many times (including starting). Consider that Robot's way is given as a line which consists of symbols "L", "R", "U" and "D". For example, if firstly the Robot goes down, then left, then right and up, it means that his way is written as "DLRU". In this task you don't need to minimize the length of the way. Find the minimum lexicographical (in alphabet order as in the dictionary) line which satisfies requirements above. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u20091000, 1u2009≤u2009_k_u2009≤u2009106) — the size of the maze and the length of the cycle. Each of the following _n_ lines contains _m_ symbols — the description of the maze. If the symbol equals to "." the current cell is empty. If the symbol equals to "*" the current cell is occupied by an obstacle. If the symbol equals to "X" then initially the Robot is in this cell and it is empty. It is guaranteed that the symbol "X" is found in the maze exactly once. Output Print the lexicographically minimum Robot's way with the length exactly _k_, which starts and ends in the cell where initially Robot is. If there is no such way, print "IMPOSSIBLE"(without quotes). Examples Input 5 6 14 ..***. *...X. ..*... ..*.* ....*. Note In the first sample two cyclic ways for the Robot with the length 2 exist — "UD" and "RL". The second cycle is lexicographically less. In the second sample the Robot should move in the following way: down, left, down, down, left, left, left, right, right, right, up, up, right, up. In the third sample the Robot can't move to the neighboring cells, because they are occupied by obstacles.
1,700
false
true
false
false
false
false
false
false
false
true
6,704
1106A
Lunar New Year is approaching, and you bought a matrix with lots of "crosses". This matrix $$$M$$$ of size $$$n imes n$$$ contains only 'X' and '.' (without quotes). The element in the $$$i$$$-th row and the $$$j$$$-th column $$$(i, j)$$$ is defined as $$$M(i, j)$$$, where $$$1 leq i, j leq n$$$. We define a cross appearing in the $$$i$$$-th row and the $$$j$$$-th column ($$$1 < i, j < n$$$) if and only if $$$M(i, j) = M(i - 1, j - 1) = M(i - 1, j + 1) = M(i + 1, j - 1) = M(i + 1, j + 1) = $$$ 'X'. The following figure illustrates a cross appearing at position $$$(2, 2)$$$ in a $$$3 imes 3$$$ matrix. X.X .X. X.X Your task is to find out the number of crosses in the given matrix $$$M$$$. Two crosses are different if and only if they appear in different rows or columns. Input The first line contains only one positive integer $$$n$$$ ($$$1 leq n leq 500$$$), denoting the size of the matrix $$$M$$$. The following $$$n$$$ lines illustrate the matrix $$$M$$$. Each line contains exactly $$$n$$$ characters, each of them is 'X' or '.'. The $$$j$$$-th element in the $$$i$$$-th line represents $$$M(i, j)$$$, where $$$1 leq i, j leq n$$$. Output Output a single line containing only one integer number $$$k$$$ — the number of crosses in the given matrix $$$M$$$. Examples Input 5 ..... .XXX. .XXX. .XXX. ..... Input 6 ...... X.X.X. .X.X.X X.X.X. .X.X.X ...... Note In the first sample, a cross appears at $$$(3, 3)$$$, so the answer is $$$1$$$. In the second sample, no crosses appear since $$$n < 3$$$, so the answer is $$$0$$$. In the third sample, crosses appear at $$$(3, 2)$$$, $$$(3, 4)$$$, $$$(4, 3)$$$, $$$(4, 5)$$$, so the answer is $$$4$$$.
800
false
false
true
false
false
false
false
false
false
false
5,175
937B
Problem - 937B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math number theory *1400 No tag edit access → Contest materials are occupied by tiny vile grasshoppers which you're at war with. These grasshoppers are known for their awesome jumping skills: the grasshopper at branch _x_ can jump to branches . Keeping this in mind, you wisely decided to choose such a branch that none of the grasshoppers could interrupt you. At the same time you wanna settle as high as possible since the view from up there is simply breathtaking. In other words, your goal is to find the highest branch that cannot be reached by any of the grasshoppers or report that it's impossible. Input The only line contains two integers _p_ and _y_ (2u2009≤u2009_p_u2009≤u2009_y_u2009≤u2009109). Output Output the number of the highest suitable branch. If there are none, print -1 instead. Examples Input 3 6 Output 5 Input 3 4 Output -1 Note In the first sample case grasshopper from branch 2 reaches branches 2, 4 and 6 while branch 3 is initially settled by another grasshopper. Therefore the answer is 5. It immediately follows that there are no valid branches in second sample case.
1,400
true
false
false
false
false
false
true
false
false
false
5,952
1791G1
The only difference between the easy and hard versions are the locations you can teleport to. Consider the points $$$0, 1, dots, n$$$ on the number line. There is a teleporter located on each of the points $$$1, 2, dots, n$$$. At point $$$i$$$, you can do the following: Move left one unit: it costs $$$1$$$ coin. Move right one unit: it costs $$$1$$$ coin. Use a teleporter at point $$$i$$$, if it exists: it costs $$$a_i$$$ coins. As a result, you teleport to point $$$0$$$. Once you use a teleporter, you can't use it again. You have $$$c$$$ coins, and you start at point $$$0$$$. What's the most number of teleporters you can use? Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 leq n leq 2cdot10^5$$$; $$$1 leq c leq 10^9$$$) xa0— the length of the array and the number of coins you have respectively. The following line contains $$$n$$$ space-separated integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the costs to use the teleporters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output the maximum number of teleporters you can use. Example Input 10 5 6 1 1 1 1 1 8 32 100 52 13 6 9 4 100 35 1 1 5 4 5 4 3 2 1 5 9 2 3 1 4 1 5 8 2 3 1 4 1 4 3 2 3 4 1 4 9 5 4 3 3 2 14 7 5 5 600000000 500000000 400000000 300000000 200000000 100000000 Output 2 2 0 1 2 2 1 1 1 2 Note In the first test case, you can move one unit to the right, use the teleporter at index $$$1$$$ and teleport to point $$$0$$$, move two units to the right and use the teleporter at index $$$2$$$. You are left with $$$6-1-1-2-1 = 1$$$ coins you don't have enough coins to use another teleporter. You have used two teleporters, so the answer is two. In the second test case, you go four units to the right and use the teleporter to go to $$$0$$$, then go six units right and use the teleporter at index $$$6$$$ to go to $$$0$$$. The total cost will be $$$4+6+6+4 = 20$$$. You are left with $$$12$$$ coins, but it is not enough to reach any other teleporter and use it so the answer is $$$2$$$. In the third test case, you don't have enough coins to use any teleporter, so the answer is zero.
1,100
false
true
false
false
false
false
false
false
true
false
1,529
454B
Problem - 454B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags implementation *1200 No tag edit access → Contest materials . The second line contains _n_ integer numbers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009105). Output If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it. Examples Input 2 2 1 Output 1 Input 3 1 3 2 Output -1 Input 2 1 2 Output 0
1,200
false
false
true
false
false
false
false
false
false
false
8,020
72I
Problem - 72I - Codeforces =============== xa0 → 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 *special problem implementation *1500 No tag edit access → Contest materials . Output Write on a single line the type of the integer: happy, happier or upset. Print the type in lowercase letters. Examples Input 99 Output happier Input 29994 Output happy Input 23 Output upset Note In the second test 29994 is only divisible by 2. In the third test 23 is a prime number.
1,500
false
false
true
false
false
false
false
false
false
false
9,603
2026B
You are given a strip divided into cells, numbered from left to right from $$$0$$$ to $$$10^{18}$$$. Initially, all cells are white. You can perform the following operation: choose two white cells $$$i$$$ and $$$j$$$, such that $$$i e j$$$ and $$$i - j le k$$$, and paint them black. A list $$$a$$$ is given. All cells from this list must be painted black. Additionally, at most one cell that is not in this list can also be painted black. Your task is to determine the minimum value of $$$k$$$ for which this is possible. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0— the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2000$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 < a_i < 10^{18}$$$; $$$a_i < a_{i + 1}$$$). Additional constraint on the input: the sum of $$$n$$$ across all test cases does not exceed $$$2000$$$. Output For each test case, print a single integerxa0— the minimum value of $$$k$$$ for which it is possible to paint all the given cells black. Example Input 4 2 1 2 1 7 3 2 4 9 5 1 5 8 10 13 Note In the first example, with $$$k=1$$$, it is possible to paint the cells $$$(1, 2)$$$. In the second example, with $$$k=1$$$, it is possible to paint the cells $$$(7, 8)$$$. In the third example, with $$$k=2$$$, it is possible to paint the cells $$$(2, 4)$$$ and $$$(8, 9)$$$. In the fourth example, with $$$k=3$$$, it is possible to paint the cells $$$(0, 1)$$$, $$$(5, 8)$$$ and $$$(10, 13)$$$.
1,300
false
true
false
false
false
true
true
true
false
false
115
171D
Problem - 171D - Codeforces =============== xa0
1,300
false
false
false
false
false
false
true
false
false
false
9,183
1081F
This is an interactive problem. Chouti was tired of studying, so he opened the computer and started playing a puzzle game. Long long ago, the boy found a sequence $$$s_1, s_2, ldots, s_n$$$ of length $$$n$$$, kept by a tricky interactor. It consisted of $$$0$$$s and $$$1$$$s only and the number of $$$1$$$s is $$$t$$$. The boy knows nothing about this sequence except $$$n$$$ and $$$t$$$, but he can try to find it out with some queries with the interactor. We define an operation called flipping. Flipping $$$ exploit the exact generator to pass this problem.
2,600
false
false
true
false
false
true
false
false
false
false
5,318
1701F
Problem - 1701F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics data structures implementation math matrices *2500 No tag edit access → Contest materials — the number of queries and the parameter for defining if a triple is beautiful, respectively. The second line contains $$$q$$$ integers $$$a_1, a_2, dots, a_q$$$ ($$$1 le a_i le 2 cdot 10^5$$$) denoting the queries. The integer $$$a_i$$$ denotes the $$$i$$$-th query in the following way: if the point $$$a_i$$$ belongs to the set, remove it; otherwise, add it; after adding or removing the point, print the number of beautiful triples. Output For each query, print one integer — the number of beautiful triples after processing the respective query. Example Input 7 5 8 5 3 2 1 5 6 Output 0 0 1 2 5 1 5
2,500
true
false
true
false
true
false
false
false
false
false
2,071
1722E
You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$. You are asked $$$q$$$ queries of the form $$$h_s w_s h_b w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$sum h_i cdot w_i$$$ for $$$i$$$ such that $$$h_s < h_i < h_b$$$ and $$$w_s < w_i < w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles. Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++). Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The first line of each test case two integers $$$n, q$$$ ($$$1 leq n leq 10^5$$$; $$$1 leq q leq 10^5$$$)xa0— the number of rectangles you own and the number of queries. Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 leq h_i, w_i leq 1000$$$)xa0— the height and width of the $$$i$$$-th rectangle. Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 leq h_s < h_b, w_s < w_b leq 1000$$$)xa0— the description of each query. The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query. Example Input 3 2 1 2 3 3 2 1 1 3 4 5 5 1 1 2 2 3 3 4 4 5 5 3 3 6 6 2 1 4 5 1 1 2 10 1 1 100 100 1 1 3 3 3 1 999 999 999 999 999 998 1 1 1000 1000 Output 6 41 9 0 54 4 2993004 Note In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 imes 1$$$ rectangle inside of it and fit into a $$$3 imes 4$$$ rectangle. Only the $$$2 imes 3$$$ rectangle works, because $$$1 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 imes 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 4$$$ (comparing widths), so it fits inside the $$$3 imes 4$$$ rectangle. The $$$3 imes 2$$$ rectangle is too tall to fit in a $$$3 imes 4$$$ rectangle. The total area is $$$2 cdot 3 = 6$$$.
1,600
false
false
true
true
true
false
true
false
false
false
1,949
1682F
You are given two integer arrays $$$a$$$ and $$$b$$$ ($$$b_i eq 0$$$ and $$$b_i leq 10^9$$$). Array $$$a$$$ is sorted in non-decreasing order. The cost of a subarray $$$a$$$ xa0— length of arrays $$$a$$$, $$$b$$$ and the number of queries. The next line contains $$$n$$$ integers $$$a_1,a_2 ldots a_n$$$ ($$$0 leq a_1 le a_2 ldots le a_n leq 10^9)$$$ xa0— the array $$$a$$$. It is guaranteed that $$$a$$$ is sorted in non-decreasing order. The next line contains $$$n$$$ integers $$$b_1,b_2 ldots b_n$$$ $$$(-10^9leq b_i leq 10^9, b_i eq 0)$$$ xa0— the array $$$b$$$. The $$$i$$$-th of the next $$$q$$$ lines contains two integers $$$l_i,r_i$$$ $$$(1leq l_i leq r_i leq n)$$$. It is guaranteed that $$$ sumlimits_{j = l_i}^{r_i} b_j = 0$$$. Output For each query $$$l_i$$$, $$$r_i$$$ xa0— print the cost of subarray $$$a[l_i:r_i]$$$ modulo $$$10^9 + 7$$$. Example Input 8 4 1 2 4 5 9 10 10 13 6 -1 1 -3 2 1 -1 1 2 3 6 7 3 5 2 6 Note In the first query, the maximum possible flow is $$$1$$$ i.e one unit from source to $$$2$$$, then one unit from $$$2$$$ to $$$3$$$, then one unit from $$$3$$$ to sink. The cost of the flow is $$$0 cdot 1 + 2 - 4 cdot 1 + 0 cdot 1 = 2$$$. In the second query, the maximum possible flow is again $$$1$$$ i.e from source to $$$7$$$, $$$7$$$ to $$$6$$$, and $$$6$$$ to sink with a cost of $$$0 cdot 10 - 10 cdot 1 + 0 cdot 1 = 0$$$. In the third query, the flow network is shown on the left with capacity written over the edge and the cost written in bracket. The image on the right shows the flow through each edge in an optimal configuration. Maximum flow is $$$3$$$ with a cost of $$$0 cdot 3 + 1 cdot 1 + 4 cdot 2 + 0 cdot 1 + 0 cdot 2 = 9$$$. In the fourth query, the flow network looks as – The minimum cost maximum flow is achieved in the configuration – The maximum flow in the above network is 4 and the minimum cost of such flow is 15.
2,700
false
true
false
false
true
false
false
false
true
true
2,174
1132E
Problem - 1132E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags dfs and similar dp greedy *2300 No tag edit access → Contest materials — the maximum total weight of a good subset. The second line denotes the set of items you have. It contains $$$8$$$ integers $$$cnt_1$$$, $$$cnt_2$$$, ..., $$$cnt_8$$$ ($$$0 le cnt_i le 10^{16}$$$), where $$$cnt_i$$$ is the number of items having weight $$$i$$$ in the set. Output Print one integer — the maximum possible weight of a good subset of items. Examples Input 10 1 2 3 4 5 6 7 8 Output 10 Input 0 0 0 0 0 0 0 0 0 Output 0 Input 3 0 4 1 0 0 9 8 3 Output 3
2,300
false
true
false
true
false
false
false
false
false
false
5,057
1325F
It's the year 5555. You have a graph, and you want to find a long cycle and a huge independent set, just because you can. But for now, let's just stick with finding either. Given a connected graph with $$$n$$$ vertices, you can choose to either: find an independent set that has exactly $$$lceilsqrt{n} ceil$$$ vertices. find a simple cycle of length at least $$$lceilsqrt{n} ceil$$$. An independent set is a set of vertices such that no two of them are connected by an edge. A simple cycle is a cycle that doesn't contain any vertex twice. I have a proof you can always solve one of these problems, but it's too long to fit this margin. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$5 le n le 10^5$$$, $$$n-1 le m le 2 cdot 10^5$$$)xa0— the number of vertices and edges in the graph. Each of the next $$$m$$$ lines contains two space-separated integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$) that mean there's an edge between vertices $$$u$$$ and $$$v$$$. It's guaranteed that the graph is connected and doesn't contain any self-loops or multiple edges. Output If you choose to solve the first problem, then on the first line print "1", followed by a line containing $$$lceilsqrt{n} ceil$$$ distinct integers not exceeding $$$n$$$, the vertices in the desired independent set. If you, however, choose to solve the second problem, then on the first line print "2", followed by a line containing one integer, $$$c$$$, representing the length of the found cycle, followed by a line containing $$$c$$$ distinct integers integers not exceeding $$$n$$$, the vertices in the desired cycle, in the order they appear in the cycle. Examples Input 6 6 1 3 3 4 4 2 2 6 5 6 5 1 Input 6 8 1 3 3 4 4 2 2 6 5 6 5 1 1 4 2 5 Note In the first sample: Notice that you can solve either problem, so printing the cycle $$$2-4-3-1-5-6$$$ is also acceptable. In the second sample: Notice that if there are multiple answers you can print any, so printing the cycle $$$2-5-6$$$, for example, is acceptable. In the third sample:
2,500
false
true
false
false
false
true
false
false
false
true
4,088
260B
A recently found Ancient Prophesy is believed to contain the exact Apocalypse date. The prophesy is a string that only consists of digits and characters "-". We'll say that some date is mentioned in the Prophesy if there is a substring in the Prophesy that is the date's record in the format "dd-mm-yyyy". We'll say that the number of the date's occurrences is the number of such substrings in the Prophesy. For example, the Prophesy "0012-10-2012-10-2012" mentions date 12-10-2012 twice (first time as "0012-10-2012-10-2012", second time as "0012-10-2012-10-2012"). The date of the Apocalypse is such correct date that the number of times it is mentioned in the Prophesy is strictly larger than that of any other correct date. A date is correct if the year lies in the range from 2013 to 2015, the month is from 1 to 12, and the number of the day is strictly more than a zero and doesn't exceed the number of days in the current month. Note that a date is written in the format "dd-mm-yyyy", that means that leading zeroes may be added to the numbers of the months or days if needed. In other words, date "1-1-2013" isn't recorded in the format "dd-mm-yyyy", and date "01-01-2013" is recorded in it. Notice, that any year between 2013 and 2015 is not a leap year. Input The first line contains the Prophesy: a non-empty string that only consists of digits and characters "-". The length of the Prophesy doesn't exceed 105 characters. Output In a single line print the date of the Apocalypse. It is guaranteed that such date exists and is unique. Examples Input 777-444---21-12-2013-12-2013-12-2013---444-777
1,600
false
false
true
false
false
false
true
false
false
false
8,794
1369E
# DeadLee Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Lee bought some food for dinner time, but Lee’s friends eat dinner in a deadly way. Lee is so scared, he doesn’t want to die, at least not before seeing Online IOI 2020. . . There are n different types of food and m Lee’s best friends. Lee has wi plates of the i-th type of food and each friend has two different favorite types of food: the i-th friend’s favorite types of food are xi and yi (xi 6 = yi). Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types . Each of the friends will go to the kitchen exactly once. The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither xi nor yi), he will eat Lee instead ×_×.Lee can choose the order of friends to call, so he’d like to determine if he can survive dinner or not. Also, he’d like to know the order itself. # Input The first line contains two integers n and m (2 ≤ n ≤ 10 5; 1 ≤ m ≤ 2 · 10 5) x16 the number of different food types and the number of Lee’s friends. The second line contains n integers w1, w 2, . . . , w n (0 ≤ wi ≤ 10 6) x16 the number of plates of each food type. The i-th line of the next m lines contains two integers xi and yi (1 ≤ xi, y i ≤ n; xi 6 = yi) x16 the favorite types of food of the i-th friend. # Output If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive). Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them. Page 1 of 2 Examples standard input standard output 3 3 1 2 1 1 2 2 3 1 3 ALIVE 3 2 1 3 2 1 1 0 1 2 1 3 ALIVE 2 1 4 4 1 2 0 1 1 3 1 2 2 3 2 4 ALIVE 1 3 2 4 5 5 1 1 1 2 1 3 4 1 2 2 3 4 5 4 5 ALIVE 5 4 1 3 2 4 10 2 4 1 4 3 2 4 2 4 1 3 1 4 1 1 3 3 2 2 1 3 1 2 4 DEAD # Note In the first example, any of the following orders of friends are correct : [1 , 3, 2] , [3 , 1, 2] , [2 , 3, 1] , [3 , 2, 1] .In the second example, Lee should call the second friend first (the friend will eat a plate of food 1) and then call the first friend (the friend will eat a plate of food 2). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food 1 and food 2 and there will be no food left for the second friend to eat. Page 2 of 2
2,400
false
true
true
false
true
false
false
false
true
false
3,823
1467D
There are $$$n$$$ cells, numbered $$$1,2,dots, n$$$ from left to right. You have to place a robot at any cell initially. The robot must make exactly $$$k$$$ moves. In one move, the robot must move one cell to the left or right, provided that it doesn't move out of bounds. In other words, if the robot was in the cell $$$i$$$, it must move to either the cell $$$i-1$$$ or the cell $$$i+1$$$, as long as it lies between $$$1$$$ and $$$n$$$ (endpoints inclusive). The cells, in the order they are visited (including the cell the robot is placed), together make a good path. Each cell $$$i$$$ has a value $$$a_i$$$ associated with it. Let $$$c_0, c_1, dots, c_k$$$ be the sequence of cells in a good path in the order they are visited ($$$c_0$$$ is the cell robot is initially placed, $$$c_1$$$ is the cell where the robot is after its first move, and so on; more formally, $$$c_i$$$ is the cell that the robot is at after $$$i$$$ moves). Then the value of the path is calculated as $$$a_{c_0} + a_{c_1} + dots + a_{c_k}$$$. Your task is to calculate the sum of values over all possible good paths. Since this number can be very large, output it modulo $$$10^9 + 7$$$. Two good paths are considered different if the starting cell differs or there exists an integer $$$i in [1, k]$$$ such that the current cell of the robot after exactly $$$i$$$ moves is different in those paths. You must process $$$q$$$ updates to $$$a$$$ and print the updated sum each time. Each update changes the value of exactly one cell. See the input format and the sample input-output for more details. Input The first line of the input contains three space-separated integers $$$n$$$, $$$k$$$ and $$$q$$$ ($$$2 le n le 5000$$$; $$$1 le k le 5000$$$; $$$1 le q le 2 cdot 10^5$$$). The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$). $$$q$$$ lines follow. Each line contains two space-separated integers $$$i$$$ and $$$x$$$ ($$$1 le i le n$$$; $$$1 le x le 10^9$$$) indicating that you must change the value of $$$a_i$$$ to $$$x$$$. Output Print $$$q$$$ integers. The $$$i$$$-th integer should be the sum of values over all good paths after the first $$$i$$$ updates are performed. Since the answers may be large, print them modulo $$$10^9 + 7$$$. Examples Input 5 1 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Input 5 2 5 3 5 1 4 2 1 9 2 4 3 6 4 6 5 2 Output 157 147 207 227 227 Input 4 40 6 92 21 82 46 3 56 1 72 4 28 1 97 2 49 2 88 Output 239185261 666314041 50729936 516818968 766409450 756910476 Note In the first example, the good paths are $$$(1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 3), (4, 5), (5, 4)$$$. Initially the values of $$$a$$$ are $$$[3, 5, 1, 4, 2]$$$. After the first update, they become $$$[9, 5, 1, 4, 2]$$$. After the second update, they become $$$[9, 4, 1, 4, 2]$$$, and so on.
2,200
true
false
false
true
false
false
false
false
false
false
3,354
543E
Please note that the memory limit differs from the standard. You really love to listen to music. During the each of next _s_ days you will listen to exactly _m_ songs from the playlist that consists of exactly _n_ songs. Let's number the songs from the playlist with numbers from 1 to _n_, inclusive. The quality of song number _i_ is _a__i_. On the _i_-th day you choose some integer _v_ (_l__i_u2009≤u2009_v_u2009≤u2009_r__i_) and listen to songs number _v_,u2009_v_u2009+u20091,u2009...,u2009_v_u2009+u2009_m_u2009-u20091. On the _i_-th day listening to one song with quality less than _q__i_ increases your displeasure by exactly one. Determine what minimum displeasure you can get on each of the _s_ next days. Input The first line contains two positive integers _n_, _m_ (1u2009≤u2009_m_u2009≤u2009_n_u2009≤u20092·105). The second line contains _n_ positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009<u2009230) — the description of songs from the playlist. The next line contains a single number _s_ (1u2009≤u2009_s_u2009≤u20092·105) — the number of days that you consider. The next _s_ lines contain three integers each _l__i_,u2009_r__i_,u2009_x__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_u2009-u2009_m_u2009+u20091; 0u2009≤u2009_x__i_u2009<u2009230) — the description of the parameters for the _i_-th day. In order to calculate value _q__i_, you need to use formula: , where _ans__i_ is the answer to the problem for day _i_. Assume that _ans_0u2009=u20090. Output Print exactly _s_ integers _ans_1,u2009_ans_2,u2009...,u2009_ans__s_, where _ans__i_ is the minimum displeasure that you can get on day _i_. Examples Input 5 3 1 2 1 2 3 5 1 1 2 1 3 2 1 3 3 1 3 5 1 3 1
3,200
false
false
false
false
true
true
false
false
false
false
7,665
README.md exists but content is empty.
Downloads last month
85