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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1838C | You are given integers $$$n$$$ and $$$m$$$. Fill an $$$n$$$ by $$$m$$$ grid with the integers $$$1$$$ through $$$ncdot m$$$, in such a way that for any two adjacent cells in the grid, the absolute difference of the values in those cells is not a prime number. Two cells in the grid are considered adjacent if they share a side. It can be shown that under the given constraints, there is always a solution. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The description of the test cases follows. The first and only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$4 le n, m le 1000$$$)xa0— the dimensions of the grid. It is guaranteed that the sum of $$$ncdot m$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, output $$$n$$$ lines of $$$m$$$ integers each, representing the final grid. Every number from $$$1$$$ to $$$ncdot m$$$ should appear exactly once in the grid. The extra spaces and blank lines in the sample output below are only present to make the output easier to read, and are not required. If there are multiple solutions, print any of them. Example Output 16 7 1 9 12 8 2 3 13 4 10 11 14 5 6 15 29 23 17 9 5 6 2 33 27 21 15 11 7 1 32 31 25 19 20 16 10 26 30 24 18 14 8 4 35 34 28 22 13 12 3 2 3 7 11 8 9 1 10 17 13 5 4 18 14 6 12 19 23 15 21 20 24 16 22 Note The first sample case corresponds to the picture above. The only absolute differences between adjacent elements in this grid are $$$1$$$, $$$4$$$, $$$6$$$, $$$8$$$, and $$$9$$$, none of which are prime. | 1,400 | true | false | false | false | false | true | false | false | false | false | 1,262 |
1695B | Mike and Joe are playing a game with some stones. Specifically, they have $$$n$$$ piles of stones of sizes $$$a_1, a_2, ldots, a_n$$$. These piles are arranged in a circle. The game goes as follows. Players take turns removing some positive number of stones from a pile in clockwise order starting from pile $$$1$$$. Formally, if a player removed stones from pile $$$i$$$ on a turn, the other player removes stones from pile $$$((ibmod n) + 1)$$$ on the next turn. If a player cannot remove any stones on their turn (because the pile is empty), they lose. Mike goes first. If Mike and Joe play optimally, who will win? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 1000$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 50$$$) xa0— the number of piles. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$) xa0— the size of the piles. Output For each test case print the winner of the game, either "Mike" or "Joe" on its own line (without quotes). Note In the first test case, Mike just takes all $$$37$$$ stones on his first turn. In the second test case, Joe can just copy Mike's moves every time. Since Mike went first, he will hit $$$0$$$ on the first pile one move before Joe does so on the second pile. | 1,000 | false | true | false | false | false | false | false | false | false | false | 2,113 |
2008B | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 970 (Div. 3) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags brute force math strings *800 No tag edit access → Contest materials Announcement (en) Tutorial #1 (en) Video Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST B. Square or Not time limit per test2 seconds memory limit per test256 megabytes A beautiful binary matrix is a matrix that has ones on its edges and zeros inside. Examples of four beautiful binary matrices. Today, Sakurako was playing with a beautiful binary matrix of size $$$r imes c$$$ and created a binary string $$$s$$$ by writing down all the rows of the matrix, starting from the first and ending with the $$$r$$$-th. More formally, the element from the matrix in the $$$i$$$-th row and $$$j$$$-th column corresponds to the $$$((i-1)*c+j)$$$-th element of the string. You need to check whether the beautiful matrix from which the string $$$s$$$ was obtained could be squared. In other words, you need to check whether the string $$$s$$$ could have been build from a square beautiful binary matrix (i.e., one where $$$r=c$$$). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the length of the string. The second line of each test case contains the string $$$s$$$ of length $$$n$$$. The string is always the result of writing out the strings of a beautiful matrix. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$2 cdot 10^5$$$. Output Print "Yes", if the original matrix could have been square, and "No" otherwise. Example input 5 2 11 4 1111 9 111101111 9 111111111 12 111110011111 output No Yes Yes No No Note For the second test case, string 1111 can be obtained from the matrix: $$$1$$$ $$$1$$$ $$$1$$$ $$$1$$$ For the third test case, string 111101111 can be obtained from the matrix: $$$1$$$ $$$1$$$ $$$1$$$ $$$1$$$ $$$0$$$ $$$1$$$ $$$1$$$ $$$1$$$ $$$1$$$ There is no square matrix in the fourth case, such that the string can be obtained from it. Codeforces (c) | 800 | true | false | false | false | false | false | true | false | false | false | 207 |
908E | Problem - 908E - Codeforces =============== xa0 ]( "g") ). The next _n_ lines will contain the elements of _T_. Each line will contain exactly _m_ zeros and ones. Elements of _T_ will be distinct. Output Print a single integer, the number of good sets modulo 109u2009+u20097. Examples Input 5 3 11010 00101 11000 Output 4 Input 30 2 010101010101010010101010101010 110110110110110011011011011011 Output 860616440 Note An example of a valid set _S_ is {00000, 00101, 00010, 00111, 11000, 11010, 11101, 11111}. | 2,500 | true | false | false | true | false | false | false | false | false | false | 6,116 |
280D | Consider integer sequence _a_1,u2009_a_2,u2009...,u2009_a__n_. You should run queries of two types: The query format is "0 _i_ _val_". In reply to this query you should make the following assignment: _a__i_u2009=u2009_val_. The query format is "1 _l_ _r_ _k_". In reply to this query you should print the maximum sum of at most _k_ non-intersecting subsegments of sequence _a__l_,u2009_a__l_u2009+u20091,u2009...,u2009_a__r_. Formally, you should choose at most _k_ pairs of integers (_x_1,u2009_y_1),u2009(_x_2,u2009_y_2),u2009...,u2009(_x__t_,u2009_y__t_) (_l_u2009≤u2009_x_1u2009≤u2009_y_1u2009<u2009_x_2u2009≤u2009_y_2u2009<u2009...u2009<u2009_x__t_u2009≤u2009_y__t_u2009≤u2009_r_;xa0_t_u2009≤u2009_k_) such that the sum _a__x_1u2009+u2009_a__x_1u2009+u20091u2009+u2009...u2009+u2009_a__y_1u2009+u2009_a__x_2u2009+u2009_a__x_2u2009+u20091u2009+u2009...u2009+u2009_a__y_2u2009+u2009...u2009+u2009_a__x__t_u2009+u2009_a__x__t_u2009+u20091u2009+u2009...u2009+u2009_a__y__t_ is as large as possible. Note that you should choose at most _k_ subsegments. Particularly, you can choose 0 subsegments. In this case the described sum considered equal to zero. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105), showing how many numbers the sequence has. The next line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009≤u2009500). The third line contains integer _m_ (1u2009≤u2009_m_u2009≤u2009105) — the number of queries. The next _m_ lines contain the queries in the format, given in the statement. All changing queries fit into limits: 1u2009≤u2009_i_u2009≤u2009_n_, _val_u2009≤u2009500. All queries to count the maximum sum of at most _k_ non-intersecting subsegments fit into limits: 1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_, 1u2009≤u2009_k_u2009≤u200920. It is guaranteed that the number of the queries to count the maximum sum of at most _k_ non-intersecting subsegments doesn't exceed 10000. Output For each query to count the maximum sum of at most _k_ non-intersecting subsegments print the reply — the maximum sum. Print the answers to the queries in the order, in which the queries follow in the input. Examples Input 9 9 -8 9 -1 -1 -1 9 -8 9 3 1 1 9 1 1 1 9 2 1 4 6 3 Input 15 -4 8 -3 -10 10 4 -7 -7 0 -6 3 8 -10 7 2 15 1 3 9 2 1 6 12 1 0 6 5 0 10 -7 1 4 9 1 1 7 9 1 0 10 -3 1 4 10 2 1 3 13 2 1 4 11 2 0 15 -9 0 13 -9 0 11 -10 1 5 14 2 1 6 12 1 Note In the first query of the first example you can select a single pair (1,u20099). So the described sum will be 17. Look at the second query of the first example. How to choose two subsegments? (1, 3) and (7, 9)? Definitely not, the sum we could get from (1, 3) and (7, 9) is 20, against the optimal configuration (1, 7) and (9, 9) with 25. The answer to the third query is 0, we prefer select nothing if all of the numbers in the given interval are negative. | 2,800 | false | false | true | false | true | false | false | false | false | true | 8,712 |
1944B | You are given an array $$$a$$$ of length $$$2n$$$, consisting of each integer from $$$1$$$ to $$$n$$$ exactly twice. You are also given an integer $$$k$$$ ($$$1 leq k leq lfloor frac{n}{2} floor $$$). You need to find two arrays $$$l$$$ and $$$r$$$ each of length $$$mathbf{2k}$$$ such that: $$$l$$$ is a subset$$$^dagger$$$ of $$$ elements of $$$y$$$ and rearranging the elements in any order. For example, $$$[3,1,2,1]$$$, $$$[1, 2, 3]$$$, $$$[1, 1]$$$ and $$$[3, 2]$$$ are subsets of $$$[1, 1, 2, 3]$$$ but $$$[4]$$$ and $$$[2, 2]$$$ are not subsets of $$$[1, 1, 2, 3]$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 5000$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains $$$2$$$ integers $$$n$$$ and $$$k$$$ ($$$2 le n le 5 cdot 10^4$$$, $$$1 leq k leq lfloor frac{n}{2} floor $$$). The second line contains $$$2n$$$ integers $$$a_1, a_2, ldots, a_{2n}$$$ ($$$1 le a_i le n$$$). It is guaranteed that every integer from $$$1$$$ to $$$n$$$ occurs exactly twice in $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5 cdot 10^4$$$. Output For each test case, output two lines. On the first line of output, output $$$2k$$$ integers $$$l_1, l_2, ldots, l_{2k}$$$. On the second line of output, output $$$2k$$$ integers $$$r_1, r_2, ldots r_{2k}$$$. If there are multiple solutions, you may output any one of them. Example Input 4 2 1 1 2 2 1 6 1 6 4 2 1 2 3 1 6 3 5 5 4 4 1 1 2 3 4 1 2 3 4 6 2 5 1 3 3 5 1 2 6 4 6 4 2 Output 2 1 2 1 6 4 1 3 1 2 1 2 5 1 3 3 6 4 2 4 Note In the first test case, we choose $$$l=[2,1]$$$ and $$$r=[2,1]$$$. $$$[2, 1]$$$ is a subset of $$$[a_1, a_2]$$$ and $$$[2, 1]$$$ is a subset of $$$[a_3, a_4]$$$, and $$$2 oplus 1 = 2 oplus 1 = 3$$$. In the second test case, $$$6 oplus 4 = 1 oplus 3 = 2$$$. | 1,100 | false | false | false | false | false | true | false | false | false | false | 614 |
924C | Arkady decides to observe a river for _n_ consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the _i_-th day this value is equal to _m__i_. Define _d__i_ as the number of marks strictly under the water level on the _i_-th day. You are to find out the minimum possible sum of _d__i_ over all days. There are no marks on the channel before the first day. Input The first line contains a single positive integer _n_ (1u2009≤u2009_n_u2009≤u2009105)xa0— the number of days. The second line contains _n_ space-separated integers _m_1,u2009_m_2,u2009...,u2009_m__n_ (0u2009≤u2009_m__i_u2009<u2009_i_)xa0— the number of marks strictly above the water on each day. Output Output one single integerxa0— the minimum possible sum of the number of marks strictly below the water level among all days. Note In the first example, the following figure shows an optimal case. Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0u2009+u20090u2009+u20092u2009+u20090u2009+u20093u2009+u20091u2009=u20096. In the second example, the following figure shows an optimal case. | 1,700 | false | true | false | true | true | false | false | false | false | false | 6,015 |
1912A | Allyn is playing a new strategy game called "Accumulator Apex". In this game, Allyn is given the initial value of an integer $$$x$$$, referred to as the accumulator, and $$$k$$$ lists of integers. Allyn can make multiple turns. On each turn, Allyn can withdraw the leftmost element from any non-empty list and add it to the accumulator $$$x$$$ if the resulting $$$x$$$ is non-negative. Allyn can end the game at any moment. The goal of the game is to get the largest possible value of the accumulator $$$x$$$. Please help Allyn find the largest possible value of the accumulator $$$x$$$ they can get in this game. Input The first line of the input contains two integers $$$x$$$ and $$$k$$$ ($$$0 leq x leq 10^9, 1 leq k leq 10^5$$$)xa0— the initial value of the accumulator $$$x$$$ and the number of lists. The next $$$k$$$ lines contain the description of lists: an integer $$$l_i$$$ ($$$l_i ge 1$$$) followed on the same line by $$$l_i$$$ elements of the list in the order from left to right. Each element of lists does not exceed $$$10^9$$$ by the absolute value, and the total size of all lists does not exceed $$$10^5$$$. Output The sole line of the output should contain the largest value of the accumulator $$$x$$$ Allyn can get. Examples Input 1 3 2 -1 2 2 -2 3 2 -3 4 Input 1 2 3 -1 -1 4 4 1 -3 -4 8 Note In the first input, we start with $$$x = 1$$$. Then, we can take the first integer from the first list and get $$$x = 0$$$xa0— adding the next integer $$$2$$$ from the first list we get $$$x = 2$$$. After that, we can add the integers from the second list and obtain $$$x = 3$$$. Finally, we can add the integers from the third list and obtain $$$x = 4$$$. In the second input, we can add the first integer from the second list and get $$$x = 2$$$. Then, by adding the elements from the first list, we get $$$x = 4$$$. We cannot add more integers to increase $$$x$$$. | 1,900 | false | false | true | false | true | false | false | false | true | false | 837 |
1617E | Christmas is coming, Icy has just received a box of chocolates from her grandparents! The box contains $$$n$$$ chocolates. The $$$i$$$-th chocolate has a non-negative integer type $$$a_i$$$. Icy believes that good things come in pairs. Unfortunately, all types of chocolates are distinct (all $$$a_i$$$ are distinct). Icy wants to make at least one pair of chocolates the same type. As a result, she asks her grandparents to perform some chocolate exchanges. Before performing any chocolate exchanges, Icy chooses two chocolates with indices $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$, $$$x e y$$$). In a chocolate exchange, Icy's grandparents choose a non-negative integer $$$k$$$, such that $$$2^k ge a_x$$$, and change the type of the chocolate $$$x$$$ from $$$a_x$$$ to $$$2^k - a_x$$$ (that is, perform $$$a_x := 2^k - a_x$$$). The chocolate exchanges will be stopped only when $$$a_x = a_y$$$. Note that other pairs of equal chocolate types do not stop the procedure. Icy's grandparents are smart, so they would choose the sequence of chocolate exchanges that minimizes the number of exchanges needed. Since Icy likes causing trouble, she wants to maximize the minimum number of exchanges needed by choosing $$$x$$$ and $$$y$$$ appropriately. She wonders what is the optimal pair $$$(x, y)$$$ such that the minimum number of exchanges needed is maximized across all possible choices of $$$(x, y)$$$. Since Icy is not good at math, she hopes that you can help her solve the problem. Input The first line of the input contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0— the number of chocolates. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$). It is guaranteed that all $$$a_i$$$ are distinct. Output Output three integers $$$x$$$, $$$y$$$, and $$$m$$$. $$$x$$$ and $$$y$$$ are indices of the optimal chocolates to perform exchanges on. Your output must satisfy $$$1 le x, y le n$$$, $$$x e y$$$. $$$m$$$ is the number of exchanges needed to obtain $$$a_x = a_y$$$. We can show that $$$m le 10^9$$$ for any pair of chocolates. If there are multiple solutions, output any. Note In the first test case, the minimum number of exchanges needed to exchange a chocolate of type $$$6$$$ to a chocolate of type $$$9$$$ is $$$5$$$. The sequence of exchanges is as follows: $$$6 ightarrow 2 ightarrow 0 ightarrow 1 ightarrow 7 ightarrow 9$$$. In the second test case, the minimum number of exchanges needed to exchange a chocolate of type $$$4$$$ to a chocolate of type $$$8$$$ is $$$2$$$. The sequence of exchanges is as follows: $$$4 ightarrow 0 ightarrow 8$$$. | 2,700 | true | false | true | true | false | false | false | false | false | true | 2,543 |
1784B | A cooperative game is played by $$$m$$$ people. In the game, there are $$$3m$$$ sheets of paper: $$$m$$$ sheets with letter 'w', $$$m$$$ sheets with letter 'i', and $$$m$$$ sheets with letter 'n'. Initially, each person is given three sheets (possibly with equal letters). The goal of the game is to allow each of the $$$m$$$ people to spell the word "win" using their sheets of paper. In other words, everyone should have one sheet with letter 'w', one sheet with letter 'i', and one sheet with letter 'n'. To achieve the goal, people can make exchanges. Two people participate in each exchange. Both of them choose exactly one sheet of paper from the three sheets they own and exchange it with each other. Find the shortest sequence of exchanges after which everyone has one 'w', one 'i', and one 'n'. 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 $$$m$$$xa0($$$2 le m le 10^5$$$)xa0— the number of people. The $$$i$$$-th of the next $$$m$$$ lines contains a string $$$s_i$$$ of length $$$3$$$ consisting of lowercase English letters 'w', 'i', and 'n', denoting the letters person $$$i$$$ has on their sheets of paper at the beginning of the game, in arbitrary order. Each of the letters 'w', 'i', and 'n' appears on the sheets of paper exactly $$$m$$$ times in total. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print a non-negative integer $$$k$$$xa0— the smallest number of exchanges people need to make everyone have one 'w', one 'i', and one 'n'. In each of the next $$$k$$$ lines print four tokens $$$a_1$$$ $$$c_1$$$ $$$a_2$$$ $$$c_2$$$, describing the exchanges in chronological order ($$$1 le a_1, a_2 le m$$$; $$$a_1 e a_2$$$; $$$c_1, c_2$$$ are one of $$${mathtt{w}, mathtt{i}, mathtt{n}}$$$): person $$$a_1$$$ gives letter $$$c_1$$$ to person $$$a_2$$$, while person $$$a_2$$$ gives letter $$$c_2$$$ to person $$$a_1$$$ at the same time. If there are multiple solutions, print any. Example Input 3 2 nwi inw 3 inn nww wii 4 win www iii nnn Output 0 2 2 w 3 i 3 w 1 n 3 2 w 3 i 2 w 4 n 3 i 4 n | 1,900 | false | false | false | false | false | true | false | false | false | false | 1,571 |
1722C | Three guys play a game: first, each person writes down $$$n$$$ distinct words of length $$$3$$$. Then, they total up the number of points as follows: if a word was written by one personxa0— that person gets 3 points, if a word was written by two peoplexa0— each of the two gets 1 point, if a word was written by allxa0— nobody gets any points. In the end, how many points does each player have? Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 1000$$$)xa0— the number of words written by each person. The following three lines each contain $$$n$$$ distinct stringsxa0— the words written by each person. Each string consists of $$$3$$$ lowercase English characters. Output For each test case, output three space-separated integersxa0— the number of points each of the three guys earned. You should output the answers in the same order as the input; the $$$i$$$-th integer should be the number of points earned by the $$$i$$$-th guy. Example Input 3 1 abc def abc 3 orz for qaq qaq orz for cod for ces 5 iat roc hem ica lly bac ter iol ogi sts bac roc lly iol iat Note In the first test case: The word $$$ exttt{abc}$$$ was written by the first and third guysxa0— they each get $$$1$$$ point. The word $$$ exttt{def}$$$ was written by the second guy onlyxa0— he gets $$$3$$$ points. | 800 | false | false | true | false | true | false | false | false | false | false | 1,951 |
200D | Recently, Valery have come across an entirely new programming language. Most of all the language attracted him with template functions and procedures. Let us remind you that templates are tools of a language, designed to encode generic algorithms, without reference to some parameters (e.g., data types, buffer sizes, default values). Valery decided to examine template procedures in this language in more detail. The description of a template procedure consists of the procedure name and the list of its parameter types. The generic type T parameters can be used as parameters of template procedures. A procedure call consists of a procedure name and a list of variable parameters. Let's call a procedure suitable for this call if the following conditions are fulfilled: its name equals to the name of the called procedure; the number of its parameters equals to the number of parameters of the procedure call; the types of variables in the procedure call match the corresponding types of its parameters. The variable type matches the type of a parameter if the parameter has a generic type T or the type of the variable and the parameter are the same. You are given a description of some set of template procedures. You are also given a list of variables used in the program, as well as direct procedure calls that use the described variables. For each call you need to count the number of procedures that are suitable for this call. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of template procedures. The next _n_ lines contain the description of the procedures specified in the following format: "void procedureName (type_1, type_2, ..., type_t)" (1u2009≤u2009_t_u2009≤u20095), where void is the keyword, procedureName is the procedure name, type_i is the type of the next parameter. Types of language parameters can be "int", "string", "double", and the keyword "T", which denotes the generic type. The next line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u20091000) — the number of used variables. Next _m_ lines specify the description of the variables in the following format: "type variableName", where type is the type of variable that can take values "int", "string", "double", variableName — the name of the variable. The next line contains a single integer _k_ (1u2009≤u2009_k_u2009≤u20091000) — the number of procedure calls. Next _k_ lines specify the procedure calls in the following format: "procedureName (var_1, var_2, ..., var_t)" (1u2009≤u2009_t_u2009≤u20095), where procedureName is the name of the procedure, var_i is the name of a variable. The lines describing the variables, template procedures and their calls may contain spaces at the beginning of the line and at the end of the line, before and after the brackets and commas. Spaces may be before and after keyword void. The length of each input line does not exceed 100 characters. The names of variables and procedures are non-empty strings of lowercase English letters and numbers with lengths of not more than 10 characters. Note that this is the only condition at the names. Only the specified variables are used in procedure calls. The names of the variables are distinct. No two procedures are the same. Two procedures are the same, if they have identical names and identical ordered sets of types of their parameters. Examples Input 4 void f(int,T) void f(T, T) void foo123 ( int, double, string,string ) void p(T,double) 3 int a string s double x123 5 f(a, a) f(s,a ) foo (a,s,s) f ( s ,x123) proc(a) Input 6 void f(string,double,int) void f(int) void f ( T ) void procedure(int,double) void f (T, double,int) void f(string, T,T) 4 int a int x string t double val 5 f(t, a, a) f(t,val,a) f(val,a, val) solve300(val, val) f (x) | 1,800 | false | false | true | false | false | false | true | true | false | false | 9,041 |
1215A | The final match of the Berland Football Cup has been held recently. The referee has shown $$$n$$$ yellow cards throughout the match. At the beginning of the match there were $$$a_1$$$ players in the first team and $$$a_2$$$ players in the second team. The rules of sending players off the game are a bit different in Berland football. If a player from the first team receives $$$k_1$$$ yellow cards throughout the match, he can no longer participate in the match — he's sent off. And if a player from the second team receives $$$k_2$$$ yellow cards, he's sent off. After a player leaves the match, he can no longer receive any yellow cards. Each of $$$n$$$ yellow cards was shown to exactly one player. Even if all players from one team (or even from both teams) leave the match, the game still continues. The referee has lost his records on who has received each yellow card. Help him to determine the minimum and the maximum number of players that could have been thrown out of the game. Input The first line contains one integer $$$a_1$$$ $$$(1 le a_1 le 1,000)$$$ — the number of players in the first team. The second line contains one integer $$$a_2$$$ $$$(1 le a_2 le 1,000)$$$ — the number of players in the second team. The third line contains one integer $$$k_1$$$ $$$(1 le k_1 le 1,000)$$$ — the maximum number of yellow cards a player from the first team can receive (after receiving that many yellow cards, he leaves the game). The fourth line contains one integer $$$k_2$$$ $$$(1 le k_2 le 1,000)$$$ — the maximum number of yellow cards a player from the second team can receive (after receiving that many yellow cards, he leaves the game). The fifth line contains one integer $$$n$$$ $$$(1 le n le a_1 cdot k_1 + a_2 cdot k_2)$$$ — the number of yellow cards that have been shown during the match. Output Print two integers — the minimum and the maximum number of players that could have been thrown out of the game. Note In the first example it could be possible that no player left the game, so the first number in the output is $$$0$$$. The maximum possible number of players that could have been forced to leave the game is $$$4$$$ — one player from the first team, and three players from the second. In the second example the maximum possible number of yellow cards has been shown $$$(3 cdot 6 + 1 cdot 7 = 25)$$$, so in any case all players were sent off. | 1,000 | true | true | true | false | false | false | false | false | false | false | 4,603 |
1343A | Recently Vova found $$$n$$$ candy wrappers. He remembers that he bought $$$x$$$ candies during the first day, $$$2x$$$ candies during the second day, $$$4x$$$ candies during the third day, $$$dots$$$, $$$2^{k-1} x$$$ candies during the $$$k$$$-th day. But there is an issue: Vova remembers neither $$$x$$$ nor $$$k$$$ but he is sure that $$$x$$$ and $$$k$$$ are positive integers and $$$k > 1$$$. Vova will be satisfied if you tell him any positive integer $$$x$$$ so there is an integer $$$k>1$$$ that $$$x + 2x + 4x + dots + 2^{k-1} x = n$$$. It is guaranteed that at least one solution exists. Note that $$$k > 1$$$. You have to answer $$$t$$$ independent test cases. Input The first line of the input contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. The only line of the test case contains one integer $$$n$$$ ($$$3 le n le 10^9$$$) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer $$$x$$$ and integer $$$k>1$$$ that $$$x + 2x + 4x + dots + 2^{k-1} x = n$$$. Output Print one integer — any positive integer value of $$$x$$$ so there is an integer $$$k>1$$$ that $$$x + 2x + 4x + dots + 2^{k-1} x = n$$$. Example Input 7 3 6 7 21 28 999999999 999999984 Output 1 2 1 7 4 333333333 333333328 Note In the first test case of the example, one of the possible answers is $$$x=1, k=2$$$. Then $$$1 cdot 1 + 2 cdot 1$$$ equals $$$n=3$$$. In the second test case of the example, one of the possible answers is $$$x=2, k=2$$$. Then $$$1 cdot 2 + 2 cdot 2$$$ equals $$$n=6$$$. In the third test case of the example, one of the possible answers is $$$x=1, k=3$$$. Then $$$1 cdot 1 + 2 cdot 1 + 4 cdot 1$$$ equals $$$n=7$$$. In the fourth test case of the example, one of the possible answers is $$$x=7, k=2$$$. Then $$$1 cdot 7 + 2 cdot 7$$$ equals $$$n=21$$$. In the fifth test case of the example, one of the possible answers is $$$x=4, k=3$$$. Then $$$1 cdot 4 + 2 cdot 4 + 4 cdot 4$$$ equals $$$n=28$$$. | 900 | true | false | false | false | false | false | true | false | false | false | 3,993 |
1152A | On a random day, Neko found $$$n$$$ treasure chests and $$$m$$$ keys. The $$$i$$$-th chest has an integer $$$a_i$$$ written on it and the $$$j$$$-th key has an integer $$$b_j$$$ on it. Neko knows those chests contain the powerful mysterious green Grapes, thus Neko wants to open as many treasure chests as possible. The $$$j$$$-th key can be used to unlock the $$$i$$$-th chest if and only if the sum of the key number and the chest number is an odd number. Formally, $$$a_i + b_j equiv 1 pmod{2}$$$. One key can be used to open at most one chest, and one chest can be opened at most once. Find the maximum number of chests Neko can open. Input The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 10^5$$$)xa0— the number of chests and the number of keys. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the numbers written on the treasure chests. The third line contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 leq b_i leq 10^9$$$)xa0— the numbers written on the keys. Output Print the maximum number of chests you can open. Examples Input 5 4 9 14 6 2 11 8 4 7 20 Note In the first example, one possible way to unlock $$$3$$$ chests is as follows: Use first key to unlock the fifth chest, Use third key to unlock the second chest, Use fourth key to unlock the first chest. In the second example, you can use the only key to unlock any single chest (note that one key can't be used twice). In the third example, no key can unlock the given chest. | 800 | true | true | true | false | false | false | false | false | false | false | 4,956 |
720E | Borya has recently found a big electronic display. The computer that manages the display stores some integer number. The number has _n_ decimal digits, the display shows the encoded version of the number, where each digit is shown using some lowercase letter of the English alphabet. There is a legend near the display, that describes how the number is encoded. For each digit position _i_ and each digit _j_ the character _c_ is known, that encodes this digit at this position. Different digits can have the same code characters. Each second the number is increased by 1. And one second after a moment when the number reaches the value that is represented as _n_ 9-s in decimal notation, the loud beep sounds. Andrew knows the number that is stored in the computer. Now he wants to know how many seconds must pass until Borya can definitely tell what was the original number encoded by the display. Assume that Borya can precisely measure time, and that the encoded number will first be increased exactly one second after Borya started watching at the display. Input Input data contains multiple test cases. The first line of input contains _t_ (1u2009≤u2009_t_u2009≤u2009100)xa0— the number of test cases. Each test case is described as follows. The first line of the description contains _n_ (1u2009≤u2009_n_u2009≤u200918)xa0— the number of digits in the number. The second line contains _n_ decimal digits without spaces (but possibly with leading zeroes)xa0— the number initially stored in the display computer. The following _n_ lines contain 10 characters each. The _j_-th character of the _i_-th of these lines is the code character for a digit _j_u2009-u20091 in position _i_, most significant digit positions are described first. Output For each test case print an integer: the number of seconds until Borya definitely knows what was the initial number stored on the display of the computer. Do not print leading zeroes. Example Input 3 2 42 abcdefghij jihgfedcba 2 42 aaaaaaaaaa aaaaaaaaaa 1 2 abcdabcdff | 3,100 | false | false | true | false | false | false | false | false | false | false | 6,933 |
650C | Little Petya is now fond of data compression algorithms. He has already studied gz, bz, zip algorithms and many others. Inspired by the new knowledge, Petya is now developing the new compression algorithm which he wants to name dis. Petya decided to compress tables. He is given a table _a_ consisting of _n_ rows and _m_ columns that is filled with positive integers. He wants to build the table _a_' consisting of positive integers such that the relative order of the elements in each row and each column remains the same. That is, if in some row _i_ of the initial table _a__i_,u2009_j_u2009<u2009_a__i_,u2009_k_, then in the resulting table _a_'_i_,u2009_j_u2009<u2009_a_'_i_,u2009_k_, and if _a__i_,u2009_j_u2009=u2009_a__i_,u2009_k_ then _a_'_i_,u2009_j_u2009=u2009_a_'_i_,u2009_k_. Similarly, if in some column _j_ of the initial table _a__i_,u2009_j_u2009<u2009_a__p_,u2009_j_ then in compressed table _a_'_i_,u2009_j_u2009<u2009_a_'_p_,u2009_j_ and if _a__i_,u2009_j_u2009=u2009_a__p_,u2009_j_ then _a_'_i_,u2009_j_u2009=u2009_a_'_p_,u2009_j_. Because large values require more space to store them, the maximum value in _a_' should be as small as possible. Petya is good in theory, however, he needs your help to implement the algorithm. Input The first line of the input contains two integers _n_ and _m_ (, the number of rows and the number of columns of the table respectively. Each of the following _n_ rows contain _m_ integers _a__i_,u2009_j_ (1u2009≤u2009_a__i_,u2009_j_u2009≤u2009109) that are the values in the table. Output Output the compressed table in form of _n_ lines each containing _m_ integers. If there exist several answers such that the maximum number in the compressed table is minimum possible, you are allowed to output any of them. Note In the first sample test, despite the fact _a_1,u20092u2009≠u2009_a_21, they are not located in the same row or column so they may become equal after the compression. | 2,200 | false | true | false | true | false | false | false | false | false | true | 7,215 |
1081D | Chouti was tired of the tedious homework, so he opened up an old programming problem he created years ago. You are given a connected undirected graph with $$$n$$$ vertices and $$$m$$$ weighted edges. There are $$$k$$$ special vertices: $$$x_1, x_2, ldots, x_k$$$. Let's define the cost of the path as the maximum weight of the edges in it. And the distance between two vertexes as the minimum cost of the paths connecting them. For each special vertex, find another special vertex which is farthest from it (in terms of the previous paragraph, i.e. the corresponding distance is maximum possible) and output the distance between them. The original constraints are really small so he thought the problem was boring. Now, he raises the constraints and hopes you can solve it for him. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 leq k leq n leq 10^5$$$, $$$n-1 leq m leq 10^5$$$)xa0— the number of vertices, the number of edges and the number of special vertices. The second line contains $$$k$$$ distinct integers $$$x_1, x_2, ldots, x_k$$$ ($$$1 leq x_i leq n$$$). Each of the following $$$m$$$ lines contains three integers $$$u$$$, $$$v$$$ and $$$w$$$ ($$$1 leq u,v leq n, 1 leq w leq 10^9$$$), denoting there is an edge between $$$u$$$ and $$$v$$$ of weight $$$w$$$. The given graph is undirected, so an edge $$$(u, v)$$$ can be used in the both directions. The graph may have multiple edges and self-loops. It is guaranteed, that the graph is connected. Output The first and only line should contain $$$k$$$ integers. The $$$i$$$-th integer is the distance between $$$x_i$$$ and the farthest special vertex from it. Examples Input 2 3 2 2 1 1 2 3 1 2 2 2 2 1 Input 4 5 3 1 2 3 1 2 5 4 2 1 2 3 2 1 4 4 1 3 3 Note In the first example, the distance between vertex $$$1$$$ and $$$2$$$ equals to $$$2$$$ because one can walk through the edge of weight $$$2$$$ connecting them. So the distance to the farthest node for both $$$1$$$ and $$$2$$$ equals to $$$2$$$. In the second example, one can find that distance between $$$1$$$ and $$$2$$$, distance between $$$1$$$ and $$$3$$$ are both $$$3$$$ and the distance between $$$2$$$ and $$$3$$$ is $$$2$$$. The graph may have multiple edges between and self-loops, as in the first example. | 1,800 | false | false | false | false | false | false | false | false | true | true | 5,320 |
1681F | Problem - 1681F - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags data structures dfs and similar divide and conquer dp dsu trees *2300 No tag edit access → Contest materials $$$ be the number of values that appear exactly once on the edges of a simple path between vertices $$$v$$$ and $$$u$$$. Calculate the sum of $$$f(v, u)$$$ over all pairs of vertices $$$v$$$ and $$$u$$$ such that $$$1 le v < u le n$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 5 cdot 10^5$$$)xa0— the number of vertices in the tree. Each of the next $$$n-1$$$ lines contains three integers $$$v, u$$$ and $$$x$$$ ($$$1 le v, u, x le n$$$)xa0— the description of an edge: the vertices it connects and the value written on it. The given edges form a tree. Output Print a single integerxa0— the sum of $$$f(v, u)$$$ over all pairs of vertices $$$v$$$ and $$$u$$$ such that $$$v < u$$$. Examples Input 3 1 2 1 1 3 2 Output 4 Input 3 1 2 2 1 3 2 Output 2 Input 5 1 4 4 1 2 3 3 4 4 4 5 5 Output 14 Input 2 2 1 1 Output 1 Input 10 10 2 3 3 8 8 4 8 9 5 8 5 3 10 7 7 8 2 5 6 6 9 3 4 1 6 3 Output 120 | 2,300 | false | false | false | true | true | false | false | false | false | false | 2,180 |
1110H | Problem - 1110H - Codeforces =============== xa0 . The second line contains one integer $$$r$$$ ($$$l le r le 10^{800}$$$). The third line contains one integer $$$n$$$ ($$$1 le n le 2,000$$$). Output In the first line, print the maximum possible number of modest substrings. In the second line, print a string of length $$$n$$$ having exactly that number of modest substrings. If there are multiple such strings, print the lexicographically smallest of them. Examples Input 1 10 3 Output 3 101 Input 1 11 3 Output 5 111 Input 12345 12346 6 Output 1 012345 Note In the first example, string «101» has modest substrings «1», «10», «1». In the second example, string «111» has modest substrings «1» ($$$3$$$ times) and «11» ($$$2$$$ times). | 3,500 | false | false | false | true | false | false | false | false | false | false | 5,142 |
1713F | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 812 (Div. 2) Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags bitmasks combinatorics constructive algorithms dp math *2900 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST F. Lost Array time limit per test1 second memory limit per test256 megabytes My orzlers, we can optimize this problem from $$$O(S^3)$$$ to $$$Oleft(T^frac{5}{9} ight)$$$! — Spyofgame, founder of Orzlim religion A long time ago, Spyofgame invented the famous array $$$a$$$ ($$$1$$$-indexed) of length $$$n$$$ that contains information about the world and life. After that, he decided to convert it into the matrix $$$b$$$ ($$$0$$$-indexed) of size $$$(n + 1) imes (n + 1)$$$ which contains information about the world, life and beyond. Spyofgame converted $$$a$$$ into $$$b$$$ with the following rules. $$$b_{i,0} = 0$$$ if $$$0 leq i leq n$$$; $$$b_{0,i} = a_{i}$$$ if $$$1 leq i leq n$$$; $$$b_{i,j} = b_{i,j-1} oplus b_{i-1,j}$$$ if $$$1 leq i, j leq n$$$. Here $$$oplus$$$ denotes the bitwise XOR operation. Today, archaeologists have discovered the famous matrix $$$b$$$. However, many elements of the matrix has been lost. They only know the values of $$$b_{i,n}$$$ for $$$1 leq i leq n$$$ (note that these are some elements of the last column, not the last row). The archaeologists want to know what a possible array of $$$a$$$ is. Can you help them reconstruct any array that could be $$$a$$$? Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 5 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$b_{1,n}, b_{2,n}, ldots, b_{n,n}$$$ ($$$0 leq b_{i,n} < 2^{30}$$$). Output If some array $$$a$$$ is consistent with the information, print a line containing $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$. If there are multiple solutions, output any. If such an array does not exist, output $$$-1$$$ instead. Examples input 3 0 2 1 output 1 2 3 input 1 199633 output 199633 input 10 346484077 532933626 858787727 369947090 299437981 416813461 865836801 141384800 157794568 691345607 output 725081944 922153789 481174947 427448285 516570428 509717938 855104873 280317429 281091129 1050390365 Note If we let $$$a = [1,2,3]$$$, then $$$b$$$ will be: $$$bf{0}$$$ $$$bf{1}$$$ $$$bf{2}$$$ $$$bf{3}$$$ $$$bf{0}$$$ $$$1$$$ $$$3$$$ $$$0$$$ $$$bf{0}$$$ $$$1$$$ $$$2$$$ $$$2$$$ $$$bf{0}$$$ $$$1$$$ $$$3$$$ $$$1$$$ The values of $$$b_{1,n}, b_{2,n}, ldots, b_{n,n}$$$ generated are $$$[0,2,1]$$$ which is consistent with what the archaeologists have discovered. Codeforces (c) | 2,900 | true | false | false | true | false | true | false | false | false | false | 2,001 |
452D | You have _k_ pieces of laundry, each of which you want to wash, dry and fold. You are at a laundromat that has _n_1 washing machines, _n_2 drying machines and _n_3 folding machines. Each machine can process only one piece of laundry at a time. You can't dry a piece of laundry before it is washed, and you can't fold it before it is dried. Moreover, after a piece of laundry is washed, it needs to be immediately moved into a drying machine, and after it is dried, it needs to be immediately moved into a folding machine. It takes _t_1 minutes to wash one piece of laundry in a washing machine, _t_2 minutes to dry it in a drying machine, and _t_3 minutes to fold it in a folding machine. Find the smallest number of minutes that is enough to wash, dry and fold all the laundry you have. Input The only line of the input contains seven integers: _k_,u2009_n_1,u2009_n_2,u2009_n_3,u2009_t_1,u2009_t_2,u2009_t_3 (1u2009≤u2009_k_u2009≤u2009104;xa01u2009≤u2009_n_1,u2009_n_2,u2009_n_3,u2009_t_1,u2009_t_2,u2009_t_3u2009≤u20091000). Output Print one integer — smallest number of minutes to do all your laundry. Note In the first example there's one instance of each machine, each taking 5 minutes to complete. You have only one piece of laundry, so it takes 15 minutes to process it. In the second example you start washing first two pieces at moment 0. If you start the third piece of laundry immediately, then by the time it is dried, there will be no folding machine available, so you have to wait, and start washing third piece at moment 2. Similarly, you can't start washing next piece until moment 5, since otherwise there will be no dryer available, when it is washed. Start time for each of the eight pieces of laundry is 0,u20090,u20092,u20095,u200910,u200910,u200912 and 15 minutes respectively. The last piece of laundry will be ready after 15u2009+u200910u2009+u20095u2009+u20092u2009=u200932 minutes. | 1,900 | false | true | true | false | false | false | false | false | false | false | 8,029 |
1360C | We call two numbers $$$x$$$ and $$$y$$$ similar if they have the same parity (the same remainder when divided by $$$2$$$), or if $$$x-y=1$$$. For example, in each of the pairs $$$(2, 6)$$$, $$$(4, 3)$$$, $$$(11, 7)$$$, the numbers are similar to each other, and in the pairs $$$(1, 4)$$$, $$$(3, 12)$$$, they are not. You are given an array $$$a$$$ of $$$n$$$ ($$$n$$$ is even) positive integers. Check if there is such a partition of the array into pairs that each element of the array belongs to exactly one pair and the numbers in each pair are similar to each other. For example, for the array $$$a = [11, 14, 16, 12]$$$, there is a partition into pairs $$$(11, 12)$$$ and $$$(14, 16)$$$. The numbers in the first pair are similar because they differ by one, and in the second pair because they are both even. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains an even positive integer $$$n$$$ ($$$2 le n le 50$$$)xa0— length of array $$$a$$$. The second line contains $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 100$$$). Note The first test case was explained in the statement. In the second test case, the two given numbers are not similar. In the third test case, any partition is suitable. | 1,100 | false | true | false | false | false | true | false | false | true | false | 3,883 |
1620A | You had $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ arranged in a circle. For each pair of neighboring numbers ($$$a_1$$$ and $$$a_2$$$, $$$a_2$$$ and $$$a_3$$$, ..., $$$a_{n - 1}$$$ and $$$a_n$$$, and $$$a_n$$$ and $$$a_1$$$), you wrote down: are the numbers in the pair equal or not. Unfortunately, you've lost a piece of paper with the array $$$a$$$. Moreover, you are afraid that even information about equality of neighboring elements may be inconsistent. So, you are wondering: is there any array $$$a$$$ which is consistent with information you have about equality or non-equality of corresponding pairs? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains a non-empty string $$$s$$$ consisting of characters E and/or N. The length of $$$s$$$ is equal to the size of array $$$n$$$ and $$$2 le n le 50$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$s_i =$$$ E then $$$a_i$$$ is equal to $$$a_{i + 1}$$$ ($$$a_n = a_1$$$ for $$$i = n$$$); if $$$s_i =$$$ N then $$$a_i$$$ is not equal to $$$a_{i + 1}$$$ ($$$a_n eq a_1$$$ for $$$i = n$$$). Output For each test case, print YES if it's possible to choose array $$$a$$$ that are consistent with information from $$$s$$$ you know. Otherwise, print NO. It can be proved, that if there exists some array $$$a$$$, then there exists an array $$$a$$$ of positive integers with values less or equal to $$$10^9$$$. Example Input 4 EEE EN ENNEENE NENN Note In the first test case, you can choose, for example, $$$a_1 = a_2 = a_3 = 5$$$. In the second test case, there is no array $$$a$$$, since, according to $$$s_1$$$, $$$a_1$$$ is equal to $$$a_2$$$, but, according to $$$s_2$$$, $$$a_2$$$ is not equal to $$$a_1$$$. In the third test case, you can, for example, choose array $$$a = [20, 20, 4, 50, 50, 50, 20]$$$. In the fourth test case, you can, for example, choose $$$a = [1, 3, 3, 7]$$$. | 800 | false | false | true | false | false | true | false | false | false | false | 2,527 |
1790D | Matryoshka is a wooden toy in the form of a painted doll, inside which you can put a similar doll of a smaller size. A set of nesting dolls contains one or more nesting dolls, their sizes are consecutive positive integers. Thus, a set of nesting dolls is described by two numbers: $$$s$$$ — the size of a smallest nesting doll in a set and $$$m$$$ — the number of dolls in a set. In other words, the set contains sizes of $$$s, s + 1, dots, s + m - 1$$$ for some integer $$$s$$$ and $$$m$$$ ($$$s,m > 0$$$). You had one or more sets of nesting dolls. Recently, you found that someone mixed all your sets in one and recorded a sequence of doll sizes — integers $$$a_1, a_2, dots, a_n$$$. You do not remember how many sets you had, so you want to find the minimum number of sets that you could initially have. For example, if a given sequence is $$$a=[2, 2, 3, 4, 3, 1]$$$. Initially, there could be $$$2$$$ sets: the first set consisting of $$$4$$$ nesting dolls with sizes $$$[1, 2, 3, 4]$$$; a second set consisting of $$$2$$$ nesting dolls with sizes $$$[2, 3]$$$. According to a given sequence of sizes of nesting dolls $$$a_1, a_2, dots, a_n$$$, determine the minimum number of nesting dolls that can make this sequence. Each set is completely used, so all its nesting dolls are used. Each element of a given sequence must correspond to exactly one doll from some set. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0— the total number of matryoshkas that were in all sets. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) xa0— the sizes of the matryoshkas. It is guaranteed that the sum of values of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. Note The first test case is described in the problem statement. In the second test case, all matryoshkas could be part of the same set with minimum size $$$s=7$$$. In the third test case, each matryoshka represents a separate set. | 1,200 | false | true | false | false | true | false | false | false | true | false | 1,539 |
1986G2 | This is the hard version of the problem. The only difference is that in this version $$$n leq 5 cdot 10^5$$$ and the sum of $$$n$$$ for all sets of input data does not exceed $$$5 cdot 10^5$$$. You are given a permutation $$$p$$$ of length $$$n$$$. Calculate the number of index pairs $$$1 leq i < j leq n$$$ such that $$$p_i cdot p_j$$$ is divisible by $$$i cdot j$$$ without remainder. A permutation is a sequence of $$$n$$$ integers, in which each integer from $$$1$$$ to $$$n$$$ occurs exactly once. For example, $$$[1]$$$, $$$[3,5,2,1,4]$$$, $$$[1,3,2]$$$ are permutations, while $$$[2,3,2]$$$, $$$[4,3,1]$$$, $$$[0]$$$ are not. Input Each test consists of several sets of input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of sets of input data. Then follows their description. The first line of each set of input data contains a single integer $$$n$$$ ($$$1 leq n leq 5 cdot 10^5$$$)xa0— the length of the permutation $$$p$$$. The second line of each set of input data contains $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$)xa0— the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ for all sets of input data does not exceed $$$5 cdot 10^5$$$. Output For each set of input data, output the number of index pairs $$$1 leq i < j leq n$$$ such that $$$p_i cdot p_j$$$ is divisible by $$$i cdot j$$$ without remainder. Example Input 6 1 1 2 1 2 3 2 3 1 5 2 4 1 3 5 12 8 9 7 12 1 10 6 3 2 4 11 5 15 1 2 4 6 8 10 12 14 3 9 15 5 7 11 13 Note In the first set of input data, there are no index pairs, as the size of the permutation is $$$1$$$. In the second set of input data, there is one index pair $$$(1, 2)$$$ and it is valid. In the third set of input data, the index pair $$$(1, 2)$$$ is valid. In the fourth set of input data, the index pairs $$$(1, 2)$$$, $$$(1, 5)$$$, and $$$(2, 5)$$$ are valid. | 2,500 | true | false | false | false | true | false | true | false | false | false | 358 |
1029E | You are given an undirected tree consisting of $$$n$$$ vertices. An undirected tree is a connected undirected graph with $$$n - 1$$$ edges. Your task is to add the minimum number of edges in such a way that the length of the shortest path from the vertex $$$1$$$ to any other vertex is at most $$$2$$$. Note that you are not allowed to add loops and multiple edges. Input The first line contains one integer $$$n$$$ ($$$2 le n le 2 cdot 10^5$$$) — the number of vertices in the tree. The following $$$n - 1$$$ lines contain edges: edge $$$i$$$ is given as a pair of vertices $$$u_i, v_i$$$ ($$$1 le u_i, v_i le n$$$). It is guaranteed that the given edges form a tree. It is guaranteed that there are no loops and multiple edges in the given edges. Output Print a single integer — the minimum number of edges you have to add in order to make the shortest distance from the vertex $$$1$$$ to any other vertex at most $$$2$$$. Note that you are not allowed to add loops and multiple edges. Note The tree corresponding to the first example: The answer is $$$2$$$, some of the possible answers are the following: $$$[(1, 5), (1, 6)]$$$, $$$[(1, 4), (1, 7)]$$$, $$$[(1, 6), (1, 7)]$$$. The tree corresponding to the second example: The answer is $$$0$$$. The tree corresponding to the third example: The answer is $$$1$$$, only one possible way to reach it is to add the edge $$$(1, 3)$$$. | 2,100 | false | true | false | true | false | false | false | false | false | true | 5,557 |
1469A | A bracket sequence is called regular if it is possible to obtain correct arithmetic expression by inserting characters + and 1 into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not. Let's call a regular bracket sequence "RBS". You are given a sequence $$$s$$$ of $$$n$$$ characters (, ), and/or ?. There is exactly one character ( and exactly one character ) in this sequence. You have to replace every character ? with either ) or ( (different characters ? can be replaced with different brackets). You cannot reorder the characters, remove them, insert other characters, and each ? must be replaced. Determine if it is possible to obtain an RBS after these replacements. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. Each test case consists of one line containing $$$s$$$ ($$$2 le s le 100$$$) — a sequence of characters (, ), and/or ?. There is exactly one character ( and exactly one character ) in this sequence. Output For each test case, print YES if it is possible to obtain a regular bracket sequence, or NO otherwise}. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). Example Input 5 () (?) (??) ??() )?(? Note In the first test case, the sequence is already an RBS. In the third test case, you can obtain an RBS as follows: ()() or (()). In the fourth test case, you can obtain an RBS as follows: ()(). | 1,000 | false | true | false | false | false | true | false | false | false | false | 3,338 |
1593C | There are one cat, $$$k$$$ mice, and one hole on a coordinate line. The cat is located at the point $$$0$$$, the hole is located at the point $$$n$$$. All mice are located between the cat and the hole: the $$$i$$$-th mouse is located at the point $$$x_i$$$ ($$$0 < x_i < n$$$). At each point, many mice can be located. In one second, the following happens. First, exactly one mouse moves to the right by $$$1$$$. If the mouse reaches the hole, it hides (i.e. the mouse will not any more move to any point and will not be eaten by the cat). Then (after that the mouse has finished its move) the cat moves to the right by $$$1$$$. If at the new cat's position, some mice are located, the cat eats them (they will not be able to move after that). The actions are performed until any mouse hasn't been hidden or isn't eaten. In other words, the first move is made by a mouse. If the mouse has reached the hole, it's saved. Then the cat makes a move. The cat eats the mice located at the pointed the cat has reached (if the cat has reached the hole, it eats nobody). Each second, you can select a mouse that will make a move. What is the maximum number of mice that can reach the hole without being eaten? Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^9$$$, $$$1 le k le 4 cdot 10^5$$$). The second line contains $$$k$$$ integers $$$x_1, x_2, dots x_k$$$ ($$$1 le x_i < n$$$) — the initial coordinates of the mice. It is guaranteed that the sum of all $$$k$$$ given in the input doesn't exceed $$$4 cdot 10^5$$$. | 1,000 | false | true | false | false | false | false | false | true | false | false | 2,684 |
1917F | You are given an array of integers $$$l_1, l_2, dots, l_n$$$ and an integer $$$d$$$. Is it possible to construct a tree satisfying the following three conditions? The tree contains $$$n + 1$$$ nodes. The length of the $$$i$$$-th edge is equal to $$$l_i$$$. The (weighted) diameter of the tree is equal to $$$d$$$. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 250$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $$$n$$$, $$$d$$$ ($$$2 leq n leq 2000, 1 leq d leq 2000$$$). The second line of each test case contains $$$n$$$ integers $$$l_1, l_2, dots, l_n$$$ ($$$1 leq l_i leq d$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output $$$ exttt{Yes}$$$ if it is possible to construct a tree that satisfies all the conditions, and $$$ exttt{No}$$$ otherwise. You can print the letters in any case (upper or lower). Example Input 3 4 10 1 2 3 4 4 7 1 4 3 4 6 18 2 4 3 7 6 7 Note Below, you are given the illustrations of trees for the first and third test cases. One of the diameters is highlighted by coloring its edges in red. | 2,500 | false | false | false | true | false | true | false | false | false | false | 789 |
1155D | You are given an array $$$a$$$ consisting of $$$n$$$ integers. Beauty of array is the maximum sum of some consecutive subarray of this array (this subarray may be empty). For example, the beauty of the array [10, -5, 10, -4, 1] is 15, and the beauty of the array [-3, -5, -1] is 0. You may choose at most one consecutive subarray of $$$a$$$ and multiply all values contained in this subarray by $$$x$$$. You want to maximize the beauty of array after applying at most one such operation. Input The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 3 cdot 10^5, -100 le x le 100$$$) — the length of array $$$a$$$ and the integer $$$x$$$ respectively. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$) — the array $$$a$$$. Output Print one integer — the maximum possible beauty of array $$$a$$$ after multiplying all values belonging to some consecutive subarray $$$x$$$. Examples Input 12 -3 1 3 3 7 1 3 3 7 1 3 3 7 Note In the first test case we need to multiply the subarray [-2, 1, -6], and the array becomes [-3, 8, 4, -2, 12] with beauty 22 ([-3, 8, 4, -2, 12]). In the second test case we don't need to multiply any subarray at all. In the third test case no matter which subarray we multiply, the beauty of array will be equal to 0. | 1,900 | false | true | false | true | true | false | true | false | false | false | 4,933 |
1872B | You are in a corridor that extends infinitely to the right, divided into square rooms. You start in room $$$1$$$, proceed to room $$$k$$$, and then return to room $$$1$$$. You can choose the value of $$$k$$$. Moving to an adjacent room takes $$$1$$$ second. Additionally, there are $$$n$$$ traps in the corridor: the $$$i$$$-th trap is located in room $$$d_i$$$ and will be activated $$$s_i$$$ seconds after you enter the room $$$boldsymbol{d_i}$$$. Once a trap is activated, you cannot enter or exit a room with that trap. A schematic representation of a possible corridor and your path to room $$$k$$$ and back. Determine the maximum value of $$$k$$$ that allows you to travel from room $$$1$$$ to room $$$k$$$ and then return to room $$$1$$$ safely. For instance, if $$$n=1$$$ and $$$d_1=2, s_1=2$$$, you can proceed to room $$$k=2$$$ and return safely (the trap will activate at the moment $$$1+s_1=1+2=3$$$, it can't prevent you to return back). But if you attempt to reach room $$$k=3$$$, the trap will activate at the moment $$$1+s_1=1+2=3$$$, preventing your return (you would attempt to enter room $$$2$$$ on your way back at second $$$3$$$, but the activated trap would block you). Any larger value for $$$k$$$ is also not feasible. Thus, the answer is $$$k=2$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The descriptions of the test cases follow. The first line of each test case description contains an integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the number of traps. The following $$$n$$$ lines of each test case description present two integers $$$d_i$$$ and $$$s_i$$$ ($$$1 le d_i, s_i le 200$$$)xa0— the parameters of a trap (you must leave room $$$d_i$$$ strictly before $$$s_i$$$ seconds have passed since entering this room). It's possible for multiple traps to occupy a single room (the values of $$$d_i$$$ can be repeated). Output For each test case, print the maximum value of $$$k$$$ that allows you to travel to room $$$k$$$ and return to room $$$1$$$ without encountering an active trap. Example Input 7 1 2 2 3 2 8 4 3 5 2 1 200 200 4 1 20 5 9 3 179 100 1 2 10 1 1 18 2 1 1 1 2 3 1 3 1 1 1 3 Note The first test case is explained in the problem statement above. In the second test case, the second trap prevents you from achieving $$$kge6$$$. If $$$kge6$$$, the second trap will activate at the moment $$$3+s_2=3+3=6$$$ (the time you enter room $$$4$$$ plus $$$s_2$$$). In the case of $$$kge6$$$, you will return to room $$$4$$$ at time $$$7$$$ or later. The trap will be active at that time. It can be shown that room $$$k=5$$$ can be reached without encountering an active trap. In the third test case, you can make it to room $$$299$$$ and then immediately return to room $$$1$$$. | 900 | false | true | true | false | false | false | false | false | false | false | 1,050 |
444C | DZY loves colors, and he enjoys painting. On a colorful day, DZY gets a colorful ribbon, which consists of _n_ units (they are numbered from 1 to _n_ from left to right). The color of the _i_-th unit of the ribbon is _i_ at first. It is colorful enough, but we still consider that the colorfulness of each unit is 0 at first. DZY loves painting, we know. He takes up a paintbrush with color _x_ and uses it to draw a line on the ribbon. In such a case some contiguous units are painted. Imagine that the color of unit _i_ currently is _y_. When it is painted by this paintbrush, the color of the unit becomes _x_, and the colorfulness of the unit increases by _x_u2009-u2009_y_. DZY wants to perform _m_ operations, each operation can be one of the following: 1. Paint all the units with numbers between _l_ and _r_ (both inclusive) with color _x_. 2. Ask the sum of colorfulness of the units between _l_ and _r_ (both inclusive). Can you help DZY? Input The first line contains two space-separated integers _n_,u2009_m_xa0(1u2009≤u2009_n_,u2009_m_u2009≤u2009105). Each of the next _m_ lines begins with a integer _type_xa0(1u2009≤u2009_type_u2009≤u20092), which represents the type of this operation. If _type_u2009=u20091, there will be 3 more integers _l_,u2009_r_,u2009_x_xa0(1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_;xa01u2009≤u2009_x_u2009≤u2009108) in this line, describing an operation 1. If _type_u2009=u20092, there will be 2 more integers _l_,u2009_r_xa0(1u2009≤u2009_l_u2009≤u2009_r_u2009≤u2009_n_) in this line, describing an operation 2. Output For each operation 2, print a line containing the answer — sum of colorfulness. Examples Input 3 3 1 1 2 4 1 2 3 5 2 1 3 Input 3 4 1 1 3 4 2 1 1 2 2 2 2 3 3 Input 10 6 1 1 5 3 1 2 7 9 1 10 10 11 1 3 8 12 1 1 10 3 2 1 10 Note In the first sample, the color of each unit is initially [1,u20092,u20093], and the colorfulness is [0,u20090,u20090]. After the first operation, colors become [4,u20094,u20093], colorfulness become [3,u20092,u20090]. After the second operation, colors become [4,u20095,u20095], colorfulness become [3,u20093,u20092]. So the answer to the only operation of type 2 is 8. | 2,400 | false | false | false | false | true | false | false | false | false | false | 8,061 |
220B | The Little Elephant loves playing with arrays. He has array _a_, consisting of _n_ positive integers, indexed from 1 to _n_. Let's denote the number with index _i_ as _a__i_. Additionally the Little Elephant has _m_ queries to the array, each query is characterised by a pair of integers _l__j_ and _r__j_ (1u2009≤u2009_l__j_u2009≤u2009_r__j_u2009≤u2009_n_). For each query _l__j_,u2009_r__j_ the Little Elephant has to count, how many numbers _x_ exist, such that number _x_ occurs exactly _x_ times among numbers _a__l__j_,u2009_a__l__j_u2009+u20091,u2009...,u2009_a__r__j_. Help the Little Elephant to count the answers to all queries. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105) — the size of array _a_ and the number of queries to it. The next line contains _n_ space-separated positive integers _a_1, _a_2, ..., _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109). Next _m_ lines contain descriptions of queries, one per line. The _j_-th of these lines contains the description of the _j_-th query as two space-separated integers _l__j_ and _r__j_ (1u2009≤u2009_l__j_u2009≤u2009_r__j_u2009≤u2009_n_). Output In _m_ lines print _m_ integers — the answers to the queries. The _j_-th line should contain the answer to the _j_-th query. Examples Input 7 2 3 1 2 2 3 3 7 1 7 3 4 | 1,800 | false | false | false | false | true | true | false | false | false | false | 8,958 |
1387A | You are given an undirected graph where each edge has one of two colors: black or red. Your task is to assign a real number to each node so that: for each black edge the sum of values at its endpoints is $$$1$$$; for each red edge the sum of values at its endpoints is $$$2$$$; the sum of the absolute values of all assigned numbers is the smallest possible. Otherwise, if it is not possible, report that there is no feasible assignment of the numbers. Input The first line contains two integers $$$N$$$ ($$$1 leq N leq 100,000$$$) and $$$M$$$ ($$$0 leq M leq 200,000$$$): the number of nodes and the number of edges, respectively. The nodes are numbered by consecutive integers: $$$1, 2, ldots, N$$$. The next $$$M$$$ lines describe the edges. Each line contains three integers $$$a$$$, $$$b$$$ and $$$c$$$ denoting that there is an edge between nodes $$$a$$$ and $$$b$$$ ($$$1 leq a, b leq N$$$) with color $$$c$$$ ($$$1$$$ denotes black, $$$2$$$ denotes red). Output If there is a solution, the first line should contain the word "YES" and the second line should contain $$$N$$$ space-separated numbers. For each $$$i$$$ ($$$1 le i le N$$$), the $$$i$$$-th number should be the number assigned to the node $$$i$$$. Output should be such that: the sum of the numbers at the endpoints of each edge differs from the precise value by less than $$$10^{-6}$$$; the sum of the absolute values of all assigned numbers differs from the smallest possible by less than $$$10^{-6}$$$. If there are several valid solutions, output any of them. If there is no solution, the only line should contain the word "NO". Scoring Subtasks: 1. (5 points) $$$N leq 5$$$, $$$M leq 14$$$ 2. (12 points) $$$N leq 100$$$ 3. (17 points) $$$N leq 1000$$$ 4. (24 points) $$$N leq 10,000$$$ 5. (42 points) No further constraints Examples Input 4 4 1 2 1 2 3 2 1 3 2 3 4 1 Output YES 0.5 0.5 1.5 -0.5 Input 3 4 1 2 2 2 2 1 2 1 1 1 2 2 Note Note that in the second example the solution is not unique. | 2,100 | true | false | false | true | false | false | false | true | false | false | 3,737 |
687B | Problem - 687B - 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 chinese remainder theorem math number theory *1800 No tag edit access → Contest materials xa0— the number of ancient integers and value _k_ that is chosen by Pari. The second line contains _n_ integers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u20091u2009000u2009000). Output Print "Yes" (without quotes) if Arya has a winning strategy independent of value of _x_, or "No" (without quotes) otherwise. Examples Input 4 5 2 3 5 12 Output Yes Input 2 7 2 3 Output No Note In the first sample, Arya can understand because 5 is one of the ancient numbers. In the second sample, Arya can't be sure what is. For example 1 and 7 have the same remainders after dividing by 2 and 3, but they differ in remainders after dividing by 7. | 1,800 | true | false | false | false | false | false | false | false | false | false | 7,075 |
43D | The territory of Berland is represented by a rectangular field _n_u2009×u2009_m_ in size. The king of Berland lives in the capital, located on the upper left square (1,u20091). The lower right square has coordinates (_n_,u2009_m_). One day the king decided to travel through the whole country and return back to the capital, having visited every square (except the capital) exactly one time. The king must visit the capital exactly two times, at the very beginning and at the very end of his journey. The king can only move to the side-neighboring squares. However, the royal advise said that the King possibly will not be able to do it. But there is a way out — one can build the system of one way teleporters between some squares so that the king could fulfill his plan. No more than one teleporter can be installed on one square, every teleporter can be used any number of times, however every time it is used, it transports to the same given for any single teleporter square. When the king reaches a square with an installed teleporter he chooses himself whether he is or is not going to use the teleport. What minimum number of teleporters should be installed for the king to complete the journey? You should also compose the journey path route for the king. Input The first line contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009100,u20092u2009≤u2009 _n_ · _m_) — the field size. The upper left square has coordinates (1,u20091), and the lower right square has coordinates of (_n_,u2009_m_). Output On the first line output integer _k_ — the minimum number of teleporters. Then output _k_ lines each containing 4 integers _x_1 _y_1 _x_2 _y_2 (1u2009≤u2009_x_1,u2009_x_2u2009≤u2009_n_,u20091u2009≤u2009_y_1,u2009_y_2u2009≤u2009_m_) — the coordinates of the square where the teleporter is installed (_x_1,u2009_y_1), and the coordinates of the square where the teleporter leads (_x_2,u2009_y_2). Then print _nm_u2009+u20091 lines containing 2 numbers each — the coordinates of the squares in the order in which they are visited by the king. The travel path must start and end at (1,u20091). The king can move to side-neighboring squares and to the squares where a teleporter leads. Besides, he also should visit the capital exactly two times and he should visit other squares exactly one time. Examples Output 1 3 3 1 1 1 1 1 2 1 3 2 3 2 2 2 1 3 1 3 2 3 3 1 1 | 2,000 | false | false | true | false | false | true | true | false | false | false | 9,771 |
1294F | You are given an unweighted tree with $$$n$$$ vertices. Recall that a tree is a connected undirected graph without cycles. Your task is to choose three distinct vertices $$$a, b, c$$$ on this tree such that the number of edges which belong to at least one of the simple paths between $$$a$$$ and $$$b$$$, $$$b$$$ and $$$c$$$, or $$$a$$$ and $$$c$$$ is the maximum possible. See the notes section for a better understanding. The simple path is the path that visits each vertex at most once. Input The first line contains one integer number $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the number of vertices in the tree. Next $$$n - 1$$$ lines describe the edges of the tree in form $$$a_i, b_i$$$ ($$$1 le a_i$$$, $$$b_i le n$$$, $$$a_i e b_i$$$). It is guaranteed that given graph is a tree. Output In the first line print one integer $$$res$$$ — the maximum number of edges which belong to at least one of the simple paths between $$$a$$$ and $$$b$$$, $$$b$$$ and $$$c$$$, or $$$a$$$ and $$$c$$$. In the second line print three integers $$$a, b, c$$$ such that $$$1 le a, b, c le n$$$ and $$$a e, b e c, a e c$$$. If there are several answers, you can print any. Example Input 8 1 2 2 3 3 4 4 5 4 6 3 7 3 8 Note The picture corresponding to the first example (and another one correct answer): If you choose vertices $$$1, 5, 6$$$ then the path between $$$1$$$ and $$$5$$$ consists of edges $$$(1, 2), (2, 3), (3, 4), (4, 5)$$$, the path between $$$1$$$ and $$$6$$$ consists of edges $$$(1, 2), (2, 3), (3, 4), (4, 6)$$$ and the path between $$$5$$$ and $$$6$$$ consists of edges $$$(4, 5), (4, 6)$$$. The union of these paths is $$$(1, 2), (2, 3), (3, 4), (4, 5), (4, 6)$$$ so the answer is $$$5$$$. It can be shown that there is no better answer. | 2,000 | false | true | false | true | false | false | false | false | false | false | 4,224 |
1815C | You are given an integer $$$n$$$, as well as $$$m$$$ pairs of integers $$$(a_i,b_i)$$$, where $$$1leq a_i , b_i leq n$$$, $$$a_i e b_i$$$. You want to construct a sequence satisfying the following requirements: All elements in the sequence are integers between $$$1$$$ and $$$n$$$. There is exactly one element with value $$$1$$$ in the sequence. For each $$$i$$$ ($$$1 le i le m$$$), between any two elements (on different positions) in the sequence with value $$$a_i$$$, there is at least one element with value $$$b_i$$$. The sequence constructed has the maximum length among all possible sequences satisfying the above properties. Sometimes, it is possible that such a sequence can be arbitrarily long, in which case you should output "INFINITE". Otherwise, you should output "FINITE" and the sequence itself. If there are multiple possible constructions that yield the maximum length, output any. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 300$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 1500$$$, $$$0 le m le 5000$$$) — the maximum possible value of the element of the sequence and the number of pairs. The $$$i$$$-th of the next $$$m$$$ lines contain two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i , b_i le n$$$, $$$a_i e b_i$$$). $$$(a_i, b_i) e (a_j, b_j)$$$ for all $$$1 le i < j le m$$$. It is guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$1500$$$ and the sum of $$$m$$$ over all test cases doesn't exceed $$$5000$$$. Output For each test case, on the first line output "INFINITE" if the sequence can be arbitrarily long, and "FINITE" otherwise. If you have outputted "FINITE", then your output should be followed by $$$2$$$ lines. The first line contains an integer $$$s$$$, the maximum length of the sequence. The second line contains $$$s$$$ integers, each are between $$$1$$$ and $$$n$$$ inclusive, representing the elements of the sequence. If there are multiple sequences with the maximum length, output any of them. It can be shown that, for all test cases with answer "FINITE", then under the constraints, the maximum possible sum of sequence lengths of those test cases does not exceed $$$2cdot10^6$$$. Example Input 5 3 2 3 1 2 1 1 0 2 0 2 2 1 2 2 1 5 5 2 1 3 1 4 2 4 5 5 1 Output FINITE 5 2 3 1 2 3 FINITE 1 1 INFINITE FINITE 3 2 1 2 FINITE 10 4 2 3 5 4 1 3 2 5 4 Note In the first test case, there is an element $$$1$$$ between two elements with value $$$3$$$ and an element $$$1$$$ between two elements with value $$$2$$$. It can be shown that there is no suitable sequences of length more than $$$5$$$. In the second case, $$$[1]$$$ is the only possible sequence because there should be exactly one element with value $$$1$$$ in the sequence. In the third case, we can get an arbitrary long sequence like $$$1, 2, 2, 2, ldots$$$. | 2,200 | false | true | false | false | false | true | false | false | false | true | 1,379 |
46B | One day a well-known sponsor of a well-known contest decided to give every participant of the contest a T-shirt as a present. A natural problem occurred: on the one hand, it is not clear how many T-shirts of what sizes should be ordered, and on the other hand, one doesn't want to order too many T-shirts (and we do not exactly paper the walls with the oversupply). After considerable brain racking and some pre-estimating, the sponsor representatives ordered a certain number of T-shirts of sizes S, M, L, XL and XXL. The T-shirts turned out to bring good luck, that's why on the contest day there built up a line of _K_ participants willing to get one. Every contestant is characterized by his/her desired T-shirt size (so it happens that for all the participants it is also one of the sizes S, M, L, XL and XXL). The participants come up to get a T-shirt one by one and try to choose the most suitable one, choosing it like this. If there is still a T-shirt of the optimal size left, that he/she takes it without further ado. Otherwise the contestant would prefer to choose a T-shirt with the size as close to the optimal one as possible (the distance between neighboring sizes is considered equal to one). If the variant of choice is not unique, the contestant will take a T-shirt of a bigger size (in case he/she grows more). For example, for a person whose optimal size is L the preference list looks like this: L, XL, M, XXL, S. Using the data on how many T-shirts of every size had been ordered by the organizers, on the size of contestants in the line determine who got a T-shirt of what size. Input The first line contains five non-negative integers _N__S_,u2009_N__M_,u2009_N__L_,u2009_N__XL_,u2009_N__XXL_ not exceeding 1000 which represent the number of T-shirts of the corresponding sizes. The second line contains an integer _K_ (1u2009≤u2009_K_u2009≤u20091000) which represents the number of participants. The next _K_ lines contain the optimal T-shirt sizes for the contestants. The sizes are given in the order in which the participants stand in the line. It is guaranteed that _N__S_u2009+u2009_N__M_u2009+u2009_N__L_u2009+u2009_N__XL_u2009+u2009_N__XXL_u2009≥u2009_K_. Output For each contestant, print a line containing the size of the T-shirt he/she got. | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,748 |
1806E | You are given a tree with $$$n$$$ weighted vertices labeled from $$$1$$$ to $$$n$$$ rooted at vertex $$$1$$$. The parent of vertex $$$i$$$ is $$$p_i$$$ and the weight of vertex $$$i$$$ is $$$a_i$$$. For convenience, define $$$p_1=0$$$. For two vertices $$$x$$$ and $$$y$$$ of the same depth$$$^dagger$$$, define $$$f(x,y)$$$ as follows: Initialize $$$mathrm{ans}=0$$$. While both $$$x$$$ and $$$y$$$ are not $$$0$$$: $$$mathrm{ans}leftarrow mathrm{ans}+a_xcdot a_y$$$; $$$xleftarrow p_x$$$; $$$yleftarrow p_y$$$. $$$f(x,y)$$$ is the value of $$$mathrm{ans}$$$. You will process $$$q$$$ queries. In the $$$i$$$-th query, you are given two integers $$$x_i$$$ and $$$y_i$$$ and you need to calculate $$$f(x_i,y_i)$$$. $$$^dagger$$$ The depth of vertex $$$v$$$ is the number of edges on the unique simple path from the root of the tree to vertex $$$v$$$. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 10^5$$$; $$$1 le q le 10^5$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^5$$$). The third line contains $$$n-1$$$ integers $$$p_2, ldots, p_n$$$ ($$$1 le p_i < i$$$). Each of the next $$$q$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1le x_i,y_ile n$$$). It is guaranteed that $$$x_i$$$ and $$$y_i$$$ are of the same depth. Output Output $$$q$$$ lines, the $$$i$$$-th line contains a single integer, the value of $$$f(x_i,y_i)$$$. Examples Input 6 2 1 5 2 3 1 1 1 2 3 3 2 4 5 6 6 Input 14 8 3 2 5 3 1 4 2 2 2 5 5 5 2 4 1 2 3 1 1 4 7 3 3 1 5 3 8 4 4 4 10 13 10 3 12 13 9 3 12 9 10 11 5 Output 47 53 48 36 42 36 48 14 Note Consider the first example: In the first query, the answer is $$$a_4cdot a_5+a_3cdot a_3+a_2cdot a_2+a_1cdot a_1=3+4+25+1=33$$$. In the second query, the answer is $$$a_6cdot a_6+a_2cdot a_2+a_1cdot a_1=1+25+1=27$$$. | 2,200 | false | false | false | false | true | false | true | false | false | false | 1,438 |
1928D | Once, the people, elves, dwarves, and other inhabitants of Middle-earth gathered to reclaim the treasures stolen from them by Smaug. In the name of this great goal, they rallied around the powerful elf Timothy and began to plan the overthrow of the ruler of the Lonely Mountain. The army of Middle-earth inhabitants will consist of several squads. It is known that each pair of creatures of the same race, which are in different squads, adds $$$b$$$ units to the total strength of the army. But since it will be difficult for Timothy to lead an army consisting of a large number of squads, the total strength of an army consisting of $$$k$$$ squads is reduced by $$$(k - 1) cdot x$$$ units. Note that the army always consists of at least one squad. It is known that there are $$$n$$$ races in Middle-earth, and the number of creatures of the $$$i$$$-th race is equal to $$$c_i$$$. Help the inhabitants of Middle-earth determine the maximum strength of the army they can assemble. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 2 cdot 10^4$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$b$$$, and $$$x$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 le b le 10^6, 0 le x le 10^9$$$) — the number of races and the constants $$$b$$$ and $$$x$$$ described above. The second line of each test case contains $$$n$$$ integers $$$c_1, c_2, ldots, c_n$$$ ($$$1 le c_i le 2 cdot 10^5$$$) — the number of creatures of each of the $$$n$$$ races. It is guaranteed that the sum of the values $$$c_1 + c_2 + ldots + c_n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output a single integer — the maximum strength of the army that the inhabitants of Middle-earth can assemble. Example Input 5 3 1 0 1 2 3 3 5 10 2 5 3 4 3 3 3 2 1 2 4 1 0 4 1 4 2 4 1 10 4 1 4 2 Note In the first test case, the inhabitants of Middle-earth can form $$$3$$$ squads. Since $$$x = 0$$$, the army's strength will not decrease due to the number of squads. The inhabitants can be distributed among the squads as follows: The single representative of the first species can be sent to the first squad. The first representative of the second species can be sent to the first squad, the second representative of the second species can be sent to the second squad. Then the total strength of the army will increase by $$$b = 1$$$. The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $$$3 cdot b = 3$$$, as they form three pairs in different squads. Thus, the total strength of the army is $$$4$$$. In the second test case, the inhabitants of Middle-earth can form $$$3$$$ squads. Since $$$x = 10$$$, the army's strength will decrease by $$$20$$$. The inhabitants can be distributed among the squads as follows: The first representative of the first species can be sent to the first squad, the second representative of the first species can be sent to the second squad. Then the total strength of the army will increase by $$$b = 5$$$. The first and second representatives of the second species can be sent to the first squad, the third and fourth representatives of the second species can be sent to the second squad, the fifth representative of the second species can be sent to the third squad. Then the total strength of the army will increase by $$$8 cdot b = 40$$$. The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $$$3 cdot b = 15$$$, as they form three pairs in different squads. Thus, the total strength of the army is $$$5 + 40 + 15 - 20 = 40$$$. | 1,900 | true | true | false | false | true | false | true | false | false | false | 720 |
1781B | A company of $$$n$$$ people is planning a visit to the cinema. Every person can either go to the cinema or not. That depends on how many other people will go. Specifically, every person $$$i$$$ said: "I want to go to the cinema if and only if at least $$$a_i$$$ other people will go, not counting myself". That means that person $$$i$$$ will become sad if: they go to the cinema, and strictly less than $$$a_i$$$ other people go; or they don't go to the cinema, and at least $$$a_i$$$ other people go. In how many ways can a set of people going to the cinema be chosen so that nobody becomes sad? 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. Each test case consists of two lines. The first line contains a single integer $$$n$$$xa0($$$2 le n le 2 cdot 10^5$$$)xa0— the number of people in the company. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$xa0($$$0 le a_i le n - 1$$$)xa0— integers from peoples' claims. 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 number of different ways to choose a set of people going to the cinema so that nobody becomes sad. Example Input 4 2 1 1 7 0 1 2 3 4 5 6 8 6 0 3 3 6 7 2 7 5 3 0 0 3 3 Note In the first test case, both people want to go to the cinema if and only if the other person goes. There are two valid options: either both people go, or neither of them goes. However, if just one of them goes, both will be sad. In the second test case, everyone has to go to the cinema. In any other case, someone will be sad. In the third test case, there are three valid options: person number $$$2$$$ goes to the cinema; or persons with indices $$$2, 3, 4, 7$$$ go; or all eight people go. | 1,000 | false | true | false | false | false | false | true | false | true | false | 1,587 |
367C | Let's call an array consisting of _n_ integer numbers _a_1, _a_2, ..., _a__n_, beautiful if it has the following property: consider all pairs of numbers _x_,u2009_y_ (_x_u2009≠u2009_y_), such that number _x_ occurs in the array _a_ and number _y_ occurs in the array _a_; for each pair _x_,u2009_y_ must exist some position _j_ (1u2009≤u2009_j_u2009<u2009_n_), such that at least one of the two conditions are met, either _a__j_u2009=u2009_x_,u2009_a__j_u2009+u20091u2009=u2009_y_, or _a__j_u2009=u2009_y_,u2009_a__j_u2009+u20091u2009=u2009_x_. Sereja wants to build a beautiful array _a_, consisting of _n_ integers. But not everything is so easy, Sereja's friend Dima has _m_ coupons, each contains two integers _q__i_,u2009_w__i_. Coupon _i_ costs _w__i_ and allows you to use as many numbers _q__i_ as you want when constructing the array _a_. Values _q__i_ are distinct. Sereja has no coupons, so Dima and Sereja have made the following deal. Dima builds some beautiful array _a_ of _n_ elements. After that he takes _w__i_ rubles from Sereja for each _q__i_, which occurs in the array _a_. Sereja believed his friend and agreed to the contract, and now he is wondering, what is the maximum amount of money he can pay. Help Sereja, find the maximum amount of money he can pay to Dima. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u20092·106,u20091u2009≤u2009_m_u2009≤u2009105). Next _m_ lines contain pairs of integers. The _i_-th line contains numbers _q__i_,u2009_w__i_ (1u2009≤u2009_q__i_,u2009_w__i_u2009≤u2009105). It is guaranteed that all _q__i_ are distinct. Output In a single line print maximum amount of money (in rubles) Sereja can pay. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Note In the first sample Sereja can pay 5 rubles, for example, if Dima constructs the following array: [1,u20092,u20091,u20092,u20092]. There are another optimal arrays for this test. In the third sample Sereja can pay 100 rubles, if Dima constructs the following array: [2]. | 2,000 | false | true | false | false | false | false | false | false | true | true | 8,363 |
574B | Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Richelimakieu is a cardinal in the city of Bearis. He is tired of dealing with crime by himself. He needs three brave warriors to help him to fight against bad guys. There are _n_ warriors. Richelimakieu wants to choose three of them to become musketeers but it's not that easy. The most important condition is that musketeers must know each other to cooperate efficiently. And they shouldn't be too well known because they could be betrayed by old friends. For each musketeer his recognition is the number of warriors he knows, excluding other two musketeers. Help Richelimakieu! Find if it is possible to choose three musketeers knowing each other, and what is minimum possible sum of their recognitions. Input The first line contains two space-separated integers, _n_ and _m_ (3u2009≤u2009_n_u2009≤u20094000, 0u2009≤u2009_m_u2009≤u20094000) — respectively number of warriors and number of pairs of warriors knowing each other. _i_-th of the following _m_ lines contains two space-separated integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_). Warriors _a__i_ and _b__i_ know each other. Each pair of warriors will be listed at most once. Output If Richelimakieu can choose three musketeers, print the minimum possible sum of their recognitions. Otherwise, print "-1" (without the quotes). Examples Input 5 6 1 2 1 3 2 3 2 4 3 4 4 5 Note In the first sample Richelimakieu should choose a triple 1, 2, 3. The first musketeer doesn't know anyone except other two musketeers so his recognition is 0. The second musketeer has recognition 1 because he knows warrior number 4. The third musketeer also has recognition 1 because he knows warrior 4. Sum of recognitions is 0u2009+u20091u2009+u20091u2009=u20092. The other possible triple is 2,u20093,u20094 but it has greater sum of recognitions, equal to 1u2009+u20091u2009+u20091u2009=u20093. In the second sample there is no triple of warriors knowing each other. | 1,500 | false | false | false | false | false | false | true | false | false | true | 7,553 |
474B | It is lunch time for Mole. His friend, Marmot, prepared him a nice game for lunch. Marmot brought Mole _n_ ordered piles of worms such that _i_-th pile contains _a__i_ worms. He labeled all these worms with consecutive integers: worms in first pile are labeled with numbers 1 to _a_1, worms in second pile are labeled with numbers _a_1u2009+u20091 to _a_1u2009+u2009_a_2 and so on. See the example for a better understanding. Mole can't eat all the worms (Marmot brought a lot) and, as we all know, Mole is blind, so Marmot tells him the labels of the best juicy worms. Marmot will only give Mole a worm if Mole says correctly in which pile this worm is contained. Poor Mole asks for your help. For all juicy worms said by Marmot, tell Mole the correct answers. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009105), the number of piles. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009103, _a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__n_u2009≤u2009106), where _a__i_ is the number of worms in the _i_-th pile. The third line contains single integer _m_ (1u2009≤u2009_m_u2009≤u2009105), the number of juicy worms said by Marmot. The fourth line contains _m_ integers _q_1,u2009_q_2,u2009...,u2009_q__m_ (1u2009≤u2009_q__i_u2009≤u2009_a_1u2009+u2009_a_2u2009+u2009...u2009+u2009_a__n_), the labels of the juicy worms. Output Print _m_ lines to the standard output. The _i_-th line should contain an integer, representing the number of the pile where the worm labeled with the number _q__i_ is. Note For the sample input: The worms with labels from [1, 2] are in the first pile. The worms with labels from [3, 9] are in the second pile. The worms with labels from [10, 12] are in the third pile. The worms with labels from [13, 16] are in the fourth pile. The worms with labels from [17, 25] are in the fifth pile. | 1,200 | false | false | true | false | false | false | false | true | false | false | 7,939 |
1023F | You are managing a mobile phone network, and want to offer competitive prices to connect a network. The network has $$$n$$$ nodes. Your competitor has already offered some connections between some nodes, with some fixed prices. These connections are bidirectional. There are initially $$$m$$$ connections the competitor is offering. The $$$i$$$-th connection your competitor is offering will connect nodes $$$fa_i$$$ and $$$fb_i$$$ and costs $$$fw_i$$$. You have a list of $$$k$$$ connections that you want to offer. It is guaranteed that this set of connection does not form any cycle. The $$$j$$$-th of these connections will connect nodes $$$ga_j$$$ and $$$gb_j$$$. These connections are also bidirectional. The cost of these connections have not been decided yet. You can set the prices of these connections to any arbitrary integer value. These prices are set independently for each connection. After setting the prices, the customer will choose such $$$n - 1$$$ connections that all nodes are connected in a single network and the total cost of chosen connections is minimum possible. If there are multiple ways to choose such networks, the customer will choose an arbitrary one that also maximizes the number of your connections in it. You want to set prices in such a way such that all your $$$k$$$ connections are chosen by the customer, and the sum of prices of your connections is maximized. Print the maximum profit you can achieve, or $$$-1$$$ if it is unbounded. Input The first line of input will contain three integers $$$n$$$, $$$k$$$ and $$$m$$$ ($$$1 leq n, k, m leq 5 cdot 10^5, k leq n-1$$$), the number of nodes, the number of your connections, and the number of competitor connections, respectively. The next $$$k$$$ lines contain two integers $$$ga_i$$$ and $$$gb_i$$$ ($$$1 leq ga_i, gb_i leq n$$$, $$$ga_i ot= gb_i$$$), representing one of your connections between nodes $$$ga_i$$$ and $$$gb_i$$$. Your set of connections is guaranteed to be acyclic. The next $$$m$$$ lines contain three integers each, $$$fa_i$$$, $$$fb_i$$$ and $$$fw_i$$$ ($$$1 leq fa_i, fb_i leq n$$$, $$$fa_i ot= fb_i$$$, $$$1 leq fw_i leq 10^9$$$), denoting one of your competitor's connections between nodes $$$fa_i$$$ and $$$fb_i$$$ with cost $$$fw_i$$$. None of these connections connects a node to itself, and no pair of these connections connect the same pair of nodes. In addition, these connections are given by non-decreasing order of cost (that is, $$$fw_{i-1} leq fw_i$$$ for all valid $$$i$$$). Note that there may be some connections that appear in both your set and your competitor's set (though no connection will appear twice in one of this sets). It is guaranteed that the union of all of your connections and your competitor's connections form a connected network. Output Print a single integer, the maximum possible profit you can achieve if you set the prices on your connections appropriately. If the profit is unbounded, print $$$-1$$$. Examples Input 4 3 6 1 2 3 4 1 3 2 3 3 3 1 4 1 2 4 4 2 8 4 3 8 4 1 10 Input 4 3 3 1 2 1 3 1 4 4 1 1000000000 4 2 1000000000 4 3 1000000000 Note In the first sample, it's optimal to give connection $$$1-3$$$ cost $$$3$$$, connection $$$1-2$$$ cost $$$3$$$, and connection $$$3-4$$$ cost $$$8$$$. In this case, the cheapest connected network has cost $$$14$$$, and the customer will choose one that chooses all of your connections. In the second sample, as long as your first connection costs $$$30$$$ or less, the customer chooses both your connections no matter what is the cost of the second connection, so you can get unbounded profit in this case. | 2,600 | false | false | false | false | false | false | false | false | false | true | 5,585 |
1630D | You are given an array $$$a$$$ of $$$n$$$ integers and a set $$$B$$$ of $$$m$$$ positive integers such that $$$1 leq b_i leq lfloor frac{n}{2} floor$$$ for $$$1le ile m$$$, where $$$b_i$$$ is the $$$i$$$-th element of $$$B$$$. You can make the following operation on $$$a$$$: 1. Select some $$$x$$$ such that $$$x$$$ appears in $$$B$$$. 2. Select an interval from array $$$a$$$ of size $$$x$$$ and multiply by $$$-1$$$ every element in the interval. Formally, select $$$l$$$ and $$$r$$$ such that $$$1leq lleq r leq n$$$ and $$$r-l+1=x$$$, then assign $$$a_i:=-a_i$$$ for every $$$i$$$ such that $$$lleq ileq r$$$. Consider the following example, let $$$a=[0,6,-2,1,-4,5]$$$ and $$$B={1,2}$$$: 1. $$$[0,6,-2,-1,4,5]$$$ is obtained after choosing size $$$2$$$ and $$$l=4$$$, $$$r=5$$$. 2. $$$[0,6,2,-1,4,5]$$$ is obtained after choosing size $$$1$$$ and $$$l=3$$$, $$$r=3$$$. Find the maximum $$$sumlimits_{i=1}^n {a_i}$$$ you can get after applying such operation any number of times (possibly zero). Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2leq n leq 10^6$$$, $$$1 leq m leq lfloor frac{n}{2} floor$$$) — the number of elements of $$$a$$$ and $$$B$$$ respectively. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$-10^9leq a_i leq 10^9$$$). The third line of each test case contains $$$m$$$ distinct positive integers $$$b_1,b_2,ldots,b_m$$$ ($$$1 leq b_i leq lfloor frac{n}{2} floor$$$) — the elements in the set $$$B$$$. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case print a single integer — the maximum possible sum of all $$$a_i$$$ after applying such operation any number of times. Example Input 3 6 2 0 6 -2 1 -4 5 1 2 7 1 1 -1 1 -1 1 -1 1 2 5 1 -1000000000 -1000000000 -1000000000 -1000000000 -1000000000 1 Note In the first test, you can apply the operation $$$x=1$$$, $$$l=3$$$, $$$r=3$$$, and the operation $$$x=1$$$, $$$l=5$$$, $$$r=5$$$, then the array becomes $$$[0, 6, 2, 1, 4, 5]$$$. In the second test, you can apply the operation $$$x=2$$$, $$$l=2$$$, $$$r=3$$$, and the array becomes $$$[1, 1, -1, -1, 1, -1, 1]$$$, then apply the operation $$$x=2$$$, $$$l=3$$$, $$$r=4$$$, and the array becomes $$$[1, 1, 1, 1, 1, -1, 1]$$$. There is no way to achieve a sum bigger than $$$5$$$. | 2,400 | false | true | false | true | false | true | false | false | false | false | 2,463 |
75B | Facetook is a well known social network website, and it will launch a new feature called Facetook Priority Wall. This feature will sort all posts from your friends according to the priority factor (it will be described). This priority factor will be affected by three types of actions: 1. "_X_ posted on _Y_'s wall" (15 points), 2. "_X_ commented on _Y_'s post" (10 points), 3. "_X_ likes _Y_'s post" (5 points). _X_ and _Y_ will be two distinct names. And each action will increase the priority factor between _X_ and _Y_ (and vice versa) by the above value of points (the priority factor between _X_ and _Y_ is the same as the priority factor between _Y_ and _X_). You will be given _n_ actions with the above format (without the action number and the number of points), and you have to print all the distinct names in these actions sorted according to the priority factor with you. Input The first line contains your name. The second line contains an integer _n_, which is the number of actions (1u2009≤u2009_n_u2009≤u2009100). Then _n_ lines follow, it is guaranteed that each one contains exactly 1 action in the format given above. There is exactly one space between each two words in a line, and there are no extra spaces. All the letters are lowercase. All names in the input will consist of at least 1 letter and at most 10 small Latin letters. Output Print _m_ lines, where _m_ is the number of distinct names in the input (excluding yourself). Each line should contain just 1 name. The names should be sorted according to the priority factor with you in the descending order (the highest priority factor should come first). If two or more names have the same priority factor, print them in the alphabetical (lexicographical) order. Note, that you should output all the names that are present in the input data (excluding yourself), even if that person has a zero priority factor. The lexicographical comparison is performed by the standard "<" operator in modern programming languages. The line _a_ is lexicographically smaller than the line _b_, if either _a_ is the prefix of _b_, or if exists such an _i_ (1u2009≤u2009_i_u2009≤u2009_min_(_a_,u2009_b_)), that _a__i_u2009<u2009_b__i_, and for any _j_ (1u2009≤u2009_j_u2009<u2009_i_) _a__j_u2009=u2009_b__j_, where _a_ and _b_ stand for the lengths of strings _a_ and _b_ correspondently. Examples Input ahmed 3 ahmed posted on fatma's wall fatma commented on ahmed's post mona likes ahmed's post Input aba 1 likes likes posted's post | 1,500 | false | false | true | false | false | false | false | false | false | false | 9,590 |
82D | Vasya has recently developed a new algorithm to optimize the reception of customer flow and he considered the following problem. Let the queue to the cashier contain _n_ people, at that each of them is characterized by a positive integer _a__i_ — that is the time needed to work with this customer. What is special about this very cashier is that it can serve two customers simultaneously. However, if two customers need _a__i_ and _a__j_ of time to be served, the time needed to work with both of them customers is equal to _max_(_a__i_,u2009_a__j_). Please note that working with customers is an uninterruptable process, and therefore, if two people simultaneously come to the cashier, it means that they begin to be served simultaneously, and will both finish simultaneously (it is possible that one of them will have to wait). Vasya used in his algorithm an ingenious heuristic — as long as the queue has more than one person waiting, then some two people of the first three standing in front of the queue are sent simultaneously. If the queue has only one customer number _i_, then he goes to the cashier, and is served within _a__i_ of time. Note that the total number of phases of serving a customer will always be equal to ⌈_n_u2009/u20092⌉. Vasya thinks that this method will help to cope with the queues we all hate. That's why he asked you to work out a program that will determine the minimum time during which the whole queue will be served using this algorithm. Input The first line of the input file contains a single number _n_ (1u2009≤u2009_n_u2009≤u20091000), which is the number of people in the sequence. The second line contains space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009106). The people are numbered starting from the cashier to the end of the queue. Output Print on the first line a single number — the minimum time needed to process all _n_ people. Then on ⌈_n_u2009/u20092⌉ lines print the order in which customers will be served. Each line (probably, except for the last one) must contain two numbers separated by a space — the numbers of customers who will be served at the current stage of processing. If _n_ is odd, then the last line must contain a single number — the number of the last served customer in the queue. The customers are numbered starting from 1. | 2,000 | false | false | false | true | false | false | false | false | false | false | 9,555 |
1853A | Call an array $$$a$$$ of length $$$n$$$ sorted if $$$a_1 leq a_2 leq ldots leq a_{n-1} leq a_n$$$. Ntarsis has an array $$$a$$$ of length $$$n$$$. He is allowed to perform one type of operation on it (zero or more times): Choose an index $$$i$$$ ($$$1 leq i leq n-1$$$). Add $$$1$$$ to $$$a_1, a_2, ldots, a_i$$$. Subtract $$$1$$$ from $$$a_{i+1}, a_{i+2}, ldots, a_n$$$. The values of $$$a$$$ can be negative after an operation. Determine the minimum operations needed to make $$$a$$$ not sorted. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 500$$$)xa0— the length of the array $$$a$$$. The next line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$)xa0— the values of array $$$a$$$. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$500$$$. Output Output the minimum number of operations needed to make the array not sorted. Example Input 4 2 1 1 4 1 8 10 13 3 1 3 2 3 1 9 14 Note In the first case, we can perform $$$1$$$ operation to make the array not sorted: Pick $$$i = 1$$$. The array $$$a$$$ then becomes $$$[2, 0]$$$, which is not sorted. In the second case, we can perform $$$2$$$ operations to make the array not sorted: Pick $$$i = 3$$$. The array $$$a$$$ then becomes $$$[2, 9, 11, 12]$$$. Pick $$$i = 3$$$. The array $$$a$$$ then becomes $$$[3, 10, 12, 11]$$$, which is not sorted. It can be proven that $$$1$$$ and $$$2$$$ operations are the minimal numbers of operations in the first and second test cases, respectively. In the third case, the array is already not sorted, so we perform $$$0$$$ operations. | 800 | true | true | false | false | false | false | true | false | false | false | 1,161 |
1593E | A tree is an undirected connected graph in which there are no cycles. This problem is about non-rooted trees. A leaf of a tree is a vertex that is connected to at most one vertex. The gardener Vitaly grew a tree from $$$n$$$ vertices. He decided to trim the tree. To do this, he performs a number of operations. In one operation, he removes all leaves of the tree. Example of a tree. For example, consider the tree shown in the figure above. The figure below shows the result of applying exactly one operation to the tree. The result of applying the operation "remove all leaves" to the tree. Note the special cases of the operation: applying an operation to an empty tree (of $$$0$$$ vertices) does not change it; applying an operation to a tree of one vertex removes this vertex (this vertex is treated as a leaf); applying an operation to a tree of two vertices removes both vertices (both vertices are treated as leaves). Vitaly applied $$$k$$$ operations sequentially to the tree. How many vertices remain? Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case is preceded by an empty line. Each test case consists of several lines. The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 4 cdot 10^5$$$, $$$1 le k le 2 cdot 10^5$$$) — the number of vertices in the tree and the number of operations, respectively. Then $$$n - 1$$$ lines follow, each of them contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges. It is guaranteed that the sum of $$$n$$$ from all test cases does not exceed $$$4 cdot 10^5$$$. Output For each test case output on a separate line a single integer — the number of vertices that remain in the tree after applying $$$k$$$ operations. Example Input 6 14 1 1 2 2 3 2 4 4 5 4 6 2 7 7 8 8 9 8 10 3 11 3 12 1 13 13 14 2 200000 1 2 3 2 1 2 2 3 5 1 5 1 3 2 2 1 5 4 6 2 5 1 2 5 5 6 4 2 3 4 7 1 4 3 5 1 1 3 6 1 1 7 2 1 Note The first test case is considered in the statement. The second test case contains a tree of two vertices. $$$200000$$$ operations are applied to it. The first one removes all two vertices, the other operations do not change the tree. In the third test case, a tree of three vertices is given. As a result of the first operation, only $$$1$$$ vertex remains in it (with the index $$$2$$$), the second operation makes the tree empty. | 1,600 | false | true | true | false | true | false | true | false | false | false | 2,681 |
500A | New Year is coming in Line World! In this world, there are _n_ cells numbered by integers from 1 to _n_, as a 1u2009×u2009_n_ board. People live in cells. However, it was hard to move between distinct cells, because of the difficulty of escaping the cell. People wanted to meet people who live in other cells. So, user tncks0121 has made a transportation system to move between these cells, to celebrate the New Year. First, he thought of _n_u2009-u20091 positive integers _a_1,u2009_a_2,u2009...,u2009_a__n_u2009-u20091. For every integer _i_ where 1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091 the condition 1u2009≤u2009_a__i_u2009≤u2009_n_u2009-u2009_i_ holds. Next, he made _n_u2009-u20091 portals, numbered by integers from 1 to _n_u2009-u20091. The _i_-th (1u2009≤u2009_i_u2009≤u2009_n_u2009-u20091) portal connects cell _i_ and cell (_i_u2009+u2009_a__i_), and one can travel from cell _i_ to cell (_i_u2009+u2009_a__i_) using the _i_-th portal. Unfortunately, one cannot use the portal backwards, which means one cannot move from cell (_i_u2009+u2009_a__i_) to cell _i_ using the _i_-th portal. It is easy to see that because of condition 1u2009≤u2009_a__i_u2009≤u2009_n_u2009-u2009_i_ one can't leave the Line World using portals. Currently, I am standing at cell 1, and I want to go to cell _t_. However, I don't know whether it is possible to go there. Please determine whether I can go to cell _t_ by only using the construted transportation system. Input The first line contains two space-separated integers _n_ (3u2009≤u2009_n_u2009≤u20093u2009×u2009104) and _t_ (2u2009≤u2009_t_u2009≤u2009_n_) — the number of cells, and the index of the cell which I want to go to. The second line contains _n_u2009-u20091 space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_u2009-u20091 (1u2009≤u2009_a__i_u2009≤u2009_n_u2009-u2009_i_). It is guaranteed, that using the given transportation system, one cannot leave the Line World. Note In the first sample, the visited cells are: 1,u20092,u20094; so we can successfully visit the cell 4. In the second sample, the possible cells to visit are: 1,u20092,u20094,u20096,u20097,u20098; so we can't visit the cell 5, which we want to visit. | 1,000 | false | false | true | false | false | false | false | false | false | true | 7,837 |
965C | $$$k$$$ people want to split $$$n$$$ candies between them. Each candy should be given to exactly one of them or be thrown away. The people are numbered from $$$1$$$ to $$$k$$$, and Arkady is the first of them. To split the candies, Arkady will choose an integer $$$x$$$ and then give the first $$$x$$$ candies to himself, the next $$$x$$$ candies to the second person, the next $$$x$$$ candies to the third person and so on in a cycle. The leftover (the remainder that is not divisible by $$$x$$$) will be thrown away. Arkady can't choose $$$x$$$ greater than $$$M$$$ as it is considered greedy. Also, he can't choose such a small $$$x$$$ that some person will receive candies more than $$$D$$$ times, as it is considered a slow splitting. Please find what is the maximum number of candies Arkady can receive by choosing some valid $$$x$$$. Input The only line contains four integers $$$n$$$, $$$k$$$, $$$M$$$ and $$$D$$$ ($$$2 le n le 10^{18}$$$, $$$2 le k le n$$$, $$$1 le M le n$$$, $$$1 le D le min{(n, 1000)}$$$, $$$M cdot D cdot k ge n$$$)xa0— the number of candies, the number of people, the maximum number of candies given to a person at once, the maximum number of times a person can receive candies. Output Print a single integerxa0— the maximum possible number of candies Arkady can give to himself. Note that it is always possible to choose some valid $$$x$$$. Note In the first example Arkady should choose $$$x = 4$$$. He will give $$$4$$$ candies to himself, $$$4$$$ candies to the second person, $$$4$$$ candies to the third person, then $$$4$$$ candies to the fourth person and then again $$$4$$$ candies to himself. No person is given candies more than $$$2$$$ times, and Arkady receives $$$8$$$ candies in total. Note that if Arkady chooses $$$x = 5$$$, he will receive only $$$5$$$ candies, and if he chooses $$$x = 3$$$, he will receive only $$$3 + 3 = 6$$$ candies as well as the second person, the third and the fourth persons will receive $$$3$$$ candies, and $$$2$$$ candies will be thrown away. He can't choose $$$x = 1$$$ nor $$$x = 2$$$ because in these cases he will receive candies more than $$$2$$$ times. In the second example Arkady has to choose $$$x = 4$$$, because any smaller value leads to him receiving candies more than $$$1$$$ time. | 2,000 | true | false | false | false | false | false | false | false | false | false | 5,840 |
1455B | You are standing on the $$$mathit{OX}$$$-axis at point $$$0$$$ and you want to move to an integer point $$$x > 0$$$. You can make several jumps. Suppose you're currently at point $$$y$$$ ($$$y$$$ may be negative) and jump for the $$$k$$$-th time. You can: either jump to the point $$$y + k$$$ or jump to the point $$$y - 1$$$. What is the minimum number of jumps you need to reach the point $$$x$$$? 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 the single integer $$$x$$$ ($$$1 le x le 10^6$$$)xa0— the destination point. Output For each test case, print the single integerxa0— the minimum number of jumps to reach $$$x$$$. It can be proved that we can reach any integer point $$$x$$$. Note In the first test case $$$x = 1$$$, so you need only one jump: the $$$1$$$-st jump from $$$0$$$ to $$$0 + 1 = 1$$$. In the second test case $$$x = 2$$$. You need at least three jumps: the $$$1$$$-st jump from $$$0$$$ to $$$0 + 1 = 1$$$; the $$$2$$$-nd jump from $$$1$$$ to $$$1 + 2 = 3$$$; the $$$3$$$-rd jump from $$$3$$$ to $$$3 - 1 = 2$$$; Two jumps are not enough because these are the only possible variants: the $$$1$$$-st jump as $$$-1$$$ and the $$$2$$$-nd one as $$$-1$$$xa0— you'll reach $$$0 -1 -1 =-2$$$; the $$$1$$$-st jump as $$$-1$$$ and the $$$2$$$-nd one as $$$+2$$$xa0— you'll reach $$$0 -1 +2 = 1$$$; the $$$1$$$-st jump as $$$+1$$$ and the $$$2$$$-nd one as $$$-1$$$xa0— you'll reach $$$0 +1 -1 = 0$$$; the $$$1$$$-st jump as $$$+1$$$ and the $$$2$$$-nd one as $$$+2$$$xa0— you'll reach $$$0 +1 +2 = 3$$$; In the third test case, you need two jumps: the $$$1$$$-st one as $$$+1$$$ and the $$$2$$$-nd one as $$$+2$$$, so $$$0 + 1 + 2 = 3$$$. In the fourth test case, you need three jumps: the $$$1$$$-st one as $$$-1$$$, the $$$2$$$-nd one as $$$+2$$$ and the $$$3$$$-rd one as $$$+3$$$, so $$$0 - 1 + 2 + 3 = 4$$$. | 1,200 | true | false | false | false | false | true | false | false | false | false | 3,401 |
869D | The fundamental prerequisite for justice is not to be correct, but to be strong. That's why justice is always the victor. The Cinderswarm Bee. Koyomi knows it. The bees, according to their nature, live in a tree. To be more specific, a complete binary tree with _n_ nodes numbered from 1 to _n_. The node numbered 1 is the root, and the parent of the _i_-th (2u2009≤u2009_i_u2009≤u2009_n_) node is . Note that, however, all edges in the tree are undirected. Koyomi adds _m_ extra undirected edges to the tree, creating more complication to trick the bees. And you're here to count the number of simple paths in the resulting graph, modulo 109u2009+u20097. A simple path is an alternating sequence of adjacent nodes and undirected edges, which begins and ends with nodes and does not contain any node more than once. Do note that a single node is also considered a valid simple path under this definition. Please refer to the examples and notes below for instances. Input The first line of input contains two space-separated integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009109, 0u2009≤u2009_m_u2009≤u20094) — the number of nodes in the tree and the number of extra edges respectively. The following _m_ lines each contains two space-separated integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_, _u_u2009≠u2009_v_) — describing an undirected extra edge whose endpoints are _u_ and _v_. Note that there may be multiple edges between nodes in the resulting graph. Output Output one integer — the number of simple paths in the resulting graph, modulo 109u2009+u20097. Note In the first example, the paths are: (1); (2); (3); (1,u20092); (2,u20091); (1,u20093); (3,u20091); (2,u20091,u20093); (3,u20091,u20092). (For the sake of clarity, the edges between nodes are omitted since there are no multiple edges in this case.) In the second example, the paths are: (1); (1,u20092); (1,u20092,u20093); (1,u20093); (1,u20093,u20092); and similarly for paths starting with 2 and 3. (5u2009×u20093u2009=u200915 paths in total.) In the third example, the paths are: (1); (2); any undirected edge connecting the two nodes travelled in either direction. (2u2009+u20095u2009×u20092u2009=u200912 paths in total.) | 2,800 | false | false | false | false | false | false | true | false | false | true | 6,263 |
622C | Problem - 622C - Codeforces =============== xa0 ]( "Educational Codeforces Round 7") so that _a__p__i_u2009≠u2009_x__i_. Input The first line contains two integers _n_,u2009_m_ (1u2009≤u2009_n_,u2009_m_u2009≤u20092·105) — the number of elements in _a_ and the number of queries. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106) — the elements of the array _a_. Each of the next _m_ lines contains three integers _l__i_,u2009_r__i_,u2009_x__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_,u20091u2009≤u2009_x__i_u2009≤u2009106) — the parameters of the _i_-th query. Output Print _m_ lines. On the _i_-th line print integer _p__i_ — the position of any number not equal to _x__i_ in segment | 1,700 | false | false | true | false | true | false | false | false | false | false | 7,344 |
883A | There is an automatic door at the entrance of a factory. The door works in the following way: when one or several people come to the door and it is closed, the door immediately opens automatically and all people immediately come inside, when one or several people come to the door and it is open, all people immediately come inside, opened door immediately closes in _d_ seconds after its opening, if the door is closing and one or several people are coming to the door at the same moment, then all of them will have enough time to enter and only after that the door will close. For example, if _d_u2009=u20093 and four people are coming at four different moments of time _t_1u2009=u20094, _t_2u2009=u20097, _t_3u2009=u20099 and _t_4u2009=u200913 then the door will open three times: at moments 4, 9 and 13. It will close at moments 7 and 12. It is known that _n_ employees will enter at moments _a_,u20092·_a_,u20093·_a_,u2009...,u2009_n_·_a_ (the value _a_ is positive integer). Also _m_ clients will enter at moments _t_1,u2009_t_2,u2009...,u2009_t__m_. Write program to find the number of times the automatic door will open. Assume that the door is initially closed. Input The first line contains four integers _n_, _m_, _a_ and _d_ (1u2009≤u2009_n_,u2009_a_u2009≤u2009109, 1u2009≤u2009_m_u2009≤u2009105, 1u2009≤u2009_d_u2009≤u20091018) — the number of the employees, the number of the clients, the moment of time when the first employee will come and the period of time in which the door closes. The second line contains integer sequence _t_1,u2009_t_2,u2009...,u2009_t__m_ (1u2009≤u2009_t__i_u2009≤u20091018) — moments of time when clients will come. The values _t__i_ are given in non-decreasing order. Output Print the number of times the door will open. Note In the first example the only employee will come at moment 3. At this moment the door will open and will stay open until the moment 7. At the same moment of time the client will come, so at first he will enter and only after it the door will close. Thus the door will open one time. | 2,200 | false | false | true | false | false | false | false | false | false | false | 6,227 |
618E | Roger is a robot. He has an arm that is a series of _n_ segments connected to each other. The endpoints of the _i_-th segment are initially located at points (_i_u2009-u20091,u20090) and (_i_,u20090). The endpoint at (_i_u2009-u20091,u20090) is colored red and the endpoint at (_i_,u20090) is colored blue for all segments. Thus, the blue endpoint of the _i_-th segment is touching the red endpoint of the (_i_u2009+u20091)-th segment for all valid _i_. Roger can move his arm in two different ways: 1. He can choose some segment and some value. This is denoted as choosing the segment number _i_ and picking some positive _l_. This change happens as follows: the red endpoint of segment number _i_ and segments from 1 to _i_u2009-u20091 are all fixed in place. Imagine a ray from the red endpoint to the blue endpoint. The blue endpoint and segments _i_u2009+u20091 through _n_ are translated _l_ units in the direction of this ray. In this picture, the red point labeled _A_ and segments before _A_ stay in place, while the blue point labeled _B_ and segments after _B_ gets translated. 2. He can choose a segment and rotate it. This is denoted as choosing the segment number _i_, and an angle _a_. The red endpoint of the _i_-th segment will stay fixed in place. The blue endpoint of that segment and segments _i_u2009+u20091 to _n_ will rotate clockwise by an angle of _a_ degrees around the red endpoint. In this picture, the red point labeled _A_ and segments before _A_ stay in place, while the blue point labeled _B_ and segments after _B_ get rotated around point _A_. Roger will move his arm _m_ times. These transformations are a bit complicated, and Roger easily loses track of where the blue endpoint of the last segment is. Help him compute the coordinates of the blue endpoint of the last segment after applying each operation. Note that these operations are cumulative, and Roger's arm may intersect itself arbitrarily during the moves. Input The first line of the input will contain two integers _n_ and _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009300u2009000)xa0— the number of segments and the number of operations to perform. Each of the next _m_ lines contains three integers _x__i_, _y__i_ and _z__i_ describing a move. If _x__i_u2009=u20091, this line describes a move of type 1, where _y__i_ denotes the segment number and _z__i_ denotes the increase in the length. If _x__i_u2009=u20092, this describes a move of type 2, where _y__i_ denotes the segment number, and _z__i_ denotes the angle in degrees. (1u2009≤u2009_x__i_u2009≤u20092,u20091u2009≤u2009_y__i_u2009≤u2009_n_,u20091u2009≤u2009_z__i_u2009≤u2009359) Output Print _m_ lines. The _i_-th line should contain two real values, denoting the coordinates of the blue endpoint of the last segment after applying operations 1,u2009...,u2009_i_. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20094. Namely, let's assume that your answer for a particular value of a coordinate is _a_ and the answer of the jury is _b_. The checker program will consider your answer correct if for all coordinates. Examples Input 5 4 1 1 3 2 3 90 2 5 48 1 4 1 Output 8.0000000000 0.0000000000 5.0000000000 -3.0000000000 4.2568551745 -2.6691306064 4.2568551745 -3.6691306064 Note The following pictures shows the state of the arm after each operation. The coordinates of point _F_ are printed after applying each operation. For simplicity, we only show the blue endpoints of a segment (with the exception for the red endpoint of the first segment). For instance, the point labeled _B_ is the blue endpoint for segment 1 and also the red endpoint for segment 2. Initial state: Extend segment 1 by 3. Rotate segment 3 by 90 degrees clockwise. Rotate segment 5 by 48 degrees clockwise. Extend segment 4 by 1. | 2,500 | false | false | false | false | true | false | false | false | false | false | 7,360 |
2032D | This is an interactive problem. Upon clearing the Waterside Area, Gretel has found a monster named Genokraken, and she's keeping it contained for her scientific studies. The monster's nerve system can be structured as a tree$$$^{dagger}$$$ of $$$n$$$ nodes (really, everything should stop resembling trees all the time$$$ldots$$$), numbered from $$$0$$$ to $$$n-1$$$, with node $$$0$$$ as the root. Gretel's objective is to learn the exact structure of the monster's nerve systemxa0— more specifically, she wants to know the values $$$p_1, p_2, ldots, p_{n-1}$$$ of the tree, where $$$p_i$$$ ($$$0 le p_i < i$$$) is the direct parent node of node $$$i$$$ ($$$1 le i le n - 1$$$). She doesn't know exactly how the nodes are placed, but she knows a few convenient facts: If we remove root node $$$0$$$ and all adjacent edges, this tree will turn into a forest consisting of only paths$$$^{ddagger}$$$. Each node that was initially adjacent to the node $$$0$$$ will be the end of some path. The nodes are indexed in a way that if $$$1 le x le y le n - 1$$$, then $$$p_x le p_y$$$. Node $$$1$$$ has exactly two adjacent nodes (including the node $$$0$$$). Gretel can make queries to the containment cell: "? a b" ($$$1 le a, b < n$$$, $$$a e b$$$) — the cell will check if the simple path between nodes $$$a$$$ and $$$b$$$ contains the node $$$0$$$. However, to avoid unexpected consequences by overstimulating the creature, Gretel wants to query at most $$$2n - 6$$$ times. Though Gretel is gifted, she can't do everything all at once, so can you give her a helping hand? $$$^{dagger}$$$A tree is a connected graph where every pair of distinct nodes has exactly one simple path connecting them. $$$^{ddagger}$$$A path is a tree whose vertices can be listed in the order $$$v_1, v_2, ldots, v_k$$$ such that the edges are $$$(v_i, v_{i+1})$$$ ($$$1 le i < k$$$). Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$4 le n le 10^4$$$)xa0— the number of nodes in Genokraken's nerve system. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^4$$$. Interaction For each test case, interaction starts by reading the integer $$$n$$$. Then you can make queries of the following type: "? a b" (without quotes) ($$$1 le a, b < n$$$, $$$a e b$$$). After the query, read an integer $$$r$$$xa0— the answer to your query. You are allowed to use at most $$$2n - 6$$$ queries of this type. If the simple path between nodes $$$a$$$ and $$$b$$$ does not contain node $$$0$$$, you will get $$$r = 0$$$. If the simple path between nodes $$$a$$$ and $$$b$$$ contains node $$$0$$$, you will get $$$r = 1$$$. In case you make more than $$$2n-6$$$ queries or make an invalid query, you will get $$$r = -1$$$. You will need to terminate after this to get the "Wrong answer" verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. When you find out the structure, output a line in the format "! $$$p_1 space p_2 ldots p_{n-1}$$$" (without quotes), where $$$p_i$$$ ($$$0 le p_i < i$$$) denotes the index of the direct parent of node $$$i$$$. This query is not counted towards the $$$2n - 6$$$ queries limit. After solving one test case, the program should immediately move on to the next one. After solving all test cases, the program should be terminated immediately. After printing any query do not forget to output an end of line and flush the output buffer. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. The interactor is non-adaptive. The tree does not change during the interaction. Hacks For hack, use the following format: The first line contains a single integer $$$t$$$ ($$$1 le t le 500$$$) — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$4 le n le 10^4$$$)xa0— the number of nodes in Genokraken's nerve system. The second line of each test case contains $$$n-1$$$ integers $$$p_1, p_2, ldots, p_{n-1}$$$ ($$$0 le p_1 le p_2 le ldots le p_{n-1} le n - 2$$$, $$$0 le p_i < i$$$)xa0— the direct parents of node $$$1$$$, $$$2$$$, ..., $$$n-1$$$ in the system, respectively. In each test case, the values $$$p_1, p_2, ldots, p_{n-1}$$$ must ensure the following in the tree: If we remove root node $$$0$$$ and all adjacent edges, this tree will turn into a forest consisting of only paths. Each node that was initially adjacent to the node $$$0$$$ will be the end of some path. Node $$$1$$$ has exactly two adjacent nodes (including the node $$$0$$$). The sum of $$$n$$$ over all test cases must not exceed $$$10^4$$$. Example Output ? 2 3 ! 0 0 1 ? 2 3 ? 2 4 ! 0 0 1 2 ! 0 0 0 1 3 5 6 7 Note In the first test case, Genokraken's nerve system forms the following tree: The answer to "? 2 3" is $$$1$$$. This means that the simple path between nodes $$$2$$$ and $$$3$$$ contains node $$$0$$$. In the second test case, Genokraken's nerve system forms the following tree: The answer to "? 2 3" is $$$1$$$. This means that the simple path between nodes $$$2$$$ and $$$3$$$ contains node $$$0$$$. The answer to "? 2 4" is $$$0$$$. This means that the simple path between nodes $$$2$$$ and $$$4$$$ doesn't contain node $$$0$$$. In the third test case, Genokraken's nerve system forms the following tree: | 1,800 | false | true | true | false | true | true | false | false | false | true | 71 |
291D | Polycarpus has a computer with _n_ processors. Also, his computer has _n_ memory cells. We'll consider the processors numbered by integers from 1 to _n_ and that the memory cells are consecutively numbered by integers from 1 to _n_. Polycarpus needs to come up with a parallel program model. For each memory cell number _i_ this program must record the value _n_u2009-u2009_i_ to this cell. In other words, for each cell you've got to find the distance to cell _n_. Let's denote the value that is written in the _i_-th cell as _a__i_. Initially, _a__i_u2009=u20091 (1u2009≤u2009_i_u2009<u2009_n_) and _a__n_u2009=u20090. We will consider that only processor _i_ can write values in the memory cell number _i_. All processors can read an information from some cell (several processors can read an information from some cell simultaneously). The parallel program is executed in several steps. During each step we execute the parallel version of the increment operation. Executing the parallel version of the increment operation goes as follows: 1. Each processor independently of the other ones chooses some memory cell. Let's say that processor _i_ has chosen a cell with number _c__i_ (1u2009≤u2009_c__i_u2009≤u2009_n_). 2. All processors simultaneously execute operation _a__i_u2009=u2009_a__i_u2009+u2009_a__c__i_. Help Polycarpus come up with the parallel program model that is executed in exactly _k_ steps. Calculate the operations that need to be executed. Note that after _k_ steps for all _i_'s value _a__i_ must be equal _n_u2009-u2009_i_. Input The first line contains two space-separated integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009104,u20091u2009≤u2009_k_u2009≤u200920). It is guaranteed that at the given _n_ and _k_ the required sequence of operations exists. Output Print exactly _n_·_k_ integers in _k_ lines. In the first line print numbers _c_1,u2009_c_2,u2009...,u2009_c__n_ (1u2009≤u2009_c__i_u2009≤u2009_n_) for the first increment operation. In the second line print the numbers for the second increment operation. In the _k_-th line print the numbers for the _k_-th increment operation. As a result of the printed operations for any _i_ value _a__i_ must equal _n_u2009-u2009_i_. | 1,600 | false | true | false | false | false | false | false | false | false | false | 8,668 |
1641D | Sam changed his school and on the first biology lesson he got a very interesting task about genes. You are given $$$n$$$ arrays, the $$$i$$$-th of them contains $$$m$$$ different integers — $$$a_{i,1}, a_{i,2},ldots,a_{i,m}$$$. Also you are given an array of integers $$$w$$$ of length $$$n$$$. Find the minimum value of $$$w_i + w_j$$$ among all pairs of integers $$$(i, j)$$$ ($$$1 le i, j le n$$$), such that the numbers $$$a_{i,1}, a_{i,2},ldots,a_{i,m}, a_{j,1}, a_{j,2},ldots,a_{j,m}$$$ are distinct. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$2 leq n leq 10^5$$$, $$$1 le m le 5$$$). The $$$i$$$-th of the next $$$n$$$ lines starts with $$$m$$$ distinct integers $$$a_{i,1}, a_{i,2}, ldots, a_{i,m}$$$ and then $$$w_i$$$ follows ($$$1leq a_{i,j} leq 10^9$$$, $$$1 leq w_{i} leq 10^9$$$). Output Print a single number — the answer to the problem. If there are no suitable pairs $$$(i, j)$$$, print $$$-1$$$. Examples Input 4 2 1 2 5 4 3 1 2 3 2 4 5 3 Input 4 3 1 2 3 5 2 3 4 2 3 4 5 3 1 3 10 10 Note In the first test the minimum value is $$$5 = w_3 + w_4$$$, because numbers $$${2, 3, 4, 5}$$$ are distinct. In the second test case, there are no suitable pair $$$(i, j)$$$. | 2,700 | true | true | false | false | false | false | true | false | false | false | 2,406 |
27C | Problem - 27C - 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 greedy *1900 No tag edit access → Contest materials ") . The second line contains _n_ space-separated integers — the given sequence. All numbers in this sequence do not exceed 106 by absolute value. Output If the given sequence does not contain any unordered subsequences, output 0. Otherwise, output the length _k_ of the shortest such subsequence. Then output _k_ integers from the range | 1,900 | false | true | false | false | false | true | false | false | false | false | 9,861 |
812B | Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off. The building consists of _n_ floors with stairs at the left and the right sides. Each floor has _m_ rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with _n_ rows and _m_u2009+u20092 columns, where the first and the last columns represent the stairs, and the _m_ columns in the middle represent rooms. Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights. Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u200915 and 1u2009≤u2009_m_u2009≤u2009100) — the number of floors and the number of rooms in each floor, respectively. The next _n_ lines contains the building description. Each line contains a binary string of length _m_u2009+u20092 representing a floor (the left stairs, then _m_ rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor. The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0. Output Print a single integer — the minimum total time needed to turn off all the lights. Examples Input 3 4 001000 000010 000010 Input 4 3 01110 01110 01110 01110 Note In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs. In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor. In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor. | 1,600 | false | false | false | true | false | false | true | false | false | false | 6,515 |
313D | Everything is great about Ilya's city, except the roads. The thing is, the only ZooVille road is represented as _n_ holes in a row. We will consider the holes numbered from 1 to _n_, from left to right. Ilya is really keep on helping his city. So, he wants to fix at least _k_ holes (perharps he can fix more) on a single ZooVille road. The city has _m_ building companies, the _i_-th company needs _c__i_ money units to fix a road segment containing holes with numbers of at least _l__i_ and at most _r__i_. The companies in ZooVille are very greedy, so, if they fix a segment containing some already fixed holes, they do not decrease the price for fixing the segment. Determine the minimum money Ilya will need to fix at least _k_ holes. Input The first line contains three integers _n_,u2009_m_,u2009_k_ (1u2009≤u2009_n_u2009≤u2009300,u20091u2009≤u2009_m_u2009≤u2009105,u20091u2009≤u2009_k_u2009≤u2009_n_). The next _m_ lines contain the companies' description. The _i_-th line contains three integers _l__i_,u2009_r__i_,u2009_c__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_n_,u20091u2009≤u2009_c__i_u2009≤u2009109). Output Print a single integer — the minimum money Ilya needs to fix at least _k_ holes. If it is impossible to fix at least _k_ holes, print -1. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 10 4 6 7 9 11 6 9 13 7 7 7 3 5 6 Input 10 7 1 3 4 15 8 9 8 5 6 8 9 10 6 1 4 2 1 4 10 8 10 13 | 2,100 | false | false | false | true | false | false | false | false | false | false | 8,592 |
1081C | On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard. There are $$$n$$$ bricks lined in a row on the ground. Chouti has got $$$m$$$ paint buckets of different colors at hand, so he painted each brick in one of those $$$m$$$ colors. Having finished painting all bricks, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are $$$k$$$ bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure). So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo $$$998,244,353$$$. Input The first and only line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 leq n,m leq 2000, 0 leq k leq n-1$$$)xa0— the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it. Output Print one integerxa0— the number of ways to color bricks modulo $$$998,244,353$$$. Note In the first example, since $$$k=0$$$, the color of every brick should be the same, so there will be exactly $$$m=3$$$ ways to color the bricks. In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all $$$4$$$ possible colorings. | 1,500 | true | false | false | true | false | false | false | false | false | false | 5,321 |
983C | You work in a big office. It is a 9xa0floor building with an elevator that can accommodate up to 4xa0people. It is your responsibility to manage this elevator. Today you are late, so there are queues on some floors already. For each person you know the floor where he currently is and the floor he wants to reach. Also, you know the order in which people came to the elevator. According to the company's rules, if an employee comes to the elevator earlier than another one, he has to enter the elevator earlier too (even if these employees stay on different floors). Note that the employees are allowed to leave the elevator in arbitrary order. The elevator has two commands: Go up or down one floor. The movement takes 1 second. Open the doors on the current floor. During this operation all the employees who have reached their destination get out of the elevator. Then all the employees on the floor get in the elevator in the order they are queued up while it doesn't contradict the company's rules and there is enough space in the elevator. Each employee spends 1 second to get inside and outside the elevator. Initially the elevator is empty and is located on the floor 1. You are interested what is the minimum possible time you need to spend to deliver all the employees to their destination. It is not necessary to return the elevator to the floor 1. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20092000)xa0— the number of employees. The _i_-th of the next _n_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u20099, _a__i_u2009≠u2009_b__i_)xa0— the floor on which an employee initially is, and the floor he wants to reach. The employees are given in the order they came to the elevator. Output Print a single integerxa0— the minimal possible time in seconds. Note Explaination for the first sample _t_u2009=u20090_t_u2009=u20092 _t_u2009=u20093 _t_u2009=u20095 _t_u2009=u20096 _t_u2009=u20097 _t_u2009=u20099 _t_u2009=u200910 | 2,400 | false | false | false | true | false | false | false | false | false | true | 5,784 |
568B | Little Johnny has recently learned about set theory. Now he is studying binary relations. You've probably heard the term "equivalence relation". These relations are very important in many areas of mathematics. For example, the equality of the two numbers is an equivalence relation. A set ρ of pairs (_a_,u2009_b_) of elements of some set _A_ is called a binary relation on set _A_. For two elements _a_ and _b_ of the set _A_ we say that they are in relation ρ, if pair , in this case we use a notation . Binary relation is equivalence relation, if: 1. It is reflexive (for any _a_ it is true that ); 2. It is symmetric (for any _a_, _b_ it is true that if , then ); 3. It is transitive (if and , than ). Little Johnny is not completely a fool and he noticed that the first condition is not necessary! Here is his "proof": Take any two elements, _a_ and _b_. If , then (according to property (2)), which means (according to property (3)). It's very simple, isn't it? However, you noticed that Johnny's "proof" is wrong, and decided to show him a lot of examples that prove him wrong. Here's your task: count the number of binary relations over a set of size _n_ such that they are symmetric, transitive, but not an equivalence relations (i.e. they are not reflexive). Since their number may be very large (not 0, according to Little Johnny), print the remainder of integer division of this number by 109u2009+u20097. Note If _n_u2009=u20091 there is only one such relationxa0— an empty one, i.e. . In other words, for a single element _x_ of set _A_ the following is hold: . If _n_u2009=u20092 there are three such relations. Let's assume that set _A_ consists of two elements, _x_ and _y_. Then the valid relations are , ρu2009=u2009{(_x_,u2009_x_)}, ρu2009=u2009{(_y_,u2009_y_)}. It is easy to see that the three listed binary relations are symmetric and transitive relations, but they are not equivalence relations. | 1,900 | true | false | false | true | false | false | false | false | false | false | 7,577 |
1400G | Polycarp plays a (yet another!) strategic computer game. In this game, he leads an army of mercenaries. Polycarp wants to gather his army for a quest. There are $$$n$$$ mercenaries for hire, and the army should consist of some subset of them. The $$$i$$$-th mercenary can be chosen if the resulting number of chosen mercenaries is not less than $$$l_i$$$ (otherwise he deems the quest to be doomed) and not greater than $$$r_i$$$ (he doesn't want to share the trophies with too many other mercenaries). Furthermore, $$$m$$$ pairs of mercenaries hate each other and cannot be chosen for the same quest. How many non-empty subsets does Polycarp need to consider? In other words, calculate the number of non-empty subsets of mercenaries such that the size of this subset belongs to $$$[l_i, r_i]$$$ for each chosen mercenary, and there are no two mercenaries in the subset that hate each other. The answer may be large, so calculate it modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 3 cdot 10^5$$$, $$$0 le m le min(20, dfrac{n(n-1)}{2})$$$) — the number of mercenaries and the number of pairs of mercenaries that hate each other. Then $$$n$$$ lines follow, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$). Then $$$m$$$ lines follow, the $$$i$$$-th of them contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i < b_i le n$$$) denoting that the mercenaries $$$a_i$$$ and $$$b_i$$$ hate each other. There are no two equal pairs in this list. Output Print one integer — the number of non-empty subsets meeting the constraints, taken modulo $$$998244353$$$. | 2,600 | true | false | false | true | false | false | true | false | false | false | 3,666 |
1621F | Dasha has $$$10^{100}$$$ coins. Recently, she found a binary string $$$s$$$ of length $$$n$$$ and some operations that allows to change this string (she can do each operation any number of times): 1. Replace substring 00 of $$$s$$$ by 0 and receive $$$a$$$ coins. 2. Replace substring 11 of $$$s$$$ by 1 and receive $$$b$$$ coins. 3. Remove 0 from any position in $$$s$$$ and pay $$$c$$$ coins. It turned out that while doing this operations Dasha should follow the rule: It is forbidden to do two operations with the same parity in a row. Operations are numbered by integers $$$1$$$-$$$3$$$ in the order they are given above. Please, calculate what is the maximum profit Dasha can get by doing these operations and following this rule. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The first line of each test case contains four integers $$$n$$$, $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 leq n leq 10^5, 1 leq a, b, c leq 10^9$$$). The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$. It is guaranteed that the total sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case print the answer. Example Input 3 5 2 2 1 01101 6 4 3 5 110001 6 3 2 1 011110 Note In the first test case one of the optimal sequences of operations is 01101 $$$ ightarrow$$$ 0101 $$$ ightarrow$$$ 011 $$$ ightarrow$$$ 01. This sequence of operations consists of operations $$$2$$$, $$$3$$$ and $$$2$$$ in this order. It satisfies all rules and gives profit $$$3$$$. It can be shown that it is impossible to achieve higher profit in this test case, so the answer is $$$3$$$. In the second test case one of the optimal sequences of operations is 110001 $$$ ightarrow$$$ 11001 $$$ ightarrow$$$ 1001 $$$ ightarrow$$$ 101. In the third test case one of the optimal sequences of operations is 011110 $$$ ightarrow$$$ 01110 $$$ ightarrow$$$ 1110 $$$ ightarrow$$$ 110 $$$ ightarrow$$$ 11 $$$ ightarrow$$$ 1. | 2,700 | false | true | true | false | true | false | false | false | false | false | 2,515 |
213C | Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of _n_ meters. The given square is split into _n_u2009×u2009_n_ cells (represented as unit squares), each cell has some number. At the beginning of the race Furik stands in a cell with coordinates (1,u20091), and Rubik stands in a cell with coordinates (_n_,u2009_n_). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (_i_,u2009_j_), then he can move to cell (_i_u2009+u20091,u2009_j_) or (_i_,u2009_j_u2009+u20091). After Furik reaches Rubik, Rubik starts running from cell with coordinates (_n_,u2009_n_) to cell with coordinates (1,u20091). If Rubik stands in cell (_i_,u2009_j_), then he can move to cell (_i_u2009-u20091,u2009_j_) or (_i_,u2009_j_u2009-u20091). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum. Print the maximum number of points Furik and Rubik can earn on the relay race. Input The first line contains a single integer (1u2009≤u2009_n_u2009≤u2009300). The next _n_ lines contain _n_ integers each: the _j_-th number on the _i_-th line _a__i_,u2009_j_ (u2009-u20091000u2009≤u2009_a__i_,u2009_j_u2009≤u20091000) is the number written in the cell with coordinates (_i_,u2009_j_). Output On a single line print a single number — the answer to the problem. Examples Input 3 25 16 25 12 18 19 11 13 8 Note Comments to the second sample: The profitable path for Furik is: (1,u20091), (1,u20092), (2,u20092), and for Rubik: (2,u20092), (2,u20091), (1,u20091). Comments to the third sample: The optimal path for Furik is: (1,u20091), (1,u20092), (1,u20093), (2,u20093), (3,u20093), and for Rubik: (3,u20093), (3,u20092), (2,u20092), (2,u20091), (1,u20091). The figure to the sample: Furik's path is marked with yellow, and Rubik's path is marked with pink. | 2,000 | false | false | false | true | false | false | false | false | false | false | 8,986 |
639F | Limak is a smart brown bear who loves chemistry, reactions and transforming elements. In Bearland (Limak's home) there are _n_ elements, numbered 1 through _n_. There are also special machines, that can transform elements. Each machine is described by two integers _a__i_,u2009_b__i_ representing two elements, not necessarily distinct. One can use a machine either to transform an element _a__i_ to _b__i_ or to transform _b__i_ to _a__i_. Machines in Bearland aren't very resistant and each of them can be used at most once. It is possible that _a__i_u2009=u2009_b__i_ and that many machines have the same pair _a__i_,u2009_b__i_. Radewoosh is Limak's biggest enemy and rival. He wants to test Limak in the chemistry. They will meet tomorrow and both of them will bring all their machines. Limak has _m_ machines but he doesn't know much about his enemy. They agreed Radewoosh will choose two distinct elements, let's denote them as _x_ and _y_. Limak will be allowed to use both his and Radewoosh's machines. He may use zero or more (maybe even all) machines to achieve the goal, each machine at most once. Limak will start from an element _x_ and his task will be to first get an element _y_ and then to again get an element _x_xa0— then we say that he succeeds. After that Radewoosh would agree that Limak knows the chemistry (and Radewoosh would go away). Radewoosh likes some particular non-empty set of favorite elements and he will choose _x_,u2009_y_ from that set. Limak doesn't know exactly which elements are in the set and also he doesn't know what machines Radewoosh has. Limak has heard _q_ gossips (queries) though and each of them consists of Radewoosh's machines and favorite elements. For each gossip Limak wonders if he would be able to succeed tomorrow for every pair _x_,u2009_y_ chosen from the set of favorite elements. If yes then print "YES" (without the quotes). But if there exists a pair (_x_,u2009_y_) from the given set that Limak wouldn't be able to succeed then you should print "NO" (without the quotes). Input The first line contains three integers _n_, _m_ and _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009300u2009000,u20090u2009≤u2009_m_u2009≤u2009300u2009000)xa0— the number of elements, the number of Limak's machines and the number of gossips, respectively. Each of the next _m_ lines contains two integers _a__i_ and _b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_) describing one of Limak's machines. Then, the description of _q_ gossips follows. The first line of the description of the _i_-th gossip contains two integers _n__i_ and _m__i_ (1u2009≤u2009_n__i_u2009≤u2009300u2009000,u20090u2009≤u2009_m__i_u2009≤u2009300u2009000). The second line contains _n__i_ distinct integers _x__i_,u20091,u2009_x__i_,u20092,u2009...,u2009_x__i_,u2009_n__i_ (1u2009≤u2009_x__i_,u2009_j_u2009≤u2009_n_)xa0— Radewoosh's favorite elements in the _i_-th gossip. Note that _n__i_u2009=u20091 is allowed, in this case there are no pairs of distinct elements, so Limak automatically wins (the answer is "YES"). Then _m__i_ lines follow, each containing two integers _a__i_,u2009_j_,u2009_b__i_,u2009_j_ (1u2009≤u2009_a__i_,u2009_j_,u2009_b__i_,u2009_j_) describing one of Radewoosh's machines in the _i_-th gossip. The sum of _n__i_ over all gossips won't exceed 300u2009000. Also, the sum of _m__i_ over all gossips won't exceed 300u2009000. Important: Because we want you to process the gossips online, in order to know the elements in Radewoosh's favorite set and elements that his machines can transform, for on each number that denotes them in the input you should use following function: int rotate(int element) { element=(element+R)%n;if (element==0) { element=n; } return element; } where _R_ is initially equal to 0 and is increased by the number of the query any time the answer is "YES". Queries are numbered starting with 1 in the order they appear in the input. Output You should print _q_ lines. The _i_-th of them should contain "YES" (without quotes) if for the _i_-th gossip for each pair of elements _x_ and _y_ (in the set _x__i_,u20091,u2009_x__i_,u20092,u2009...,u2009_x__i_,u2009_n__i_) Limak is able to succeed. Otherwise you should print "NO" (without quotes). Examples Input 6 5 4 1 2 2 3 3 4 2 4 5 6 2 0 4 2 2 1 6 2 3 4 3 2 6 3 4 2 5 4 6 2 1 1 2 1 2 Input 7 6 2 1 2 1 3 2 4 2 5 3 6 3 7 7 2 1 2 3 4 5 6 7 4 5 6 7 7 2 1 2 3 4 5 6 7 4 6 5 7 Note Lets look at first sample: In first gossip Radewoosh's favorite set is {4,u20092} and he has no machines. Limak can tranform element 4 into 2 (so half of a task is complete) and then 2 into 3, and 3 into 4. Answer is "YES", so _R_ is increased by 1. In second gossip set in the input is denoted by {6,u20092} and machine by (3,u20094), but _R_ is equal to 1, so set is {1,u20093} and machine is (4,u20095). Answer is "NO", so _R_ isn't changed. In third gossip set {6,u20094,u20093} and machines (2,u20095) and (4,u20096) are deciphered to be {1,u20095,u20094}, (3,u20096) and (5,u20091). Consider Radewoosh's choices: If he chooses elements 1 and 5, then Limak is able to transform 1 into 5, then 6 into 3, 3 into 2 and 2 into 1. If he chooses elements 5 and 4, then Limak is able to transform 5 into 6, 6 into 3, 3 into 4 (half way already behind him), 4 into 2, 2 into 1, 1 into 5. If he chooses elements 1 and 4, then Limak is able to transform 1 into 2, 2 into 4, 4 into 3, 3 into 6, 6 into 5 and 5 into 1. So Limak is able to execute task. Answer is "YES" and _R_ is increased by 3 (it's equal to 4 now). In last gossip {1,u20092} and (1,u20092) are deciphered to be {5,u20096} and (5,u20096). Now there are 2 machines (5,u20096) so Limak is able to execute task again. | 3,300 | false | false | false | false | true | false | false | false | false | true | 7,252 |
1598C | Monocarp has got an array $$$a$$$ consisting of $$$n$$$ integers. Let's denote $$$k$$$ as the mathematic mean of these elements (note that it's possible that $$$k$$$ is not an integer). The mathematic mean of an array of $$$n$$$ elements is the sum of elements divided by the number of these elements (i.u2009e. sum divided by $$$n$$$). Monocarp wants to delete exactly two elements from $$$a$$$ so that the mathematic mean of the remaining $$$(n - 2)$$$ elements is still equal to $$$k$$$. Your task is to calculate the number of pairs of positions $$$[i, j]$$$ ($$$i < j$$$) such that if the elements on these positions are deleted, the mathematic mean of $$$(n - 2)$$$ remaining elements is equal to $$$k$$$ (that is, it is equal to the mathematic mean of $$$n$$$ elements of the original array $$$a$$$). Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of testcases. The first line of each testcase contains one integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$) — the number of elements in the array. The second line contains a sequence of integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^{9}$$$), where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output Print one integer — the number of pairs of positions $$$[i, j]$$$ ($$$i < j$$$) such that if the elements on these positions are deleted, the mathematic mean of $$$(n - 2)$$$ remaining elements is equal to $$$k$$$ (that is, it is equal to the mathematic mean of $$$n$$$ elements of the original array $$$a$$$). Example Input 4 4 8 8 8 8 3 50 20 10 5 1 4 7 3 5 7 1 2 3 4 5 6 7 Note In the first example, any pair of elements can be removed since all of them are equal. In the second example, there is no way to delete two elements so the mathematic mean doesn't change. In the third example, it is possible to delete the elements on positions $$$1$$$ and $$$3$$$, or the elements on positions $$$4$$$ and $$$5$$$. | 1,200 | true | false | true | true | true | false | false | false | false | false | 2,669 |
1551B2 | This problem is an extension of the problem "Wonderful Coloring - 1". It has quite many differences, so you should read this statement completely. Recently, Paul and Mary have found a new favorite sequence of integers $$$a_1, a_2, dots, a_n$$$. They want to paint it using pieces of chalk of $$$k$$$ colors. The coloring of a sequence is called wonderful if the following conditions are met: 1. each element of the sequence is either painted in one of $$$k$$$ colors or isn't painted; 2. each two elements which are painted in the same color are different (i.u2009e. there's no two equal values painted in the same color); 3. let's calculate for each of $$$k$$$ colors the number of elements painted in the color — all calculated numbers must be equal; 4. the total number of painted elements of the sequence is the maximum among all colorings of the sequence which meet the first three conditions. E.u2009g. consider a sequence $$$a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$$$ and $$$k=3$$$. One of the wonderful colorings of the sequence is shown in the figure. The example of a wonderful coloring of the sequence $$$a=[3, 1, 1, 1, 1, 10, 3, 10, 10, 2]$$$ and $$$k=3$$$. Note that one of the elements isn't painted. Help Paul and Mary to find a wonderful coloring of a given sequence $$$a$$$. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10000$$$) — the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of two lines. The first one contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2cdot10^5$$$, $$$1 le k le n$$$) — the length of a given sequence and the number of colors, respectively. The second one contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le n$$$). It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 cdot 10^5$$$. Output Output $$$t$$$ lines, each of them must contain a description of a wonderful coloring for the corresponding test case. Each wonderful coloring must be printed as a sequence of $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$0 le c_i le k$$$) separated by spaces where $$$c_i=0$$$, if $$$i$$$-th element isn't painted; $$$c_i>0$$$, if $$$i$$$-th element is painted in the $$$c_i$$$-th color. Remember that you need to maximize the total count of painted elements for the wonderful coloring. If there are multiple solutions, print any one. Example Input 6 10 3 3 1 1 1 1 10 3 10 10 2 4 4 1 1 1 1 1 1 1 13 1 3 1 4 1 5 9 2 6 5 3 5 8 9 13 2 3 1 4 1 5 9 2 6 5 3 5 8 9 13 3 3 1 4 1 5 9 2 6 5 3 5 8 9 Output 1 1 0 2 3 2 2 1 3 3 4 2 1 3 1 0 0 1 1 0 1 1 1 0 1 1 1 0 2 1 2 2 1 1 1 1 2 1 0 2 2 1 1 3 2 1 3 3 1 2 2 3 2 0 Note In the first test case, the answer is shown in the figure in the statement. The red color has number $$$1$$$, the blue color — $$$2$$$, the green — $$$3$$$. | 1,400 | false | true | false | false | true | true | false | true | false | false | 2,890 |
180F | Students of group 199 have written their lectures dismally. Now an exam on Mathematical Analysis is approaching and something has to be done asap (that is, quickly). Let's number the students of the group from 1 to _n_. Each student _i_ (1u2009≤u2009_i_u2009≤u2009_n_) has a best friend _p_[_i_] (1u2009≤u2009_p_[_i_]u2009≤u2009_n_). In fact, each student is a best friend of exactly one student. In other words, all _p_[_i_] are different. It is possible that the group also has some really "special individuals" for who _i_u2009=u2009_p_[_i_]. Each student wrote exactly one notebook of lecture notes. We know that the students agreed to act by the following algorithm: on the first day of revising each student studies his own Mathematical Analysis notes, in the morning of each following day each student gives the notebook to his best friend and takes a notebook from the student who calls him the best friend. Thus, on the second day the student _p_[_i_] (1u2009≤u2009_i_u2009≤u2009_n_) studies the _i_-th student's notes, on the third day the notes go to student _p_[_p_[_i_]] and so on. Due to some characteristics of the boys' friendship (see paragraph 1), each day each student has exactly one notebook to study. You are given two sequences that describe the situation on the third and fourth days of revising: _a_1,u2009_a_2,u2009...,u2009_a__n_, where _a__i_ means the student who gets the _i_-th student's notebook on the third day of revising; _b_1,u2009_b_2,u2009...,u2009_b__n_, where _b__i_ means the student who gets the _i_-th student's notebook on the fourth day of revising. You do not know array _p_, that is you do not know who is the best friend to who. Write a program that finds _p_ by the given sequences _a_ and _b_. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105) — the number of students in the group. The second line contains sequence of different integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009_n_). The third line contains the sequence of different integers _b_1,u2009_b_2,u2009...,u2009_b__n_ (1u2009≤u2009_b__i_u2009≤u2009_n_). Output Print sequence _n_ of different integers _p_[1],u2009_p_[2],u2009...,u2009_p_[_n_] (1u2009≤u2009_p_[_i_]u2009≤u2009_n_). It is guaranteed that the solution exists and that it is unique. | 1,200 | true | false | true | false | false | true | false | false | false | false | 9,117 |
286B | Problem - 286B - 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 *2200 No tag edit access → Contest materials is an integer, the transformation parameter, _r_ is such maximum integer that _rk_u2009≤u2009_n_. If _rk_u2009=u2009_n_, then elements _p__rk_u2009+u20091,u2009_p__rk_u2009+u20092 and so on are omitted. In other words, the described transformation of permutation _p_ cyclically shifts to the left each consecutive block of length _k_ and the last block with the length equal to the remainder after dividing _n_ by _k_. John Doe thinks that permutation _f_(_f_(xa0...xa0_f_(_p_u2009=u2009 | 2,200 | false | false | true | false | false | false | false | false | false | false | 8,690 |
1844E | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 884 (Div. 1 + Div. 2) 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 2-sat constructive algorithms dfs and similar dsu graphs *2400 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST E. Great Grids time limit per test1 second memory limit per test256 megabytes An $$$n imes m$$$ grid of characters is called great if it satisfies these three conditions: Each character is either 'A', 'B', or 'C'. Every $$$2 imes 2$$$ contiguous subgrid contains all three different letters. Any two cells that share a common edge contain different letters. Let $$$(x,y)$$$ denote the cell in the $$$x$$$-th row from the top and $$$y$$$-th column from the left. You want to construct a great grid that satisfies $$$k$$$ constraints. Each constraint consists of two cells, $$$(x_{i,1},y_{i,1})$$$ and $$$(x_{i,2},y_{i,2})$$$, that share exactly one corner. You want your great grid to have the same letter in cells $$$(x_{i,1},y_{i,1})$$$ and $$$(x_{i,2},y_{i,2})$$$. Determine whether there exists a great grid satisfying all the constraints. 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 three integers, $$$n$$$, $$$m$$$, and $$$k$$$ ($$$2 le n,m le 2 cdot 10^3$$$, $$$1 le k le 4 cdot 10^3$$$). Each of the next $$$k$$$ lines contains four integers, $$$x_{i,1}$$$, $$$y_{i,1}$$$, $$$x_{i,2}$$$, and $$$y_{i,2}$$$ ($$$1 le x_{i,1} < x_{i,2} le n$$$, $$$1 le y_{i,1},y_{i,2} le m$$$). It is guaranteed that either $$$(x_{i,2},y_{i,2}) = (x_{i,1}+1,y_{i,1}+1)$$$ or $$$(x_{i,2},y_{i,2}) = (x_{i,1}+1,y_{i,1}-1)$$$. The pairs of cells are pairwise distinct, i.e. for all $$$1 le i < j le k$$$, it is not true that $$$x_{i,1} = x_{j,1}$$$, $$$y_{i,1} = y_{j,1}$$$, $$$x_{i,2} = x_{j,2}$$$, and $$$y_{i,2} = y_{j,2}$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^3$$$. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$2 cdot 10^3$$$. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$4 cdot 10^3$$$. Output For each test case, output "YES" if a great grid satisfying all the constraints exists and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example input 4 3 4 4 1 1 2 2 2 1 3 2 1 4 2 3 2 3 3 2 2 7 2 1 1 2 2 1 2 2 1 8 5 4 1 2 2 1 1 5 2 4 7 1 8 2 7 4 8 5 8 5 4 1 2 2 1 1 5 2 4 7 1 8 2 7 5 8 4 output YES NO YES NO Note In the first test case, the following great grid satisfies all the constraints: B A B C C B C A A C A B In the second test case, the two constraints imply that cells $$$(1,1)$$$ and $$$(2,2)$$$ have the same letter and cells $$$(1,2)$$$ and $$$(2,1)$$$ have the same letter, which makes it impossible for the only $$$2 imes 2$$$ subgrid to contain all three different letters. Codeforces (c) | 2,400 | false | false | false | false | false | true | false | false | false | true | 1,219 |
51D | Problem - 51D - Codeforces =============== xa0 — the number of elements in the given sequence. The second line contains the given sequence. The numbers are space-separated. All the elements of the given sequence are integers and their absolute value does not exceed 104. Output Print 0, if the given sequence is a geometric progression. Otherwise, check if it is possible to make the sequence a geometric progression by deleting a single element. If it is possible, print 1. If it is impossible, print 2. Examples Input 4 3 6 12 24 Output 0 Input 4 -8 -16 24 -32 Output 1 Input 4 0 1 2 3 Output 2 | 2,200 | false | false | true | false | false | false | false | false | false | false | 9,716 |
837C | One very important person has a piece of paper in the form of a rectangle _a_u2009×u2009_b_. Also, he has _n_ seals. Each seal leaves an impression on the paper in the form of a rectangle of the size _x__i_u2009×u2009_y__i_. Each impression must be parallel to the sides of the piece of paper (but seal can be rotated by 90 degrees). A very important person wants to choose two different seals and put them two impressions. Each of the selected seals puts exactly one impression. Impressions should not overlap (but they can touch sides), and the total area occupied by them should be the largest possible. What is the largest area that can be occupied by two seals? Input The first line contains three integer numbers _n_, _a_ and _b_ (1u2009≤u2009_n_,u2009_a_,u2009_b_u2009≤u2009100). Each of the next _n_ lines contain two numbers _x__i_, _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009100). Output Print the largest total area that can be occupied by two seals. If you can not select two seals, print 0. Examples Input 4 10 9 2 3 1 1 5 10 9 11 Note In the first example you can rotate the second seal by 90 degrees. Then put impression of it right under the impression of the first seal. This will occupy all the piece of paper. In the second example you can't choose the last seal because it doesn't fit. By choosing the first and the third seals you occupy the largest area. In the third example there is no such pair of seals that they both can fit on a piece of paper. | 1,500 | false | false | true | false | false | false | true | false | false | false | 6,419 |
598B | You are given a string _s_ and should process _m_ queries. Each query is described by two 1-based indices _l__i_, _r__i_ and integer _k__i_. It means that you should cyclically shift the substring _s_[_l__i_... _r__i_] _k__i_ times. The queries should be processed one after another in the order they are given. One operation of a cyclic shift (rotation) is equivalent to moving the last character to the position of the first character and shifting all other characters one position to the right. For example, if the string _s_ is abacaba and the query is _l_1u2009=u20093,u2009_r_1u2009=u20096,u2009_k_1u2009=u20091 then the answer is abbacaa. If after that we would process the query _l_2u2009=u20091,u2009_r_2u2009=u20094,u2009_k_2u2009=u20092 then we would get the string baabcaa. Input The first line of the input contains the string _s_ (1u2009≤u2009_s_u2009≤u200910u2009000) in its initial state, where _s_ stands for the length of _s_. It contains only lowercase English letters. Second line contains a single integer _m_ (1u2009≤u2009_m_u2009≤u2009300)xa0— the number of queries. The _i_-th of the next _m_ lines contains three integers _l__i_, _r__i_ and _k__i_ (1u2009≤u2009_l__i_u2009≤u2009_r__i_u2009≤u2009_s_,u20091u2009≤u2009_k__i_u2009≤u20091u2009000u2009000)xa0— the description of the _i_-th query. Output Print the resulting string _s_ after processing all _m_ queries. Note The sample is described in problem statement. | 1,300 | false | false | true | false | false | false | false | false | false | false | 7,456 |
173A | Nikephoros and Polycarpus play rock-paper-scissors. The loser gets pinched (not too severely!). Let us remind you the rules of this game. Rock-paper-scissors is played by two players. In each round the players choose one of three items independently from each other. They show the items with their hands: a rock, scissors or paper. The winner is determined by the following rules: the rock beats the scissors, the scissors beat the paper and the paper beats the rock. If the players choose the same item, the round finishes with a draw. Nikephoros and Polycarpus have played _n_ rounds. In each round the winner gave the loser a friendly pinch and the loser ended up with a fresh and new red spot on his body. If the round finished in a draw, the players did nothing and just played on. Nikephoros turned out to have worked out the following strategy: before the game began, he chose some sequence of items _A_u2009=u2009(_a_1,u2009_a_2,u2009...,u2009_a__m_), and then he cyclically showed the items from this sequence, starting from the first one. Cyclically means that Nikephoros shows signs in the following order: _a_1, _a_2, ..., _a__m_, _a_1, _a_2, ..., _a__m_, _a_1, ... and so on. Polycarpus had a similar strategy, only he had his own sequence of items _B_u2009=u2009(_b_1,u2009_b_2,u2009...,u2009_b__k_). Determine the number of red spots on both players after they've played _n_ rounds of the game. You can consider that when the game began, the boys had no red spots on them. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20092·109) — the number of the game's rounds. The second line contains sequence _A_ as a string of _m_ characters and the third line contains sequence _B_ as a string of _k_ characters (1u2009≤u2009_m_,u2009_k_u2009≤u20091000). The given lines only contain characters "R", "S" and "P". Character "R" stands for the rock, character "S" represents the scissors and "P" represents the paper. Note In the first sample the game went like this: R - R. Draw. P - S. Nikephoros loses. S - P. Polycarpus loses. R - P. Nikephoros loses. P - R. Polycarpus loses. S - S. Draw. R - P. Nikephoros loses. Thus, in total Nikephoros has 3 losses (and 3 red spots), and Polycarpus only has 2. | 1,300 | true | false | true | false | false | false | false | false | false | false | 9,173 |
1118E | The king of Berland organizes a ball! $$$n$$$ pair are invited to the ball, they are numbered from $$$1$$$ to $$$n$$$. Each pair consists of one man and one woman. Each dancer (either man or woman) has a monochrome costume. The color of each costume is represented by an integer from $$$1$$$ to $$$k$$$, inclusive. Let $$$b_i$$$ be the color of the man's costume and $$$g_i$$$ be the color of the woman's costume in the $$$i$$$-th pair. You have to choose a color for each dancer's costume (i.e. values $$$b_1, b_2, dots, b_n$$$ and $$$g_1, g_2, dots g_n$$$) in such a way that: 1. for every $$$i$$$: $$$b_i$$$ and $$$g_i$$$ are integers between $$$1$$$ and $$$k$$$, inclusive; 2. there are no two completely identical pairs, i.e. no two indices $$$i, j$$$ ($$$i e j$$$) such that $$$b_i = b_j$$$ and $$$g_i = g_j$$$ at the same time; 3. there is no pair such that the color of the man's costume is the same as the color of the woman's costume in this pair, i.e. $$$b_i e g_i$$$ for every $$$i$$$; 4. for each two consecutive (adjacent) pairs both man's costume colors and woman's costume colors differ, i.e. for every $$$i$$$ from $$$1$$$ to $$$n-1$$$ the conditions $$$b_i e b_{i + 1}$$$ and $$$g_i e g_{i + 1}$$$ hold. Let's take a look at the examples of bad and good color choosing (for $$$n=4$$$ and $$$k=3$$$, man is the first in a pair and woman is the second): Bad color choosing: $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$, $$$(1, 2)$$$ — contradiction with the second rule (there are equal pairs); $$$(2, 3)$$$, $$$(1, 1)$$$, $$$(3, 2)$$$, $$$(1, 3)$$$ — contradiction with the third rule (there is a pair with costumes of the same color); $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(1, 3)$$$, $$$(2, 1)$$$ — contradiction with the fourth rule (there are two consecutive pairs such that colors of costumes of men/women are the same). Good color choosing: $$$(1, 2)$$$, $$$(2, 1)$$$, $$$(1, 3)$$$, $$$(3, 1)$$$; $$$(1, 2)$$$, $$$(3, 1)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$; $$$(3, 1)$$$, $$$(1, 2)$$$, $$$(2, 3)$$$, $$$(3, 2)$$$. You have to find any suitable color choosing or say that no suitable choosing exists. Input The only line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n, k le 2 cdot 10^5$$$) — the number of pairs and the number of colors. Output If it is impossible to find any suitable colors choosing, print "NO". Otherwise print "YES" and then the colors of the costumes of pairs in the next $$$n$$$ lines. The $$$i$$$-th line should contain two integers $$$b_i$$$ and $$$g_i$$$ — colors of costumes of man and woman in the $$$i$$$-th pair, respectively. You can print each letter in any case (upper or lower). For example, "YeS", "no" and "yES" are all acceptable. Examples Output YES 3 1 1 3 3 2 2 3 Output YES 2 1 1 3 4 2 3 4 4 3 3 2 2 4 4 1 1 4 3 1 | 1,700 | false | false | true | false | false | true | false | false | false | false | 5,097 |
932E | Problem - 932E - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags combinatorics dp math *2400 No tag edit access → Contest materials representing total number of people and _k_ (1u2009≤u2009_k_u2009≤u20095000). Output Output the sum of costs for all non empty subsets modulo 109u2009+u20097. Examples Input 1 1 Output 1 Input 3 2 Output 24 Note In the first example, there is only one non-empty subset {1} with cost 11u2009=u20091. In the second example, there are seven non-empty subsets. - {1} with cost 12u2009=u20091 - {2} with cost 12u2009=u20091 - {1,u20092} with cost 22u2009=u20094 - {3} with cost 12u2009=u20091 - {1,u20093} with cost 22u2009=u20094 - {2,u20093} with cost 22u2009=u20094 - {1,u20092,u20093} with cost 32u2009=u20099 The total cost is 1u2009+u20091u2009+u20094u2009+u20091u2009+u20094u2009+u20094u2009+u20099u2009=u200924. | 2,400 | true | false | false | true | false | false | false | false | false | false | 5,974 |
868G | The prehistoric caves of El Toll are located in Moià (Barcelona). You have heard that there is a treasure hidden in one of _n_ possible spots in the caves. You assume that each of the spots has probability 1u2009/u2009_n_ to contain a treasure. You cannot get into the caves yourself, so you have constructed a robot that can search the caves for treasure. Each day you can instruct the robot to visit exactly _k_ distinct spots in the caves. If none of these spots contain treasure, then the robot will obviously return with empty hands. However, the caves are dark, and the robot may miss the treasure even when visiting the right spot. Formally, if one of the visited spots does contain a treasure, the robot will obtain it with probability 1u2009/u20092, otherwise it will return empty. Each time the robot searches the spot with the treasure, his success probability is independent of all previous tries (that is, the probability to miss the treasure after searching the right spot _x_ times is 1u2009/u20092_x_). What is the expected number of days it will take to obtain the treasure if you choose optimal scheduling for the robot? Output the answer as a rational number modulo 109u2009+u20097. Formally, let the answer be an irreducible fraction _P_u2009/u2009_Q_, then you have to output . It is guaranteed that _Q_ is not divisible by 109u2009+u20097. Input The first line contains the number of test cases _T_ (1u2009≤u2009_T_u2009≤u20091000). Each of the next _T_ lines contains two integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u20095·108). Output For each test case output the answer in a separate line. Example Output 2 500000007 777777786 Note In the first case the robot will repeatedly search in the only spot. The expected number of days in this case is 2. Note that in spite of the fact that we know the treasure spot from the start, the robot still has to search there until he succesfully recovers the treasure. In the second case the answer can be shown to be equal to 7u2009/u20092 if we search the two spots alternatively. In the third case the answer is 25u2009/u20099. | 3,300 | true | false | false | false | false | false | false | false | false | false | 6,267 |
769A | There is the faculty of Computer Science in Berland. In the social net "TheContact!" for each course of this faculty there is the special group whose name equals the year of university entrance of corresponding course of students at the university. Each of students joins the group of his course and joins all groups for which the year of student's university entrance differs by no more than _x_ from the year of university entrance of this student, where _x_ — some non-negative integer. A value _x_ is not given, but it can be uniquely determined from the available data. Note that students don't join other groups. You are given the list of groups which the student Igor joined. According to this information you need to determine the year of Igor's university entrance. Input The first line contains the positive odd integer _n_ (1u2009≤u2009_n_u2009≤u20095) — the number of groups which Igor joined. The next line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (2010u2009≤u2009_a__i_u2009≤u20092100) — years of student's university entrance for each group in which Igor is the member. It is guaranteed that the input data is correct and the answer always exists. Groups are given randomly. Output Print the year of Igor's university entrance. Note In the first test the value _x_u2009=u20091. Igor entered the university in 2015. So he joined groups members of which are students who entered the university in 2014, 2015 and 2016. In the second test the value _x_u2009=u20090. Igor entered only the group which corresponds to the year of his university entrance. | 800 | false | false | true | false | false | false | false | false | true | false | 6,706 |
1333B | Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem: There are two arrays of integers $$$a$$$ and $$$b$$$ of length $$$n$$$. It turned out that array $$$a$$$ contains only elements from the set $$${-1, 0, 1}$$$. Anton can perform the following sequence of operations any number of times: 1. Choose any pair of indexes $$$(i, j)$$$ such that $$$1 le i < j le n$$$. It is possible to choose the same pair $$$(i, j)$$$ more than once. 2. Add $$$a_i$$$ to $$$a_j$$$. In other words, $$$j$$$-th element of the array becomes equal to $$$a_i + a_j$$$. For example, if you are given array $$$[1, -1, 0]$$$, you can transform it only to $$$[1, -1, -1]$$$, $$$[1, 0, 0]$$$ and $$$[1, -1, 1]$$$ by one operation. Anton wants to predict if it is possible to apply some number (zero or more) of these operations to the array $$$a$$$ so that it becomes equal to array $$$b$$$. Can you help him? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10000$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) xa0— the length of arrays. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$-1 le a_i le 1$$$) xa0— elements of array $$$a$$$. There can be duplicates among elements. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$-10^9 le b_i le 10^9$$$) xa0— elements of array $$$b$$$. There can be duplicates among elements. It is guaranteed that the sum of $$$n$$$ over all test cases doesn't exceed $$$10^5$$$. Output For each test case, output one line containing "YES" if it's possible to make arrays $$$a$$$ and $$$b$$$ equal by performing the described operations, or "NO" if it's impossible. You can print each letter in any case (upper or lower). Example Input 5 3 1 -1 0 1 1 -2 3 0 1 1 0 2 2 2 1 0 1 41 2 -1 0 -1 -41 5 0 1 -1 1 -1 1 1 -1 1 -1 Note In the first test-case we can choose $$$(i, j)=(2, 3)$$$ twice and after that choose $$$(i, j)=(1, 2)$$$ twice too. These operations will transform $$$[1, -1, 0] o [1, -1, -2] o [1, 1, -2]$$$ In the second test case we can't make equal numbers on the second position. In the third test case we can choose $$$(i, j)=(1, 2)$$$ $$$41$$$ times. The same about the fourth test case. In the last lest case, it is impossible to make array $$$a$$$ equal to the array $$$b$$$. | 1,100 | false | true | true | false | false | false | false | false | false | false | 4,042 |
1656A | You are given an array $$$a_1, a_2, ldots, a_n$$$ of positive integers. A good pair is a pair of indices $$$(i, j)$$$ with $$$1 leq i, j leq n$$$ such that, for all $$$1 leq k leq n$$$, the following equality holds: $$$$$$ a_i - a_k + a_k - a_j = a_i - a_j, $$$$$$ where $$$x$$$ denotes the absolute value of $$$x$$$. Find a good pair. Note that $$$i$$$ can be equal to $$$j$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) — the number of test cases. Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 leq n leq 10^5$$$) — the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq 10^9$$$) where $$$a_i$$$ is the $$$i$$$-th element of the array. The sum of $$$n$$$ for all test cases is at most $$$2 cdot 10^5$$$. Output For each test case, print a single line with two space-separated indices $$$i$$$ and $$$j$$$ which form a good pair of the array. The case $$$i=j$$$ is allowed. It can be shown that such a pair always exists. If there are multiple good pairs, print any of them. Note In the first case, for $$$i = 2$$$ and $$$j = 3$$$ the equality holds true for all $$$k$$$: $$$k = 1$$$: $$$a_2 - a_1 + a_1 - a_3 = 2 - 5 + 5 - 7 = 5 = 2 - 7 = a_2-a_3$$$, $$$k = 2$$$: $$$a_2 - a_2 + a_2 - a_3 = 2 - 2 + 2 - 7 = 5 = 2 - 7 = a_2-a_3$$$, $$$k = 3$$$: $$$a_2 - a_3 + a_3 - a_3 = 2 - 7 + 7 - 7 = 5 = 2 - 7 = a_2-a_3$$$. | 800 | true | false | false | false | false | false | false | false | true | false | 2,354 |
1041B | Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than $$$a$$$ and screen height not greater than $$$b$$$. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is $$$w$$$, and the height of the screen is $$$h$$$, then the following condition should be met: $$$frac{w}{h} = frac{x}{y}$$$. There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers $$$w$$$ and $$$h$$$ there is a TV set with screen width $$$w$$$ and height $$$h$$$ in the shop. Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers $$$w$$$ and $$$h$$$, beforehand, such that $$$(w le a)$$$, $$$(h le b)$$$ and $$$(frac{w}{h} = frac{x}{y})$$$. In other words, Monocarp wants to determine the number of TV sets having aspect ratio $$$frac{x}{y}$$$, screen width not exceeding $$$a$$$, and screen height not exceeding $$$b$$$. Two TV sets are considered different if they have different screen width or different screen height. Input The first line contains four integers $$$a$$$, $$$b$$$, $$$x$$$, $$$y$$$ ($$$1 le a, b, x, y le 10^{18}$$$)xa0— the constraints on the screen width and height, and on the aspect ratio. Output Print one integerxa0— the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints. Examples Input 1000000000000000000 1000000000000000000 999999866000004473 999999822000007597 Note In the first example, there are $$$3$$$ possible variants: $$$(5, 3)$$$, $$$(10, 6)$$$, $$$(15, 9)$$$. In the second example, there is no TV set meeting the constraints. In the third example, there is only one variant: $$$(3, 2)$$$. | 1,000 | true | false | false | false | false | false | false | false | false | false | 5,494 |
1627C | You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$, with edges numbered from $$$1$$$ to $$$n-1$$$. A tree is a connected undirected graph without cycles. You have to assign integer weights to each edge of the tree, such that the resultant graph is a prime tree. A prime tree is a tree where the weight of every path consisting of one or two edges is prime. A path should not visit any vertex twice. The weight of a path is the sum of edge weights on that path. Consider the graph below. It is a prime tree as the weight of every path of two or less edges is prime. For example, the following path of two edges: $$$2 o 1 o 3$$$ has a weight of $$$11 + 2 = 13$$$, which is prime. Similarly, the path of one edge: $$$4 o 3$$$ has a weight of $$$5$$$, which is also prime. Print any valid assignment of weights such that the resultant tree is a prime tree. If there is no such assignment, then print $$$-1$$$. It can be proven that if a valid assignment exists, one exists with weights between $$$1$$$ and $$$10^5$$$ as well. Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 10^5$$$)xa0— the number of vertices in the tree. Then, $$$n-1$$$ lines follow. The $$$i$$$-th line contains two integers $$$u$$$ and $$$v$$$ ($$$1 leq u, v leq n$$$) denoting that edge number $$$i$$$ is between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, if a valid assignment exists, then print a single line containing $$$n-1$$$ integers $$$a_1, a_2, dots, a_{n-1}$$$ ($$$1 leq a_i le 10^5$$$), where $$$a_i$$$ denotes the weight assigned to the edge numbered $$$i$$$. Otherwise, print $$$-1$$$. If there are multiple solutions, you may print any. Example Input 3 2 1 2 4 1 3 4 3 2 1 7 1 2 1 3 3 4 3 5 6 2 7 2 Note For the first test case, there are only two paths having one edge each: $$$1 o 2$$$ and $$$2 o 1$$$, both having a weight of $$$17$$$, which is prime. The second test case is described in the statement. It can be proven that no such assignment exists for the third test case. | 1,400 | false | false | false | false | false | true | false | false | false | false | 2,479 |
961B | Your friend Mishka and you attend a calculus lecture. Lecture lasts _n_ minutes. Lecturer tells _a__i_ theorems during the _i_-th minute. Mishka is really interested in calculus, though it is so hard to stay awake for all the time of lecture. You are given an array _t_ of Mishka's behavior. If Mishka is asleep during the _i_-th minute of the lecture then _t__i_ will be equal to 0, otherwise it will be equal to 1. When Mishka is awake he writes down all the theorems he is being told — _a__i_ during the _i_-th minute. Otherwise he writes nothing. You know some secret technique to keep Mishka awake for _k_ minutes straight. However you can use it only once. You can start using it at the beginning of any minute between 1 and _n_u2009-u2009_k_u2009+u20091. If you use it on some minute _i_ then Mishka will be awake during minutes _j_ such that and will write down all the theorems lecturer tells. You task is to calculate the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up. Input The first line of the input contains two integer numbers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105) — the duration of the lecture in minutes and the number of minutes you can keep Mishka awake. The second line of the input contains _n_ integer numbers _a_1,u2009_a_2,u2009... _a__n_ (1u2009≤u2009_a__i_u2009≤u2009104) — the number of theorems lecturer tells during the _i_-th minute. The third line of the input contains _n_ integer numbers _t_1,u2009_t_2,u2009... _t__n_ (0u2009≤u2009_t__i_u2009≤u20091) — type of Mishka's behavior at the _i_-th minute of the lecture. Output Print only one integer — the maximum number of theorems Mishka will be able to write down if you use your technique only once to wake him up. Example Input 6 3 1 3 5 2 5 4 1 1 0 1 0 0 Note In the sample case the better way is to use the secret technique at the beginning of the third minute. Then the number of theorems Mishka will be able to write down will be equal to 16. | 1,200 | false | false | true | true | true | false | false | false | false | false | 5,862 |
796C | Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. There are _n_ banks, numbered from 1 to _n_. There are also _n_u2009-u20091 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank _i_ has initial strength _a__i_. Let us define some keywords before we proceed. Bank _i_ and bank _j_ are neighboring if and only if there exists a wire directly connecting them. Bank _i_ and bank _j_ are semi-neighboring if and only if there exists an online bank _k_ such that bank _i_ and bank _k_ are neighboring and bank _k_ and bank _j_ are neighboring. When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1. To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank _x_ if and only if all these conditions are met: 1. Bank _x_ is online. That is, bank _x_ is not hacked yet. 2. Bank _x_ is neighboring to some offline bank. 3. The strength of bank _x_ is less than or equal to the strength of Inzane's computer. Determine the minimum strength of the computer Inzane needs to hack all the banks. Input The first line contains one integer _n_ (1u2009≤u2009_n_u2009≤u20093·105)xa0— the total number of banks. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (u2009-u2009109u2009≤u2009_a__i_u2009≤u2009109)xa0— the strengths of the banks. Each of the next _n_u2009-u20091 lines contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_)xa0— meaning that there is a wire directly connecting banks _u__i_ and _v__i_. It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength. Note In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1,u20092,u20093,u20094,u20095]. He hacks bank 5, then strengths of the banks become [1,u20092,u20094,u20095,u2009u2009-u2009]. He hacks bank 4, then strengths of the banks become [1,u20093,u20095,u2009u2009-u2009,u2009u2009-u2009]. He hacks bank 3, then strengths of the banks become [2,u20094,u2009u2009-u2009,u2009u2009-u2009,u2009u2009-u2009]. He hacks bank 2, then strengths of the banks become [3,u2009u2009-u2009,u2009u2009-u2009,u2009u2009-u2009,u2009u2009-u2009]. He completes his goal by hacking bank 1. In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93. | 1,900 | false | false | false | true | true | true | false | false | false | false | 6,591 |
665D | Problem - 665D - Codeforces =============== xa0 ]( "Educational Codeforces Round 12") (1u2009u2009≤u2009_i_u2009u2009<u2009u2009_j_u2009≤u2009_k_), _x__i_u2009u2009+u2009u2009_x__j_ is a prime. You are given an array _a_ with _n_ positive integers _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_ (not necessary distinct). You want to find a simple subset of the array _a_ with the maximum size. A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Let's define a subset of the array _a_ as a tuple that can be obtained from _a_ by removing some (possibly all) elements of it. Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the number of integers in the array _a_. The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009106) — the elements of the array _a_. Output On the first line print integer _m_ — the maximum possible size of simple subset of _a_. On the second line print _m_ integers _b__l_ — the elements of the simple subset of the array _a_ with the maximum size. If there is more than one solution you can print any of them. You can print the elements of the subset in any order. Examples Input 2 2 3 Output 2 3 2 Input 2 2 2 Output 1 2 Input 3 2 1 1 Output 3 1 1 2 Input 2 83 14 Output 2 14 83 | 1,800 | false | true | false | false | false | true | false | false | false | false | 7,158 |
141B | So nearly half of the winter is over and Maria is dreaming about summer. She's fed up with skates and sleds, she was dreaming about Hopscotch all night long. It's a very popular children's game. The game field, the court, looks as is shown in the figure (all blocks are square and are numbered from bottom to top, blocks in the same row are numbered from left to right). Let us describe the hopscotch with numbers that denote the number of squares in the row, staring from the lowest one: 1-1-2-1-2-1-2-(1-2)..., where then the period is repeated (1-2). The coordinate system is defined as shown in the figure. Side of all the squares are equal and have length _a_. Maria is a very smart and clever girl, and she is concerned with quite serious issues: if she throws a stone into a point with coordinates (_x_,u2009_y_), then will she hit some square? If the answer is positive, you are also required to determine the number of the square. It is believed that the stone has fallen into the square if it is located strictly inside it. In other words a stone that has fallen on the square border is not considered a to hit a square. Input The only input line contains three integers: _a_, _x_, _y_, where _a_ (1u2009≤u2009_a_u2009≤u2009100) is the side of the square, _x_ and _y_ (u2009-u2009106u2009≤u2009_x_u2009≤u2009106,u20090u2009≤u2009_y_u2009≤u2009106) are coordinates of the stone. Output Print the number of the square, inside which the stone fell. If the stone is on a border of some stone or outside the court, print "-1" without the quotes. | 1,400 | true | false | false | false | false | false | false | false | false | false | 9,311 |
1263C | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainxa0— it is not given to any of the participants. For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating. Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help. For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$lfloor n/k floor$$$ for some positive integer $$$k$$$ (where $$$lfloor x floor$$$ is the value of $$$x$$$ rounded down): $$$0 = lfloor 5/7 floor$$$, $$$1 = lfloor 5/5 floor$$$, $$$2 = lfloor 5/2 floor$$$, $$$5 = lfloor 5/1 floor$$$. Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. Input The first line contains integer number $$$t$$$ ($$$1 le t le 10$$$)xa0— the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 le n le 10^9$$$)xa0— the total number of the rating units being drawn. Output Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$xa0— the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderxa0— the values of possible rating increments. | 1,400 | true | false | false | false | false | false | false | true | false | false | 4,385 |
1701D | Monocarp had a permutation $$$a$$$ of $$$n$$$ integers $$$1$$$, $$$2$$$, ..., $$$n$$$ (a permutation is an array where each element from $$$1$$$ to $$$n$$$ occurs exactly once). Then Monocarp calculated an array of integers $$$b$$$ of size $$$n$$$, where $$$b_i = leftlfloor frac{i}{a_i} ight floor$$$. For example, if the permutation $$$a$$$ is $$$[2, 1, 4, 3]$$$, then the array $$$b$$$ is equal to $$$left[ leftlfloor frac{1}{2} ight floor, leftlfloor frac{2}{1} ight floor, leftlfloor frac{3}{4} ight floor, leftlfloor frac{4}{3} ight floor ight] = [0, 2, 0, 1]$$$. Unfortunately, the Monocarp has lost his permutation, so he wants to restore it. Your task is to find a permutation $$$a$$$ that corresponds to the given array $$$b$$$. If there are multiple possible permutations, then print any of them. The tests are constructed in such a way that least one suitable permutation exists. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$). The second line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$0 le b_i le n$$$). Additional constrains on the input: the sum of $$$n$$$ over test cases does not exceed $$$5 cdot 10^5$$$; there exists at least one permutation $$$a$$$ that would yield this array $$$b$$$. Output For each test case, print $$$n$$$ integersxa0— a permutation $$$a$$$ that corresponds to the given array $$$b$$$. If there are multiple possible permutations, then print any of them. | 1,900 | true | true | false | false | true | false | false | true | true | false | 2,073 |
241B | Problem - 241B - Codeforces =============== xa0 ]( --- Finished → Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. → Problem tags binary search bitmasks data structures math *2700 No tag edit access → Contest materials of integers _a__i_ and _a__j_. You want to take pictures in a way that the total sum of attractiveness of your pictures is maximized. You have to calculate this value. Since the result may not fit in a 32-bit integer number, print it modulo 1000000007 (109u2009+u20097). Input The first line of input contains two integers _n_ and _m_ — the number of friends and the number of pictures that you want to take. Next line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009109) — the values of attractiveness of the friends. Output The only line of output should contain an integer — the optimal total sum of attractiveness of your pictures. Examples Input 3 1 1 2 3 Output 3 Input 3 2 1 2 3 Output 5 Input 3 3 1 2 3 Output 6 | 2,700 | true | false | false | false | true | false | false | true | false | false | 8,874 |
218A | Little Bolek has found a picture with _n_ mountain peaks painted on it. The _n_ painted peaks are represented by a non-closed polyline, consisting of 2_n_ segments. The segments go through 2_n_u2009+u20091 points with coordinates (1,u2009_y_1), (2,u2009_y_2), ..., (2_n_u2009+u20091,u2009_y_2_n_u2009+u20091), with the _i_-th segment connecting the point (_i_,u2009_y__i_) and the point (_i_u2009+u20091,u2009_y__i_u2009+u20091). For any even _i_ (2u2009≤u2009_i_u2009≤u20092_n_) the following condition holds: _y__i_u2009-u20091u2009<u2009_y__i_ and _y__i_u2009>u2009_y__i_u2009+u20091. We shall call a vertex of a polyline with an even _x_ coordinate a mountain peak. The figure to the left shows the initial picture, the figure to the right shows what the picture looks like after Bolek's actions. The affected peaks are marked red, _k_ = 2. Bolek fancied a little mischief. He chose exactly _k_ mountain peaks, rubbed out the segments that went through those peaks and increased each peak's height by one (that is, he increased the _y_ coordinate of the corresponding points). Then he painted the missing segments to get a new picture of mountain peaks. Let us denote the points through which the new polyline passes on Bolek's new picture as (1,u2009_r_1), (2,u2009_r_2), ..., (2_n_u2009+u20091,u2009_r_2_n_u2009+u20091). Given Bolek's final picture, restore the initial one. Input The first line contains two space-separated integers _n_ and _k_ (1u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009100). The next line contains 2_n_u2009+u20091 space-separated integers _r_1,u2009_r_2,u2009...,u2009_r_2_n_u2009+u20091 (0u2009≤u2009_r__i_u2009≤u2009100) — the _y_ coordinates of the polyline vertices on Bolek's picture. It is guaranteed that we can obtain the given picture after performing the described actions on some picture of mountain peaks. Output Print 2_n_u2009+u20091 integers _y_1,u2009_y_2,u2009...,u2009_y_2_n_u2009+u20091 — the _y_ coordinates of the vertices of the polyline on the initial picture. If there are multiple answers, output any one of them. | 1,100 | false | false | true | false | false | true | true | false | false | false | 8,966 |
81A | Polycarp thinks about the meaning of life very often. He does this constantly, even when typing in the editor. Every time he starts brooding he can no longer fully concentrate and repeatedly presses the keys that need to be pressed only once. For example, instead of the phrase "how are you" he can type "hhoow aaaare yyoouu". Polycarp decided to automate the process of correcting such errors. He decided to write a plug-in to the text editor that will remove pairs of identical consecutive letters (if there are any in the text). Of course, this is not exactly what Polycarp needs, but he's got to start from something! Help Polycarp and write the main plug-in module. Your program should remove from a string all pairs of identical letters, which are consecutive. If after the removal there appear new pairs, the program should remove them as well. Technically, its work should be equivalent to the following: while the string contains a pair of consecutive identical letters, the pair should be deleted. Note that deleting of the consecutive identical letters can be done in any order, as any order leads to the same result. Input The input data consists of a single line to be processed. The length of the line is from 1 to 2·105 characters inclusive. The string contains only lowercase Latin letters. Output Print the given string after it is processed. It is guaranteed that the result will contain at least one character. | 1,400 | false | false | true | false | false | false | false | false | false | false | 9,563 |
1200F | Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th vertex has $$$m_i$$$ outgoing edges that are labeled as $$$e_i[0]$$$, $$$e_i[1]$$$, $$$ldots$$$, $$$e_i[m_i-1]$$$, each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The $$$i$$$-th vertex also has an integer $$$k_i$$$ written on itself. A travel on this graph works as follows. 1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable $$$c$$$ to this integer. 2. After arriving at the vertex $$$i$$$, or when Gildong begins the travel at some vertex $$$i$$$, add $$$k_i$$$ to $$$c$$$. 3. The next vertex is $$$e_i[x]$$$ where $$$x$$$ is an integer $$$0 le x le m_i-1$$$ satisfying $$$x equiv c pmod {m_i}$$$. Go to the next vertex and go back to step 2. It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly. For example, assume that Gildong starts at vertex $$$1$$$ with $$$c = 5$$$, and $$$m_1 = 2$$$, $$$e_1[0] = 1$$$, $$$e_1[1] = 2$$$, $$$k_1 = -3$$$. Right after he starts at vertex $$$1$$$, $$$c$$$ becomes $$$2$$$. Since the only integer $$$x$$$ ($$$0 le x le 1$$$) where $$$x equiv c pmod {m_i}$$$ is $$$0$$$, Gildong goes to vertex $$$e_1[0] = 1$$$. After arriving at vertex $$$1$$$ again, $$$c$$$ becomes $$$-1$$$. The only integer $$$x$$$ satisfying the conditions is $$$1$$$, so he goes to vertex $$$e_1[1] = 2$$$, and so on. Since Gildong is quite inquisitive, he's going to ask you $$$q$$$ queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of $$$c$$$. Note that you should not count the vertices that will be visited only finite times. Input The first line of the input contains an integer $$$n$$$ ($$$1 le n le 1000$$$), the number of vertices in the graph. The second line contains $$$n$$$ integers. The $$$i$$$-th integer is $$$k_i$$$ ($$$-10^9 le k_i le 10^9$$$), the integer written on the $$$i$$$-th vertex. Next $$$2 cdot n$$$ lines describe the edges of each vertex. The $$$(2 cdot i + 1)$$$-st line contains an integer $$$m_i$$$ ($$$1 le m_i le 10$$$), the number of outgoing edges of the $$$i$$$-th vertex. The $$$(2 cdot i + 2)$$$-nd line contains $$$m_i$$$ integers $$$e_i[0]$$$, $$$e_i[1]$$$, $$$ldots$$$, $$$e_i[m_i-1]$$$, each having an integer value between $$$1$$$ and $$$n$$$, inclusive. Next line contains an integer $$$q$$$ ($$$1 le q le 10^5$$$), the number of queries Gildong wants to ask. Next $$$q$$$ lines contains two integers $$$x$$$ and $$$y$$$ ($$$1 le x le n$$$, $$$-10^9 le y le 10^9$$$) each, which mean that the start vertex is $$$x$$$ and the starting value of $$$c$$$ is $$$y$$$. Output For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex $$$x$$$ with starting integer $$$y$$$. Examples Input 4 0 0 0 0 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Input 4 4 -5 -3 -1 2 2 3 1 2 3 2 4 1 4 3 1 2 1 6 1 0 2 0 3 -1 4 -2 1 1 1 5 Note The first example can be shown like the following image: Three integers are marked on $$$i$$$-th vertex: $$$i$$$, $$$k_i$$$, and $$$m_i$$$ respectively. The outgoing edges are labeled with an integer representing the edge number of $$$i$$$-th vertex. The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex ($$$c$$$ after $$$k_i$$$ added)". $$$1(0) o 2(0) o 2(0) o ldots$$$ $$$2(0) o 2(0) o ldots$$$ $$$3(-1) o 1(-1) o 3(-1) o ldots$$$ $$$4(-2) o 2(-2) o 2(-2) o ldots$$$ $$$1(1) o 3(1) o 4(1) o 1(1) o ldots$$$ $$$1(5) o 3(5) o 1(5) o ldots$$$ The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example. The queries for the second example works as follows: $$$1(4) o 2(-1) o 2(-6) o ldots$$$ $$$2(-5) o 2(-10) o ldots$$$ $$$3(-4) o 1(0) o 2(-5) o 2(-10) o ldots$$$ $$$4(-3) o 1(1) o 3(-2) o 4(-3) o ldots$$$ $$$1(5) o 3(2) o 1(6) o 2(1) o 2(-4) o ldots$$$ $$$1(9) o 3(6) o 2(1) o 2(-4) o ldots$$$ | 2,300 | true | false | true | true | true | false | true | false | false | true | 4,696 |
1975D | 378QAQ has a tree with $$$n$$$ vertices. Initially, all vertices are white. There are two chess pieces called $$$P_A$$$ and $$$P_B$$$ on the tree. $$$P_A$$$ and $$$P_B$$$ are initially located on vertices $$$a$$$ and $$$b$$$ respectively. In one step, 378QAQ will do the following in order: 1. Move $$$P_A$$$ to a neighboring vertex. If the target vertex is white, this vertex will be painted red. 2. Move $$$P_B$$$ to a neighboring vertex. If the target vertex is colored in red, this vertex will be painted blue. Initially, the vertex $$$a$$$ is painted red. If $$$a=b$$$, the vertex $$$a$$$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time. 378QAQ wants to know the minimum number of steps to paint all vertices blue. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1leq tleq 10^4$$$). The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$1leq nleq 2cdot 10^5$$$). The second line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1leq a,bleq n$$$). Then $$$n - 1$$$ lines follow, each line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i,y_i le n$$$), indicating an edge between vertices $$$x_i$$$ and $$$y_i$$$. It is guaranteed that these edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output the minimum number of steps to paint all vertices blue. Example Input 3 2 1 2 1 2 5 1 2 1 2 1 3 1 4 1 5 8 5 4 7 1 1 5 1 8 8 3 7 2 8 6 3 4 Note In the first test case, 378QAQ can paint all vertices blue in the following order: Initially, $$$P_A$$$ is located on the vertex $$$1$$$, and $$$P_B$$$ is located on the vertex $$$2$$$. The vertex $$$1$$$ is painted red and the vertex $$$2$$$ is white. 378QAQ moves $$$P_A$$$ to the vertex $$$2$$$ and paints it red. Then 378QAQ moves $$$P_B$$$ to the vertex $$$1$$$ and paints it blue. 378QAQ moves $$$P_A$$$ to the vertex $$$1$$$. Then 378QAQ moves $$$P_B$$$ to the vertex $$$2$$$ and paints it blue. | 1,700 | false | true | false | true | false | false | true | false | false | false | 439 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.