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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
939C | In distant future on Earth day lasts for _n_ hours and that's why there are _n_ timezones. Local times in adjacent timezones differ by one hour. For describing local time, hours numbers from 1 to _n_ are used, i.e. there is no time "0 hours", instead of it "_n_ hours" is used. When local time in the 1-st timezone is 1 hour, local time in the _i_-th timezone is _i_ hours. Some online programming contests platform wants to conduct a contest that lasts for an hour in such a way that its beginning coincides with beginning of some hour (in all time zones). The platform knows, that there are _a__i_ people from _i_-th timezone who want to participate in the contest. Each person will participate if and only if the contest starts no earlier than _s_ hours 00 minutes local time and ends not later than _f_ hours 00 minutes local time. Values _s_ and _f_ are equal for all time zones. If the contest starts at _f_ hours 00 minutes local time, the person won't participate in it. Help platform select such an hour, that the number of people who will participate in the contest is maximum. Input The first line contains a single integer _n_ (2u2009β€u2009_n_u2009β€u2009100u2009000)xa0β the number of hours in day. The second line contains _n_ space-separated integers _a_1, _a_2, ..., _a__n_ (1u2009β€u2009_a__i_u2009β€u200910u2009000), where _a__i_ is the number of people in the _i_-th timezone who want to participate in the contest. The third line contains two space-separated integers _s_ and _f_ (1u2009β€u2009_s_u2009<u2009_f_u2009β€u2009_n_). Output Output a single integerxa0β the time of the beginning of the contest (in the first timezone local time), such that the number of participants will be maximum possible. If there are many answers, output the smallest among them. Note In the first example, it's optimal to start competition at 3 hours (in first timezone). In this case, it will be 1 hour in the second timezone and 2 hours in the third timezone. Only one person from the first timezone won't participate. In second example only people from the third and the fourth timezones will participate. | 1,600 | false | false | false | false | false | false | false | true | false | false | 5,942 |
1954C | You are given two integers $$$x$$$ and $$$y$$$ of the same length, consisting of digits from $$$1$$$ to $$$9$$$. You can perform the following operation any number of times (possibly zero): swap the $$$i$$$-th digit in $$$x$$$ and the $$$i$$$-th digit in $$$y$$$. For example, if $$$x=73$$$ and $$$y=31$$$, you can swap the $$$2$$$-nd digits and get $$$x=71$$$ and $$$y=33$$$. Your task is to maximize the product of $$$x$$$ and $$$y$$$ using the aforementioned operation any number of times. If there are multiple answers, print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$x$$$ ($$$1 le x < 10^{100}$$$). The second line of each test case contains a single integer $$$y$$$ ($$$1 le y < 10^{100}$$$). Additional constraint on input: the integers $$$x$$$ and $$$y$$$ consist only of digits from $$$1$$$ to $$$9$$$. Output For each test case, print two linesxa0β the first line should contain the number $$$x$$$ after performing the operations; similarly, the second line should contain the number $$$y$$$ after performing the operations. If there are multiple answers, print any of them. Example Output 71 33 5 2 3912 3586 | 1,200 | true | true | false | false | false | false | false | false | false | false | 552 |
246E | Polycarpus got hold of a family tree. The found tree describes the family relations of _n_ people, numbered from 1 to _n_. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique. We call the man with a number _a_ a 1-ancestor of the man with a number _b_, if the man with a number _a_ is a direct ancestor of the man with a number _b_. We call the man with a number _a_ a _k_-ancestor (_k_u2009>u20091) of the man with a number _b_, if the man with a number _b_ has a 1-ancestor, and the man with a number _a_ is a (_k_u2009-u20091)-ancestor of the 1-ancestor of the man with a number _b_. In the tree the family ties do not form cycles. In other words there isn't a person who is his own direct or indirect ancestor (that is, who is an _x_-ancestor of himself, for some _x_, _x_u2009>u20090). We call a man with a number _a_ the _k_-son of the man with a number _b_, if the man with a number _b_ is a _k_-ancestor of the man with a number _a_. Polycarpus is very much interested in how many sons and which sons each person has. He took a piece of paper and wrote _m_ pairs of numbers _v__i_, _k__i_. Help him to learn for each pair _v__i_, _k__i_ the number of distinct names among all names of the _k__i_-sons of the man with number _v__i_. Input The first line of the input contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of people in the tree. Next _n_ lines contain the description of people in the tree. The _i_-th line contains space-separated string _s__i_ and integer _r__i_ (0u2009β€u2009_r__i_u2009β€u2009_n_), where _s__i_ is the name of the man with a number _i_, and _r__i_ is either the number of the direct ancestor of the man with a number _i_ or 0, if the man with a number _i_ has no direct ancestor. The next line contains a single integer _m_ (1u2009β€u2009_m_u2009β€u2009105) β the number of Polycarpus's records. Next _m_ lines contain space-separated pairs of integers. The _i_-th line contains integers _v__i_, _k__i_ (1u2009β€u2009_v__i_,u2009_k__i_u2009β€u2009_n_). It is guaranteed that the family relationships do not form cycles. The names of all people are non-empty strings, consisting of no more than 20 lowercase English letters. | 2,400 | false | false | false | true | true | false | false | true | true | false | 8,844 |
687C | Pari wants to buy an expensive chocolate from Arya. She has _n_ coins, the value of the _i_-th coin is _c__i_. The price of the chocolate is _k_, so Pari will take a subset of her coins with sum equal to _k_ and give it to Arya. Looking at her coins, a question came to her mind: after giving the coins to Arya, what values does Arya can make with them? She is jealous and she doesn't want Arya to make a lot of values. So she wants to know all the values _x_, such that Arya will be able to make _x_ using some subset of coins with the sum _k_. Formally, Pari wants to know the values _x_ such that there exists a subset of coins with the sum _k_ such that some subset of this subset has the sum _x_, i.e. there is exists some way to pay for the chocolate, such that Arya will be able to make the sum _x_ using these coins. Input The first line contains two integers _n_ and _k_ (1u2009u2009β€u2009u2009_n_,u2009_k_u2009u2009β€u2009u2009500)xa0β the number of coins and the price of the chocolate, respectively. Next line will contain _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009β€u2009_c__i_u2009β€u2009500)xa0β the values of Pari's coins. It's guaranteed that one can make value _k_ using these coins. Output First line of the output must contain a single integer _q_β the number of suitable values _x_. Then print _q_ integers in ascending orderxa0β the values that Arya can make for some subset of coins of Pari that pays for the chocolate. Examples Output 16 0 1 2 3 5 6 7 8 10 11 12 13 15 16 17 18 | 1,900 | false | false | false | true | false | false | false | false | false | false | 7,074 |
1276B | There are $$$n$$$ cities in Berland and some pairs of them are connected by two-way roads. It is guaranteed that you can pass from any city to any other, moving along the roads. Cities are numerated from $$$1$$$ to $$$n$$$. Two fairs are currently taking place in Berland β they are held in two different cities $$$a$$$ and $$$b$$$ ($$$1 le a, b le n$$$; $$$a e b$$$). Find the number of pairs of cities $$$x$$$ and $$$y$$$ ($$$x e a, x e b, y e a, y e b$$$) such that if you go from $$$x$$$ to $$$y$$$ you will have to go through both fairs (the order of visits doesn't matter). Formally, you need to find the number of pairs of cities $$$x,y$$$ such that any path from $$$x$$$ to $$$y$$$ goes through $$$a$$$ and $$$b$$$ (in any order). Print the required number of pairs. The order of two cities in a pair does not matter, that is, the pairs $$$(x,y)$$$ and $$$(y,x)$$$ must be taken into account only once. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 4cdot10^4$$$) β the number of test cases in the input. Next, $$$t$$$ test cases are specified. The first line of each test case contains four integers $$$n$$$, $$$m$$$, $$$a$$$ and $$$b$$$ ($$$4 le n le 2cdot10^5$$$, $$$n - 1 le m le 5cdot10^5$$$, $$$1 le a,b le n$$$, $$$a e b$$$) β numbers of cities and roads in Berland and numbers of two cities where fairs are held, respectively. The following $$$m$$$ lines contain descriptions of roads between cities. Each of road description contains a pair of integers $$$u_i, v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i e v_i$$$) β numbers of cities connected by the road. Each road is bi-directional and connects two different cities. It is guaranteed that from any city you can pass to any other by roads. There can be more than one road between a pair of cities. The sum of the values of $$$n$$$ for all sets of input data in the test does not exceed $$$2cdot10^5$$$. The sum of the values of $$$m$$$ for all sets of input data in the test does not exceed $$$5cdot10^5$$$. Output Print $$$t$$$ integers β the answers to the given test cases in the order they are written in the input. Example Input 3 7 7 3 5 1 2 2 3 3 4 4 5 5 6 6 7 7 5 4 5 2 3 1 2 2 3 3 4 4 1 4 2 4 3 2 1 1 2 2 3 4 1 | 1,900 | false | false | false | false | false | false | false | false | false | true | 4,314 |
2003E1 | This is an easy version of this problem. The differences between the versions are the constraint on $$$m$$$ and $$$r_i < l_{i + 1}$$$ holds for each $$$i$$$ from $$$1$$$ to $$$m - 1$$$ in the easy version. You can make hacks only if both versions of the problem are solved. Turtle gives you $$$m$$$ intervals $$$[l_1, r_1], [l_2, r_2], ldots, [l_m, r_m]$$$. He thinks that a permutation $$$p$$$ is interesting if there exists an integer $$$k_i$$$ for every interval ($$$l_i le k_i < r_i$$$), and if he lets $$$a_i = maxlimits_{j = l_i}^{k_i} p_j, b_i = minlimits_{j = k_i + 1}^{r_i} p_j$$$ for every integer $$$i$$$ from $$$1$$$ to $$$m$$$, the following condition holds: $$$$$$maxlimits_{i = 1}^m a_i < minlimits_{i = 1}^m b_i$$$$$$ Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $$$n$$$, or tell him if there is no interesting permutation. An inversion of a permutation $$$p$$$ is a pair of integers $$$(i, j)$$$ ($$$1 le i < j le n$$$) such that $$$p_i > p_j$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n, m$$$ ($$$2 le n le 5 cdot 10^3, 0 le m le frac{n}{2}$$$) β the length of the permutation and the number of intervals. The $$$i$$$-th of the following $$$m$$$ lines contains two integers $$$l_i, r_i$$$ ($$$1 le l_i < r_i le n$$$) β the $$$i$$$-th interval. Additional constraint on the input in this version: $$$r_i < l_{i + 1}$$$ holds for each $$$i$$$ from $$$1$$$ to $$$m - 1$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^3$$$. Output For each test case, if there is no interesting permutation, output a single integer $$$-1$$$. Otherwise, output a single integer β the maximum number of inversions. Example Input 6 2 0 2 1 1 2 5 1 2 4 8 2 1 4 6 8 7 2 1 3 4 7 7 3 1 2 3 4 5 6 Note In the third test case, the interesting permutation with the maximum number of inversions is $$$[5, 2, 4, 3, 1]$$$. In the fourth test case, the interesting permutation with the maximum number of inversions is $$$[4, 8, 7, 6, 3, 2, 1, 5]$$$. In this case, we can let $$$[k_1, k_2] = [1, 7]$$$. In the fifth test case, the interesting permutation with the maximum number of inversions is $$$[4, 7, 6, 3, 2, 1, 5]$$$. In the sixth test case, the interesting permutation with the maximum number of inversions is $$$[4, 7, 3, 6, 2, 5, 1]$$$. | 2,600 | true | true | false | true | false | false | true | false | false | false | 234 |
180C | Problem - 180C - 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 *1400 No tag edit access β Contest materials . Patrick got interested in the following question: what minimum number of actions do we need to make a message fancy? Changing a letter's case in the message counts as one action. Patrick cannot perform any other actions. Input The only line of the input contains a non-empty string consisting of uppercase and lowercase letters. The string's length does not exceed 105. Output Print a single number β the least number of actions needed to make the message fancy. Examples Input PRuvetSTAaYA Output 5 Input OYPROSTIYAOPECHATALSYAPRIVETSTASYA Output 0 Input helloworld Output 0 | 1,400 | false | false | false | true | false | false | false | false | false | false | 9,120 |
18A | Problem - 18A - 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 geometry *1500 No tag edit access β Contest materials ") Tutorial") | 1,500 | false | false | false | false | false | false | true | false | false | false | 9,905 |
845F | Polycarp owns a shop in the capital of Berland. Recently the criminal activity in the capital increased, so Polycarp is thinking about establishing some better security in the storehouse of his shop. The storehouse can be represented as a matrix with _n_ rows and _m_ columns. Each element of the matrix is either . (an empty space) or x (a wall). Polycarp wants to hire some guards (possibly zero) to watch for the storehouse. Each guard will be in some cell of matrix and will protect every cell to the right of his own cell and every cell to the bottom of his own cell, until the nearest wall. More formally, if the guard is standing in the cell (_x_0,u2009_y_0), then he protects cell (_x_1,u2009_y_1) if all these conditions are met: (_x_1,u2009_y_1) is an empty cell; either _x_0u2009=u2009_x_1 and _y_0u2009β€u2009_y_1, or _x_0u2009β€u2009_x_1 and _y_0u2009=u2009_y_1; there are no walls between cells (_x_0,u2009_y_0) and (_x_1,u2009_y_1). There can be a guard between these cells, guards can look through each other. Guards can be placed only in empty cells (and can protect only empty cells). The plan of placing the guards is some set of cells where guards will be placed (of course, two plans are different if there exists at least one cell that is included in the first plan, but not included in the second plan, or vice versa). Polycarp calls a plan suitable if there is not more than one empty cell that is not protected. Polycarp wants to know the number of suitable plans. Since it can be very large, you have to output it modulo 109u2009+u20097. Input The first line contains two numbers _n_ and _m_ β the length and the width of the storehouse (1u2009β€u2009_n_,u2009_m_u2009β€u2009250, 1u2009β€u2009_nm_u2009β€u2009250). Then _n_ lines follow, _i_th line contains a string consisting of _m_ characters β _i_th row of the matrix representing the storehouse. Each character is either . or x. Note In the first example you have to put at least one guard, so there are three possible arrangements: one guard in the cell (1,u20091), one guard in the cell (1,u20093), and two guards in both these cells. | 2,500 | false | false | false | true | false | false | false | false | false | false | 6,379 |
365B | Problem - 365B - 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 *1100 No tag edit access β Contest materials is good if _a__i_u2009=u2009_a__i_u2009-u20091u2009+u2009_a__i_u2009-u20092, for all _i_ (_l_u2009+u20092u2009β€u2009_i_u2009β€u2009_r_). Let's define _len_( | 1,100 | false | false | true | false | false | false | false | false | false | false | 8,371 |
1509C | The student council is preparing for the relay race at the sports festival. The council consists of $$$n$$$ members. They will run one after the other in the race, the speed of member $$$i$$$ is $$$s_i$$$. The discrepancy $$$d_i$$$ of the $$$i$$$-th stage is the difference between the maximum and the minimum running speed among the first $$$i$$$ members who ran. Formally, if $$$a_i$$$ denotes the speed of the $$$i$$$-th member who participated in the race, then $$$d_i = max(a_1, a_2, dots, a_i) - min(a_1, a_2, dots, a_i)$$$. You want to minimize the sum of the discrepancies $$$d_1 + d_2 + dots + d_n$$$. To do this, you are allowed to change the order in which the members run. What is the minimum possible sum that can be achieved? Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2000$$$) xa0β the number of members of the student council. The second line contains $$$n$$$ integers $$$s_1, s_2, dots, s_n$$$ ($$$1 le s_i le 10^9$$$) xa0β the running speeds of the members. Output Print a single integer xa0β the minimum possible value of $$$d_1 + d_2 + dots + d_n$$$ after choosing the order of the members. Examples Input 6 104 943872923 6589 889921234 1000000000 69 Note In the first test case, we may choose to make the third member run first, followed by the first member, and finally the second. Thus $$$a_1 = 2$$$, $$$a_2 = 3$$$, and $$$a_3 = 1$$$. We have: $$$d_1 = max(2) - min(2) = 2 - 2 = 0$$$. $$$d_2 = max(2, 3) - min(2, 3) = 3 - 2 = 1$$$. $$$d_3 = max(2, 3, 1) - min(2, 3, 1) = 3 - 1 = 2$$$. The resulting sum is $$$d_1 + d_2 + d_3 = 0 + 1 + 2 = 3$$$. It can be shown that it is impossible to achieve a smaller value. In the second test case, the only possible rearrangement gives $$$d_1 = 0$$$, so the minimum possible result is $$$0$$$. | 1,800 | false | true | false | true | false | false | false | false | false | false | 3,128 |
1545B | Cirno gave AquaMoon a chessboard of size $$$1 imes n$$$. Its cells are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. In the beginning, some of the cells are occupied with at most one pawn, and other cells are unoccupied. In each operation, AquaMoon can choose a cell $$$i$$$ with a pawn, and do either of the following (if possible): Move pawn from it to the $$$(i+2)$$$-th cell, if $$$i+2 leq n$$$ and the $$$(i+1)$$$-th cell is occupied and the $$$(i+2)$$$-th cell is unoccupied. Move pawn from it to the $$$(i-2)$$$-th cell, if $$$i-2 geq 1$$$ and the $$$(i-1)$$$-th cell is occupied and the $$$(i-2)$$$-th cell is unoccupied. You are given an initial state of the chessboard. AquaMoon wants to count the number of states reachable from the initial state with some sequence of operations. But she is not good at programming. Can you help her? As the answer can be large find it modulo $$$998,244,353$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10,000$$$) β the number of test cases. The first line contains a single integer $$$n$$$ ($$$1 leq n leq 10^5$$$) β the size of the chessboard. The second line contains a string of $$$n$$$ characters, consists of characters "0" and "1". If the $$$i$$$-th character is "1", the $$$i$$$-th cell is initially occupied; otherwise, the $$$i$$$-th cell is initially unoccupied. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print the number of states that reachable from the initial state with some sequence of operations modulo $$$998,244,353$$$. Example Input 6 4 0110 6 011011 5 01010 20 10001111110110111000 20 00110110100110111101 20 11101111011000100010 Output 3 6 1 1287 1287 715 Note In the first test case the strings "1100", "0110" and "0011" are reachable from the initial state with some sequence of operations. | 1,900 | true | false | false | false | false | false | false | false | false | false | 2,921 |
259B | Little Elephant loves magic squares very much. A magic square is a 3u2009Γu20093 table, each cell contains some positive integer. At that the sums of integers in all rows, columns and diagonals of the table are equal. The figure below shows the magic square, the sum of integers in all its rows, columns and diagonals equals 15. The Little Elephant remembered one magic square. He started writing this square on a piece of paper, but as he wrote, he forgot all three elements of the main diagonal of the magic square. Fortunately, the Little Elephant clearly remembered that all elements of the magic square did not exceed 105. Help the Little Elephant, restore the original magic square, given the Elephant's notes. Input The first three lines of the input contain the Little Elephant's notes. The first line contains elements of the first row of the magic square. The second line contains the elements of the second row, the third line is for the third row. The main diagonal elements that have been forgotten by the Elephant are represented by zeroes. It is guaranteed that the notes contain exactly three zeroes and they are all located on the main diagonal. It is guaranteed that all positive numbers in the table do not exceed 105. Output Print three lines, in each line print three integers β the Little Elephant's magic square. If there are multiple magic squares, you are allowed to print any of them. Note that all numbers you print must be positive and not exceed 105. It is guaranteed that there exists at least one magic square that meets the conditions. | 1,100 | false | false | true | false | false | false | true | false | false | false | 8,796 |
1056H | You successfully found poor Arkady near the exit of the station you've perfectly predicted. You sent him home on a taxi and suddenly came up with a question. There are $$$n$$$ crossroads in your city and several bidirectional roads connecting some of them. A taxi ride is a path from some crossroads to another one without passing the same crossroads twice. You have a collection of rides made by one driver and now you wonder if this driver can be a robot or they are definitely a human. You think that the driver can be a robot if for every two crossroads $$$a$$$ and $$$b$$$ the driver always chooses the same path whenever he drives from $$$a$$$ to $$$b$$$. Note that $$$a$$$ and $$$b$$$ here do not have to be the endpoints of a ride and that the path from $$$b$$$ to $$$a$$$ can be different. On the contrary, if the driver ever has driven two different paths from $$$a$$$ to $$$b$$$, they are definitely a human. Given the system of roads and the description of all rides available to you, determine if the driver can be a robot or not. Input Each test contains one or more test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 3 cdot 10^5$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0β the number of crossroads in the city. The next line contains a single integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$)xa0β the number of rides available to you. Each of the following $$$q$$$ lines starts with a single integer $$$k$$$ ($$$2 le k le n$$$)xa0β the number of crossroads visited by the driver on this ride. It is followed by $$$k$$$ integers $$$c_1$$$, $$$c_2$$$, ..., $$$c_k$$$ ($$$1 le c_i le n$$$)xa0β the crossroads in the order the driver visited them. It is guaranteed that all crossroads in one ride are distinct. It is guaranteed that the sum of values $$$k$$$ among all rides of all test cases does not exceed $$$3 cdot 10^5$$$. It is guaranteed that the sum of values $$$n$$$ and the sum of values $$$q$$$ doesn't exceed $$$3 cdot 10^5$$$ among all test cases. Output Output a single line for each test case. If the driver can be a robot, output "Robot" in a single line. Otherwise, output "Human". You can print each letter in any case (upper or lower). Examples Input 1 5 2 4 1 2 3 5 3 1 4 3 Input 1 4 4 3 1 2 3 3 2 3 4 3 3 4 1 3 4 1 2 Note In the first example it is clear that the driver used two different ways to get from crossroads $$$1$$$ to crossroads $$$3$$$. It must be a human. In the second example the driver always drives the cycle $$$1 o 2 o 3 o 4 o 1$$$ until he reaches destination. | 3,200 | false | false | false | false | true | false | false | false | false | false | 5,425 |
172A | Polycarpus has _n_ friends in Tarasov city. Polycarpus knows phone numbers of all his friends: they are strings _s_1,u2009_s_2,u2009...,u2009_s__n_. All these strings consist only of digits and have the same length. Once Polycarpus needed to figure out Tarasov city phone code. He assumed that the phone code of the city is the longest common prefix of all phone numbers of his friends. In other words, it is the longest string _c_ which is a prefix (the beginning) of each _s__i_ for all _i_ (1u2009β€u2009_i_u2009β€u2009_n_). Help Polycarpus determine the length of the city phone code. Input The first line of the input contains an integer _n_ (2u2009β€u2009_n_u2009β€u20093Β·104) β the number of Polycarpus's friends. The following _n_ lines contain strings _s_1,u2009_s_2,u2009...,u2009_s__n_ β the phone numbers of Polycarpus's friends. It is guaranteed that all strings consist only of digits and have the same length from 1 to 20, inclusive. It is also guaranteed that all strings are different. Output Print the number of digits in the city phone code. Examples Input 4 00209 00219 00999 00909 Input 3 77012345678999999999 77012345678901234567 77012345678998765432 Note A prefix of string _t_ is a string that is obtained by deleting zero or more digits from the end of string _t_. For example, string "00209" has 6 prefixes: "" (an empty prefix), "0", "00", "002", "0020", "00209". In the first sample the city phone code is string "00". In the second sample the city phone code is an empty string. In the third sample the city phone code is string "770123456789". | 800 | false | false | true | false | false | false | true | false | false | false | 9,178 |
870B | You are given an array _a_1,u2009_a_2,u2009...,u2009_a__n_ consisting of _n_ integers, and an integer _k_. You have to split the array into exactly _k_ non-empty subsegments. You'll then compute the minimum integer on each subsegment, and take the maximum integer over the _k_ obtained minimums. What is the maximum possible integer you can get? Definitions of subsegment and array splitting are given in notes. Input The first line contains two integers _n_ and _k_ (1u2009β€u2009_k_u2009β€u2009_n_u2009β€u2009u2009105) β the size of the array _a_ and the number of subsegments you have to split the array to. The second line contains _n_ integers _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_ (u2009-u2009109u2009u2009β€u2009u2009_a__i_u2009β€u2009u2009109). Note A subsegment [_l_,u2009u2009_r_] (_l_u2009β€u2009_r_) of array _a_ is the sequence _a__l_,u2009u2009_a__l_u2009+u20091,u2009u2009...,u2009u2009_a__r_. Splitting of array _a_ of _n_ elements into _k_ subsegments [_l_1,u2009_r_1], [_l_2,u2009_r_2], ..., [_l__k_,u2009_r__k_] (_l_1u2009=u20091, _r__k_u2009=u2009_n_, _l__i_u2009=u2009_r__i_u2009-u20091u2009+u20091 for all _i_u2009>u20091) is _k_ sequences (_a__l_1,u2009...,u2009_a__r_1),u2009...,u2009(_a__l__k_,u2009...,u2009_a__r__k_). In the first example you should split the array into subsegments [1,u20094] and [5,u20095] that results in sequences (1,u20092,u20093,u20094) and (5). The minimums are _min_(1,u20092,u20093,u20094)u2009=u20091 and _min_(5)u2009=u20095. The resulting maximum is _max_(1,u20095)u2009=u20095. It is obvious that you can't reach greater result. In the second example the only option you have is to split the array into one subsegment [1,u20095], that results in one sequence (u2009-u20094,u2009u2009-u20095,u2009u2009-u20093,u2009u2009-u20092,u2009u2009-u20091). The only minimum is _min_(u2009-u20094,u2009u2009-u20095,u2009u2009-u20093,u2009u2009-u20092,u2009u2009-u20091)u2009=u2009u2009-u20095. The resulting maximum is u2009-u20095. | 1,200 | false | true | false | false | false | false | false | false | false | false | 6,260 |
242C | The black king is standing on a chess field consisting of 109 rows and 109 columns. We will consider the rows of the field numbered with integers from 1 to 109 from top to bottom. The columns are similarly numbered with integers from 1 to 109 from left to right. We will denote a cell of the field that is located in the _i_-th row and _j_-th column as (_i_,u2009_j_). You know that some squares of the given chess field are allowed. All allowed cells of the chess field are given as _n_ segments. Each segment is described by three integers _r__i_,u2009_a__i_,u2009_b__i_ (_a__i_u2009β€u2009_b__i_), denoting that cells in columns from number _a__i_ to number _b__i_ inclusive in the _r__i_-th row are allowed. Your task is to find the minimum number of moves the king needs to get from square (_x_0,u2009_y_0) to square (_x_1,u2009_y_1), provided that he only moves along the allowed cells. In other words, the king can be located only on allowed cells on his way. Let us remind you that a chess king can move to any of the neighboring cells in one move. Two cells of a chess field are considered neighboring if they share at least one point. Input The first line contains four space-separated integers _x_0,u2009_y_0,u2009_x_1,u2009_y_1 (1u2009β€u2009_x_0,u2009_y_0,u2009_x_1,u2009_y_1u2009β€u2009109), denoting the initial and the final positions of the king. The second line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105), denoting the number of segments of allowed cells. Next _n_ lines contain the descriptions of these segments. The _i_-th line contains three space-separated integers _r__i_,u2009_a__i_,u2009_b__i_ (1u2009β€u2009_r__i_,u2009_a__i_,u2009_b__i_u2009β€u2009109,u2009_a__i_u2009β€u2009_b__i_), denoting that cells in columns from number _a__i_ to number _b__i_ inclusive in the _r__i_-th row are allowed. Note that the segments of the allowed cells can intersect and embed arbitrarily. It is guaranteed that the king's initial and final position are allowed cells. It is guaranteed that the king's initial and the final positions do not coincide. It is guaranteed that the total length of all given segments doesn't exceed 105. Output If there is no path between the initial and final position along allowed cells, print -1. Otherwise print a single integer β the minimum number of moves the king needs to get from the initial position to the final one. Examples Input 5 7 6 11 3 5 3 8 6 7 11 5 2 5 Input 3 4 3 10 3 3 1 4 4 5 9 3 10 10 Input 1 1 2 10 2 1 1 3 2 6 10 | 1,800 | false | false | false | false | false | false | false | false | false | true | 8,866 |
1618D | You are given an array $$$a$$$ of $$$n$$$ integers, and another integer $$$k$$$ such that $$$2k le n$$$. You have to perform exactly $$$k$$$ operations with this array. In one operation, you have to choose two elements of the array (let them be $$$a_i$$$ and $$$a_j$$$; they can be equal or different, but their positions in the array must not be the same), remove them from the array, and add $$$lfloor frac{a_i}{a_j} floor$$$ to your score, where $$$lfloor frac{x}{y} floor$$$ is the maximum integer not exceeding $$$frac{x}{y}$$$. Initially, your score is $$$0$$$. After you perform exactly $$$k$$$ operations, you add all the remaining elements of the array to the score. Calculate the minimum possible score you can get. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 500$$$) β the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100$$$; $$$0 le k le lfloor frac{n}{2} floor$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2 cdot 10^5$$$). Output Print one integer β the minimum possible score you can get. Example Input 5 7 3 1 1 1 2 1 3 1 5 1 5 5 5 5 5 4 2 1 3 3 7 2 0 4 2 9 2 1 10 10 1 10 2 7 10 3 Note Let's consider the example test. In the first test case, one way to obtain a score of $$$2$$$ is the following one: 1. choose $$$a_7 = 1$$$ and $$$a_4 = 2$$$ for the operation; the score becomes $$$0 + lfloor frac{1}{2} floor = 0$$$, the array becomes $$$[1, 1, 1, 1, 3]$$$; 2. choose $$$a_1 = 1$$$ and $$$a_5 = 3$$$ for the operation; the score becomes $$$0 + lfloor frac{1}{3} floor = 0$$$, the array becomes $$$[1, 1, 1]$$$; 3. choose $$$a_1 = 1$$$ and $$$a_2 = 1$$$ for the operation; the score becomes $$$0 + lfloor frac{1}{1} floor = 1$$$, the array becomes $$$[1]$$$; 4. add the remaining element $$$1$$$ to the score, so the resulting score is $$$2$$$. In the second test case, no matter which operations you choose, the resulting score is $$$16$$$. In the third test case, one way to obtain a score of $$$0$$$ is the following one: 1. choose $$$a_1 = 1$$$ and $$$a_2 = 3$$$ for the operation; the score becomes $$$0 + lfloor frac{1}{3} floor = 0$$$, the array becomes $$$[3, 7]$$$; 2. choose $$$a_1 = 3$$$ and $$$a_2 = 7$$$ for the operation; the score becomes $$$0 + lfloor frac{3}{7} floor = 0$$$, the array becomes empty; 3. the array is empty, so the score doesn't change anymore. In the fourth test case, no operations can be performed, so the score is the sum of the elements of the array: $$$4 + 2 = 6$$$. | 1,300 | true | true | false | true | false | false | false | false | false | false | 2,539 |
698C | While creating high loaded systems one should pay a special attention to caching. This problem will be about one of the most popular caching algorithms called LRU (Least Recently Used). Suppose the cache may store no more than _k_ objects. At the beginning of the workflow the cache is empty. When some object is queried we check if it is present in the cache and move it here if it's not. If there are more than _k_ objects in the cache after this, the least recently used one should be removed. In other words, we remove the object that has the smallest time of the last query. Consider there are _n_ videos being stored on the server, all of the same size. Cache can store no more than _k_ videos and caching algorithm described above is applied. We know that any time a user enters the server he pick the video _i_ with probability _p__i_. The choice of the video is independent to any events before. The goal of this problem is to count for each of the videos the probability it will be present in the cache after 10100 queries. Input The first line of the input contains two integers _n_ and _k_ (1u2009β€u2009_k_u2009β€u2009_n_u2009β€u200920)xa0β the number of videos and the size of the cache respectively. Next line contains _n_ real numbers _p__i_ (0u2009β€u2009_p__i_u2009β€u20091), each of them is given with no more than two digits after decimal point. It's guaranteed that the sum of all _p__i_ is equal to 1. Output Print _n_ real numbers, the _i_-th of them should be equal to the probability that the _i_-th video will be present in the cache after 10100 queries. You answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Examples Output 0.675 0.4857142857142857 0.8392857142857143 | 2,400 | true | false | false | true | false | false | false | false | false | false | 7,031 |
994A | You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits. Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10$$$) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints. The next line contains $$$n$$$ distinct space-separated integers $$$x_1, x_2, ldots, x_n$$$ ($$$0 le x_i le 9$$$) representing the sequence. The next line contains $$$m$$$ distinct space-separated integers $$$y_1, y_2, ldots, y_m$$$ ($$$0 le y_i le 9$$$) β the keys with fingerprints. Output In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable. Examples Input 7 3 3 5 7 1 6 2 8 1 2 7 Note In the first example, the only digits with fingerprints are $$$1$$$, $$$2$$$ and $$$7$$$. All three of them appear in the sequence you know, $$$7$$$ first, then $$$1$$$ and then $$$2$$$. Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence. In the second example digits $$$0$$$, $$$1$$$, $$$7$$$ and $$$9$$$ have fingerprints, however only $$$0$$$ and $$$1$$$ appear in the original sequence. $$$1$$$ appears earlier, so the output is 1 0. Again, the order is important. | 800 | false | false | true | false | false | false | false | false | false | false | 5,728 |
1698F | Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 500$$$)xa0β the length of array $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0β elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$)xa0β elements of the array $$$b$$$. It is guaranteed that $$$b$$$ is a permutation of $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output For each test case, output "NO" (without quotes) if it is impossible to turn $$$a$$$ into $$$b$$$ using at most $$$n^2$$$ operations. Otherwise, output "YES" (without quotes). Then output an integer $$$k$$$ ($$$0 leq k leq n^2$$$) denoting the number of operations you will perform. Note that you don't have to minimize the number of operations. Afterwards, output $$$k$$$ lines. The $$$i$$$-th line should contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 leq l_i leq r_i leq n$$$)xa0β the left and right indices for the $$$i$$$-th operation. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response). If there are multiple possible sequences of operations, you may output any of them. Note In the first test case, we can perform the following operations: $$$$$$[1,2,4,3,1,2,1,1] xrightarrow[l=5,,r=8]{} [1,2,4,3,1,1,2,1] xrightarrow[l=1,,r=6]{} [1,1,3,4,2,1,2,1].$$$$$$ In the second test case, we can perform the following operations: $$$$$$[1,2,3,1,3,2,3] xrightarrow[l=1,,r=4]{} [1,3,2,1,3,2,3] xrightarrow[l=3,,r=6]{} [1,3,2,3,1,2,3].$$$$$$ It can be proven that it is impossible to turn $$$a$$$ into $$$b$$$ in the third and fourth test cases. | 2,800 | true | false | true | false | false | true | false | false | false | true | 2,089 |
32E | Victor and Peter are playing hide-and-seek. Peter has hidden, and Victor is to find him. In the room where they are playing, there is only one non-transparent wall and one double-sided mirror. Victor and Peter are points with coordinates (_x__v_,u2009_y__v_) and (_x__p_,u2009_y__p_) respectively. The wall is a segment joining points with coordinates (_x__w_,u20091,u2009_y__w_,u20091) and (_x__w_,u20092,u2009_y__w_,u20092), the mirror β a segment joining points (_x__m_,u20091,u2009_y__m_,u20091) and (_x__m_,u20092,u2009_y__m_,u20092). If an obstacle has a common point with a line of vision, it's considered, that the boys can't see each other with this line of vision. If the mirror has a common point with the line of vision, it's considered, that the boys can see each other in the mirror, i.e. reflection takes place. The reflection process is governed by laws of physics β the angle of incidence is equal to the angle of reflection. The incident ray is in the same half-plane as the reflected ray, relative to the mirror. I.e. to see each other Victor and Peter should be to the same side of the line, containing the mirror (see example 1). If the line of vision is parallel to the mirror, reflection doesn't take place, and the mirror isn't regarded as an obstacle (see example 4). Victor got interested if he can see Peter, while standing at the same spot. Help him solve this problem. Input The first line contains two numbers _x__v_ and _y__v_ β coordinates of Victor. The second line contains two numbers _x__p_ and _y__p_ β coordinates of Peter. The third line contains 4 numbers _x__w_,u20091, _y__w_,u20091, _x__w_,u20092, _y__w_,u20092 β coordinates of the wall. The forth line contains 4 numbers _x__m_,u20091, _y__m_,u20091, _x__m_,u20092, _y__m_,u20092 β coordinates of the mirror. All the coordinates are integer numbers, and don't exceed 104 in absolute value. It's guaranteed, that the segments don't have common points, Victor and Peter are not on any of the segments, coordinates of Victor and Peter aren't the same, the segments don't degenerate into points. Output Output YES, if Victor can see Peter without leaving the initial spot. Otherwise output NO. Examples Input -1 3 1 3 0 2 0 4 0 0 0 1 Input 0 0 1 1 0 1 1 0 -100 -100 -101 -101 Input 0 0 1 1 0 1 1 0 -1 1 1 3 Input 0 0 10 0 100 100 101 101 1 0 3 0 | 2,400 | false | false | true | false | false | false | false | false | false | false | 9,834 |
1773G | Genie is taking part in an intellectual game. The game consists of $$$n$$$ questions, and there are $$$m$$$ participants numbered from $$$1$$$ to $$$m$$$. Genie is the participant number $$$1$$$. For each question $$$i$$$ and participant $$$j$$$, it is known whether the participant will answer the question correctly or not. The goal of the game is to be the last participant staying in the game. The game is conducted as follows. First, all $$$n$$$ questions get shuffled uniformly at random (all $$$n!$$$ permutations are equally likely). Then, the questions are asked one by one. Each participant answers the question. If all participants still in the game answer the question correctly, or if all of them answer the question incorrectly, nothing happens. Otherwise, those participants who answer the question incorrectly lose and leave the game. After all $$$n$$$ questions are asked, all participants who are still in the game are declared to be the winners. What is the probability that Genie will win the game? Input The first line contains two integers $$$n$$$ and $$$m$$$xa0β the number of questions and the number of participants ($$$1 le n le 2 cdot 10^5$$$; $$$2 le m le 17$$$). The $$$i$$$-th of the next $$$n$$$ lines contains $$$m$$$ characters $$$s_{i, 1}, s_{i, 2}, ldots, s_{i, m}$$$. Character $$$s_{i, j}$$$ is '1' if participant $$$j$$$ answers question $$$i$$$ correctly or '0' otherwise. Output Print the probability that Genie will win the game. Your answer will be considered correct if its absolute or relative error does not exceed $$$10^{-9}$$$. Examples Input 6 4 1011 0110 1111 0110 0000 1101 Note In the first example, there is a single question and Genie will answer it correctly, thus winning the game (along with participants $$$2$$$ and $$$4$$$). In the second example, one participant will leave after the first asked question, and another participant will leave after the second asked question. Each participant will win with probabilityxa0$$$frac{1}{3}$$$. | 2,800 | true | false | false | true | false | false | false | false | false | false | 1,650 |
203E | Valera came to Japan and bought many robots for his research. He's already at the airport, the plane will fly very soon and Valera urgently needs to bring all robots to the luggage compartment. The robots are self-propelled (they can potentially move on their own), some of them even have compartments to carry other robots. More precisely, for the _i_-th robot we know value _c__i_ β the number of robots it can carry. In this case, each of _c__i_ transported robots can additionally carry other robots. However, the robots need to be filled with fuel to go, so Valera spent all his last money and bought _S_ liters of fuel. He learned that each robot has a restriction on travel distances. Thus, in addition to features _c__i_, the _i_-th robot has two features _f__i_ and _l__i_ β the amount of fuel (in liters) needed to move the _i_-th robot, and the maximum distance that the robot can go. Due to the limited amount of time and fuel, Valera wants to move the maximum number of robots to the luggage compartment. He operates as follows. First Valera selects some robots that will travel to the luggage compartment on their own. In this case the total amount of fuel required to move all these robots must not exceed _S_. Then Valera seats the robots into the compartments, so as to transport as many robots as possible. Note that if a robot doesn't move by itself, you can put it in another not moving robot that is moved directly or indirectly by a moving robot. After that all selected and seated robots along with Valera go to the luggage compartment and the rest robots will be lost. There are _d_ meters to the luggage compartment. Therefore, the robots that will carry the rest, must have feature _l__i_ of not less than _d_. During the moving Valera cannot stop or change the location of the robots in any way. Help Valera calculate the maximum number of robots that he will be able to take home, and the minimum amount of fuel he will have to spend, because the remaining fuel will come in handy in Valera's research. Input The first line contains three space-separated integers _n_,u2009_d_,u2009_S_ (1u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_d_,u2009_S_u2009β€u2009109). The first number represents the number of robots, the second one β the distance to the luggage compartment and the third one β the amount of available fuel. Next _n_ lines specify the robots. The _i_-th line contains three space-separated integers _c__i_,u2009_f__i_,u2009_l__i_ (0u2009β€u2009_c__i_,u2009_f__i_,u2009_l__i_u2009β€u2009109) β the _i_-th robot's features. The first number is the number of robots the _i_-th robot can carry, the second number is the amount of fuel needed for the _i_-th robot to move and the third one shows the maximum distance the _i_-th robot can go. Output Print two space-separated integers β the maximum number of robots Valera can transport to the luggage compartment and the minimum amount of fuel he will need for that. If Valera won't manage to get any robots to the luggage compartment, print two zeroes. | 2,300 | false | true | false | false | false | false | false | false | true | false | 9,028 |
309B | Advertising has become part of our routine. And now, in the era of progressive technologies, we need your ideas to make advertising better! In this problem we'll look at a simplified version of context advertising. You've got a text, consisting of exactly _n_ words. A standard advertising banner has exactly _r_ lines, each line can contain at most _c_ characters. The potential customer always likes it when they can see lots of advertising, so you should determine which maximum number of consecutive words from the text can be written on the banner. Single words in one line of the banner should be separated by spaces. You are allowed to insert more than one space at once. Note that you are not allowed to break the words, that is, each word in the text must occupy exactly one line in the banner. Besides, you cannot change the word order, that is, if you read the banner text consecutively, from top to bottom and from left to right, you should get some consecutive part of the advertisement text. More formally, the statement can be written like that. Let's say that all words are indexed from 1 to _n_ in the order in which they occur in the advertisement text. Then you have to choose all words, starting from some _i_-th one and ending with some _j_-th one (1u2009β€u2009_i_u2009β€u2009_j_u2009β€u2009_n_), so that all of them could be written on the banner. There must be as many words as possible. See the samples for clarifications. Input The first input line contains three integers _n_, _r_, _c_ (1u2009β€u2009_n_,u2009_r_,u2009_c_u2009β€u2009106;xa0_r_u2009Γu2009_c_u2009β€u2009106). The next line contains a text, consisting of _n_ words. The words consist only of lowercase English letters and are not empty. The words in the lines are separated by single spaces. The total number of characters in all words doesn't exceed 5Β·106. Output Print at most _r_ lines, in each line print at most _c_ characters β the optimal advertisement banner. If there are multiple advertisement banners, print any of them. Note that some lines of the banner can be empty. You are allowed not to print such lines. Examples Input 9 4 12 this is a sample text for croc final round Output this is a sample text for croc final round Input 9 1 9 this is a sample text for croc final round Input 6 2 3 croc a a a croc a | 2,100 | false | false | false | true | false | false | false | false | false | false | 8,606 |
45D | Problem - 45D - 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 meet-in-the-middle sortings *1900 No tag edit access β Contest materials ") on which the event could have taken place. However Vasya also remembers that there was at most one event in one day. Help him choose such _n_ dates of famous events that will fulfill both conditions. It is guaranteed that it is possible. Input The first line contains one integer _n_ (1u2009β€u2009_n_u2009β€u2009100) β the number of known events. Then follow _n_ lines containing two integers _l__i_ and _r__i_ each (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009107) β the earliest acceptable date and the latest acceptable date of the _i_-th event. Output Print _n_ numbers β the dates on which the events took place. If there are several solutions, print any of them. It is guaranteed that a solution exists. Examples Input 3 1 2 2 3 3 4 Output 1 2 3 Input 2 1 3 1 3 Output 1 2 | 1,900 | false | true | false | false | false | false | false | false | true | false | 9,756 |
1371C | Anna is a girl so brave that she is loved by everyone in the city and citizens love her cookies. She is planning to hold a party with cookies. Now she has $$$a$$$ vanilla cookies and $$$b$$$ chocolate cookies for the party. She invited $$$n$$$ guests of the first type and $$$m$$$ guests of the second type to the party. They will come to the party in some order. After coming to the party, each guest will choose the type of cookie (vanilla or chocolate) to eat. There is a difference in the way how they choose that type: If there are $$$v$$$ vanilla cookies and $$$c$$$ chocolate cookies at the moment, when the guest comes, then if the guest of the first type: if $$$v>c$$$ the guest selects a vanilla cookie. Otherwise, the guest selects a chocolate cookie. if the guest of the second type: if $$$v>c$$$ the guest selects a chocolate cookie. Otherwise, the guest selects a vanilla cookie. After that: If there is at least one cookie of the selected type, the guest eats one. Otherwise (there are no cookies of the selected type), the guest gets angry and returns to home. Anna wants to know if there exists some order of guests, such that no one guest gets angry. Your task is to answer her question. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. Next $$$t$$$ lines contain descriptions of test cases. For each test case, the only line contains four integers $$$a$$$, $$$b$$$, $$$n$$$, $$$m$$$ ($$$0 le a,b,n,m le 10^{18}, n+m eq 0$$$). Output For each test case, print the answer in one line. If there exists at least one valid order, print "Yes". Otherwise, print "No". You can print each letter in any case (upper or lower). Example Input 6 2 2 1 2 0 100 0 1 12 13 25 1 27 83 14 25 0 0 1 0 1000000000000000000 1000000000000000000 1000000000000000000 1000000000000000000 Output Yes No No Yes No Yes Note In the first test case, let's consider the order $$${1, 2, 2}$$$ of types of guests. Then: The first guest eats a chocolate cookie. After that, there are $$$2$$$ vanilla cookies and $$$1$$$ chocolate cookie. The second guest eats a chocolate cookie. After that, there are $$$2$$$ vanilla cookies and $$$0$$$ chocolate cookies. The last guest selects a chocolate cookie, but there are no chocolate cookies. So, the guest gets angry. So, this order can't be chosen by Anna. Let's consider the order $$${2, 2, 1}$$$ of types of guests. Then: The first guest eats a vanilla cookie. After that, there is $$$1$$$ vanilla cookie and $$$2$$$ chocolate cookies. The second guest eats a vanilla cookie. After that, there are $$$0$$$ vanilla cookies and $$$2$$$ chocolate cookies. The last guest eats a chocolate cookie. After that, there are $$$0$$$ vanilla cookies and $$$1$$$ chocolate cookie. So, the answer to this test case is "Yes". In the fifth test case, it is illustrated, that the number of cookies ($$$a + b$$$) can be equal to zero, but the number of guests ($$$n + m$$$) can't be equal to zero. In the sixth test case, be careful about the overflow of $$$32$$$-bit integer type. | 1,300 | true | true | true | false | false | false | false | false | false | false | 3,812 |
203C | Valera's lifelong ambition was to be a photographer, so he bought a new camera. Every day he got more and more clients asking for photos, and one day Valera needed a program that would determine the maximum number of people he can serve. The camera's memory is _d_ megabytes. Valera's camera can take photos of high and low quality. One low quality photo takes _a_ megabytes of memory, one high quality photo take _b_ megabytes of memory. For unknown reasons, each client asks him to make several low quality photos and several high quality photos. More formally, the _i_-th client asks to make _x__i_ low quality photos and _y__i_ high quality photos. Valera wants to serve as many clients per day as possible, provided that they will be pleased with his work. To please the _i_-th client, Valera needs to give him everything he wants, that is, to make _x__i_ low quality photos and _y__i_ high quality photos. To make one low quality photo, the camera must have at least _a_ megabytes of free memory space. Similarly, to make one high quality photo, the camera must have at least _b_ megabytes of free memory space. Initially the camera's memory is empty. Valera also does not delete photos from the camera so that the camera's memory gradually fills up. Calculate the maximum number of clients Valera can successfully serve and print the numbers of these clients. Input The first line contains two integers _n_ and _d_ (1u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_d_u2009β€u2009109) β the number of clients and the camera memory size, correspondingly. The second line contains two integers _a_ and _b_ (1u2009β€u2009_a_u2009β€u2009_b_u2009β€u2009104) β the size of one low quality photo and of one high quality photo, correspondingly. Next _n_ lines describe the clients. The _i_-th line contains two integers _x__i_ and _y__i_ (0u2009β€u2009_x__i_,u2009_y__i_u2009β€u2009105) β the number of low quality photos and high quality photos the _i_-th client wants, correspondingly. All numbers on all lines are separated by single spaces. Output On the first line print the answer to the problem β the maximum number of clients that Valera can successfully serve. Print on the second line the numbers of the client in any order. All numbers must be distinct. If there are multiple answers, print any of them. The clients are numbered starting with 1 in the order in which they are defined in the input data. | 1,400 | false | true | false | false | false | false | false | false | true | false | 9,030 |
1750G | We call an array $$$a$$$ of length $$$n$$$ fancy if for each $$$1 < i le n$$$ it holds that $$$a_i = a_{i-1} + 1$$$. Let's call $$$f(p)$$$ applied to a permutation$$$^dagger$$$ of length $$$n$$$ as the minimum number of subarrays it can be partitioned such that each one of them is fancy. For example $$$f([1,2,3]) = 1$$$, while $$$f([3,1,2]) = 2$$$ and $$$f([3,2,1]) = 3$$$. Given $$$n$$$ and a permutation $$$p$$$ of length $$$n$$$, we define a permutation $$$p'$$$ of length $$$n$$$ to be $$$k$$$-special if and only if: $$$p'$$$ is lexicographically smaller$$$^ddagger$$$ than $$$p$$$, and $$$f(p') = k$$$. Your task is to count for each $$$1 le k le n$$$ the number of $$$k$$$-special permutations modulo $$$m$$$. $$$^dagger$$$ A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). $$$^ddagger$$$ A permutation $$$a$$$ of length $$$n$$$ is lexicographically smaller than a permutation $$$b$$$ of length $$$n$$$ if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the permutation $$$a$$$ has a smaller element than the corresponding element in $$$b$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2000$$$, $$$10 le m le 10^9$$$) β the length of the permutation and the required modulo. The second line contains $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$) β the permutation $$$p$$$. Note In the first example, the permutations that are lexicographically smaller than $$$[1,3,4,2]$$$ are: $$$[1,2,3,4]$$$, $$$f([1,2,3,4])=1$$$; $$$[1,2,4,3]$$$, $$$f([1,2,4,3])=3$$$; $$$[1,3,2,4]$$$, $$$f([1,3,2,4])=4$$$. Thus our answer is $$$[1,0,1,1]$$$. In the second example, the permutations that are lexicographically smaller than $$$[3,2,1]$$$ are: $$$[1,2,3]$$$, $$$f([1,2,3])=1$$$; $$$[1,3,2]$$$, $$$f([1,3,2])=3$$$; $$$[2,1,3]$$$, $$$f([2,1,3])=3$$$; $$$[2,3,1]$$$, $$$f([2,3,1])=2$$$; $$$[3,1,2]$$$, $$$f([3,1,2])=2$$$. Thus our answer is $$$[1,2,2]$$$. | 3,300 | true | false | false | true | false | false | false | false | false | false | 1,779 |
347A | You want to arrange _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers. More formally, let's denote some arrangement as a sequence of integers _x_1,u2009_x_2,u2009...,u2009_x__n_, where sequence _x_ is a permutation of sequence _a_. The value of such an arrangement is (_x_1u2009-u2009_x_2)u2009+u2009(_x_2u2009-u2009_x_3)u2009+u2009...u2009+u2009(_x__n_u2009-u20091u2009-u2009_x__n_). Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence _x_ that corresponds to an arrangement of the largest possible value. Input The first line of the input contains integer _n_ (2u2009β€u2009_n_u2009β€u2009100). The second line contains _n_ space-separated integers _a_1, _a_2, ..., _a__n_ (_a__i_u2009β€u20091000). Output Print the required sequence _x_1,u2009_x_2,u2009...,u2009_x__n_. Sequence _x_ should be the lexicographically smallest permutation of _a_ that corresponds to an arrangement of the largest possible value. Note In the sample test case, the value of the output arrangement is (100u2009-u2009(u2009-u200950))u2009+u2009((u2009-u200950)u2009-u20090)u2009+u2009(0u2009-u200950)u2009+u2009(50u2009-u2009(u2009-u2009100))u2009=u2009200. No other arrangement has a larger value, and among all arrangements with the value of 200, the output arrangement is the lexicographically smallest one. Sequence _x_1,u2009_x_2,u2009... ,u2009_x__p_ is lexicographically smaller than sequence _y_1,u2009_y_2,u2009... ,u2009_y__p_ if there exists an integer _r_ (0u2009β€u2009_r_u2009<u2009_p_) such that _x_1u2009=u2009_y_1,u2009_x_2u2009=u2009_y_2,u2009... ,u2009_x__r_u2009=u2009_y__r_ and _x__r_u2009+u20091u2009<u2009_y__r_u2009+u20091. | 1,300 | false | false | true | false | false | true | false | false | true | false | 8,444 |
1472B | Alice and Bob received $$$n$$$ candies from their parents. Each candy weighs either 1 gram or 2 grams. Now they want to divide all candies among themselves fairly so that the total weight of Alice's candies is equal to the total weight of Bob's candies. Check if they can do that. Note that candies are not allowed to be cut in half. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the number of candies that Alice and Bob received. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0β the weights of the candies. The weight of each candy is either $$$1$$$ or $$$2$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output on a separate line: "YES", if all candies can be divided into two sets with the same weight; "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 5 2 1 1 2 1 2 4 1 2 1 2 3 2 2 2 3 2 1 2 Note In the first test case, Alice and Bob can each take one candy, then both will have a total weight of $$$1$$$. In the second test case, any division will be unfair. In the third test case, both Alice and Bob can take two candies, one of weight $$$1$$$ and one of weight $$$2$$$. In the fourth test case, it is impossible to divide three identical candies between two people. In the fifth test case, any division will also be unfair. | 800 | true | true | false | true | false | false | false | false | false | false | 3,323 |
1886F | Monocarp is the most famous thief in Berland. This time, he decided to steal two diamonds. Unfortunately for Monocarp, there are $$$n$$$ cameras monitoring the diamonds. Each camera has two parameters, $$$t_i$$$ and $$$s_i$$$. The first parameter determines whether the camera is monitoring the first diamond only ($$$t_i=1$$$), the second diamond only ($$$t_i=2$$$), or both diamonds ($$$t_i=3$$$). The second parameter determines the number of seconds the camera will be disabled after it is hacked. Every second, Monocarp can perform one of the following three actions: do nothing; choose a camera and hack it; if Monocarp hacks the $$$i$$$-th camera, it will be disabled for the next $$$s_i$$$ seconds (if the current second is the $$$T$$$-th one, the camera will be disabled from the $$$(T+1)$$$-th to the $$$(T+s_i)$$$-th second, inclusive); steal a diamond if all cameras monitoring it are currently disabled. Monocarp cannot steal the second diamond if he hasn't stolen the first diamond yet. Note that Monocarp can hack a camera multiple times, even if it is currently disabled. Your task is to determine the minimum time it will take Monocarp to steal both diamonds, beginning with the first diamond, or report that it is impossible. Input The first line contains a single integer $$$n$$$ ($$$0 le n le 1500$$$)xa0β the number of cameras. Then $$$n$$$ lines follow, the $$$i$$$-th of them contains two integers $$$t_i$$$ and $$$s_i$$$ ($$$1 le t_i le 3$$$; $$$1 le s_i le 2n$$$)xa0β the parameters of the $$$i$$$-th camera. Output Print a single integerxa0β the minimum time it will take for Monocarp to steal the first diamond first and then the second diamond. If it is impossible, print -1. Examples Input 8 2 1 2 2 3 5 3 6 1 2 1 3 1 4 1 5 | 3,300 | false | true | false | false | true | false | false | false | false | false | 972 |
851B | Problem - 851B - 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 geometry math *1400 No tag edit access β Contest materials ") . Help Arpa determine if the question has a solution or not. Input The only line contains six integers _a__x_,u2009_a__y_,u2009_b__x_,u2009_b__y_,u2009_c__x_,u2009_c__y_ (_a__x_,u2009_a__y_,u2009_b__x_,u2009_b__y_,u2009_c__x_,u2009_c__y_u2009β€u2009109). It's guaranteed that the points are distinct. Output Print "Yes" if the problem has a solution, "No" otherwise. You can print each letter in any case (upper or lower). Examples Input 0 1 1 1 1 0 Output Yes Input 1 1 0 0 1000 1000 Output No Note In the first sample test, rotate the page around (0.5,u20090.5) by . In the second sample test, you can't find any solution. | 1,400 | true | false | false | false | false | false | false | false | false | false | 6,344 |
1634C | People worry that computers will get too smart and take over the world, but the real problem is that they're too stupid and they've already taken over the world. β Pedro Domingos You work for a well-known department store that uses leading technologies and employs mechanistic workxa0β that is, robots! The department you work in sells $$$n cdot k$$$ items. The first item costs $$$1$$$ dollar, the second item costs $$$2$$$ dollars, and so on: $$$i$$$-th item costs $$$i$$$ dollars. The items are situated on shelves. The items form a rectangular grid: there are $$$n$$$ shelves in total, and each shelf contains exactly $$$k$$$ items. We will denote by $$$a_{i,j}$$$ the price of $$$j$$$-th item (counting from the left) on the $$$i$$$-th shelf, $$$1 le i le n, 1 le j le k$$$. Occasionally robots get curious and ponder on the following question: what is the mean price (arithmetic average) of items $$$a_{i,l}, a_{i,l+1}, ldots, a_{i,r}$$$ for some shelf $$$i$$$ and indices $$$l le r$$$? Unfortunately, the old robots can only work with whole numbers. If the mean price turns out not to be an integer, they break down. You care about robots' welfare. You want to arrange the items in such a way that the robots cannot theoretically break. Formally, you want to choose such a two-dimensional array $$$a$$$ that: Every number from $$$1$$$ to $$$n cdot k$$$ (inclusively) occurs exactly once. For each $$$i, l, r$$$, the mean price of items from $$$l$$$ to $$$r$$$ on $$$i$$$-th shelf is an integer. Find out if such an arrangement is possible, and if it is, give any example of such arrangement. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$)xa0β the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 500$$$)xa0β the number of shelves and length of each shelf, respectively. It is guaranteed that the sum $$$n$$$ over all test cases does not exceed $$$500$$$ and the sum $$$k$$$ over all test cases does not exceed $$$500$$$. Output Print the answer for each test case. If such an arrangement exists, print "YES" on a single line. After that, print any example on $$$n$$$ lines of $$$k$$$ numbers each, one line per shelf. Each number from $$$1$$$ to $$$n cdot k$$$ must occur exactly once in the output. If no good arrangement exists, print a single word "NO" on its own line. | 1,000 | false | false | false | false | false | true | false | false | false | false | 2,444 |
1005E1 | You are given a permutation $$$p_1, p_2, dots, p_n$$$. A permutation of length $$$n$$$ is a sequence such that each integer between $$$1$$$ and $$$n$$$ occurs exactly once in the sequence. Find the number of pairs of indices $$$(l, r)$$$ ($$$1 le l le r le n$$$) such that the value of the median of $$$p_l, p_{l+1}, dots, p_r$$$ is exactly the given number $$$m$$$. The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used. For example, if $$$a=[4, 2, 7, 5]$$$ then its median is $$$4$$$ since after sorting the sequence, it will look like $$$[2, 4, 5, 7]$$$ and the left of two middle elements is equal to $$$4$$$. The median of $$$[7, 1, 2, 9, 6]$$$ equals $$$6$$$ since after sorting, the value $$$6$$$ will be in the middle of the sequence. Write a program to find the number of pairs of indices $$$(l, r)$$$ ($$$1 le l le r le n$$$) such that the value of the median of $$$p_l, p_{l+1}, dots, p_r$$$ is exactly the given number $$$m$$$. Input The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2cdot10^5$$$, $$$1 le m le n$$$) β the length of the given sequence and the required value of the median. The second line contains a permutation $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$). Each integer between $$$1$$$ and $$$n$$$ occurs in $$$p$$$ exactly once. Note In the first example, the suitable pairs of indices are: $$$(1, 3)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$ and $$$(2, 4)$$$. | 1,800 | false | false | false | false | false | false | false | false | true | false | 5,658 |
1993B | Given an array $$$a$$$ of $$$n$$$ positive integers. In one operation, you can pick any pair of indexes $$$(i, j)$$$ such that $$$a_i$$$ and $$$a_j$$$ have distinct parity, then replace the smaller one with the sum of them. More formally: If $$$a_i < a_j$$$, replace $$$a_i$$$ with $$$a_i + a_j$$$; Otherwise, replace $$$a_j$$$ with $$$a_i + a_j$$$. Find the minimum number of operations needed to make all elements of the array have the same parity. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β the elements of array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, all integers already have the same parity. Therefore, no operation is needed. In the third test case, we can perform two operations $$$(1, 2)$$$ and $$$(1, 3)$$$. The array $$$a$$$ transforms as follows: $$$a = [color{red}2, color{red}3, 4] longrightarrow [color{red}5, 3, color{red}4] longrightarrow [5, 3, 9]$$$. In the fourth test case, an example of an optimal sequence of operations is $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, and $$$(1, 4)$$$. The array $$$a$$$ transforms as follows: $$$a = [color{red}3, color{red}2, 2, 8] longrightarrow [color{red}3, 5, color{red}2, 8] longrightarrow [color{red}3, 5, 5, color{red}8] longrightarrow [color{red}{11}, 5, 5, color{red}8] longrightarrow [11, 5, 5, 19]$$$. | 1,100 | false | true | false | false | false | true | false | false | false | false | 311 |
335E | A number of skyscrapers have been built in a line. The number of skyscrapers was chosen uniformly at random between 2 and 314! (314 factorial, a very large number). The height of each skyscraper was chosen randomly and independently, with height _i_ having probability 2u2009-u2009_i_ for all positive integers _i_. The floors of a skyscraper with height _i_ are numbered 0 through _i_u2009-u20091. To speed up transit times, a number of zip lines were installed between skyscrapers. Specifically, there is a zip line connecting the _i_-th floor of one skyscraper with the _i_-th floor of another skyscraper if and only if there are no skyscrapers between them that have an _i_-th floor. Alice and Bob decide to count the number of skyscrapers. Alice is thorough, and wants to know exactly how many skyscrapers there are. She begins at the leftmost skyscraper, with a counter at 1. She then moves to the right, one skyscraper at a time, adding 1 to her counter each time she moves. She continues until she reaches the rightmost skyscraper. Bob is impatient, and wants to finish as fast as possible. He begins at the leftmost skyscraper, with a counter at 1. He moves from building to building using zip lines. At each stage Bob uses the highest available zip line to the right, but ignores floors with a height greater than _h_ due to fear of heights. When Bob uses a zip line, he travels too fast to count how many skyscrapers he passed. Instead, he just adds 2_i_ to his counter, where _i_ is the number of the floor he's currently on. He continues until he reaches the rightmost skyscraper. Consider the following example. There are 6 buildings, with heights 1, 4, 3, 4, 1, 2 from left to right, and _h_u2009=u20092. Alice begins with her counter at 1 and then adds 1 five times for a result of 6. Bob begins with his counter at 1, then he adds 1, 4, 4, and 2, in order, for a result of 12. Note that Bob ignores the highest zip line because of his fear of heights (_h_u2009=u20092). Bob's counter is at the top of the image, and Alice's counter at the bottom. All zip lines are shown. Bob's path is shown by the green dashed line and Alice's by the pink dashed line. The floors of the skyscrapers are numbered, and the zip lines Bob uses are marked with the amount he adds to his counter. When Alice and Bob reach the right-most skyscraper, they compare counters. You will be given either the value of Alice's counter or the value of Bob's counter, and must compute the expected value of the other's counter. Input The first line of input will be a name, either string "Alice" or "Bob". The second line of input contains two integers _n_ and _h_ (2u2009β€u2009_n_u2009β€u200930000, 0u2009β€u2009_h_u2009β€u200930). If the name is "Alice", then _n_ represents the value of Alice's counter when she reaches the rightmost skyscraper, otherwise _n_ represents the value of Bob's counter when he reaches the rightmost skyscraper; _h_ represents the highest floor number Bob is willing to use. | 2,800 | true | false | false | true | false | false | false | false | false | false | 8,494 |
2035A | Red was ejected. They were not the imposter. There are $$$n$$$ rows of $$$m$$$ people. Let the position in the $$$r$$$-th row and the $$$c$$$-th column be denoted by $$$(r, c)$$$. Number each person starting from $$$1$$$ in row-major order, i.e., the person numbered $$$(r-1)cdot m+c$$$ is initially at $$$(r,c)$$$. The person at $$$(r, c)$$$ decides to leave. To fill the gap, let the person who left be numbered $$$i$$$. Each person numbered $$$j>i$$$ will move to the position where the person numbered $$$j-1$$$ is initially at. The following diagram illustrates the case where $$$n=2$$$, $$$m=3$$$, $$$r=1$$$, and $$$c=2$$$. Calculate the sum of the Manhattan distances of each person's movement. If a person was initially at $$$(r_0, c_0)$$$ and then moved to $$$(r_1, c_1)$$$, the Manhattan distance is $$$r_0-r_1+c_0-c_1$$$. Input The first line contains a single integer $$$t$$$ ($$$1le tle 10^4$$$)xa0β the number of test cases. The only line of each testcase contains $$$4$$$ integers $$$n$$$, $$$m$$$, $$$r$$$, and $$$c$$$ ($$$1le rle nle 10^6$$$, $$$1 le c le m le 10^6$$$), where $$$n$$$ is the number of rows, $$$m$$$ is the number of columns, and $$$(r,c)$$$ is the position where the person who left is initially at. Output For each test case, output a single integer denoting the sum of the Manhattan distances. Example Input 4 2 3 1 2 2 2 2 1 1 1 1 1 1000000 1000000 1 1 Output 6 1 0 1999998000000 Note For the first test case, the person numbered $$$2$$$ leaves, and the distances of the movements of the person numbered $$$3$$$, $$$4$$$, $$$5$$$, and $$$6$$$ are $$$1$$$, $$$3$$$, $$$1$$$, and $$$1$$$, respectively. So the answer is $$$1+3+1+1=6$$$. For the second test case, the person numbered $$$3$$$ leaves, and the person numbered $$$4$$$ moves. The answer is $$$1$$$. | 800 | true | false | true | false | false | false | false | false | false | false | 61 |
832D | Misha and Grisha are funny boys, so they like to use new underground. The underground has _n_ stations connected with _n_u2009-u20091 routes so that each route connects two stations, and it is possible to reach every station from any other. The boys decided to have fun and came up with a plan. Namely, in some day in the morning Misha will ride the underground from station _s_ to station _f_ by the shortest path, and will draw with aerosol an ugly text "Misha was here" on every station he will pass through (including _s_ and _f_). After that on the same day at evening Grisha will ride from station _t_ to station _f_ by the shortest path and will count stations with Misha's text. After that at night the underground workers will wash the texts out, because the underground should be clean. The boys have already chosen three stations _a_, _b_ and _c_ for each of several following days, one of them should be station _s_ on that day, another should be station _f_, and the remaining should be station _t_. They became interested how they should choose these stations _s_, _f_, _t_ so that the number Grisha will count is as large as possible. They asked you for help. Input The first line contains two integers _n_ and _q_ (2u2009β€u2009_n_u2009β€u2009105, 1u2009β€u2009_q_u2009β€u2009105)xa0β the number of stations and the number of days. The second line contains _n_u2009-u20091 integers _p_2,u2009_p_3,u2009...,u2009_p__n_ (1u2009β€u2009_p__i_u2009β€u2009_n_). The integer _p__i_ means that there is a route between stations _p__i_ and _i_. It is guaranteed that it's possible to reach every station from any other. The next _q_ lines contains three integers _a_, _b_ and _c_ each (1u2009β€u2009_a_,u2009_b_,u2009_c_u2009β€u2009_n_)xa0β the ids of stations chosen by boys for some day. Note that some of these ids could be same. Output Print _q_ lines. In the _i_-th of these lines print the maximum possible number Grisha can get counting when the stations _s_, _t_ and _f_ are chosen optimally from the three stations on the _i_-th day. Note In the first example on the first day if _s_ = 1, _f_ = 2, _t_ = 3, Misha would go on the route 1 2, and Grisha would go on the route 3 1 2. He would see the text at the stations 1 and 2. On the second day, if _s_ = 3, _f_ = 2, _t_ = 3, both boys would go on the route 3 1 2. Grisha would see the text at 3 stations. In the second examle if _s_ = 1, _f_ = 3, _t_ = 2, Misha would go on the route 1 2 3, and Grisha would go on the route 2 3 and would see the text at both stations. | 1,900 | false | false | false | false | false | false | false | false | false | true | 6,436 |
1472A | For the New Year, Polycarp decided to send postcards to all his $$$n$$$ friends. He wants to make postcards with his own hands. For this purpose, he has a sheet of paper of size $$$w imes h$$$, which can be cut into pieces. Polycarp can cut any sheet of paper $$$w imes h$$$ that he has in only two cases: If $$$w$$$ is even, then he can cut the sheet in half and get two sheets of size $$$frac{w}{2} imes h$$$; If $$$h$$$ is even, then he can cut the sheet in half and get two sheets of size $$$w imes frac{h}{2}$$$; If $$$w$$$ and $$$h$$$ are even at the same time, then Polycarp can cut the sheet according to any of the rules above. After cutting a sheet of paper, the total number of sheets of paper is increased by $$$1$$$. Help Polycarp to find out if he can cut his sheet of size $$$w imes h$$$ at into $$$n$$$ or more pieces, using only the rules described above. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing three integers $$$w$$$, $$$h$$$, $$$n$$$ ($$$1 le w, h le 10^4, 1 le n le 10^9$$$)xa0β the width and height of the sheet Polycarp has and the number of friends he needs to send a postcard to. Output For each test case, output on a separate line: "YES", if it is possible to cut a sheet of size $$$w imes h$$$ into at least $$$n$$$ pieces; "NO" otherwise. You can output "YES" and "NO" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive). Example Input 5 2 2 3 3 3 2 5 10 2 11 13 1 1 4 4 Note In the first test case, you can first cut the $$$2 imes 2$$$ sheet into two $$$2 imes 1$$$ sheets, and then cut each of them into two more sheets. As a result, we get four sheets $$$1 imes 1$$$. We can choose any three of them and send them to our friends. In the second test case, a $$$3 imes 3$$$ sheet cannot be cut, so it is impossible to get two sheets. In the third test case, you can cut a $$$5 imes 10$$$ sheet into two $$$5 imes 5$$$ sheets. In the fourth test case, there is no need to cut the sheet, since we only need one sheet. In the fifth test case, you can first cut the $$$1 imes 4$$$ sheet into two $$$1 imes 2$$$ sheets, and then cut each of them into two more sheets. As a result, we get four sheets $$$1 imes 1$$$. | 800 | true | true | false | false | false | false | false | false | false | false | 3,324 |
513A | Problem - 513A - Codeforces =============== xa0 ]( "16140") | 800 | true | false | false | false | false | true | false | false | false | false | 7,793 |
676B | Mary has just graduated from one well-known University and is now attending celebration party. Students like to dream of a beautiful life, so they used champagne glasses to construct a small pyramid. The height of the pyramid is _n_. The top level consists of only 1 glass, that stands on 2 glasses on the second level (counting from the top), then 3 glasses on the third level and so on.The bottom level consists of _n_ glasses. Vlad has seen in the movies many times how the champagne beautifully flows from top levels to bottom ones, filling all the glasses simultaneously. So he took a bottle and started to pour it in the glass located at the top of the pyramid. Each second, Vlad pours to the top glass the amount of champagne equal to the size of exactly one glass. If the glass is already full, but there is some champagne flowing in it, then it pours over the edge of the glass and is equally distributed over two glasses standing under. If the overflowed glass is at the bottom level, then the champagne pours on the table. For the purpose of this problem we consider that champagne is distributed among pyramid glasses immediately. Vlad is interested in the number of completely full glasses if he stops pouring champagne in _t_ seconds. Pictures below illustrate the pyramid consisting of three levels. Input The only line of the input contains two integers _n_ and _t_ (1u2009β€u2009_n_u2009β€u200910,u20090u2009β€u2009_t_u2009β€u200910u2009000)xa0β the height of the pyramid and the number of seconds Vlad will be pouring champagne from the bottle. Output Print the single integerxa0β the number of completely full glasses after _t_ seconds. Note In the first sample, the glasses full after 5 seconds are: the top glass, both glasses on the second level and the middle glass at the bottom level. Left and right glasses of the bottom level will be half-empty. | 1,500 | true | false | true | false | false | false | false | false | false | false | 7,125 |
101E | Little Gerald and his coach Mike play an interesting game. At the beginning of the game there is a pile consisting of _n_ candies and a pile consisting of _m_ stones. Gerald and Mike move in turns, Mike goes first. During his move Mike checks how many candies and stones Gerald has eaten. Let Gerald eat _a_ candies and _b_ stones. Then Mike awards Gerald _f_(_a_,u2009_b_) prize points. Gerald during his move either eats a candy from the pile of candies or a stone from the pile of stones. As Mike sees that Gerald has eaten everything apart one candy and one stone, he awards points for the last time and the game ends. Gerald is not allowed to eat all the candies, and he is not allowed to eat all the stones too. Tell Gerald how to play to get the largest possible number of points: it is required to find one of the possible optimal playing strategies for Gerald. Input The first line contains three integers _n_,u2009_m_,u2009_p_ (1u2009β€u2009_n_,u2009_m_u2009β€u200920000, 1u2009β€u2009_p_u2009β€u2009109). The second line contains _n_ integers _x_0, _x_1, ..., _x__n_u2009-u20091 (0u2009β€u2009_x__i_u2009β€u200920000). The third line contains _m_ integers _y_0, _y_1, ..., _y__m_u2009-u20091 (0u2009β€u2009_y__i_u2009β€u200920000). The value of _f_(_a_,u2009_b_) is calculated as a remainder of the division of the sum _x__a_u2009+u2009_y__b_ by number _p_. Output Print on the first line the only number: the maximal number of points Gerald can earn. Print on the second line a sting consisting of _n_u2009+u2009_m_u2009-u20092 characters, each of which is either a "C" or "S", the _i_-th character should be "C" if Gerald's _i_-th move should be eating a candy and "S" if he should eat a stone. Note In the first test if Gerald's first move is eating a stone, he will receive a point for it and if he eats a candy, he will get zero pints. In any way Gerald will get 0 points before his first move, and 1 after his second one. This, the maximum number of points Gerald can get equals to 2, and for that he should first eat a stone, then a candy. | 2,500 | false | false | false | true | false | false | false | false | false | false | 9,475 |
245B | Vasya is an active Internet user. One day he came across an Internet resource he liked, so he wrote its address in the notebook. We know that the address of the written resource has format: <protocol>://<domain>.ru[/<context>] where: <protocol> can equal either "http" (without the quotes) or "ftp" (without the quotes), <domain> is a non-empty string, consisting of lowercase English letters, the /<context> part may not be present. If it is present, then <context> is a non-empty string, consisting of lowercase English letters. If string <context> isn't present in the address, then the additional character "/" isn't written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context). When the boy came home, he found out that the address he wrote in his notebook had no punctuation marks. Vasya must have been in a lot of hurry and didn't write characters ":", "/", ".". Help Vasya to restore the possible address of the recorded Internet resource. Input The first line contains a non-empty string that Vasya wrote out in his notebook. This line consists of lowercase English letters only. It is guaranteed that the given string contains at most 50 letters. It is guaranteed that the given string can be obtained from some correct Internet resource address, described above. Output Print a single line β the address of the Internet resource that Vasya liked. If there are several addresses that meet the problem limitations, you are allowed to print any of them. Note In the second sample there are two more possible answers: "ftp://httpruru.ru" and "ftp://httpru.ru/ru". | 1,100 | false | false | true | false | false | false | false | false | false | false | 8,855 |
1461B | Holidays are coming up really soon. Rick realized that it's time to think about buying a traditional spruce tree. But Rick doesn't want real trees to get hurt so he decided to find some in an $$$n imes m$$$ matrix consisting of "*" and ".". To find every spruce first let's define what a spruce in the matrix is. A set of matrix cells is called a spruce of height $$$k$$$ with origin at point $$$(x, y)$$$ if: All cells in the set contain an "*". For each $$$1 le i le k$$$ all cells with the row number $$$x+i-1$$$ and columns in range $$$[y - i + 1, y + i - 1]$$$ must be a part of the set. All other cells cannot belong to the set. Examples of correct and incorrect spruce trees: Now Rick wants to know how many spruces his $$$n imes m$$$ matrix contains. Help Rick solve this problem. Input Each test contains one or more test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10$$$). The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 500$$$)xa0β matrix size. Next $$$n$$$ lines of each test case contain $$$m$$$ characters $$$c_{i, j}$$$xa0β matrix contents. It is guaranteed that $$$c_{i, j}$$$ is either a "." or an "*". It is guaranteed that the sum of $$$n cdot m$$$ over all test cases does not exceed $$$500^2$$$ ($$$sum n cdot m le 500^2$$$). Output For each test case, print single integerxa0β the total number of spruces in the matrix. Example Input 4 2 3 .*. *** 2 3 .*. **. 4 5 .***. ***** ***** *.*.* 5 7 ..*.*.. .*****. ******* .*****. ..*.*.. Note In the first test case the first spruce of height $$$2$$$ has its origin at point $$$(1, 2)$$$, the second spruce of height $$$1$$$ has its origin at point $$$(1, 2)$$$, the third spruce of height $$$1$$$ has its origin at point $$$(2, 1)$$$, the fourth spruce of height $$$1$$$ has its origin at point $$$(2, 2)$$$, the fifth spruce of height $$$1$$$ has its origin at point $$$(2, 3)$$$. In the second test case the first spruce of height $$$1$$$ has its origin at point $$$(1, 2)$$$, the second spruce of height $$$1$$$ has its origin at point $$$(2, 1)$$$, the third spruce of height $$$1$$$ has its origin at point $$$(2, 2)$$$. | 1,400 | false | false | true | true | false | false | true | false | false | false | 3,385 |
343D | Mad scientist Mike has constructed a rooted tree, which consists of _n_ vertices. Each vertex is a reservoir which can be either empty or filled with water. The vertices of the tree are numbered from 1 to _n_ with the root at vertex 1. For each vertex, the reservoirs of its children are located below the reservoir of this vertex, and the vertex is connected with each of the children by a pipe through which water can flow downwards. Mike wants to do the following operations with the tree: 1. Fill vertex _v_ with water. Then _v_ and all its children are filled with water. 2. Empty vertex _v_. Then _v_ and all its ancestors are emptied. 3. Determine whether vertex _v_ is filled with water at the moment. Initially all vertices of the tree are empty. Mike has already compiled a full list of operations that he wants to perform in order. Before experimenting with the tree Mike decided to run the list through a simulation. Help Mike determine what results will he get after performing all the operations. Input The first line of the input contains an integer _n_ (1u2009β€u2009_n_u2009β€u2009500000) β the number of vertices in the tree. Each of the following _n_u2009-u20091 lines contains two space-separated numbers _a__i_, _b__i_ (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_, _a__i_u2009β u2009_b__i_) β the edges of the tree. The next line contains a number _q_ (1u2009β€u2009_q_u2009β€u2009500000) β the number of operations to perform. Each of the following _q_ lines contains two space-separated numbers _c__i_ (1u2009β€u2009_c__i_u2009β€u20093), _v__i_ (1u2009β€u2009_v__i_u2009β€u2009_n_), where _c__i_ is the operation type (according to the numbering given in the statement), and _v__i_ is the vertex on which the operation is performed. It is guaranteed that the given graph is a tree. Output For each type 3 operation print 1 on a separate line if the vertex is full, and 0 if the vertex is empty. Print the answers to queries in the order in which the queries are given in the input. Examples Input 5 1 2 5 1 2 3 4 2 12 1 1 2 3 3 1 3 2 3 3 3 4 1 2 2 4 3 1 3 3 3 4 3 5 | 2,100 | false | false | false | false | true | false | false | false | false | true | 8,460 |
1993C | There is an apartment consisting of $$$n$$$ rooms, each with its light initially turned off. To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chips are installed at different times. Specifically, these times are represented by the array $$$a_1, a_2, ldots, a_n$$$, where $$$a_i$$$ is the time (in minutes) at which a chip is installed in the $$$i$$$-th room. As soon as a chip is installed, it changes the room's light status every $$$k$$$ minutesxa0β it turns on the light for $$$k$$$ minutes, then turns it off for the next $$$k$$$ minutes, then turns it back on for the next $$$k$$$ minutes, and so on. In other words, the light status is changed by the chip at minute $$$a_i$$$, $$$a_i + k$$$, $$$a_i + 2k$$$, $$$a_i + 3k$$$, $$$ldots$$$ for the $$$i$$$-th room. What is the earliest moment when all rooms in the apartment have their lights turned on? 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 k le n le 2 cdot 10^5$$$)xa0β the number of rooms in the apartment and the period of the chips. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β the moments when the chips are installed. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, print a single integerxa0β the answer to the question (in minutes). If there is no such moment that the lights are turned on in all the rooms, print $$$-1$$$ instead. Example Input 9 4 4 2 3 4 5 4 3 2 3 4 5 4 3 3 4 8 9 3 3 6 2 1 1 1 1 7 5 14 34 6 25 46 7 17 6 5 40 80 99 60 90 50 6 5 64 40 50 68 70 10 2 1 1 1000000000 Output 5 -1 10 8 1 47 100 -1 -1 Note In the first test case, all lights will be on by the minute $$$5$$$ without any of them being turned off by the chips. The answer is $$$5$$$. In the second test case, due to $$$k=3$$$, the $$$1$$$-st light will be on at minutes $$$2, 3, 4, 8, 9, 10, 14, ldots$$$; meanwhile, the $$$4$$$-th light will be on at minutes $$$5, 6, 7, 11, 12, 13, 17, ldots$$$. These two sequences don't have any number in common, so they will never be on at the same time. In the third test case, it can be seen that the $$$1$$$-st and $$$2$$$-nd lights will be turned off at minutes $$$6$$$ and $$$7$$$, but the chips will turn them back on at minutes $$$9$$$ and $$$10$$$. The $$$3$$$-rd and $$$4$$$-th lights will also be on at minute $$$10$$$, so the answer is $$$10$$$. | 1,400 | true | false | true | false | false | false | false | false | false | false | 310 |
1579D | An important meeting is to be held and there are exactly $$$n$$$ people invited. At any moment, any two people can step back and talk in private. The same two people can talk several (as many as they want) times per meeting. Each person has limited sociability. The sociability of the $$$i$$$-th person is a non-negative integer $$$a_i$$$. This means that after exactly $$$a_i$$$ talks this person leaves the meeting (and does not talk to anyone else anymore). If $$$a_i = 0$$$, the $$$i$$$-th person leaves the meeting immediately after it starts. A meeting is considered most productive if the maximum possible number of talks took place during it. You are given an array of sociability $$$a$$$, determine which people should talk to each other so that the total number of talks is as large as possible. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. The next $$$2t$$$ lines contain descriptions of the test cases. The first line of each test case description contains an integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0βthe number of people in the meeting. The second line consists of $$$n$$$ space-separated integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 2 cdot 10^5$$$)xa0β the sociability parameters of all people. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. It is also guaranteed that the sum of all $$$a_i$$$ (over all test cases and all $$$i$$$) does not exceed $$$2 cdot 10^5$$$. Output Print $$$t$$$ answers to all test cases. On the first line of each answer print the number $$$k$$$xa0β the maximum number of talks possible in a meeting. On each of the next $$$k$$$ lines print two integers $$$i$$$ and $$$j$$$ ($$$1 le i, j le n$$$ and $$$i eq j$$$)xa0β the numbers of people who will have another talk. If there are several possible answers, you may print any of them. Example Input 8 2 2 3 3 1 2 3 4 1 2 3 4 3 0 0 2 2 6 2 3 0 0 2 5 8 2 0 1 1 5 0 1 0 0 6 Output 2 1 2 1 2 3 1 3 2 3 2 3 5 1 3 2 4 2 4 3 4 3 4 0 2 1 2 1 2 0 4 1 2 1 5 1 4 1 2 1 5 2 | 1,400 | false | true | false | false | false | true | false | false | false | true | 2,738 |
855C | Harry, Ron and Hermione have figured out that Helga Hufflepuff's cup is a horcrux. Through her encounter with Bellatrix Lestrange, Hermione came to know that the cup is present in Bellatrix's family vault in Gringott's Wizarding Bank. The Wizarding bank is in the form of a tree with total _n_ vaults where each vault has some type, denoted by a number between 1 to _m_. A tree is an undirected connected graph with no cycles. The vaults with the highest security are of type _k_, and all vaults of type _k_ have the highest security. There can be at most _x_ vaults of highest security. Also, if a vault is of the highest security, its adjacent vaults are guaranteed to not be of the highest security and their type is guaranteed to be less than _k_. Harry wants to consider every possibility so that he can easily find the best path to reach Bellatrix's vault. So, you have to tell him, given the tree structure of Gringotts, the number of possible ways of giving each vault a type such that the above conditions hold. Input The first line of input contains two space separated integers, _n_ and _m_xa0β the number of vaults and the number of different vault types possible. (1u2009β€u2009_n_u2009β€u2009105,u20091u2009β€u2009_m_u2009β€u2009109). Each of the next _n_u2009-u20091 lines contain two space separated integers _u__i_ and _v__i_ (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_) representing the _i_-th edge, which shows there is a path between the two vaults _u__i_ and _v__i_. It is guaranteed that the given graph is a tree. The last line of input contains two integers _k_ and _x_ (1u2009β€u2009_k_u2009β€u2009_m_,u20091u2009β€u2009_x_u2009β€u200910), the type of the highest security vault and the maximum possible number of vaults of highest security. Output Output a single integer, the number of ways of giving each vault a type following the conditions modulo 109u2009+u20097. Note In test case 1, we cannot have any vault of the highest security as its type is 1 implying that its adjacent vaults would have to have a vault type less than 1, which is not allowed. Thus, there is only one possible combination, in which all the vaults have type 2. | 2,000 | false | false | false | true | false | false | false | false | false | false | 6,325 |
361A | Problem - 361A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms implementation *800 No tag edit access β Contest materials . Output Print any beautiful table. Levko doesn't like too big numbers, so all elements of the table mustn't exceed 1000 in their absolute value. If there are multiple suitable tables, you are allowed to print any of them. Examples Input 2 4 Output 1 3 3 1 Input 4 7 Output 2 1 0 4 4 0 2 1 1 3 3 0 0 3 2 2 Note In the first sample the sum in the first row is 1u2009+u20093u2009=u20094, in the second row β 3u2009+u20091u2009=u20094, in the first column β 1u2009+u20093u2009=u20094 and in the second column β 3u2009+u20091u2009=u20094. There are other beautiful tables for this sample. In the second sample the sum of elements in each row and each column equals 7. Besides, there are other tables that meet the statement requirements. | 800 | false | false | true | false | false | true | false | false | false | false | 8,389 |
1930D1 | This is the easy version of the problem. The only difference between the two versions is the constraint on $$$t$$$ and $$$n$$$. You can make hacks only if both versions of the problem are solved. For a binary$$$^dagger$$$ pattern $$$p$$$ and a binary string $$$q$$$, both of length $$$m$$$, $$$q$$$ is called $$$p$$$-good if for every $$$i$$$ ($$$1 leq i leq m$$$), there exist indices $$$l$$$ and $$$r$$$ such that: $$$1 leq l leq i leq r leq m$$$, and $$$p_i$$$ is a mode$$$^ddagger$$$ of the string $$$q_l q_{l+1} ldots q_{r}$$$. For a pattern $$$p$$$, let $$$f(p)$$$ be the minimum possible number of $$$mathtt{1}$$$s in a $$$p$$$-good binary string (of the same length as the pattern). You are given a binary string $$$s$$$ of size $$$n$$$. Find $$$$$$sum_{i=1}^{n} sum_{j=i}^{n} f(s_i s_{i+1} ldots s_j).$$$$$$ In other words, you need to sum the values of $$$f$$$ over all $$$frac{n(n+1)}{2}$$$ substrings of $$$s$$$. $$$^dagger$$$ A binary pattern is a string that only consists of characters $$$mathtt{0}$$$ and $$$mathtt{1}$$$. $$$^ddagger$$$ Character $$$c$$$ is a mode of string $$$t$$$ of length $$$m$$$ if the number of occurrences of $$$c$$$ in $$$t$$$ is at least $$$lceil frac{m}{2} ceil$$$. For example, $$$mathtt{0}$$$ is a mode of $$$mathtt{010}$$$, $$$mathtt{1}$$$ is not a mode of $$$mathtt{010}$$$, and both $$$mathtt{0}$$$ and $$$mathtt{1}$$$ are modes of $$$mathtt{011010}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 500$$$)xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the length of the binary string $$$s$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$ consisting of only characters $$$mathtt{0}$$$ and $$$mathtt{1}$$$. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^4$$$. Output For each test case, output the sum of values of $$$f$$$ over all substrings of $$$s$$$. Example Input 4 1 1 2 10 5 00000 20 11110110000000111111 Note In the first test case, the only $$$mathtt{1}$$$-good string is $$$mathtt{1}$$$. Thus, $$$f(mathtt{1})=1$$$. In the second test case, $$$f(mathtt{10})=1$$$ because $$$mathtt{01}$$$ is $$$mathtt{10}$$$-good, and $$$mathtt{00}$$$ is not $$$mathtt{10}$$$-good. Thus, the answer is $$$f(mathtt{1})+f(mathtt{10})+f(mathtt{0}) = 1 + 1 + 0 = 2$$$. In the third test case, $$$f$$$ equals to $$$0$$$ for all $$$1 leq i leq j leq 5$$$. Thus, the answer is $$$0$$$. | 1,800 | false | true | false | true | false | false | true | false | false | false | 708 |
1801G | Philip is very fond of tasks on the lines. He had already solved all the problems known to him, but this was not enough for him. Therefore, Philip decided to come up with his own task. To do this, he took the string $$$t$$$ and a set of $$$n$$$ strings $$$s_1$$$, $$$s_2$$$, $$$s_3$$$, ..., $$$s_n$$$. Philip has $$$m$$$ queries, in the $$$i$$$th of them, Philip wants to take a substring of the string $$$t$$$ from $$$l_i$$$th to $$$r_i$$$th character, and count the number of its substrings that match some string from the set. More formally, Philip wants to count the number of pairs of positions $$$a$$$, $$$b$$$, such that $$$l_i le a le b le r_i$$$, and the substring of the string $$$t$$$ from $$$a$$$th to $$$b$$$th character coincides with some string $$$s_j$$$ from the set. A substring of the string $$$t$$$ from $$$a$$$th to $$$b$$$th character is a string obtained from $$$t$$$ by removing the $$$a - 1$$$ character from the beginning and $$$t - b$$$ characters from the end, where $$$t$$$ denotes the length of the string $$$t$$$. Philip has already solved this problem, but can you? Input The first line contains two positive integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 500,000$$$)xa0β the number of rows in the set and the number of queries. The second line contains a single string $$$t$$$ consisting of lowercase letters of the English alphabet ($$$1 le t le 5 cdot 10^6$$$). The following $$$n$$$ lines describe the strings from the set. In the $$$i$$$th of them, a single string $$$s_i$$$ is given, consisting of lowercase letters of the English alphabet. Denote by $$$S$$$ the total length of all strings from the set. It is guaranteed that $$$S le 10^6$$$, as well as that all strings of $$$s_i$$$ are different. In the following lines, queries are entered. The $$$i$$$th of them contains two positive integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le t$$$)xa0β the left and right border of the substring $$$t$$$ from the $$$i$$$-th query. Output In a single line, print $$$m$$$ integers, $$$i$$$th of them should be equal to the answers to the $$$i$$$th query. Examples Input 3 5 abacaba aba a ac 1 7 1 3 2 7 2 5 4 5 Input 4 4 abcdca ab ca bcd openolympiad 1 5 2 2 2 6 1 6 Note In the first example, the first query requires the entire string to count the number of substrings that are included in the set. The substrings $$$[1, 3]$$$ and $$$[4, 6]$$$ coincide with the string "aba". The substrings match with the string "a" $$$[1, 1]$$$, $$$[3, 3]$$$, $$$[5, 5]$$$, $$$[7, 7]$$$. The substring $$$[3, 4]$$$ matches the string "ac". In total, it turns out that 7 substrings of the string "abacaba" match the strings from the set. In the second query, a substring from position 1 to position 3 is taken from the source string, this is the string "aba". The string "aba" enters it 1 time, the string "a" enters it 2 times and the string "ac" does not enter it once as a substring. In the third query, a substring from the 2nd to the 7th position is taken from the source string, this is the string "bacaba". The string "aba" is included in it 1 time, the string "a" is included 3 times and the string "ac" is included 1 time as a substring. | 3,400 | false | false | false | false | true | false | false | false | false | false | 1,460 |
883C | Only _T_ milliseconds left before the start of well-known online programming contest Codehorses Round 2017. Polycarp needs to download B++ compiler to take part in the contest. The size of the file is _f_ bytes. Polycarp's internet tariff allows to download data at the rate of one byte per _t_0 milliseconds. This tariff is already prepaid, and its use does not incur any expense for Polycarp. In addition, the Internet service provider offers two additional packages: download _a_1 bytes at the rate of one byte per _t_1 milliseconds, paying _p_1 burles for the package; download _a_2 bytes at the rate of one byte per _t_2 milliseconds, paying _p_2 burles for the package. Polycarp can buy any package many times. When buying a package, its price (_p_1 or _p_2) is prepaid before usage. Once a package is bought it replaces the regular tariff until package data limit is completely used. After a package is consumed Polycarp can immediately buy a new package or switch to the regular tariff without loosing any time. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff. Find the minimum amount of money Polycarp has to spend to download an _f_ bytes file no more than in _T_ milliseconds. Note that because of technical reasons Polycarp can download only integer number of bytes using regular tariff and both packages. I.e. in each of three downloading modes the number of downloaded bytes will be integer. It means that Polycarp can't download a byte partially using the regular tariff or/and both packages. Input The first line contains three integer numbers _f_, _T_ and _t_0 (1u2009β€u2009_f_,u2009_T_,u2009_t_0u2009β€u2009107) β size of the file to download (in bytes), maximal time to download the file (in milliseconds) and number of milliseconds to download one byte using the regular internet tariff. The second line contains a description of the first additional package. The line contains three integer numbers _a_1, _t_1 and _p_1 (1u2009β€u2009_a_1,u2009_t_1,u2009_p_1u2009β€u2009107), where _a_1 is maximal sizes of downloaded data (in bytes), _t_1 is time to download one byte (in milliseconds), _p_1 is price of the package (in burles). The third line contains a description of the second additional package. The line contains three integer numbers _a_2, _t_2 and _p_2 (1u2009β€u2009_a_2,u2009_t_2,u2009_p_2u2009β€u2009107), where _a_2 is maximal sizes of downloaded data (in bytes), _t_2 is time to download one byte (in milliseconds), _p_2 is price of the package (in burles). Polycarp can buy any package many times. Once package is bought it replaces the regular tariff until package data limit is completely used. While a package is in use Polycarp can't buy another package or switch back to the regular internet tariff. Output Print the minimum amount of money that Polycarp needs to pay to download B++ compiler no more than in _T_ milliseconds. If there is no solution, print the only integer -1. Examples Input 120 964 20 26 8 8 13 10 4 Input 8 81 11 4 10 16 3 10 12 Input 8 79 11 4 10 16 3 10 12 Note In the first example Polycarp has to buy the first additional package 5 times and do not buy the second additional package. He downloads 120 bytes (of total 26Β·5u2009=u2009130 bytes) in 120Β·8u2009=u2009960 milliseconds (960u2009β€u2009964). He spends 8Β·5u2009=u200940 burles on it. In the second example Polycarp has enough time to download 10 bytes. It takes 10Β·20u2009=u2009200 milliseconds which equals to upper constraint on download time. In the third example Polycarp has to buy one first additional package and one second additional package. In the fourth example Polycarp has no way to download the file on time. | 2,300 | false | false | true | false | false | false | false | true | false | false | 6,225 |
1227E | The Berland Forest can be represented as an infinite cell plane. Every cell contains a tree. That is, contained before the recent events. A destructive fire raged through the Forest, and several trees were damaged by it. Precisely speaking, you have a $$$n imes m$$$ rectangle map which represents the damaged part of the Forest. The damaged trees were marked as "X" while the remaining ones were marked as ".". You are sure that all burnt trees are shown on the map. All the trees outside the map are undamaged. The firemen quickly extinguished the fire, and now they are investigating the cause of it. The main version is that there was an arson: at some moment of time (let's consider it as $$$0$$$) some trees were set on fire. At the beginning of minute $$$0$$$, only the trees that were set on fire initially were burning. At the end of each minute, the fire spread from every burning tree to each of $$$8$$$ neighboring trees. At the beginning of minute $$$T$$$, the fire was extinguished. The firemen want to find the arsonists as quickly as possible. The problem is, they know neither the value of $$$T$$$ (how long the fire has been raging) nor the coordinates of the trees that were initially set on fire. They want you to find the maximum value of $$$T$$$ (to know how far could the arsonists escape) and a possible set of trees that could be initially set on fire. Note that you'd like to maximize value $$$T$$$ but the set of trees can be arbitrary. Input The first line contains two integer $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^6$$$, $$$1 le n cdot m le 10^6$$$) β the sizes of the map. Next $$$n$$$ lines contain the map. The $$$i$$$-th line corresponds to the $$$i$$$-th row of the map and contains $$$m$$$-character string. The $$$j$$$-th character of the $$$i$$$-th string is "X" if the corresponding tree is burnt and "." otherwise. It's guaranteed that the map contains at least one "X". Output In the first line print the single integer $$$T$$$ β the maximum time the Forest was on fire. In the next $$$n$$$ lines print the certificate: the map ($$$n imes m$$$ rectangle) where the trees that were set on fire are marked as "X" and all other trees are marked as ".". Examples Input 3 6 XXXXXX XXXXXX XXXXXX Output 1 ...... .X.XX. ...... Input 10 10 .XXXXXX... .XXXXXX... .XXXXXX... .XXXXXX... .XXXXXXXX. ...XXXXXX. ...XXXXXX. ...XXXXXX. ...XXXXXX. .......... Output 2 .......... .......... ...XX..... .......... .......... .......... .....XX... .......... .......... .......... Input 4 5 X.... ..XXX ..XXX ..XXX Output 0 X.... ..XXX ..XXX ..XXX | 2,200 | false | false | false | false | false | false | false | true | false | true | 4,539 |
368A | Sereja owns a restaurant for _n_ people. The restaurant hall has a coat rack with _n_ hooks. Each restaurant visitor can use a hook to hang his clothes on it. Using the _i_-th hook costs _a__i_ rubles. Only one person can hang clothes on one hook. Tonight Sereja expects _m_ guests in the restaurant. Naturally, each guest wants to hang his clothes on an available hook with minimum price (if there are multiple such hooks, he chooses any of them). However if the moment a guest arrives the rack has no available hooks, Sereja must pay a _d_ ruble fine to the guest. Help Sereja find out the profit in rubles (possibly negative) that he will get tonight. You can assume that before the guests arrive, all hooks on the rack are available, all guests come at different time, nobody besides the _m_ guests is visiting Sereja's restaurant tonight. Input The first line contains two integers _n_ and _d_ (1u2009β€u2009_n_,u2009_d_u2009β€u2009100). The next line contains integers _a_1, _a_2, ..., _a__n_ (1u2009β€u2009_a__i_u2009β€u2009100). The third line contains integer _m_ (1u2009β€u2009_m_u2009β€u2009100). Output In a single line print a single integer β the answer to the problem. Note In the first test both hooks will be used, so Sereja gets 1u2009+u20092u2009=u20093 rubles. In the second test both hooks will be used but Sereja pays a fine 8 times, so the answer is 3u2009-u20098u2009=u2009u2009-u20095. | 1,000 | false | false | true | false | false | false | false | false | false | false | 8,360 |
1367D | # Task On The Board Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 256 megabytes Polycarp wrote on the board a string s containing only lowercase Latin letters (β aβ-β zβ). This string is known for you and given in the input. After that, he erased some letters from the string s, and he rewrote the remaining letters in any order. As a result, he got some new string t. You have to find it with some additional information. Suppose that the string t has length m and the characters are numbered from left to right from 1 to m.You are given a sequence of m integers: b1, b 2, . . . , b m, where bi is the sum of the distances i β j from the index i to all such indices j that tj > t i (consider that β aβ<β bβ<. . . <β zβ). In other words, to calculate bi, Polycarp finds all such indices j that the index j contains a letter that is later in the alphabet than ti and sums all the values i β j.For example, if t = β abzb β, then: β’ since t1=β aβ, all other indices contain letters which are later in the alphabet, that is: b1 = 1 β 2 + 1 β 3 + 1 β 4 = 1 + 2 + 3 = 6 ; β’ since t2=β bβ, only the index j = 3 contains the letter, which is later in the alphabet, that is: b2 = 2 β 3 = 1 ; β’ since t3=β zβ, then there are no indexes j such that tj > t i, thus b3 = 0 ; β’ since t4=β bβ, only the index j = 3 contains the letter, which is later in the alphabet, that is: b4 = 4 β 3 = 1 .Thus, if t = β abzb β, then b = [6 , 1, 0, 1] .Given the string s and the array b, find any possible string t for which the following two requirements are fulfilled simultaneously: β’ t is obtained from s by erasing some letters (possibly zero) and then writing the rest in any order; β’ the array, constructed from the string t according to the rules above, equals to the array b specified in the input data. # Input The first line contains an integer q (1 β€ q β€ 100 ) x16 the number of test cases in the test. Then q test cases follow. Each test case consists of three lines: β’ the first line contains string s, which has a length from 1 to 50 and consists of lowercase English letters; β’ the second line contains positive integer m (1 β€ m β€ s), where s is the length of the string s,and m is the length of the array b; β’ the third line contains the integers b1, b 2, . . . , b m (0 β€ bi β€ 1225 ). It is guaranteed that in each test case an answer exists. Page 1 of 2 Output Output q lines: the k-th of them should contain the answer (string t) to the k-th test case. It is guaranteed that an answer to each test case exists. If there are several answers, output any. # Example standard input standard output 4abac 32 1 0 abc 10abba 31 0 1 ecoosdcefr 10 38 13 24 14 11 5 3 24 17 0 aac baba codeforces # Note In the first test case, such strings t are suitable: β aac β, β aab β. In the second test case, such trings t are suitable: β aβ, β bβ, β cβ. In the third test case, only the string t equals to β aba β is suitable, but the character β bβ can be from the second or third position. Page 2 of 2 | 1,800 | false | true | true | false | false | true | false | false | true | false | 3,840 |
1031E | You are given an array $$$a$$$ of length $$$n$$$ that consists of zeros and ones. You can perform the following operation multiple times. The operation consists of two steps: 1. Choose three integers $$$1 le x < y < z le n$$$, that form an arithmetic progression ($$$y - x = z - y$$$). 2. Flip the values $$$a_x, a_y, a_z$$$ (i.e. change $$$1$$$ to $$$0$$$, change $$$0$$$ to $$$1$$$). Determine if it is possible to make all elements of the array equal to zero. If yes, print the operations that lead the the all-zero state. Your solution should not contain more than $$$(lfloor frac{n}{3} floor + 12)$$$ operations. Here $$$lfloor q floor$$$ denotes the number $$$q$$$ rounded down. We can show that it is possible to make all elements equal to zero in no more than this number of operations whenever it is possible to do so at all. Input The first line contains a single integer $$$n$$$ ($$$3 le n le 10^5$$$) β the length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 1$$$) β the elements of the array. Output Print "YES" (without quotes) if the answer exists, otherwise print "NO" (without quotes). You can print each letter in any case (upper or lower). If there is an answer, in the second line print an integer $$$m$$$ ($$$0 le m le (lfloor frac{n}{3} floor + 12)$$$) β the number of operations in your answer. After that in ($$$i + 2$$$)-th line print the $$$i$$$-th operationsxa0β the integers $$$x_i, y_i, z_i$$$. You can print them in arbitrary order. Note In the first sample the shown output corresponds to the following solution: 1 1 0 1 1 (initial state); 0 1 1 1 0 (the flipped positions are the first, the third and the fifth elements); 0 0 0 0 0 (the flipped positions are the second, the third and the fourth elements). Other answers are also possible. In this test the number of operations should not exceed $$$lfloor frac{5}{3} floor + 12 = 1 + 12 = 13$$$. In the second sample the only available operation is to flip all the elements. This way it is only possible to obtain the arrays 0 1 0 and 1 0 1, but it is impossible to make all elements equal to zero. | 2,600 | false | false | false | false | false | true | false | false | false | false | 5,544 |
1924F | This is an interactive problem! Mr. 1048576 is one of those faculty who hates wasting his time in taking class attendance. Instead of taking attendance the old-fashioned way, he decided to try out something new today. There are $$$n$$$ students in his class, having roll numbers $$$1$$$ to $$$n$$$. He knows that exactly $$$1$$$ student is absent today. In order to determine who is absent, he can ask some queries to the class. In each query, he can provide two integers $$$l$$$ and $$$r$$$ ($$$1leq lleq rleq n$$$) and all students whose roll numbers are between $$$l$$$ and $$$r$$$ (inclusive) will raise their hands. He then counts them to determine if the roll number of the absent student lies between these values. Things seemed fine until his teaching assistant noticed something β the students are dishonest! Some students whose roll numbers lie in the given range may not raise their hands, while some other students whose roll number does not lie in the given range may raise their hands. But the students don't want to raise much suspicion. So, only the following $$$4$$$ cases are possible for a particular query $$$(l,r)$$$ β 1. True Positive: $$$r-l+1$$$ students are present and $$$r-l+1$$$ students raised their hands. 2. True Negative: $$$r-l$$$ students are present and $$$r-l$$$ students raised their hands. 3. False Positive: $$$r-l$$$ students are present but $$$r-l+1$$$ students raised their hands. 4. False Negative: $$$r-l+1$$$ students are present but $$$r-l$$$ students raised their hands. In the first two cases, the students are said to be answering honestly, while in the last two cases, the students are said to be answering dishonestly. The students can mutually decide upon their strategy, not known to Mr. 1048576. Also, the students do not want to raise any suspicion and at the same time, want to create a lot of confusion. So, their strategy always meets the following two conditions β 1. The students will never answer honestly $$$3$$$ times in a row. 2. The students will never answer dishonestly $$$3$$$ times in a row. Mr. 1048576 is frustrated by this act of students. So, he is willing to mark at most $$$2$$$ students as absent (though he knows that only one is). The attendance is said to be successful if the student who is actually absent is among those two. Also, due to limited class time, he can only ask up to $$$lceillog_{1.116}{n} ceil-1$$$ queries (weird numbers but okay). Help him complete a successful attendance. Interaction First read a line containing a single integer $$$t$$$ ($$$1leq tleq 2048$$$) denoting the number of independent test cases that you must solve. For each test case, first read a line containing a single integer $$$n$$$ ($$$3leq nleq 10^5$$$). Then you may ask up to $$$lceillog_{1.116}{n} ceil-1$$$ queries. To ask a query, print a single line in the format "? l r" (without quotes) $$$(1leq lleq rleq n)$$$. Then read a single line containing a single integer $$$x$$$ ($$$r-lleq xleq r-l+1$$$) denoting the number of students who raised their hands corresponding to the query. To mark a student as absent, print a single line in the format "! a" (without quotes) $$$(1leq aleq n)$$$. Then read a single integer $$$y$$$ ($$$yin{0,1}$$$). If the student with roll number $$$a$$$ was absent, $$$y=1$$$, else, $$$y=0$$$. Note that this operation does not count as a query but you can do this operation at most $$$2$$$ times. To end a test case, print a single line in the format "#" (without quotes). Then you must continue solving the remaining test cases. If you ask more queries than allowed or ask an invalid query, you will get the Wrong answer verdict. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. After printing the answers, do not forget to output end of line and flush the output buffer. Otherwise, you will get the verdict Idleness limit exceeded. To flush the buffer, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; Read documentation for other languages. Note that the grader for this problem is adaptive meaning that the answer may change depending on your queries but will always remain consistent with the constraints and the answer to the previous queries. Input format for Hacks The test cases for this problem use both non-adaptive and adaptive graders. You can use the non-adaptive grader for making hacks. The first line of input contains a single integer $$$t$$$ ($$$1leq tleq 2048$$$). The first line of each test case contains three integers $$$g$$$, $$$n$$$ and $$$x$$$ where $$$g=1$$$ (to identify that this test case must use the non-adaptive grader), $$$n$$$ ($$$3leq nleq 10^5$$$) represents the number of students in the class and $$$x$$$ ($$$1leq xleq n$$$) represents the roll number of the student who is absent. You must ensure that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. The second line of each test case contains a single string $$$S$$$ ($$$1leqvert Svertleq 120, S_iin { exttt{T}, exttt{F}}$$$). This string represents the pattern of the truth sequence. If $$$S_{(i-1)bmod vert Svert+1}= exttt{T}$$$, the students will act honestly during the $$$i$$$-th query, otherwise they will act dishonestly. You must also ensure that there is no index $$$i$$$ such that $$$S_{(i-1)bmod vert Svert+1} = S_{ibmod vert Svert+1} = S_{(i+1)bmod vert Svert+1}$$$. Note For the first test case, the student with roll number $$$2$$$ is absent and the truth sequence (see section for hacks) is TFFTFTTF. During execution of your solution, this test case will use a non-adaptive grader. For the second test case, the student with roll number $$$4$$$ is absent, and the truth sequence is FFTFTTFT. During the execution of your solution, in this test case your program will interact with an adaptive grader. So, the actual answer might be different depending on your queries but will always remain consistent with the responses to the previous queries. | 3,500 | false | false | false | true | false | true | false | false | false | false | 741 |
1973F | For $$$k$$$ positive integers $$$x_1, x_2, ldots, x_k$$$, the value $$$gcd(x_1, x_2, ldots, x_k)$$$ is the greatest common divisor of the integers $$$x_1, x_2, ldots, x_k$$$xa0β the largest integer $$$z$$$ such that all the integers $$$x_1, x_2, ldots, x_k$$$ are divisible by $$$z$$$. You are given three arrays $$$a_1, a_2, ldots, a_n$$$, $$$b_1, b_2, ldots, b_n$$$ and $$$c_1, c_2, ldots, c_n$$$ of length $$$n$$$, containing positive integers. You also have a machine that allows you to swap $$$a_i$$$ and $$$b_i$$$ for any $$$i$$$ ($$$1 le i le n$$$). Each swap costs you $$$c_i$$$ coins. Find the maximum possible value of $$$$$$gcd(a_1, a_2, ldots, a_n) + gcd(b_1, b_2, ldots, b_n)$$$$$$ that you can get by paying in total at most $$$d$$$ coins for swapping some elements. The amount of coins you have changes a lot, so find the answer to this question for each of the $$$q$$$ possible values $$$d_1, d_2, ldots, d_q$$$. Input There are two integers on the first linexa0β the numbers $$$n$$$ and $$$q$$$ ($$$1 leq n leq 5 cdot 10^5$$$, $$$1 leq q leq 5 cdot 10^5$$$). On the second line, there are $$$n$$$ integersxa0β the numbers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^8$$$). On the third line, there are $$$n$$$ integersxa0β the numbers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq 10^8$$$). On the fourth line, there are $$$n$$$ integersxa0β the numbers $$$c_1, c_2, ldots, c_n$$$ ($$$1 leq c_i leq 10^9$$$). On the fifth line, there are $$$q$$$ integersxa0β the numbers $$$d_1, d_2, ldots, d_q$$$ ($$$0 leq d_i leq 10^{15}$$$). Output Print $$$q$$$ integersxa0β the maximum value you can get for each of the $$$q$$$ possible values $$$d$$$. Examples Input 3 4 1 2 3 4 5 6 1 1 1 0 1 2 3 Input 5 5 3 4 6 8 4 8 3 4 9 3 10 20 30 40 50 5 55 13 1000 113 Note In the first query of the first example, we are not allowed to do any swaps at all, so the answer is $$$gcd(1, 2, 3) + gcd(4, 5, 6) = 2$$$. In the second query, one of the ways to achieve the optimal value is to swap $$$a_2$$$ and $$$b_2$$$, then the answer is $$$gcd(1, 5, 3) + gcd(4, 2, 6) = 3$$$. In the second query of the second example, it's optimal to perform swaps on positions $$$1$$$ and $$$3$$$, then the answer is $$$gcd(3, 3, 6, 9, 3) + gcd(8, 4, 4, 8, 4) = 7$$$ and we have to pay $$$40$$$ coins in total. | 3,100 | false | false | true | true | false | false | true | false | false | false | 450 |
1154D | There is a robot staying at $$$X=0$$$ on the $$$Ox$$$ axis. He has to walk to $$$X=n$$$. You are controlling this robot and controlling how he goes. The robot has a battery and an accumulator with a solar panel. The $$$i$$$-th segment of the path (from $$$X=i-1$$$ to $$$X=i$$$) can be exposed to sunlight or not. The array $$$s$$$ denotes which segments are exposed to sunlight: if segment $$$i$$$ is exposed, then $$$s_i = 1$$$, otherwise $$$s_i = 0$$$. The robot has one battery of capacity $$$b$$$ and one accumulator of capacity $$$a$$$. For each segment, you should choose which type of energy storage robot will use to go to the next point (it can be either battery or accumulator). If the robot goes using the battery, the current charge of the battery is decreased by one (the robot can't use the battery if its charge is zero). And if the robot goes using the accumulator, the current charge of the accumulator is decreased by one (and the robot also can't use the accumulator if its charge is zero). If the current segment is exposed to sunlight and the robot goes through it using the battery, the charge of the accumulator increases by one (of course, its charge can't become higher than it's maximum capacity). If accumulator is used to pass some segment, its charge decreases by 1 no matter if the segment is exposed or not. You understand that it is not always possible to walk to $$$X=n$$$. You want your robot to go as far as possible. Find the maximum number of segments of distance the robot can pass if you control him optimally. Input The first line of the input contains three integers $$$n, b, a$$$ ($$$1 le n, b, a le 2 cdot 10^5$$$) β the robot's destination point, the battery capacity and the accumulator capacity, respectively. The second line of the input contains $$$n$$$ integers $$$s_1, s_2, dots, s_n$$$ ($$$0 le s_i le 1$$$), where $$$s_i$$$ is $$$1$$$ if the $$$i$$$-th segment of distance is exposed to sunlight, and $$$0$$$ otherwise. Output Print one integer β the maximum number of segments the robot can pass if you control him optimally. Note In the first example the robot can go through the first segment using the accumulator, and charge levels become $$$b=2$$$ and $$$a=0$$$. The second segment can be passed using the battery, and charge levels become $$$b=1$$$ and $$$a=1$$$. The third segment can be passed using the accumulator, and charge levels become $$$b=1$$$ and $$$a=0$$$. The fourth segment can be passed using the battery, and charge levels become $$$b=0$$$ and $$$a=1$$$. And the fifth segment can be passed using the accumulator. In the second example the robot can go through the maximum number of segments using battery two times and accumulator one time in any order. | 1,500 | false | true | false | false | false | false | false | false | false | false | 4,940 |
1334A | Polycarp has recently created a new level in this cool new game Berlio Maker 85 and uploaded it online. Now players from all over the world can try his level. All levels in this game have two stats to them: the number of plays and the number of clears. So when a player attempts the level, the number of plays increases by $$$1$$$. If he manages to finish the level successfully then the number of clears increases by $$$1$$$ as well. Note that both of the statistics update at the same time (so if the player finishes the level successfully then the number of plays will increase at the same time as the number of clears). Polycarp is very excited about his level, so he keeps peeking at the stats to know how hard his level turns out to be. So he peeked at the stats $$$n$$$ times and wrote down $$$n$$$ pairs of integers β $$$(p_1, c_1), (p_2, c_2), dots, (p_n, c_n)$$$, where $$$p_i$$$ is the number of plays at the $$$i$$$-th moment of time and $$$c_i$$$ is the number of clears at the same moment of time. The stats are given in chronological order (i.e. the order of given pairs is exactly the same as Polycarp has written down). Between two consecutive moments of time Polycarp peeked at the stats many players (but possibly zero) could attempt the level. Finally, Polycarp wonders if he hasn't messed up any records and all the pairs are correct. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then he considers his records correct. Help him to check the correctness of his records. For your convenience you have to answer multiple independent test cases. Input The first line contains a single integer $$$T$$$ $$$(1 le T le 500)$$$ β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$) β the number of moments of time Polycarp peeked at the stats. Each of the next $$$n$$$ lines contains two integers $$$p_i$$$ and $$$c_i$$$ ($$$0 le p_i, c_i le 1000$$$) β the number of plays and the number of clears of the level at the $$$i$$$-th moment of time. Note that the stats are given in chronological order. Output For each test case print a single line. If there could exist such a sequence of plays (and clears, respectively) that the stats were exactly as Polycarp has written down, then print "YES". Otherwise, print "NO". You can print each letter in any case (upper or lower). Example Input 6 3 0 0 1 1 1 2 2 1 0 1000 3 4 10 1 15 2 10 2 15 2 1 765 432 2 4 4 4 3 5 0 0 1 0 1 0 1 0 1 0 Output NO YES NO YES NO YES Note In the first test case at the third moment of time the number of clears increased but the number of plays did not, that couldn't have happened. The second test case is a nice example of a Super Expert level. In the third test case the number of plays decreased, which is impossible. The fourth test case is probably an auto level with a single jump over the spike. In the fifth test case the number of clears decreased, which is also impossible. Nobody wanted to play the sixth test case; Polycarp's mom attempted it to make him feel better, however, she couldn't clear it. | 1,200 | true | false | true | false | false | false | false | false | false | false | 4,037 |
1781E | You have a rectangular grid of height $$$2$$$ and width $$$10^9$$$ consisting of unit cells. There are $$$n$$$ rectangles placed on this grid, and the borders of these rectangles pass along cell borders. The $$$i$$$-th rectangle covers all cells in rows from $$$u_i$$$ to $$$d_i$$$ inclusive and columns from $$$l_i$$$ to $$$r_i$$$ inclusive ($$$1 le u_i le d_i le 2$$$; $$$1 le l_i le r_i le 10^9$$$). The initial rectangles can intersect, be nested, and coincide arbitrarily. You should either remove each rectangle, or replace it with any of its non-empty subrectangles. In the latter case, the new subrectangle must lie inside the initial rectangle, and its borders must still pass along cell borders. In particular, it is allowed for the subrectangle to be equal to the initial rectangle. After that replacement, no two (non-removed) rectangles are allowed to have common cells, and the total area covered with the new rectangles must be as large as possible. Illustration for the first test case. The initial rectangles are given at the top, the new rectangles are given at the bottom. Rectangle number $$$4$$$ is removed. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$xa0($$$1 le n le 2 cdot 10^5$$$)xa0β the number of rectangles. Each of the next $$$n$$$ lines contains four integers $$$u_i, l_i, d_i, r_i$$$xa0($$$1 le u_i le d_i le 2$$$; $$$1 le l_i le r_i le 10^9$$$)xa0β the coordinates of cells located in the top-left and the bottom-right corners of the rectangle, respectively. 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 print an integer $$$s$$$xa0β the largest possible covered by new rectangles area. Then print $$$n$$$ lines with your solution to cover this area. In the $$$i$$$-th of these lines print four integers $$$u'_i, l'_i, d'_i, r'_i$$$. If you remove the $$$i$$$-th rectangle, print $$$u'_i = l'_i = d'_i = r'_i = 0$$$. Otherwise, these numbers denote the new coordinates of the top-left and the bottom-right corners of the $$$i$$$-th rectangle, satisfying $$$u_i le u'_i le d'_i le d_i$$$; $$$l_i le l'_i le r'_i le r_i$$$. If there are multiple solutions, print any. Example Input 8 5 1 2 2 4 2 4 2 8 1 4 2 7 1 2 1 2 1 9 1 10 2 1 1 1 10 1 5 1 15 2 1 1 1 10 1 1 1 10 5 1 3 1 7 1 3 1 8 1 1 1 4 1 2 1 7 1 10 1 11 2 1 1 2 10 1 5 1 8 2 1 5 2 10 1 2 1 7 2 1 5 2 10 2 2 2 15 5 2 6 2 7 1 4 2 5 1 5 1 9 1 7 2 10 1 2 1 6 Output 15 1 2 2 4 2 5 2 8 1 5 1 7 0 0 0 0 1 9 1 10 15 1 1 1 10 1 11 1 15 10 1 1 1 10 0 0 0 0 10 0 0 0 0 1 8 1 8 1 1 1 4 1 5 1 7 1 10 1 11 20 1 1 2 10 0 0 0 0 15 1 5 2 10 1 2 1 4 20 1 5 1 10 2 2 2 15 16 2 6 2 6 2 4 2 5 0 0 0 0 1 7 2 10 1 2 1 6 Note The picture in the statement illustrates the first test case. | 2,300 | false | true | true | false | true | false | true | true | false | false | 1,584 |
1905A | Gridlandia has been hit by flooding and now has to reconstruct all of it's cities. Gridlandia can be described by an $$$n imes m$$$ matrix. Initially, all of its cities are in economic collapse. The government can choose to rebuild certain cities. Additionally, any collapsed city which has at least one vertically neighboring rebuilt city and at least one horizontally neighboring rebuilt city can ask for aid from them and become rebuilt without help from the government. More formally, collapsed city positioned in $$$(i, j)$$$ can become rebuilt if both of the following conditions are satisfied: At least one of cities with positions $$$(i + 1, j)$$$ and $$$(i - 1, j)$$$ is rebuilt; At least one of cities with positions $$$(i, j + 1)$$$ and $$$(i, j - 1)$$$ is rebuilt. If the city is located on the border of the matrix and has only one horizontally or vertically neighbouring city, then we consider only that city. Illustration of two possible ways cities can be rebuilt by adjacent aid. White cells are collapsed cities, yellow cells are initially rebuilt cities (either by the government or adjacent aid), and orange cells are rebuilt cities after adjacent aid. The government wants to know the minimum number of cities it has to rebuild such that after some time all the cities can be rebuild. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n, m le 100$$$) β the sizes of Gridlandia. Output For each test case, output a single integer β the minimum number of cities the government needs to rebuild. Note In the first test case, it's enough for the government to rebuild cities $$$(1, 2)$$$ and $$$(2, 1)$$$. In the second test case, it's enough for the government to rebuild cities $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(3, 1)$$$, $$$(3, 6)$$$, $$$(4, 3)$$$, $$$(5, 5)$$$, $$$(5, 7)$$$. | 800 | true | false | false | false | false | true | false | false | false | false | 883 |
1716A | Problem - 1716A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags greedy math *800 No tag edit access β Contest materials . Note that the new coordinate can become negative. Your task is to find the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. Then $$$t$$$ lines describing the test cases follow. The $$$i$$$-th of these lines contains one integer $$$n$$$ ($$$1 le n le 10^9$$$) β the goal of the $$$i$$$-th test case. Output For each test case, print one integer β the minimum number of minutes required to get from the point $$$0$$$ to the point $$$n$$$ for the corresponding test case. Example Input 4 1 3 4 12 Output 2 1 2 4 | 800 | true | true | false | false | false | false | false | false | false | false | 1,987 |
1406D | You are given a sequence of $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$. You have to construct two sequences of integers $$$b$$$ and $$$c$$$ with length $$$n$$$ that satisfy: for every $$$i$$$ ($$$1leq ileq n$$$) $$$b_i+c_i=a_i$$$ $$$b$$$ is non-decreasing, which means that for every $$$1<ileq n$$$, $$$b_igeq b_{i-1}$$$ must hold $$$c$$$ is non-increasing, which means that for every $$$1<ileq n$$$, $$$c_ileq c_{i-1}$$$ must hold You have to minimize $$$max(b_i,c_i)$$$. In other words, you have to minimize the maximum number in sequences $$$b$$$ and $$$c$$$. Also there will be $$$q$$$ changes, the $$$i$$$-th change is described by three integers $$$l,r,x$$$. You should add $$$x$$$ to $$$a_l,a_{l+1}, ldots, a_r$$$. You have to find the minimum possible value of $$$max(b_i,c_i)$$$ for the initial sequence and for sequence after each change. Input The first line contains an integer $$$n$$$ ($$$1leq nleq 10^5$$$). The secound line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$1leq ileq n$$$, $$$-10^9leq a_ileq 10^9$$$). The third line contains an integer $$$q$$$ ($$$1leq qleq 10^5$$$). Each of the next $$$q$$$ lines contains three integers $$$l,r,x$$$ ($$$1leq lleq rleq n,-10^9leq xleq 10^9$$$), desribing the next change. Output Print $$$q+1$$$ lines. On the $$$i$$$-th ($$$1 leq i leq q+1$$$) line, print the answer to the problem for the sequence after $$$i-1$$$ changes. Examples Input 4 2 -1 7 3 2 2 4 -3 3 4 2 Input 6 -9 -10 -9 -6 -5 4 3 2 6 -9 1 2 -10 4 6 -3 Note In the first test: The initial sequence $$$a = (2, -1, 7, 3)$$$. Two sequences $$$b=(-3,-3,5,5),c=(5,2,2,-2)$$$ is a possible choice. After the first change $$$a = (2, -4, 4, 0)$$$. Two sequences $$$b=(-3,-3,5,5),c=(5,-1,-1,-5)$$$ is a possible choice. After the second change $$$a = (2, -4, 6, 2)$$$. Two sequences $$$b=(-4,-4,6,6),c=(6,0,0,-4)$$$ is a possible choice. | 2,200 | true | true | false | false | true | true | false | false | false | false | 3,643 |
486C | Nam is playing with a string on his computer. The string consists of _n_ lowercase English letters. It is meaningless, so Nam decided to make the string more beautiful, that is to make it be a palindrome by using 4 arrow keys: left, right, up, down. There is a cursor pointing at some symbol of the string. Suppose that cursor is at position _i_ (1u2009β€u2009_i_u2009β€u2009_n_, the string uses 1-based indexing) now. Left and right arrow keys are used to move cursor around the string. The string is cyclic, that means that when Nam presses left arrow key, the cursor will move to position _i_u2009-u20091 if _i_u2009>u20091 or to the end of the string (i. e. position _n_) otherwise. The same holds when he presses the right arrow key (if _i_u2009=u2009_n_, the cursor appears at the beginning of the string). When Nam presses up arrow key, the letter which the text cursor is pointing to will change to the next letter in English alphabet (assuming that alphabet is also cyclic, i. e. after 'z' follows 'a'). The same holds when he presses the down arrow key. Initially, the text cursor is at position _p_. Because Nam has a lot homework to do, he wants to complete this as fast as possible. Can you help him by calculating the minimum number of arrow keys presses to make the string to be a palindrome? Input The first line contains two space-separated integers _n_ (1u2009β€u2009_n_u2009β€u2009105) and _p_ (1u2009β€u2009_p_u2009β€u2009_n_), the length of Nam's string and the initial position of the text cursor. The next line contains _n_ lowercase characters of Nam's string. Note A string is a palindrome if it reads the same forward or reversed. In the sample test, initial Nam's string is: (cursor position is shown bold). In optimal solution, Nam may do 6 following steps: The result, , is now a palindrome. | 1,700 | false | true | true | false | false | false | true | false | false | false | 7,893 |
1494E | You are given a directed graph consisting of $$$n$$$ vertices. Each directed edge (or arc) labeled with a single character. Initially, the graph is empty. You should process $$$m$$$ queries with it. Each query is one of three types: "$$$+$$$ $$$u$$$ $$$v$$$ $$$c$$$"xa0β add arc from $$$u$$$ to $$$v$$$ with label $$$c$$$. It's guaranteed that there is no arc $$$(u, v)$$$ in the graph at this moment; "$$$-$$$ $$$u$$$ $$$v$$$"xa0β erase arc from $$$u$$$ to $$$v$$$. It's guaranteed that the graph contains arc $$$(u, v)$$$ at this moment; "$$$?$$$ $$$k$$$"xa0β find the sequence of $$$k$$$ vertices $$$v_1, v_2, dots, v_k$$$ such that there exist both routes $$$v_1 o v_2 o dots o v_k$$$ and $$$v_k o v_{k - 1} o dots o v_1$$$ and if you write down characters along both routes you'll get the same string. You can visit the same vertices any number of times. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$1 le m le 2 cdot 10^5$$$)xa0β the number of vertices in the graph and the number of queries. The next $$$m$$$ lines contain queriesxa0β one per line. Each query is one of three types: "$$$+$$$ $$$u$$$ $$$v$$$ $$$c$$$" ($$$1 le u, v le n$$$; $$$u eq v$$$; $$$c$$$ is a lowercase Latin letter); "$$$-$$$ $$$u$$$ $$$v$$$" ($$$1 le u, v le n$$$; $$$u eq v$$$); "$$$?$$$ $$$k$$$" ($$$2 le k le 10^5$$$). It's guaranteed that you don't add multiple edges and erase only existing edges. Also, there is at least one query of the third type. Output For each query of the third type, print YES if there exist the sequence $$$v_1, v_2, dots, v_k$$$ described above, or NO otherwise. Example Input 3 11 + 1 2 a + 2 3 b + 3 2 a + 2 1 b ? 3 ? 2 - 2 1 - 3 2 + 2 1 c + 3 2 d ? 5 Note In the first query of the third type $$$k = 3$$$, we can, for example, choose a sequence $$$[1, 2, 3]$$$, since $$$1 xrightarrow{ ext{a}} 2 xrightarrow{ ext{b}} 3$$$ and $$$3 xrightarrow{ ext{a}} 2 xrightarrow{ ext{b}} 1$$$. In the second query of the third type $$$k = 2$$$, and we can't find sequence $$$p_1, p_2$$$ such that arcs $$$(p_1, p_2)$$$ and $$$(p_2, p_1)$$$ have the same characters. In the third query of the third type, we can, for example, choose a sequence $$$[1, 2, 3, 2, 1]$$$, where $$$1 xrightarrow{ ext{a}} 2 xrightarrow{ ext{b}} 3 xrightarrow{ ext{d}} 2 xrightarrow{ ext{c}} 1$$$. | 2,400 | false | false | false | false | true | true | false | false | false | true | 3,198 |
1705B | Mark is cleaning a row of $$$n$$$ rooms. The $$$i$$$-th room has a nonnegative dust level $$$a_i$$$. He has a magical cleaning machine that can do the following three-step operation. Select two indices $$$i<j$$$ such that the dust levels $$$a_i$$$, $$$a_{i+1}$$$, $$$dots$$$, $$$a_{j-1}$$$ are all strictly greater than $$$0$$$. Set $$$a_i$$$ to $$$a_i-1$$$. Set $$$a_j$$$ to $$$a_j+1$$$. Mark's goal is to make $$$a_1 = a_2 = ldots = a_{n-1} = 0$$$ so that he can nicely sweep the $$$n$$$-th room. Determine the minimum number of operations needed to reach his goal. Input The first line contains a single integer $$$t$$$ ($$$1leq tleq 10^4$$$) β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2leq nleq 2cdot 10^5$$$) β the number of rooms. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0leq a_ileq 10^9$$$) β the dust level of each room. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, print a line containing a single integer β the minimum number of operations. It can be proven that there is a sequence of operations that meets the goal. Example Input 4 3 2 0 0 5 0 2 0 2 0 6 2 0 3 0 4 6 4 0 0 0 10 Note In the first case, one possible sequence of operations is as follows. Choose $$$i=1$$$ and $$$j=2$$$, yielding the array $$$[1,1,0]$$$. Choose $$$i=1$$$ and $$$j=3$$$, yielding the array $$$[0,1,1]$$$. Choose $$$i=2$$$ and $$$j=3$$$, yielding the array $$$[0,0,2]$$$. At this point, $$$a_1=a_2=0$$$, completing the process. In the second case, one possible sequence of operations is as follows. Choose $$$i=4$$$ and $$$j=5$$$, yielding the array $$$[0,2,0,1,1]$$$. Choose $$$i=2$$$ and $$$j=3$$$, yielding the array $$$[0,1,1,1,1]$$$. Choose $$$i=2$$$ and $$$j=5$$$, yielding the array $$$[0,0,1,1,2]$$$. Choose $$$i=3$$$ and $$$j=5$$$, yielding the array $$$[0,0,0,1,3]$$$. Choose $$$i=4$$$ and $$$j=5$$$, yielding the array $$$[0,0,0,0,4]$$$. In the last case, the array already satisfies the condition. | 900 | false | true | true | false | false | true | false | false | false | false | 2,045 |
1473C | You have a sequence $$$a$$$ with $$$n$$$ elements $$$1, 2, 3, dots, k - 1, k, k - 1, k - 2, dots, k - (n - k)$$$ ($$$k le n < 2k$$$). Let's call as inversion in $$$a$$$ a pair of indices $$$i < j$$$ such that $$$a[i] > a[j]$$$. Suppose, you have some permutation $$$p$$$ of size $$$k$$$ and you build a sequence $$$b$$$ of size $$$n$$$ in the following manner: $$$b[i] = p[a[i]]$$$. Your goal is to find such permutation $$$p$$$ that the total number of inversions in $$$b$$$ doesn't exceed the total number of inversions in $$$a$$$, and $$$b$$$ is lexicographically maximum. Small reminder: the sequence of $$$k$$$ integers is called a permutation if it contains all integers from $$$1$$$ to $$$k$$$ exactly once. Another small reminder: a sequence $$$s$$$ is lexicographically smaller than another sequence $$$t$$$, if either $$$s$$$ is a prefix of $$$t$$$, or for the first $$$i$$$ such that $$$s_i e t_i$$$, $$$s_i < t_i$$$ holds (in the first position that these sequences are different, $$$s$$$ has smaller number than $$$t$$$). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$k le n < 2k$$$; $$$1 le k le 10^5$$$)xa0β the length of the sequence $$$a$$$ and its maximum. It's guaranteed that the total sum of $$$k$$$ over test cases doesn't exceed $$$10^5$$$. Output For each test case, print $$$k$$$ integersxa0β the permutation $$$p$$$ which maximizes $$$b$$$ lexicographically without increasing the total number of inversions. It can be proven that $$$p$$$ exists and is unique. Note In the first test case, the sequence $$$a = [1]$$$, there is only one permutation $$$p = [1]$$$. In the second test case, the sequence $$$a = [1, 2]$$$. There is no inversion in $$$a$$$, so there is only one permutation $$$p = [1, 2]$$$ which doesn't increase the number of inversions. In the third test case, $$$a = [1, 2, 1]$$$ and has $$$1$$$ inversion. If we use $$$p = [2, 1]$$$, then $$$b = [p[a[1]], p[a[2]], p[a[3]]] = [2, 1, 2]$$$ and also has $$$1$$$ inversion. In the fourth test case, $$$a = [1, 2, 3, 2]$$$, and since $$$p = [1, 3, 2]$$$ then $$$b = [1, 3, 2, 3]$$$. Both $$$a$$$ and $$$b$$$ have $$$1$$$ inversion and $$$b$$$ is the lexicographically maximum. | 1,500 | true | false | false | false | false | true | false | false | false | false | 3,315 |
1404A | A bitstring is a string consisting only of the characters 0 and 1. A bitstring is called $$$k$$$-balanced if every substring of size $$$k$$$ of this bitstring has an equal amount of 0 and 1 characters ($$$frac{k}{2}$$$ of each). You are given an integer $$$k$$$ and a string $$$s$$$ which is composed only of characters 0, 1, and ?. You need to determine whether you can make a $$$k$$$-balanced bitstring by replacing every ? characters in $$$s$$$ with either 0 or 1. A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le k le n le 3 cdot 10^5$$$, $$$k$$$ is even) xa0β the length of the string and the parameter for a balanced bitstring. The next line contains the string $$$s$$$ ($$$s = n$$$). It is given that $$$s$$$ consists of only 0, 1, and ?. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, print YES if we can replace every ? in $$$s$$$ with 0 or 1 such that the resulting bitstring is $$$k$$$-balanced, or NO if it is not possible. Example Input 9 6 4 100110 3 2 1?1 3 2 1?0 4 4 ???? 7 4 1?0??1? 10 10 11??11??11 4 2 1??1 4 4 ?0?0 6 2 ????00 Output YES YES NO YES YES NO NO YES NO Note For the first test case, the string is already a $$$4$$$-balanced bitstring. For the second test case, the string can be transformed into 101. For the fourth test case, the string can be transformed into 0110. For the fifth test case, the string can be transformed into 1100110. | 1,500 | false | false | true | false | false | false | false | false | false | false | 3,653 |
1765F | Monocarp is planning on opening a chemistry lab. During the first month, he's going to distribute solutions of a certain acid. First, he will sign some contracts with a local chemistry factory. Each contract provides Monocarp with an unlimited supply of some solution of the same acid. The factory provides $$$n$$$ contract options, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th solution has a concentration of $$$x_i%$$$, the contract costs $$$w_i$$$ burles, and Monocarp will be able to sell it for $$$c_i$$$ burles per liter. Monocarp is expecting $$$k$$$ customers during the first month. Each customer will buy a liter of a $$$y%$$$-solution, where $$$y$$$ is a real number chosen uniformly at random from $$$0$$$ to $$$100$$$ independently for each customer. More formally, the probability of number $$$y$$$ being less than or equal to some $$$t$$$ is $$$P(y le t) = frac{t}{100}$$$. Monocarp can mix the solution that he signed the contracts with the factory for, at any ratio. More formally, if he has contracts for $$$m$$$ solutions with concentrations $$$x_1, x_2, dots, x_m$$$, then, for these solutions, he picks their volumes $$$a_1, a_2, dots, a_m$$$ so that $$$sum limits_{i=1}^{m} a_i = 1$$$ (exactly $$$1$$$ since each customer wants exactly one liter of a certain solution). The concentration of the resulting solution is $$$sum limits_{i=1}^{m} x_i cdot a_i$$$. The price of the resulting solution is $$$sum limits_{i=1}^{m} c_i cdot a_i$$$. If Monocarp can obtain a solution of concentration $$$y%$$$, then he will do it while maximizing its price (the cost for the customer). Otherwise, the customer leaves without buying anything, and the price is considered equal to $$$0$$$. Monocarp wants to sign some contracts with a factory (possibly, none or all of them) so that the expected profit is maximizedxa0β the expected total price of the sold solutions for all $$$k$$$ customers minus the total cost of signing the contracts from the factory. Print the maximum expected profit Monocarp can achieve. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 5000$$$; $$$1 le k le 10^5$$$)xa0β the number of contracts the factory provides and the number of customers. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$x_i, w_i$$$ and $$$c_i$$$ ($$$0 le x_i le 100$$$; $$$1 le w_i le 10^9$$$; $$$1 le c_i le 10^5$$$)xa0β the concentration of the solution, the cost of the contract and the cost per liter for the customer, for the $$$i$$$-th contract. Output Print a single real numberxa0β the maximum expected profit Monocarp can achieve. Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$. Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$frac{a - b}{max{(1, b)}} le 10^{-6}$$$. | 2,200 | false | false | false | true | false | false | false | false | false | false | 1,712 |
1839A | You are given two integers $$$n$$$ and $$$k$$$. An array $$$a_1, a_2, ldots, a_n$$$ of length $$$n$$$, consisting of zeroes and ones is good if for all integers $$$i$$$ from $$$1$$$ to $$$n$$$ both of the following conditions are satisfied: at least $$$lceil frac{i}{k} ceil$$$ of the first $$$i$$$ elements of $$$a$$$ are equal to $$$1$$$, at least $$$lceil frac{i}{k} ceil$$$ of the last $$$i$$$ elements of $$$a$$$ are equal to $$$1$$$. Here, $$$lceil frac{i}{k} ceil$$$ denotes the result of division of $$$i$$$ by $$$k$$$, rounded up. For example, $$$lceil frac{6}{3} ceil = 2$$$, $$$lceil frac{11}{5} ceil = lceil 2.2 ceil = 3$$$ and $$$lceil frac{7}{4} ceil = lceil 1.75 ceil = 2$$$. Find the minimum possible number of ones in a good array. Input Each test contains multiple test cases. 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 two integers $$$n$$$, $$$k$$$ ($$$2 le n le 100$$$, $$$1 le k le n$$$)xa0β the length of array and parameter $$$k$$$ from the statement. Output For each test case output one integerxa0β the minimum possible number of ones in a good array. It can be shown that under the given constraints at least one good array always exists. Example Input 7 3 2 5 2 9 3 7 1 10 4 9 5 8 8 Note In the first test case, $$$n = 3$$$ and $$$k = 2$$$: Array $$$[ , 1, 0, 1 , ]$$$ is good and the number of ones in it is $$$2$$$. Arrays $$$[ , 0, 0, 0 , ]$$$, $$$[ , 0, 1, 0 , ]$$$ and $$$[ , 0, 0, 1 , ]$$$ are not good since for $$$i=1$$$ the first condition from the statement is not satisfied. Array $$$[ , 1, 0, 0 , ]$$$ is not good since for $$$i=1$$$ the second condition from the statement is not satisfied. All other arrays of length $$$3$$$ contain at least $$$2$$$ ones. Thus, the answer is $$$2$$$. In the second test case, $$$n = 5$$$ and $$$k = 2$$$: Array $$$[ , 1, 1, 0, 0, 1 , ]$$$ is not good since for $$$i=3$$$ the second condition is not satisfied. Array $$$[ , 1, 0, 1, 0, 1 , ]$$$ is good and the number of ones in it is $$$3$$$. It can be shown that there is no good array with less than $$$3$$$ ones, so the answer is $$$3$$$. In the third test case, $$$n = 9$$$ and $$$k = 3$$$: Array $$$[ , 1, 0, 1, 0, 0, 0, 1, 0, 1 , ]$$$ is good and the number of ones in it is $$$4$$$. It can be shown that there is no good array with less than $$$4$$$ ones, so the answer is $$$4$$$. In the fourth test case, $$$n = 7$$$ and $$$k = 1$$$. The only good array is $$$[ , 1, 1, 1, 1, 1, 1, 1, ]$$$, so the answer is $$$7$$$. | 800 | true | true | true | false | false | false | false | false | false | false | 1,258 |
1801B | Little Sasha has two friends, whom he wants to please with gifts on the Eighth of March. To do this, he went to the largest shopping center in the city. There are $$$n$$$ departments in the mall, each of which has exactly two stores. For convenience, we number the departments with integers from $$$1$$$ to $$$n$$$. It is known that gifts in the first store of the $$$i$$$ department cost $$$a_i$$$ rubles, and in the second store of the $$$i$$$ departmentxa0β $$$b_i$$$ rubles. Entering the mall, Sasha will visit each of the $$$n$$$ departments of the mall, and in each department, he will enter exactly one store. When Sasha gets into the $$$i$$$-th department, he will perform exactly one of two actions: 1. Buy a gift for the first friend, spending $$$a_i$$$ rubles on it. 2. Buy a gift for the second friend, spending $$$b_i$$$ rubles on it. Sasha is going to buy at least one gift for each friend. Moreover, he wants to pick up gifts in such a way that the price difference of the most expensive gifts bought for friends is as small as possible so that no one is offended. More formally: let $$$m_1$$$xa0 be the maximum price of a gift bought to the first friend, and $$$m_2$$$xa0 be the maximum price of a gift bought to the second friend. Sasha wants to choose gifts in such a way as to minimize the value of $$$lvert m_1 - m_2 vert$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1,000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 500,000$$$)xa0β the number of departments in the mall. Each of the following $$$n$$$ lines of each test case contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$0 le a_i, b_i le 10^9$$$)xa0β the prices of gifts in the first and second store of the $$$i$$$ department, respectively. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500,000$$$. Output Print one integerxa0β the minimum price difference of the most expensive gifts bought to friends. Example Input 2 2 1 2 2 1 5 1 5 2 7 3 3 4 10 2 5 Note In the first test case, Sasha has two possible options: buy a gift for the first friend in the first department, and the second friend xa0β in the second department, or vice versa. In the first case, $$$m_1 = m_2 = 1$$$, and in the second casexa0β $$$m_1 = m_2 = 2$$$. In both cases, the answer is $$$0$$$. In the second test case, you can buy gifts for the first friend in the $$$2$$$, $$$4$$$ and $$$5$$$ departments, and for the second friend xa0β in the $$$1$$$ and $$$3$$$ departments.So $$$m_1 = max(2, 4, 2) = 4$$$, $$$m_2 = max(5, 3) = 5$$$. The answer is $$$lvert 4 - 5 vert = 1$$$. | 1,800 | false | true | false | false | true | false | false | false | true | false | 1,465 |
552B | Problem - 552B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation math *1200 No tag edit access β Contest materials ") Editorial ") β the number of books in the library. Output Print the number of digits needed to number all the books. Examples Input 13 Output 17 Input 4 Output 4 Note Note to the first test. The books get numbers 1,u20092,u20093,u20094,u20095,u20096,u20097,u20098,u20099,u200910,u200911,u200912,u200913, which totals to 17 digits. Note to the second sample. The books get numbers 1,u20092,u20093,u20094, which totals to 4 digits. | 1,200 | true | false | true | false | false | false | false | false | false | false | 7,626 |
1418B | You are given an array $$$a$$$, consisting of $$$n$$$ integers. Each position $$$i$$$ ($$$1 le i le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were. For example, let $$$a = [-1, 1, underline{3}, 2, underline{-2}, 1, -4, underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays: $$$[-1, 1, underline{3}, 2, underline{-2}, 1, -4, underline{0}]$$$; $$$[-4, -1, underline{3}, 2, underline{-2}, 1, 1, underline{0}]$$$; $$$[1, -1, underline{3}, 2, underline{-2}, 1, -4, underline{0}]$$$; $$$[1, 2, underline{3}, -1, underline{-2}, -4, 1, underline{0}]$$$; and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$dots$$$, $$$p_n = a_1 + a_2 + dots + a_n$$$. Let $$$k$$$ be the maximum $$$j$$$ ($$$1 le j le n$$$) such that $$$p_j < 0$$$. If there are no $$$j$$$ such that $$$p_j < 0$$$, then $$$k = 0$$$. Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible. Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0β the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0β the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^5 le a_i le 10^5$$$)xa0β the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, dots, l_n$$$ ($$$0 le l_i le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked. Output Print $$$n$$$ integersxa0β the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j < 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them. Example Input 5 3 1 3 2 0 0 0 4 2 -3 4 -1 1 1 1 1 7 -8 4 -2 -6 4 7 1 1 0 0 0 1 1 0 5 0 1 -4 6 3 0 0 0 1 1 6 -1 7 10 4 -8 -1 1 0 0 0 0 1 Output 1 2 3 2 -3 4 -1 -8 -6 1 4 4 7 -2 -4 0 1 6 3 -1 4 7 -8 10 -1 Note In the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j < 0$$$. Thus, $$$k = 0$$$. In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one. In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k < 5$$$. In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$. In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$. | 1,300 | false | true | false | false | false | false | false | false | true | false | 3,593 |
1933D | Given an array $$$a_1, a_2, ldots, a_n$$$, determine whether it is possible to rearrange its elements into $$$b_1, b_2, ldots, b_n$$$, such that $$$b_1 bmod b_2 bmod ldots bmod b_n eq 0$$$. Here $$$x bmod y$$$ denotes the remainder from dividing $$$x$$$ by $$$y$$$. Also, the modulo operations are calculated from left to right. That is, $$$x bmod y bmod z = (x bmod y) bmod z$$$. For example, $$$2024 bmod 1000 bmod 8 = (2024 bmod 1000) bmod 8 = 24 bmod 8 = 0$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 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$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if it is possible, "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 8 6 1 2 3 4 5 6 5 3 3 3 3 3 3 2 2 3 5 1 1 2 3 7 3 1 2 2 3 1 1 2 6 5 2 10 10 10 2 4 3 6 9 3 Output YES NO YES NO YES NO YES NO Note In the first test case, rearranging the array into $$$b = [1, 2, 3, 4, 5, 6]$$$ (doing nothing) would result in $$$1 bmod 2 bmod 3 bmod 4 bmod 5 bmod 6 = 1$$$. Hence it is possible to achieve the goal. In the second test case, the array $$$b$$$ must be equal to $$$[3, 3, 3, 3, 3]$$$, which would result in $$$3 bmod 3 bmod 3 bmod 3 bmod 3 = 0$$$. Hence it is impossible to achieve the goal. In the third test case, rearranging the array into $$$b = [3, 2, 2]$$$ would result in $$$3 bmod 2 bmod 2 = 1$$$. Hence it is possible to achieve the goal. | 1,200 | true | true | false | false | false | true | false | false | true | false | 684 |
699A | There will be a launch of a new, powerful and unusual collider very soon, which located along a straight line. _n_ particles will be launched inside it. All of them are located in a straight line and there can not be two or more particles located in the same point. The coordinates of the particles coincide with the distance in meters from the center of the collider, _x__i_ is the coordinate of the _i_-th particle and its position in the collider at the same time. All coordinates of particle positions are even integers. You know the direction of each particle movementxa0β it will move to the right or to the left after the collider's launch start. All particles begin to move simultaneously at the time of the collider's launch start. Each particle will move straight to the left or straight to the right with the constant speed of 1 meter per microsecond. The collider is big enough so particles can not leave it in the foreseeable time. Write the program which finds the moment of the first collision of any two particles of the collider. In other words, find the number of microseconds before the first moment when any two particles are at the same point. Input The first line contains the positive integer _n_ (1u2009β€u2009_n_u2009β€u2009200u2009000)xa0β the number of particles. The second line contains _n_ symbols "L" and "R". If the _i_-th symbol equals "L", then the _i_-th particle will move to the left, otherwise the _i_-th symbol equals "R" and the _i_-th particle will move to the right. The third line contains the sequence of pairwise distinct even integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (0u2009β€u2009_x__i_u2009β€u2009109)xa0β the coordinates of particles in the order from the left to the right. It is guaranteed that the coordinates of particles are given in the increasing order. Output In the first line print the only integerxa0β the first moment (in microseconds) when two particles are at the same point and there will be an explosion. Print the only integer -1, if the collision of particles doesn't happen. Note In the first sample case the first explosion will happen in 1 microsecond because the particles number 1 and 2 will simultaneously be at the same point with the coordinate 3. In the second sample case there will be no explosion because there are no particles which will simultaneously be at the same point. | 1,000 | false | false | true | false | false | false | false | false | false | false | 7,027 |
1753F | Little Misha goes to the programming club and solves nothing there. It may seem strange, but when you find out that Misha is filming a Minecraft series, everything will fall into place... Misha is inspired by Manhattan, so he built a city in Minecraft that can be imagined as a table of size $$$n imes m$$$. $$$k$$$ students live in a city, the $$$i$$$-th student lives in the house, located at the intersection of the $$$x_i$$$-th row and the $$$y_i$$$-th column. Also, each student has a degree of his aggressiveness $$$w_i$$$. Since the city turned out to be very large, Misha decided to territorially limit the actions of his series to some square $$$s$$$, which sides are parallel to the coordinate axes. The length of the side of the square should be an integer from $$$1$$$ to $$$min(n, m)$$$ cells. According to the plot, the main hero will come to the city and accidentally fall into the square $$$s$$$. Possessing a unique degree of aggressiveness $$$0$$$, he will be able to show his leadership qualities and assemble a team of calm, moderate and aggressive students. In order for the assembled team to be versatile and close-knit, degrees of aggressiveness of all students of the team must be pairwise distinct and must form a single segment of consecutive integers. Formally, if there exist students with degrees of aggressiveness $$$l, l+1, ldots, -1, 1, ldots, r-1, r$$$ inside the square $$$s$$$, where $$$l le 0 le r$$$, the main hero will be able to form a team of $$$r-l+1$$$ people (of course, he is included in this team). Notice, that it is not required to take all students from square $$$s$$$ to the team. Misha thinks that the team should consist of at least $$$t$$$ people. That is why he is interested, how many squares are there in the table in which the main hero will be able to form a team of at least $$$t$$$ people. Help him to calculate this. Input The first line contains four integers $$$n$$$, $$$m$$$, $$$k$$$ and $$$t$$$ ($$$1 le n, m le 40,000$$$, $$$1 le n cdot m le 40,000$$$, $$$1 le k le 10^6$$$, $$$1 le t le k + 1$$$)xa0β the number of rows and columns in the table, and the number of students living in the city, respectively. Each of the following $$$k$$$ lines contains three integers $$$x_i$$$, $$$y_i$$$ and $$$w_i$$$ ($$$1 le x_i le n$$$, $$$1 le y_i le m$$$, $$$1 le lvert w_i vert le 10^9$$$)xa0β the number of row and column, where the $$$i$$$-th student is living, and the degree of his aggressiveness. | 3,500 | false | false | false | false | false | false | true | false | false | false | 1,771 |
1675D | You are given a rooted tree consisting of $$$n$$$ vertices. Vertices are numbered from $$$1$$$ to $$$n$$$. Any vertex can be the root of a tree. A tree is a connected undirected graph without cycles. A rooted tree is a tree with a selected vertex, which is called the root. The tree is specified by an array of parents $$$p$$$ containing $$$n$$$ numbers: $$$p_i$$$ is a parent of the vertex with the index $$$i$$$. The parent of a vertex $$$u$$$ is a vertex that is the next vertex on the shortest path from $$$u$$$ to the root. For example, on the simple path from $$$5$$$ to $$$3$$$ (the root), the next vertex would be $$$1$$$, so the parent of $$$5$$$ is $$$1$$$. The root has no parent, so for it, the value of $$$p_i$$$ is $$$i$$$ (the root is the only vertex for which $$$p_i=i$$$). Find such a set of paths that: each vertex belongs to exactly one path, each path can contain one or more vertices; in each path each next vertexxa0β is a son of the current vertex (that is, paths always lead downxa0β from parent to son); number of paths is minimal. For example, if $$$n=5$$$ and $$$p=[3, 1, 3, 3, 1]$$$, then the tree can be divided into three paths: 1. $$$3 ightarrow 1 ightarrow 5$$$ (path of $$$3$$$ vertices), 2. $$$4$$$ (path of $$$1$$$ vertices). 3. $$$2$$$ (path of $$$1$$$ vertices). Example of splitting a root tree into three paths for $$$n=5$$$, the root of the treexa0β node $$$3$$$. Input The first line of input data contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases in the test. Each test case consists of two lines. The first of them contains an integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$). It is the number of vertices in the tree. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$). It is guaranteed that the $$$p$$$ array encodes some rooted tree. It is guaranteed that the sum of the values $$$n$$$ over all test cases in the test does not exceed $$$2 cdot 10^5$$$. Output For each test case on the first line, output an integer $$$m$$$ β the minimum number of non-intersecting leading down paths that can cover all vertices of the tree. Then print $$$m$$$ pairs of lines containing path descriptions. In the first of them print the length of the path, in the second β the sequence of vertices specifying that path in the order from top to bottom. You can output the paths in any order. If there are several answers, output any of them. | 1,300 | false | false | true | false | false | false | false | false | false | true | 2,219 |
898E | Ann and Borya have _n_ piles with candies and _n_ is even number. There are _a__i_ candies in pile with number _i_. Ann likes numbers which are square of some integer and Borya doesn't like numbers which are square of any integer. During one move guys can select some pile with candies and add one candy to it (this candy is new and doesn't belong to any other pile) or remove one candy (if there is at least one candy in this pile). Find out minimal number of moves that is required to make exactly _n_u2009/u20092 piles contain number of candies that is a square of some integer and exactly _n_u2009/u20092 piles contain number of candies that is not a square of any integer. Input First line contains one even integer _n_ (2u2009β€u2009_n_u2009β€u2009200u2009000)xa0β number of piles with candies. Second line contains sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009β€u2009109)xa0β amounts of candies in each pile. Output Output minimal number of steps required to make exactly _n_u2009/u20092 piles contain number of candies that is a square of some integer and exactly _n_u2009/u20092 piles contain number of candies that is not a square of any integer. If condition is already satisfied output 0. Examples Input 6 120 110 23 34 25 45 Input 10 121 56 78 81 45 100 1 0 54 78 Note In first example you can satisfy condition in two moves. During each move you should add one candy to second pile. After it size of second pile becomes 16. After that Borya and Ann will have two piles with number of candies which is a square of integer (second and fourth pile) and two piles with number of candies which is not a square of any integer (first and third pile). In second example you should add two candies to any three piles. | 1,600 | false | true | false | false | false | true | false | false | false | false | 6,154 |
1554C | You are given two integers $$$n$$$ and $$$m$$$. Find the $$$operatorname{MEX}$$$ of the sequence $$$n oplus 0, n oplus 1, ldots, n oplus m$$$. Here, $$$oplus$$$ is the = 3$$$, and $$$operatorname{MEX}(1, 2021) = 0$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 30,000$$$) xa0β the number of test cases. The first and only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$0 le n, m le 10^9$$$). Note In the first test case, the sequence is $$$3 oplus 0, 3 oplus 1, 3 oplus 2, 3 oplus 3, 3 oplus 4, 3 oplus 5$$$, or $$$3, 2, 1, 0, 7, 6$$$. The smallest non-negative integer which isn't present in the sequence i.xa0e. the $$$operatorname{MEX}$$$ of the sequence is $$$4$$$. In the second test case, the sequence is $$$4 oplus 0, 4 oplus 1, 4 oplus 2, 4 oplus 3, 4 oplus 4, 4 oplus 5, 4 oplus 6$$$, or $$$4, 5, 6, 7, 0, 1, 2$$$. The smallest non-negative integer which isn't present in the sequence i.xa0e. the $$$operatorname{MEX}$$$ of the sequence is $$$3$$$. In the third test case, the sequence is $$$3 oplus 0, 3 oplus 1, 3 oplus 2$$$, or $$$3, 2, 1$$$. The smallest non-negative integer which isn't present in the sequence i.xa0e. the $$$operatorname{MEX}$$$ of the sequence is $$$0$$$. | 1,800 | false | true | true | false | false | false | false | true | false | false | 2,864 |
436D | Have you ever played Pudding Monsters? In this task, a simplified one-dimensional model of this game is used. Imagine an infinite checkered stripe, the cells of which are numbered sequentially with integers. Some cells of the strip have monsters, other cells of the strip are empty. All monsters are made of pudding, so if there are two monsters in the neighboring cells, they stick to each other (literally). Similarly, if several monsters are on consecutive cells, they all stick together in one block of monsters. We will call the stuck together monsters a block of monsters. A detached monster, not stuck to anyone else, is also considered a block. In one move, the player can take any block of monsters and with a movement of his hand throw it to the left or to the right. The selected monsters will slide on until they hit some other monster (or a block of monsters). For example, if a strip has three monsters in cells 1, 4 and 5, then there are only four possible moves: to send a monster in cell 1 to minus infinity, send the block of monsters in cells 4 and 5 to plus infinity, throw monster 1 to the right (it will stop in cell 3), throw a block of monsters in cells 4 and 5 to the left (they will stop in cells 2 and 3). Some cells on the strip are marked with stars. These are the special cells. The goal of the game is to make the largest possible number of special cells have monsters on them. You are given the numbers of the special cells on a strip as well as the initial position of all monsters. What is the maximum number of special cells that will contain monsters in the optimal game? Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u2009105;xa01u2009β€u2009_m_u2009β€u20092000) β the number of monsters on the strip and the number of special cells. The second line contains _n_ distinct integers β the numbers of the cells with monsters, then the third line contains _m_ distinct integers β the numbers of the special cells. It is guaranteed that all the numbers of the cells are positive integers not exceeding 2Β·105. | 2,800 | false | false | false | true | false | false | false | false | false | false | 8,094 |
2030A | While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return... The orangutan has gifted you an array $$$a$$$ of length $$$n$$$. Using $$$a$$$, you will construct two arrays $$$b$$$ and $$$c$$$, both containing $$$n$$$ elements, in the following manner: $$$b_i = min(a_1, a_2, ldots, a_i)$$$ for each $$$1 leq i leq n$$$. $$$c_i = max(a_1, a_2, ldots, a_i)$$$ for each $$$1 leq i leq n$$$. Define the score of $$$a$$$ as $$$sum_{i=1}^n c_i - b_i$$$ (i.e. the sum of $$$c_i - b_i$$$ over all $$$1 leq i leq n$$$). Before you calculate the score, you can shuffle the elements of $$$a$$$ however you want. Find the maximum score that you can get if you shuffle the elements of $$$a$$$ optimally. Input The first line contains $$$t$$$ ($$$1 leq t leq 100$$$)xa0β the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 1000$$$)xa0β the number of elements in $$$a$$$. The following line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 1000$$$)xa0β the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$1000$$$. Output For each test case, output the maximum score that you can get. Example Input 3 1 69 3 7 6 5 5 1 1 1 2 2 Note In the first test case, there is no other way to rearrange $$$a$$$. So, $$$b = [69]$$$ and $$$c = [69]$$$. The only possible score is $$$69 - 69 = 0$$$. In the second test case, you can rearrange $$$a$$$ as $$$[7, 5, 6]$$$. Here, $$$b = [7, 5, 5]$$$ and $$$c = [7, 7, 7]$$$. The score in this case is $$$(7 - 7) + (7 - 5) + (7 - 5) = 4$$$. It can be shown this is the maximum possible score. | 800 | true | true | false | false | false | true | false | false | true | false | 88 |
883I | Evlampiy has found one more cool application to process photos. However the application has certain limitations. Each photo _i_ has a contrast _v__i_. In order for the processing to be truly of high quality, the application must receive at least _k_ photos with contrasts which differ as little as possible. Evlampiy already knows the contrast _v__i_ for each of his _n_ photos. Now he wants to split the photos into groups, so that each group contains at least _k_ photos. As a result, each photo must belong to exactly one group. He considers a processing time of the _j_-th group to be the difference between the maximum and minimum values of _v__i_ in the group. Because of multithreading the processing time of a division into groups is the maximum processing time among all groups. Split _n_ photos into groups in a such way that the processing time of the division is the minimum possible, i.e. that the the maximum processing time over all groups as least as possible. Input The first line contains two integers _n_ and _k_ (1u2009β€u2009_k_u2009β€u2009_n_u2009β€u20093Β·105) β number of photos and minimum size of a group. The second line contains _n_ integers _v_1,u2009_v_2,u2009...,u2009_v__n_ (1u2009β€u2009_v__i_u2009β€u2009109), where _v__i_ is the contrast of the _i_-th photo. Output Print the minimal processing time of the division into groups. Examples Input 5 2 50 110 130 40 120 Note In the first example the photos should be split into 2 groups: [40,u200950] and [110,u2009120,u2009130]. The processing time of the first group is 10, and the processing time of the second group is 20. Maximum among 10 and 20 is 20. It is impossible to split the photos into groups in a such way that the processing time of division is less than 20. In the second example the photos should be split into four groups, each containing one photo. So the minimal possible processing time of a division is 0. | 1,900 | false | false | false | true | false | false | false | true | false | false | 6,219 |
518A | Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time. During the last lesson the teacher has provided two strings _s_ and _t_ to Vitaly. The strings have the same length, they consist of lowercase English letters, string _s_ is lexicographically smaller than string _t_. Vitaly wondered if there is such string that is lexicographically larger than string _s_ and at the same is lexicographically smaller than string _t_. This string should also consist of lowercase English letters and have the length equal to the lengths of strings _s_ and _t_. Let's help Vitaly solve this easy problem! Input The first line contains string _s_ (1u2009β€u2009_s_u2009β€u2009100), consisting of lowercase English letters. Here, _s_ denotes the length of the string. The second line contains string _t_ (_t_u2009=u2009_s_), consisting of lowercase English letters. It is guaranteed that the lengths of strings _s_ and _t_ are the same and string _s_ is lexicographically less than string _t_. Output If the string that meets the given requirements doesn't exist, print a single string "No such string" (without the quotes). If such string exists, print it. If there are multiple valid strings, you may print any of them. Note String _s_u2009=u2009_s_1_s_2... _s__n_ is said to be lexicographically smaller than _t_u2009=u2009_t_1_t_2... _t__n_, if there exists such _i_, that _s_1u2009=u2009_t_1,u2009_s_2u2009=u2009_t_2,u2009... _s__i_u2009-u20091u2009=u2009_t__i_u2009-u20091,u2009_s__i_u2009<u2009_t__i_. | 1,600 | false | false | false | false | false | true | false | false | false | false | 7,768 |
1637D | You are given two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$. You can perform the following operation any number of times (possibly zero): select an index $$$i$$$ ($$$1 leq i leq n$$$) and swap $$$a_i$$$ and $$$b_i$$$. Let's define the cost of the array $$$a$$$ as $$$sum_{i=1}^{n} sum_{j=i + 1}^{n} (a_i + a_j)^2$$$. Similarly, the cost of the array $$$b$$$ is $$$sum_{i=1}^{n} sum_{j=i + 1}^{n} (b_i + b_j)^2$$$. Your task is to minimize the total cost of two arrays. Input Each test case consists of several test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 40$$$)xa0β the number of test cases. The following is a description of the input data sets. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0β the length of both arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 100$$$)xa0β elements of the first array. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 leq b_i leq 100$$$)xa0β elements of the second array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$. Output For each test case, print the minimum possible total cost. Example Input 3 1 3 6 4 3 6 6 6 2 7 4 1 4 6 7 2 4 2 5 3 5 Note In the second test case, in one of the optimal answers after all operations $$$a = [2, 6, 4, 6]$$$, $$$b = [3, 7, 6, 1]$$$. The cost of the array $$$a$$$ equals to $$$(2 + 6)^2 + (2 + 4)^2 + (2 + 6)^2 + (6 + 4)^2 + (6 + 6)^2 + (4 + 6)^2 = 508$$$. The cost of the array $$$b$$$ equals to $$$(3 + 7)^2 + (3 + 6)^2 + (3 + 1)^2 + (7 + 6)^2 + (7 + 1)^2 + (6 + 1)^2 = 479$$$. The total cost of two arrays equals to $$$508 + 479 = 987$$$. | 1,800 | true | true | false | true | false | false | false | false | false | false | 2,431 |
257E | The _m_-floor (_m_u2009>u20091) office of international corporation CodeForces has the advanced elevator control system established. It works as follows. All office floors are sequentially numbered with integers from 1 to _m_. At time _t_u2009=u20090, the elevator is on the first floor, the elevator is empty and nobody is waiting for the elevator on other floors. Next, at times _t__i_ (_t__i_u2009>u20090) people come to the elevator. For simplicity, we assume that one person uses the elevator only once during the reported interval. For every person we know three parameters: the time at which the person comes to the elevator, the floor on which the person is initially, and the floor to which he wants to go. The movement of the elevator between the floors is as follows. At time _t_ (_t_u2009β₯u20090, _t_ is an integer) the elevator is always at some floor. First the elevator releases all people who are in the elevator and want to get to the current floor. Then it lets in all the people waiting for the elevator on this floor. If a person comes to the elevator exactly at time _t_, then he has enough time to get into it. We can assume that all of these actions (going in or out from the elevator) are made instantly. After that the elevator decides, which way to move and at time (_t_u2009+u20091) the elevator gets to the selected floor. The elevator selects the direction of moving by the following algorithm. If the elevator is empty and at the current time no one is waiting for the elevator on any floor, then the elevator remains at the current floor. Otherwise, let's assume that the elevator is on the floor number _x_ (1u2009β€u2009_x_u2009β€u2009_m_). Then elevator calculates the directions' "priorities" _p__up_ and _p__down_: _p__up_ is the sum of the number of people waiting for the elevator on the floors with numbers greater than _x_, and the number of people in the elevator, who want to get to the floors with the numbers greater than _x_; _p__down_ is the sum of the number of people waiting for the elevator on the floors with numbers less than _x_, and the number of people in the elevator, who want to get to the floors with the numbers less than _x_. If _p__up_u2009β₯u2009_p__down_, then the elevator goes one floor above the current one (that is, from floor _x_ to floor _x_u2009+u20091), otherwise the elevator goes one floor below the current one (that is, from floor _x_ to floor _x_u2009-u20091). Your task is to simulate the work of the elevator and for each person to tell the time when the elevator will get to the floor this person needs. Please note that the elevator is large enough to accommodate all the people at once. Input The first line contains two space-separated integers: _n_,u2009_m_ (1u2009β€u2009_n_u2009β€u2009105,u20092u2009β€u2009_m_u2009β€u2009105) β the number of people and floors in the building, correspondingly. Next _n_ lines each contain three space-separated integers: _t__i_,u2009_s__i_,u2009_f__i_ (1u2009β€u2009_t__i_u2009β€u2009109,u20091u2009β€u2009_s__i_,u2009_f__i_u2009β€u2009_m_,u2009_s__i_u2009β u2009_f__i_) β the time when the _i_-th person begins waiting for the elevator, the floor number, where the _i_-th person was initially located, and the number of the floor, where he wants to go. Output Print _n_ lines. In the _i_-th line print a single number β the moment of time, when the _i_-th person gets to the floor he needs. The people are numbered in the order, in which they are given in the input. Please don't use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. Note In the first sample the elevator worked as follows: _t_u2009=u20091. The elevator is on the floor number 1. The elevator is empty. The floor number 2 has one person waiting. _p__up_u2009=u20091u2009+u20090u2009=u20091,u2009_p__down_u2009=u20090u2009+u20090u2009=u20090,u2009_p__up_u2009β₯u2009_p__down_. So the elevator goes to the floor number 2. _t_u2009=u20092. The elevator is on the floor number 2. One person enters the elevator, he wants to go to the floor number 7. _p__up_u2009=u20090u2009+u20091u2009=u20091,u2009_p__down_u2009=u20090u2009+u20090u2009=u20090,u2009_p__up_u2009β₯u2009_p__down_. So the elevator goes to the floor number 3. _t_u2009=u20093. The elevator is on the floor number 3. There is one person in the elevator, he wants to go to floor 7. The floors number 4 and 6 have two people waiting for the elevator. _p__up_u2009=u20092u2009+u20091u2009=u20093,u2009_p__down_u2009=u20090u2009+u20090u2009=u20090,u2009_p__up_u2009β₯u2009_p__down_. So the elevator goes to the floor number 4. _t_u2009=u20094. The elevator is on the floor number 4. There is one person in the elevator who wants to go to the floor number 7. One person goes into the elevator, he wants to get to the floor number 8. The floor number 6 has one man waiting. _p__up_u2009=u20091u2009+u20092u2009=u20093,u2009_p__down_u2009=u20090u2009+u20090u2009=u20090,u2009_p__up_u2009β₯u2009_p__down_. So the elevator goes to the floor number 5. _t_u2009=u20095. The elevator is on the floor number 5. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. There is one person waiting for the elevator on the floor number 6. _p__up_u2009=u20091u2009+u20092u2009=u20093,u2009_p__down_u2009=u20090u2009+u20090u2009=u20090,u2009_p__up_u2009β₯u2009_p__down_. So the elevator goes to the floor number 6. _t_u2009=u20096. The elevator is on the floor number 6. There are two people in the elevator, they want to get to the floors number 7 and 8, correspondingly. One man enters the elevator, he wants to get to the floor number 5. _p__up_u2009=u20090u2009+u20092u2009=u20092,u2009_p__down_u2009=u20090u2009+u20091u2009=u20091,u2009_p__up_u2009β₯u2009_p__down_. So the elevator goes to the floor number 7. _t_u2009=u20097. The elevator is on the floor number 7. One person leaves the elevator, this person wanted to get to the floor number 7. There are two people in the elevator, they want to get to the floors with numbers 8 and 5, correspondingly. _p__up_u2009=u20090u2009+u20091u2009=u20091,u2009_p__down_u2009=u20090u2009+u20091u2009=u20091,u2009_p__up_u2009β₯u2009_p__down_. So the elevator goes to the floor number 8. _t_u2009=u20098. The elevator is on the floor number 8. One person leaves the elevator, this person wanted to go to the floor number 8. There is one person in the elevator, he wants to go to the floor number 5. _p__up_u2009=u20090u2009+u20090u2009=u20090,u2009_p__down_u2009=u20090u2009+u20091u2009=u20091,u2009_p__up_u2009<u2009_p__down_. So the elevator goes to the floor number 7. _t_u2009=u20099. The elevator is on the floor number 7. There is one person in the elevator, this person wants to get to the floor number 5. _p__up_u2009=u20090u2009+u20090u2009=u20090,u2009_p__down_u2009=u20090u2009+u20091u2009=u20091,u2009_p__up_u2009<u2009_p__down_. So the elevator goes to the floor number 6. _t_u2009=u200910. The elevator is on the floor number 6. There is one person in the elevator, he wants to get to the floor number 5. _p__up_u2009=u20090u2009+u20090u2009=u20090,u2009_p__down_u2009=u20090u2009+u20091u2009=u20091,u2009_p__up_u2009<u2009_p__down_. So the elevator goes to the floor number 5. _t_u2009=u200911. The elevator is on the floor number 5. One person leaves the elevator, this person initially wanted to get to the floor number 5. The elevator is empty and nobody needs it, so the elevator remains at the floor number 5. | 2,200 | false | false | true | false | true | false | false | false | false | false | 8,803 |
1203A | There are $$$n$$$ students standing in a circle in some order. The index of the $$$i$$$-th student is $$$p_i$$$. It is guaranteed that all indices of students are distinct integers from $$$1$$$ to $$$n$$$ (i.u2009e. they form a permutation). Students want to start a round dance. A clockwise round dance can be started if the student $$$2$$$ comes right after the student $$$1$$$ in clockwise order (there are no students between them), the student $$$3$$$ comes right after the student $$$2$$$ in clockwise order, and so on, and the student $$$n$$$ comes right after the student $$$n - 1$$$ in clockwise order. A counterclockwise round dance is almost the same thing β the only difference is that the student $$$i$$$ should be right after the student $$$i - 1$$$ in counterclockwise order (this condition should be met for every $$$i$$$ from $$$2$$$ to $$$n$$$). For example, if the indices of students listed in clockwise order are $$$[2, 3, 4, 5, 1]$$$, then they can start a clockwise round dance. If the students have indices $$$[3, 2, 1, 4]$$$ in clockwise order, then they can start a counterclockwise round dance. Your task is to determine whether it is possible to start a round dance. Note that the students cannot change their positions before starting the dance; they cannot swap or leave the circle, and no other student can enter the circle. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 200$$$) β the number of queries. Then $$$q$$$ queries follow. The first line of the query contains one integer $$$n$$$ ($$$1 le n le 200$$$) β the number of students. The second line of the query contains a permutation of indices $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$), where $$$p_i$$$ is the index of the $$$i$$$-th student (in clockwise order). It is guaranteed that all $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$ (i.u2009e. they form a permutation). Output For each query, print the answer on it. If a round dance can be started with the given order of students, print "YES". Otherwise print "NO". | 1,000 | false | false | true | false | false | false | false | false | false | false | 4,683 |
1975I | This is the way it always was. This is the way it always will be. All will be forgotten again soon... Jellyfish is playing a one-player card game called "Slay the Spire". There are $$$n$$$ cards in total numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th card has power $$$c_i$$$. There is a binary string $$$s$$$ of length $$$n$$$. If $$$s_i = exttt{0}$$$, the $$$i$$$-th card is initially in the draw pile. If $$$s_i = exttt{1}$$$, the $$$i$$$-th card is initially in Jellyfish's hand. Jellyfish will repeat the following process until either her hand or the draw pile is empty. 1. Let $$$x$$$ be the power of the card with the largest power in her hand. 2. Place a single card with power $$$x$$$ back into the draw pile. 3. Randomly draw $$$x$$$ cards from the draw pile. All subsets of $$$x$$$ cards from the draw pile have an equal chance of being drawn. If there are fewer than $$$x$$$ cards in the draw pile, Jellyfish will draw all cards. At the end of this process, find the probability that Jellyfish can empty the draw pile, modulo $$$1,000,000,007$$$. Formally, let $$$M=1,000,000,007$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1leq tleq 100$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 leq n leq 120$$$)xa0β the number of cards. The second line of each test case contains $$$n$$$ integers $$$c_1,c_2,ldots,c_n$$$ ($$$0 leq c_i leq n$$$)xa0β the powers of the cards. It is guaranteed that $$$c_1 leq c_2 leq ldots leq c_n$$$. The third line of each test case contains a binary string $$$s$$$ of length $$$n$$$. If $$$s_i = exttt{0}$$$, the $$$i$$$-th card is initially in the draw pile. If $$$s_i = exttt{1}$$$, the $$$i$$$-th card is initially in Jellyfish's hand. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$120^2$$$. Output For each test case, output the probability that Jellyfish can empty the draw pile modulo $$$1,000,000,007$$$. Example Input 4 5 0 1 1 1 2 00100 3 2 3 3 000 10 0 0 0 0 0 0 0 1 1 1 1111011111 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 3 3 4 00000000001000101010 Output 500000004 0 0 675898154 Note In the first test case, Jellyfish will keep playing cards with power $$$1$$$ until Jellyfish draws a card with power $$$0$$$ or power $$$2$$$. If Jellyfish draws a card with power $$$0$$$, she will eventually empty her hand. If Jellyfish draws a card with power $$$2$$$, she will eventually empty the draw pile. Since there is an equal chance of drawing $$$0$$$ or $$$2$$$, the answer is $$$frac{1}{2}$$$, and $$$2 cdot 500,000,004 equiv 1 pmod {10^9+7}$$$ | 3,500 | false | false | false | true | false | false | false | false | false | false | 434 |
1216D | There were $$$n$$$ types of swords in the theater basement which had been used during the plays. Moreover there were exactly $$$x$$$ swords of each type. $$$y$$$ people have broken into the theater basement and each of them has taken exactly $$$z$$$ swords of some single type. Note that different people might have taken different types of swords. Note that the values $$$x, y$$$ and $$$z$$$ are unknown for you. The next morning the director of the theater discovers the loss. He counts all swords β exactly $$$a_i$$$ swords of the $$$i$$$-th type are left untouched. The director has no clue about the initial number of swords of each type in the basement, the number of people who have broken into the basement and how many swords each of them have taken. For example, if $$$n=3$$$, $$$a = [3, 12, 6]$$$ then one of the possible situations is $$$x=12$$$, $$$y=5$$$ and $$$z=3$$$. Then the first three people took swords of the first type and the other two people took swords of the third type. Note that you don't know values $$$x, y$$$ and $$$z$$$ beforehand but know values of $$$n$$$ and $$$a$$$. Thus he seeks for your help. Determine the minimum number of people $$$y$$$, which could have broken into the theater basement, and the number of swords $$$z$$$ each of them has taken. Input The first line of the input contains one integer $$$n$$$ $$$(2 le n le 2 cdot 10^{5})$$$ β the number of types of swords. The second line of the input contains the sequence $$$a_1, a_2, dots, a_n$$$ $$$(0 le a_i le 10^{9})$$$, where $$$a_i$$$ equals to the number of swords of the $$$i$$$-th type, which have remained in the basement after the theft. It is guaranteed that there exists at least one such pair of indices $$$(j, k)$$$ that $$$a_j eq a_k$$$. Output Print two integers $$$y$$$ and $$$z$$$ β the minimum number of people which could have broken into the basement and the number of swords each of them has taken. Examples Input 7 2 1000000000 4 6 8 4 2 Note In the first example the minimum value of $$$y$$$ equals to $$$5$$$, i.e. the minimum number of people who could have broken into the basement, is $$$5$$$. Each of them has taken $$$3$$$ swords: three of them have taken $$$3$$$ swords of the first type, and two others have taken $$$3$$$ swords of the third type. In the second example the minimum value of $$$y$$$ is $$$1$$$, i.e. the minimum number of people who could have broken into the basement, equals to $$$1$$$. He has taken $$$7$$$ swords of the first type. | 1,300 | true | false | false | false | false | false | false | false | false | false | 4,594 |
335B | Problem - 335B - Codeforces =============== xa0 containing only lowercase English letters. Output If _s_ contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of _s_. If _s_ doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of _s_ and is as long as possible. If there exists multiple answers, you are allowed to print any of them. Examples Input bbbabcbbb Output bbbcbbb Input rquwmzexectvnbanemsmdufrg Output rumenanemur Note A subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward. | 1,900 | false | false | false | true | false | true | false | false | false | false | 8,497 |
1623A | A robot cleaner is placed on the floor of a rectangle room, surrounded by walls. The floor consists of $$$n$$$ rows and $$$m$$$ columns. The rows of the floor are numbered from $$$1$$$ to $$$n$$$ from top to bottom, and columns of the floor are numbered from $$$1$$$ to $$$m$$$ from left to right. The cell on the intersection of the $$$r$$$-th row and the $$$c$$$-th column is denoted as $$$(r,c)$$$. The initial position of the robot is $$$(r_b, c_b)$$$. In one second, the robot moves by $$$dr$$$ rows and $$$dc$$$ columns, that is, after one second, the robot moves from the cell $$$(r, c)$$$ to $$$(r + dr, c + dc)$$$. Initially $$$dr = 1$$$, $$$dc = 1$$$. If there is a vertical wall (the left or the right walls) in the movement direction, $$$dc$$$ is reflected before the movement, so the new value of $$$dc$$$ is $$$-dc$$$. And if there is a horizontal wall (the upper or lower walls), $$$dr$$$ is reflected before the movement, so the new value of $$$dr$$$ is $$$-dr$$$. Each second (including the moment before the robot starts moving), the robot cleans every cell lying in the same row or the same column as its position. There is only one dirty cell at $$$(r_d, c_d)$$$. The job of the robot is to clean that dirty cell. Illustration for the first example. The blue arc is the robot. The red star is the target dirty cell. Each second the robot cleans a row and a column, denoted by yellow stripes. Given the floor size $$$n$$$ and $$$m$$$, the robot's initial position $$$(r_b, c_b)$$$ and the dirty cell's position $$$(r_d, c_d)$$$, find the time for the robot to do its job. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). Description of the test cases follows. A test case consists of only one line, containing six integers $$$n$$$, $$$m$$$, $$$r_b$$$, $$$c_b$$$, $$$r_d$$$, and $$$c_d$$$ ($$$1 le n, m le 100$$$, $$$1 le r_b, r_d le n$$$, $$$1 le c_b, c_d le m$$$)xa0β the sizes of the room, the initial position of the robot and the position of the dirt cell. Output For each test case, print an integer β the time for the robot to clean the dirty cell. We can show that the robot always cleans the dirty cell eventually. Example Input 5 10 10 6 1 2 8 10 10 9 9 1 1 9 8 5 6 2 1 6 9 2 2 5 8 2 2 1 1 2 1 Note In the first example, the floor has the size of $$$10 imes 10$$$. The initial position of the robot is $$$(6, 1)$$$ and the position of the dirty cell is $$$(2, 8)$$$. See the illustration of this example in the problem statement. In the second example, the floor is the same, but the initial position of the robot is now $$$(9, 9)$$$, and the position of the dirty cell is $$$(1, 1)$$$. In this example, the robot went straight to the dirty cell and clean it. In the third example, the floor has the size $$$9 imes 8$$$. The initial position of the robot is $$$(5, 6)$$$, and the position of the dirty cell is $$$(2, 1)$$$. In the fourth example, the floor has the size $$$6 imes 9$$$. The initial position of the robot is $$$(2, 2)$$$ and the position of the dirty cell is $$$(5, 8)$$$. In the last example, the robot was already standing in the same column as the dirty cell, so it can clean the cell right away. | 800 | true | false | true | false | false | false | true | false | false | false | 2,505 |
177A2 | Problem - 177A2 - Codeforces =============== xa0 separated by single spaces β the elements of the given matrix. The input limitations for getting 30 points are: 1u2009β€u2009_n_u2009β€u20095 The input limitations for getting 100 points are: 1u2009β€u2009_n_u2009β€u2009101 Output Print a single integer β the sum of good matrix elements. Examples Input 3 1 2 3 4 5 6 7 8 9 Output 45 Input 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Output 17 Note In the first sample all matrix elements will be good. Good elements in the second sample are shown on the figure. | 800 | false | false | true | false | false | false | false | false | false | false | 9,153 |
1804B | Ethan runs a vaccination station to help people combat the seasonal flu. He analyses the historical data in order to develop an optimal strategy for vaccine usage. Consider there are $$$n$$$ patients coming to the station on a particular day. The $$$i$$$-th patient comes at the moment $$$t_i$$$. We know that each of these patients can be asked to wait for no more than $$$w$$$ time moments. That means the $$$i$$$-th patient can get vaccine at moments $$$t_i, t_i + 1, ldots, t_i + w$$$. Vaccines come in packs, each pack consists of $$$k$$$ doses. Each patient needs exactly one dose. Packs are stored in a special fridge. After a pack is taken out of the fridge and opened, it can no longer be put back. The lifetime of the vaccine outside the fridge is $$$d$$$ moments of time. Thus, if the pack was taken out of the fridge and opened at moment $$$x$$$, its doses can be used to vaccinate patients at moments $$$x, x + 1, ldots, x + d$$$. At moment $$$x + d + 1$$$ all the remaining unused doses of this pack are thrown away. Assume that the vaccination station has enough staff to conduct an arbitrary number of operations at every moment of time. What is the minimum number of vaccine packs required to vaccinate all $$$n$$$ patients? Input The first line of the input contains the number of test cases $$$t$$$ ($$$1 leq t leq 10^4$$$). Then follow $$$t$$$ descriptions of the test cases. The first line of each test case contains four integers $$$n$$$, $$$k$$$, $$$d$$$ and $$$w$$$ ($$$1 leq n, k leq 2 cdot 10^5$$$, $$$0 leq d, w leq 10^6$$$). They are the number of patients, the number of doses per vaccine pack, the number of moments of time the vaccine can live outside the fridge, and the number of moments of time each of the patients can wait, respectively. The second line of each test case contains a non-decreasing sequence $$$t_1, t_2, ldots, t_n$$$ ($$$0 leq t_1 leq t_2 leq ldots leq t_n leq 10^6$$$). The $$$i$$$-th element of this sequence is the moment when the $$$i$$$-th patient comes to the vaccination station. It is guaranteed that the sum of $$$n$$$ over all test cases won't exceed $$$2 cdot 10^5$$$. Output Output one integer, the minimum number of vaccine packs required to vaccinate all $$$n$$$ patients. Example Input 5 6 3 5 3 1 2 3 10 11 18 6 4 0 0 3 3 3 3 3 4 9 10 2 2 0 1 2 3 4 5 6 7 8 3 10 3 6 10 20 30 5 5 4 4 0 2 4 6 8 Note In the first example, the first pack can be opened at moment $$$1$$$ to vaccinate patient $$$1$$$. The vaccine is durable enough to be used at moments $$$2$$$ and $$$3$$$ for patients $$$2$$$ and $$$3$$$, respectively. Then the staff needs to ask patients $$$4$$$ and $$$5$$$ to wait for moment $$$13$$$. At moment $$$13$$$ the staff opens the second vaccine pack and serves patients $$$4$$$ and $$$5$$$. Finally, the last patient comes at moment $$$18$$$ and immediately gets the last dose of the second pack while it is still fine. In the second example, the vaccine should be used exactly at the moment it is taken out of the fridge. Moreover, all the patients want to be served at exactly the same moment they come. That means the staff needs to open two packs at moment $$$3$$$ and use five doses on patients $$$1$$$, $$$2$$$, $$$3$$$, $$$4$$$, and $$$5$$$. There will be three doses left ouf of these two packs but they can't be used for patient $$$6$$$. When patient $$$6$$$ comes at moment $$$4$$$ the staff needs to open a new pack just to use only one dose out of it. | 1,000 | false | true | true | false | false | false | false | false | false | false | 1,456 |
1993E | Given a matrix $$$a$$$ of size $$$n imes m$$$, each cell of which contains a non-negative integer. The integer lying at the intersection of the $$$i$$$-th row and the $$$j$$$-th column of the matrix is called $$$a_{i,j}$$$. Let's define $$$f(i)$$$ and $$$g(j)$$$ as the $$$ for each $$$1 le j le m$$$; or Select any column $$$j$$$, then assign $$$a_{i,j} := f(i)$$$ for each $$$1 le i le n$$$. An example of applying an operation on column $$$2$$$ of the matrix. In this example, as we apply an operation on column $$$2$$$, all elements in this column are changed: $$$a_{1,2} := f(1) = a_{1,1} oplus a_{1,2} oplus a_{1,3} oplus a_{1,4} = 1 oplus 1 oplus 1 oplus 1 = 0$$$ $$$a_{2,2} := f(2) = a_{2,1} oplus a_{2,2} oplus a_{2,3} oplus a_{2,4} = 2 oplus 3 oplus 5 oplus 7 = 3$$$ $$$a_{3,2} := f(3) = a_{3,1} oplus a_{3,2} oplus a_{3,3} oplus a_{3,4} = 2 oplus 0 oplus 3 oplus 0 = 1$$$ $$$a_{4,2} := f(4) = a_{4,1} oplus a_{4,2} oplus a_{4,3} oplus a_{4,4} = 10 oplus 11 oplus 12 oplus 16 = 29$$$ You can apply the operations any number of times. Then, we calculate the $$$ extit{beauty}$$$ of the final matrix by summing the absolute differences between all pairs of its adjacent cells. More formally, $$$ extit{beauty}(a) = suma_{x,y} - a_{r,c}$$$ for all cells $$$(x, y)$$$ and $$$(r, c)$$$ if they are adjacent. Two cells are considered adjacent if they share a side. Find the minimum $$$ extit{beauty}$$$ among all obtainable matrices. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 250$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 15$$$)xa0β the number of rows and columns of $$$a$$$, respectively. The next $$$n$$$ lines, each containing $$$m$$$ integers $$$a_{i,1}, a_{i,2}, ldots, a_{i,m}$$$ ($$$0 le a_{i,j} < 2^{20}$$$)xa0β description of the matrix $$$a$$$. It is guaranteed that the sum of $$$(n^2 + m^2)$$$ over all test cases does not exceed $$$500$$$. Output For each test case, print a single integer $$$b$$$xa0β the smallest possible $$$ extit{beauty}$$$ of the matrix. Example Input 4 1 2 1 3 2 3 0 1 0 5 4 4 2 3 0 2 4 4 5 1 3 3 1 2 3 4 5 6 7 8 9 Note Let's denote $$$r(i)$$$ as the first type operation applied on the $$$i$$$-th row, and $$$c(j)$$$ as the second type operation applied on the $$$j$$$-th column. In the first test case, you can apply an operation $$$c(1)$$$, which assigns $$$a_{1,1} := 1 oplus 3 = 2$$$. Then, we'll receive this matrix: In the second test case, you can apply an operation $$$r(1)$$$, which assigns: $$$a_{1,1} := g(1) = 0 oplus 5 = 5$$$ $$$a_{1,2} := g(2) = 1 oplus 4 = 5$$$ $$$a_{1,3} := g(3) = 0 oplus 4 = 4$$$ The resulting matrix after performing the operation is: In the third test case, the best way to achieve minimum $$$ extit{beauty}$$$ is applying three operations: $$$c(3)$$$, $$$r(2)$$$, and $$$c(2)$$$. The resulting matrix is: | 2,700 | false | false | true | true | false | true | false | false | false | false | 308 |
534C | Polycarp has _n_ dice _d_1,u2009_d_2,u2009...,u2009_d__n_. The _i_-th dice shows numbers from 1 to _d__i_. Polycarp rolled all the dice and the sum of numbers they showed is _A_. Agrippina didn't see which dice showed what number, she knows only the sum _A_ and the values _d_1,u2009_d_2,u2009...,u2009_d__n_. However, she finds it enough to make a series of statements of the following type: dice _i_ couldn't show number _r_. For example, if Polycarp had two six-faced dice and the total sum is _A_u2009=u200911, then Agrippina can state that each of the two dice couldn't show a value less than five (otherwise, the remaining dice must have a value of at least seven, which is impossible). For each dice find the number of values for which it can be guaranteed that the dice couldn't show these values if the sum of the shown values is _A_. Input The first line contains two integers _n_,u2009_A_ (1u2009β€u2009_n_u2009β€u20092Β·105,u2009_n_u2009β€u2009_A_u2009β€u2009_s_) β the number of dice and the sum of shown values where _s_u2009=u2009_d_1u2009+u2009_d_2u2009+u2009...u2009+u2009_d__n_. The second line contains _n_ integers _d_1,u2009_d_2,u2009...,u2009_d__n_ (1u2009β€u2009_d__i_u2009β€u2009106), where _d__i_ is the maximum value that the _i_-th dice can show. Output Print _n_ integers _b_1,u2009_b_2,u2009...,u2009_b__n_, where _b__i_ is the number of values for which it is guaranteed that the _i_-th dice couldn't show them. Note In the first sample from the statement _A_ equal to 8 could be obtained in the only case when both the first and the second dice show 4. Correspondingly, both dice couldn't show values 1, 2 or 3. In the second sample from the statement _A_ equal to 3 could be obtained when the single dice shows 3. Correspondingly, it couldn't show 1, 2, 4 or 5. In the third sample from the statement _A_ equal to 3 could be obtained when one dice shows 1 and the other dice shows 2. That's why the first dice doesn't have any values it couldn't show and the second dice couldn't show 3. | 1,600 | true | false | false | false | false | false | false | false | false | false | 7,699 |
113A | Petya got interested in grammar on his third year in school. He invented his own language called Petya's. Petya wanted to create a maximally simple language that would be enough to chat with friends, that's why all the language's grammar can be described with the following set of rules: There are three parts of speech: the adjective, the noun, the verb. Each word in his language is an adjective, noun or verb. There are two genders: masculine and feminine. Each word in his language has gender either masculine or feminine. Masculine adjectives end with -lios, and feminine adjectives end with -liala. Masculine nouns end with -etr, and feminime nouns end with -etra. Masculine verbs end with -initis, and feminime verbs end with -inites. Thus, each word in the Petya's language has one of the six endings, given above. There are no other endings in Petya's language. It is accepted that the whole word consists of an ending. That is, words "lios", "liala", "etr" and so on belong to the Petya's language. There aren't any punctuation marks, grammatical tenses, singular/plural forms or other language complications. A sentence is either exactly one valid language word or exactly one statement. Statement is any sequence of the Petya's language, that satisfy both conditions: Words in statement follow in the following order (from the left to the right): zero or more adjectives followed by exactly one noun followed by zero or more verbs. All words in the statement should have the same gender. After Petya's friend Vasya wrote instant messenger (an instant messaging program) that supported the Petya's language, Petya wanted to add spelling and grammar checking to the program. As Vasya was in the country and Petya didn't feel like waiting, he asked you to help him with this problem. Your task is to define by a given sequence of words, whether it is true that the given text represents exactly one sentence in Petya's language. Input The first line contains one or more words consisting of lowercase Latin letters. The overall number of characters (including letters and spaces) does not exceed 105. It is guaranteed that any two consecutive words are separated by exactly one space and the input data do not contain any other spaces. It is possible that given words do not belong to the Petya's language. Output If some word of the given text does not belong to the Petya's language or if the text contains more that one sentence, print "NO" (without the quotes). Otherwise, print "YES" (without the quotes). Examples Input etis atis animatis etis atis amatis Input nataliala kataliala vetra feinites | 1,600 | false | false | true | false | false | false | false | false | false | false | 9,435 |
1779B | MKnez wants to construct an array $$$s_1,s_2, ldots , s_n$$$ satisfying the following conditions: Each element is an integer number different from $$$0$$$; For each pair of adjacent elements their sum is equal to the sum of the whole array. More formally, $$$s_i eq 0$$$ must hold for each $$$1 leq i leq n$$$. Moreover, it must hold that $$$s_1 + s_2 + cdots + s_n = s_i + s_{i+1}$$$ for each $$$1 leq i < n$$$. Help MKnez to construct an array with these properties or determine that it does not exist. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 leq t leq 100$$$). The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 1000$$$)xa0β the length of the array. Output For each test case, print "YES" if an array of length $$$n$$$ satisfying the conditions exists. Otherwise, print "NO". If the answer is "YES", on the next line print a sequence $$$s_1,s_2, ldots, s_n$$$ satisfying the conditions. Each element should be a non-zero integer in the range $$$[-5000,5000]$$$, i.xa0e. $$$-5000 leq s_i leq 5000$$$ and $$$s_i eq 0$$$ should hold for each $$$1 leq i leq n$$$. It can be proved that if a solution exists then there also exists one which satisfies the additional constraints on the range. If there are several correct answers, print any of them. Note In the first test case, $$$[9,5]$$$ is a valid answer since $$$9+5$$$ (the sum of the two adjacent elements $$$s_1+s_2$$$) is equal to $$$9+5$$$ (the sum of all elements). Other solutions include $$$[6,-9], [-1,-2], [-5000,5000], ldots$$$ For the second test case, let us show why some arrays do not satisfy the constraints: $$$[1,1,1]$$$ xa0β $$$s_1+s_2 = 1+1 = 2$$$ and $$$s_1+s_2+s_3=1+1+1 = 3$$$ differ; $$$[1,-1,1]$$$ xa0β $$$s_1+s_2=1+(-1)=0$$$ and $$$s_1+s_2+s_3=1+(-1)+1 = 1$$$ differ; $$$[0,0,0]$$$ xa0β The array $$$s$$$ cannot contain a $$$0$$$. This is not a proof, but it can be shown that the answer is "NO". | 900 | true | false | false | false | false | true | false | false | false | false | 1,601 |
221B | Problem - 221B - 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 *1300 No tag edit access β Contest materials digit in their decimal representations. Help the Little Elephant to find the described number. Input A single line contains a single integer _x_ (1u2009β€u2009_x_u2009β€u2009109). Output In a single line print an integer β the answer to the problem. Examples Input 1 Output 1 Input 10 Output 2 | 1,300 | false | false | true | false | false | false | false | false | false | false | 8,953 |
802L | Heidi is terrified by your estimate and she found it unrealistic that her friends would collaborate to drive her into debt. She expects that, actually, each person will just pick a random friend to send Heidi to. (This randomness assumption implies, however, that she can now visit the same friend an arbitrary number of times...) Moreover, if a person only has one friend in common with Heidi (i.e., if that person is in a leaf of the tree), then that person will not send Heidi back (so that Heidi's travel will end at some point). Heidi also found unrealistic the assumption that she can make all the travels in one day. Therefore now she assumes that every time she travels a route between two friends, she needs to buy a new ticket. She wants to know: how much should she expect to spend on the trips? For what it's worth, Heidi knows that Jenny has at least two friends. Input The first line contains the number of friends _n_ (3u2009β€u2009_n_u2009β€u2009105). The next _n_u2009-u20091 lines each contain three space-separated integers _u_, _v_ and _c_ (0u2009β€u2009_u_,u2009_v_u2009β€u2009_n_u2009-u20091, 1u2009β€u2009_c_u2009β€u2009104) meaning that _u_ and _v_ are friends and the cost for traveling between _u_ and _v_ is _c_ (paid every time!). It is again guaranteed that the social network of the input forms a tree. Output Assume that the expected cost of the trips is written as an irreducible fraction _a_u2009/u2009_b_ (that is, _a_ and _b_ are coprime). Then Heidi, the weird cow that she is, asks you to output . (Output a single integer between 0 and 109u2009+u20096.) Examples Input 7 0 1 3 0 5 7 1 2 2 1 3 1 1 4 5 5 6 8 Input 11 1 0 6646 2 0 8816 3 2 9375 4 2 5950 5 1 8702 6 2 2657 7 2 885 8 7 2660 9 2 5369 10 6 3798 Input 6 0 1 8 0 2 24 1 3 40 1 4 16 4 5 8 Note In the first example, with probability 1u2009/u20092 Heidi will go to 1 from 0, and with probability 1u2009/u20092 she will go to 2. In the first case the cost would be 10, and in the second it would be 20. After reaching 1 or 2 she will stop, as 1 and 2 are leaves of the social tree. Hence, the expected cost she has to pay is 10Β·1u2009/u20092u2009+u200920Β·1u2009/u20092u2009=u200915. In the third example, the expected cost is 81u2009/u20095. You should output 400000019. In her travels, Heidi has learned an intriguing fact about the structure of her social network. She tells you the following: The mysterious determinant that you might be wondering about is such that it does not cause strange errors in your reasonable solution... Did we mention that Heidi is a weird cow? | 2,400 | true | false | false | true | false | false | false | false | false | false | 6,556 |