info
large_stringlengths
120
50k
question
large_stringlengths
504
10.4k
avg@8_qwen3_4b_instruct_2507
float64
0
0.88
{"tests": "{\"inputs\": [\"5\\n1 2 3 2 1\\n\", \"3\\n10 6 8\\n\", \"7\\n1 2 1 2 1 2 1\\n\", \"7\\n1 2 1 2 1 3 1\\n\", \"5\\n1 1 4 2 2\\n\", \"6\\n1 3 4 3 5 4\\n\", \"1\\n1\\n\", \"7\\n6 2 2 6 5 7 7\\n\", \"7\\n2 4 1 2 3 1 2\\n\", \"7\\n2 1 2 1 2 2 1\\n\", \"7\\n5 2 4 2 4 1 1\\n\", \"6\\n3 2 1 7 3 7\\n\", \"100\\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\\n\"], \"outputs\": [\"1 2 3 2 1 \\n\", \"10 6 6 \\n\", \"1 2 1 1 1 1 1 \\n\", \"1 1 1 1 1 3 1 \\n\", \"1 1 4 2 2 \\n\", \"1 3 3 3 5 4 \\n\", \"1 \\n\", \"2 2 2 5 5 7 7 \\n\", \"2 4 1 1 1 1 1 \\n\", \"1 1 1 1 2 2 1 \\n\", \"5 2 2 2 2 1 1 \\n\", \"1 1 1 7 3 3 \\n\", \"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 22 22 53 99 59 52 52 31 31 31 31 23 23 9 9 9 7 7 7 7 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 \\n\"]}", "source": "primeintellect"}
This is a harder version of the problem. In this version $n \le 500\,000$ The outskirts of the capital are being actively built up in Berland. The company "Kernel Panic" manages the construction of a residential complex of skyscrapers in New Berlskva. All skyscrapers are built along the highway. It is known that the company has already bought $n$ plots along the highway and is preparing to build $n$ skyscrapers, one skyscraper per plot. Architects must consider several requirements when planning a skyscraper. Firstly, since the land on each plot has different properties, each skyscraper has a limit on the largest number of floors it can have. Secondly, according to the design code of the city, it is unacceptable for a skyscraper to simultaneously have higher skyscrapers both to the left and to the right of it. Formally, let's number the plots from $1$ to $n$. Then if the skyscraper on the $i$-th plot has $a_i$ floors, it must hold that $a_i$ is at most $m_i$ ($1 \le a_i \le m_i$). Also there mustn't be integers $j$ and $k$ such that $j < i < k$ and $a_j > a_i < a_k$. Plots $j$ and $k$ are not required to be adjacent to $i$. The company wants the total number of floors in the built skyscrapers to be as large as possible. Help it to choose the number of floors for each skyscraper in an optimal way, i.e. in such a way that all requirements are fulfilled, and among all such construction plans choose any plan with the maximum possible total number of floors. -----Input----- The first line contains a single integer $n$ ($1 \leq n \leq 500\,000$) — the number of plots. The second line contains the integers $m_1, m_2, \ldots, m_n$ ($1 \leq m_i \leq 10^9$) — the limit on the number of floors for every possible number of floors for a skyscraper on each plot. -----Output----- Print $n$ integers $a_i$ — the number of floors in the plan for each skyscraper, such that all requirements are met, and the total number of floors in all skyscrapers is the maximum possible. If there are multiple answers possible, print any of them. -----Examples----- Input 5 1 2 3 2 1 Output 1 2 3 2 1 Input 3 10 6 8 Output 10 6 6 -----Note----- In the first example, you can build all skyscrapers with the highest possible height. In the second test example, you cannot give the maximum height to all skyscrapers as this violates the design code restriction. The answer $[10, 6, 6]$ is optimal. Note that the answer of $[6, 6, 8]$ also satisfies all restrictions, but is not optimal. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 5 1 3\\n1 1 2\\n2 2 3\\n3 3 3\\n4 1 1\\n10 1 3\\n\", \"2 3 2 1\\n1 1 2\\n2 1 2\\n4 1 2\\n\", \"5 11 1 5\\n1 1 5\\n2 2 2\\n3 1 1\\n4 3 3\\n5 3 3\\n6 1 1\\n7 4 4\\n8 4 5\\n10 1 3\\n11 5 5\\n13 1 5\\n\", \"4 6 4 2\\n2 2 2\\n3 3 3\\n4 1 1\\n10 1 4\\n11 2 3\\n12 2 4\\n\", \"7 5 7 6\\n1 4 5\\n2 7 7\\n3 6 6\\n4 3 4\\n5 1 3\\n\", \"4 4 3 4\\n1 2 4\\n2 1 2\\n3 3 4\\n4 2 3\\n\", \"10 10 1 10\\n1 1 10\\n2 1 1\\n3 7 10\\n4 6 7\\n5 9 9\\n6 4 9\\n7 2 5\\n8 3 10\\n9 2 10\\n10 7 9\\n\", \"20 20 17 20\\n1 16 20\\n2 12 13\\n3 14 16\\n4 13 15\\n5 3 15\\n6 2 11\\n7 18 18\\n8 5 15\\n9 6 12\\n10 19 19\\n11 9 11\\n12 14 17\\n13 19 19\\n14 12 20\\n15 1 1\\n16 11 17\\n17 13 14\\n18 5 17\\n19 2 10\\n20 19 20\\n\", \"100 5 99 1\\n1 1 2\\n2 2 3\\n3 3 3\\n4 1 1\\n10 1 3\\n\", \"5 1 1 5\\n1 1 1\\n\", \"3 5 1 3\\n1 1 2\\n2 2 3\\n3 3 3\\n4 1 1\\n1000000000 1 3\\n\", \"2 2 1 2\\n1 1 2\\n1000000000 1 2\\n\", \"10 1 1 10\\n1 5 6\\n\"], \"outputs\": [\"XXRR\\n\", \"XXL\\n\", \"XXXRXRXXRR\\n\", \"LXXL\\n\", \"L\\n\", \"XR\\n\", \"XXRRRXXXXRRRRRR\\n\", \"XRRR\\n\", \"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL\\n\", \"XRRRR\\n\", \"XXRR\\n\", \"XR\\n\", \"RRRRRRRRR\\n\"]}", "source": "primeintellect"}
Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined up in a row. We'll consider the spies numbered from 1 to n from left to right. Spy s has an important note. He has to pass the note to spy f. Xenia interrogates the spies in several steps. During one step the spy keeping the important note can pass the note to one of his neighbours in the row. In other words, if this spy's number is x, he can pass the note to another spy, either x - 1 or x + 1 (if x = 1 or x = n, then the spy has only one neighbour). Also during a step the spy can keep a note and not pass it to anyone. But nothing is that easy. During m steps Xenia watches some spies attentively. Specifically, during step t_{i} (steps are numbered from 1) Xenia watches spies numbers l_{i}, l_{i} + 1, l_{i} + 2, ..., r_{i} (1 ≤ l_{i} ≤ r_{i} ≤ n). Of course, if during some step a spy is watched, he can't do anything: neither give the note nor take it from some other spy. Otherwise, Xenia reveals the spies' cunning plot. Nevertheless, if the spy at the current step keeps the note, Xenia sees nothing suspicious even if she watches him. You've got s and f. Also, you have the steps during which Xenia watches spies and which spies she is going to watch during each step. Find the best way the spies should act in order to pass the note from spy s to spy f as quickly as possible (in the minimum number of steps). -----Input----- The first line contains four integers n, m, s and f (1 ≤ n, m ≤ 10^5; 1 ≤ s, f ≤ n; s ≠ f; n ≥ 2). Each of the following m lines contains three integers t_{i}, l_{i}, r_{i} (1 ≤ t_{i} ≤ 10^9, 1 ≤ l_{i} ≤ r_{i} ≤ n). It is guaranteed that t_1 < t_2 < t_3 < ... < t_{m}. -----Output----- Print k characters in a line: the i-th character in the line must represent the spies' actions on step i. If on step i the spy with the note must pass the note to the spy with a lesser number, the i-th character should equal "L". If on step i the spy with the note must pass it to the spy with a larger number, the i-th character must equal "R". If the spy must keep the note at the i-th step, the i-th character must equal "X". As a result of applying the printed sequence of actions spy s must pass the note to spy f. The number of printed characters k must be as small as possible. Xenia must not catch the spies passing the note. If there are miltiple optimal solutions, you can print any of them. It is guaranteed that the answer exists. -----Examples----- Input 3 5 1 3 1 1 2 2 2 3 3 3 3 4 1 1 10 1 3 Output XXRR Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"7 7 3\\n1 2\\n2 1\\n5 5\\n\", \"10 5 1\\n3 3\\n\", \"4 5 19\\n1 1\\n1 2\\n1 3\\n1 4\\n1 5\\n2 1\\n2 2\\n2 3\\n2 5\\n3 1\\n3 2\\n3 3\\n3 4\\n3 5\\n4 1\\n4 2\\n4 3\\n4 4\\n4 5\\n\", \"5 5 24\\n1 2\\n1 3\\n1 4\\n1 5\\n2 1\\n2 2\\n2 3\\n2 4\\n2 5\\n3 1\\n3 2\\n3 3\\n3 4\\n3 5\\n4 1\\n4 2\\n4 3\\n4 4\\n4 5\\n5 1\\n5 2\\n5 3\\n5 4\\n5 5\\n\", \"10 10 20\\n7 5\\n4 5\\n5 4\\n7 4\\n1 5\\n6 7\\n9 5\\n8 4\\n1 4\\n2 10\\n3 10\\n4 10\\n3 6\\n5 7\\n6 2\\n1 1\\n10 9\\n7 6\\n6 4\\n8 2\\n\", \"1000 1000 1\\n944 844\\n\", \"1000 1000 10\\n630 843\\n959 696\\n75 966\\n691 982\\n390 44\\n863 380\\n467 293\\n94 96\\n415 411\\n161 497\\n\", \"1000000 1000000 1\\n721767 525996\\n\", \"1000000000 1000000000 1\\n243387905 466399354\\n\", \"1000000000 1000000000 10\\n946218581 843926244\\n730038846 389721428\\n400508549 779159281\\n631299905 559948749\\n352088629 132899572\\n391396726 503295408\\n57387601 686114055\\n298922091 222110600\\n523083048 28286278\\n587730768 379340837\\n\", \"5 1 2\\n4 1\\n5 1\\n\", \"6 4 3\\n5 2\\n5 4\\n3 3\\n\", \"4 5 4\\n1 5\\n2 1\\n2 3\\n4 1\\n\"], \"outputs\": [\"3\\n\", \"2\\n\", \"0\\n\", \"0\\n\", \"2\\n\", \"500\\n\", \"304\\n\", \"500000\\n\", \"500000000\\n\", \"313885945\\n\", \"1\\n\", \"2\\n\", \"1\\n\"]}", "source": "primeintellect"}
The capital of Berland looks like a rectangle of size n × m of the square blocks of same size. Fire! It is known that k + 1 blocks got caught on fire (k + 1 ≤ n·m). Those blocks are centers of ignition. Moreover positions of k of these centers are known and one of these stays unknown. All k + 1 positions are distinct. The fire goes the following way: during the zero minute of fire only these k + 1 centers of ignition are burning. Every next minute the fire goes to all neighbouring blocks to the one which is burning. You can consider blocks to burn for so long that this time exceeds the time taken in the problem. The neighbouring blocks are those that touch the current block by a side or by a corner. Berland Fire Deparment wants to estimate the minimal time it takes the fire to lighten up the whole city. Remember that the positions of k blocks (centers of ignition) are known and (k + 1)-th can be positioned in any other block. Help Berland Fire Department to estimate the minimal time it takes the fire to lighten up the whole city. -----Input----- The first line contains three integers n, m and k (1 ≤ n, m ≤ 10^9, 1 ≤ k ≤ 500). Each of the next k lines contain two integers x_{i} and y_{i} (1 ≤ x_{i} ≤ n, 1 ≤ y_{i} ≤ m) — coordinates of the i-th center of ignition. It is guaranteed that the locations of all centers of ignition are distinct. -----Output----- Print the minimal time it takes the fire to lighten up the whole city (in minutes). -----Examples----- Input 7 7 3 1 2 2 1 5 5 Output 3 Input 10 5 1 3 3 Output 2 -----Note----- In the first example the last block can have coordinates (4, 4). In the second example the last block can have coordinates (8, 3). Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1 7\\n0000000\\n\", \"2 5\\n0010010\\n0010010\\n\", \"3 5\\n0100001\\n1001001\\n1010011\\n\", \"10 0\\n0111010\\n1101111\\n1111011\\n0111010\\n1101011\\n1101011\\n1110111\\n1010010\\n1111111\\n0010010\\n\", \"10 10\\n0101111\\n0000000\\n1111011\\n1011011\\n1011011\\n1111011\\n0010010\\n1010010\\n1101111\\n0000000\\n\", \"10 10\\n1100011\\n1010011\\n0000111\\n1110110\\n0101011\\n0111111\\n1001111\\n1000000\\n1111011\\n0111000\\n\", \"10 10\\n1101001\\n0110000\\n0111010\\n0010000\\n1010000\\n0111000\\n1011011\\n1010010\\n1101011\\n1111110\\n\", \"10 6\\n1101011\\n1101111\\n1010000\\n1110111\\n1101110\\n1100111\\n1110011\\n1011101\\n0111010\\n1010010\\n\", \"10 10\\n0000000\\n0100000\\n0000000\\n0010000\\n0000000\\n0000001\\n0000000\\n0001000\\n0000001\\n0101000\\n\", \"10 10\\n1110111\\n0111111\\n1111111\\n1111111\\n0111111\\n1111111\\n0111111\\n1111110\\n1111111\\n1111111\\n\", \"1 1\\n1010010\\n\", \"1 2\\n0010010\\n\", \"3 8\\n0111010\\n1011011\\n1011101\\n\"], \"outputs\": [\"8\", \"97\", \"-1\", \"4694550781\", \"8993391761\", \"-1\", \"9941743758\", \"9870669247\", \"-1\", \"-1\", \"-1\", \"4\", \"-1\"]}", "source": "primeintellect"}
Denis, after buying flowers and sweets (you will learn about this story in the next task), went to a date with Nastya to ask her to become a couple. Now, they are sitting in the cafe and finally... Denis asks her to be together, but ... Nastya doesn't give any answer. The poor boy was very upset because of that. He was so sad that he punched some kind of scoreboard with numbers. The numbers are displayed in the same way as on an electronic clock: each digit position consists of $7$ segments, which can be turned on or off to display different numbers. The picture shows how all $10$ decimal digits are displayed: [Image] After the punch, some segments stopped working, that is, some segments might stop glowing if they glowed earlier. But Denis remembered how many sticks were glowing and how many are glowing now. Denis broke exactly $k$ segments and he knows which sticks are working now. Denis came up with the question: what is the maximum possible number that can appear on the board if you turn on exactly $k$ sticks (which are off now)? It is allowed that the number includes leading zeros. -----Input----- The first line contains integer $n$ $(1 \leq n \leq 2000)$  — the number of digits on scoreboard and $k$ $(0 \leq k \leq 2000)$  — the number of segments that stopped working. The next $n$ lines contain one binary string of length $7$, the $i$-th of which encodes the $i$-th digit of the scoreboard. Each digit on the scoreboard consists of $7$ segments. We number them, as in the picture below, and let the $i$-th place of the binary string be $0$ if the $i$-th stick is not glowing and $1$ if it is glowing. Then a binary string of length $7$ will specify which segments are glowing now. [Image] Thus, the sequences "1110111", "0010010", "1011101", "1011011", "0111010", "1101011", "1101111", "1010010", "1111111", "1111011" encode in sequence all digits from $0$ to $9$ inclusive. -----Output----- Output a single number consisting of $n$ digits  — the maximum number that can be obtained if you turn on exactly $k$ sticks or $-1$, if it is impossible to turn on exactly $k$ sticks so that a correct number appears on the scoreboard digits. -----Examples----- Input 1 7 0000000 Output 8 Input 2 5 0010010 0010010 Output 97 Input 3 5 0100001 1001001 1010011 Output -1 -----Note----- In the first test, we are obliged to include all $7$ sticks and get one $8$ digit on the scoreboard. In the second test, we have sticks turned on so that units are formed. For $5$ of additionally included sticks, you can get the numbers $07$, $18$, $34$, $43$, $70$, $79$, $81$ and $97$, of which we choose the maximum  — $97$. In the third test, it is impossible to turn on exactly $5$ sticks so that a sequence of numbers appears on the scoreboard. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"abac\\n\", \"acccc\\n\", \"hannah\\n\", \"vux\\n\", \"evmo\\n\", \"nxccb\\n\", \"vyppyq\\n\", \"eaebyud\\n\", \"mcrpwaao\\n\", \"vdgcwgxbh\\n\", \"ittmcsvmoa\\n\", \"dasca\\n\", \"tifui\\n\"], \"outputs\": [\"3\\nL 2\\nR 2\\nR 7\\n\", \"3\\nL 2\\nR 2\\nR 9\\n\", \"3\\nL 2\\nR 2\\nR 11\\n\", \"3\\nL 2\\nR 2\\nR 5\\n\", \"3\\nL 2\\nR 2\\nR 7\\n\", \"3\\nL 2\\nR 2\\nR 9\\n\", \"3\\nL 2\\nR 2\\nR 11\\n\", \"3\\nL 2\\nR 2\\nR 13\\n\", \"3\\nL 2\\nR 2\\nR 15\\n\", \"3\\nL 2\\nR 2\\nR 17\\n\", \"3\\nL 2\\nR 2\\nR 19\\n\", \"3\\nL 2\\nR 2\\nR 9\\n\", \"3\\nL 2\\nR 2\\nR 9\\n\"]}", "source": "primeintellect"}
Ringo found a string $s$ of length $n$ in his yellow submarine. The string contains only lowercase letters from the English alphabet. As Ringo and his friends love palindromes, he would like to turn the string $s$ into a palindrome by applying two types of operations to the string. The first operation allows him to choose $i$ ($2 \le i \le n-1$) and to append the substring $s_2s_3 \ldots s_i$ ($i - 1$ characters) reversed to the front of $s$. The second operation allows him to choose $i$ ($2 \le i \le n-1$) and to append the substring $s_i s_{i + 1}\ldots s_{n - 1}$ ($n - i$ characters) reversed to the end of $s$. Note that characters in the string in this problem are indexed from $1$. For example suppose $s=$abcdef. If he performs the first operation with $i=3$ then he appends cb to the front of $s$ and the result will be cbabcdef. Performing the second operation on the resulted string with $i=5$ will yield cbabcdefedc. Your task is to help Ringo make the entire string a palindrome by applying any of the two operations (in total) at most $30$ times. The length of the resulting palindrome must not exceed $10^6$ It is guaranteed that under these constraints there always is a solution. Also note you do not have to minimize neither the number of operations applied, nor the length of the resulting string, but they have to fit into the constraints. -----Input----- The only line contains the string $S$ ($3 \le |s| \le 10^5$) of lowercase letters from the English alphabet. -----Output----- The first line should contain $k$ ($0\le k \le 30$)  — the number of operations performed. Each of the following $k$ lines should describe an operation in form L i or R i. $L$ represents the first operation, $R$ represents the second operation, $i$ represents the index chosen. The length of the resulting palindrome must not exceed $10^6$. -----Examples----- Input abac Output 2 R 2 R 5 Input acccc Output 2 L 4 L 2 Input hannah Output 0 -----Note----- For the first example the following operations are performed: abac $\to$ abacab $\to$ abacaba The second sample performs the following operations: acccc $\to$ cccacccc $\to$ ccccacccc The third example is already a palindrome so no operations are required. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n2 5 -3\\n\", \"2\\n3 6\\n\", \"2\\n0 1\\n\", \"3\\n0 1000000000 -1000000000\\n\", \"10\\n-35 77 72 -62 76 90 58 97 -74 94\\n\", \"20\\n14 8 6 -16 5 -9 0 11 -7 10 20 -6 -17 -13 -11 7 -2 9 -19 19\\n\", \"20\\n55 -14 -28 13 -67 -23 58 2 -87 92 -80 62 -44 86 18 97 -47 63 32 94\\n\", \"50\\n29 80 0 91 93 36 83 44 79 60 53 89 18 64 37 13 15 54 98 90 68 88 86 38 63 92 16 70 58 45 46 96 62 21 31 14 30 42 7 9 69 97 50 85 84 57 10 59 33 23\\n\", \"50\\n-482 -431 -457 -473 -428 -427 -406 -422 -426 -472 -407 -441 -408 -475 -463 -443 -447 -450 -412 -436 -481 -454 -465 -403 -411 -460 -453 -466 -468 -459 -486 -413 -420 -421 -424 -470 -492 -409 -400 -425 -493 -438 -418 -456 -499 -410 -415 -487 -430 -476\\n\", \"100\\n404 523 -303 876 982 -275 498 287 255 491 -723 289 203 -796 -469 -299 -435 -869 58 577 55 600 153 -948 -11 726 129 797 -323 99 -934 -419 101 -307 -525 502 353 44 -905 371 -946 925 -538 614 -171 -867 -929 702 -429 720 94 -390 997 -803 451 379 57 -377 -545 -890 442 525 -975 -484 808 -498 -523 641 725 -425 621 -961 -530 -863 724 -501 -389 348 -263 -396 -225 -489 339 -619 -964 935 -950 210 -245 -326 -850 533 -261 -106 46 270 936 698 -392 -514\\n\", \"10\\n9 1 2 3 5 7 4 10 6 8\\n\", \"10\\n-994167199 -21213955 -162630040 335515257 -234713251 -101691063 235271021 -401255443 591241065 803570234\\n\", \"8\\n-9 -17 -18 -19 9 7 18 19\\n\"], \"outputs\": [\"2\\n\", \"1\\n\", \"0\\n\", \"1\\n\", \"38\\n\", \"110\\n\", \"96\\n\", \"650\\n\", \"1225\\n\", \"2751\\n\", \"25\\n\", \"14\\n\", \"19\\n\"]}", "source": "primeintellect"}
The legend of the foundation of Vectorland talks of two integers $x$ and $y$. Centuries ago, the array king placed two markers at points $|x|$ and $|y|$ on the number line and conquered all the land in between (including the endpoints), which he declared to be Arrayland. Many years later, the vector king placed markers at points $|x - y|$ and $|x + y|$ and conquered all the land in between (including the endpoints), which he declared to be Vectorland. He did so in such a way that the land of Arrayland was completely inside (including the endpoints) the land of Vectorland. Here $|z|$ denotes the absolute value of $z$. Now, Jose is stuck on a question of his history exam: "What are the values of $x$ and $y$?" Jose doesn't know the answer, but he believes he has narrowed the possible answers down to $n$ integers $a_1, a_2, \dots, a_n$. Now, he wants to know the number of unordered pairs formed by two different elements from these $n$ integers such that the legend could be true if $x$ and $y$ were equal to these two values. Note that it is possible that Jose is wrong, and that no pairs could possibly make the legend true. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$)  — the number of choices. The second line contains $n$ pairwise distinct integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$) — the choices Jose is considering. -----Output----- Print a single integer number — the number of unordered pairs $\{x, y\}$ formed by different numbers from Jose's choices that could make the legend true. -----Examples----- Input 3 2 5 -3 Output 2 Input 2 3 6 Output 1 -----Note----- Consider the first sample. For the pair $\{2, 5\}$, the situation looks as follows, with the Arrayland markers at $|2| = 2$ and $|5| = 5$, while the Vectorland markers are located at $|2 - 5| = 3$ and $|2 + 5| = 7$: [Image] The legend is not true in this case, because the interval $[2, 3]$ is not conquered by Vectorland. For the pair $\{5, -3\}$ the situation looks as follows, with Arrayland consisting of the interval $[3, 5]$ and Vectorland consisting of the interval $[2, 8]$: [Image] As Vectorland completely contains Arrayland, the legend is true. It can also be shown that the legend is true for the pair $\{2, -3\}$, for a total of two pairs. In the second sample, the only pair is $\{3, 6\}$, and the situation looks as follows: [Image] Note that even though Arrayland and Vectorland share $3$ as endpoint, we still consider Arrayland to be completely inside of Vectorland. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.5
{"tests": "{\"inputs\": [\"1 1\\n1\\n\", \"4 2\\n6 3 8 1\\n\", \"94 58\\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 29 58 1 1 1 29 58 58 1 1 29 1 1 1 1 2 1 58 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 29 1 1 1 1 1 58 1 29 1 1 1 1 1 1 1 1 1 1 1 1 58 1 1 1 1 1 2 1 1 1\\n\", \"6 76\\n1 38 1 1 1 1\\n\", \"16 53\\n53 1 1 1 1 1 53 1 1 1 1 1 1 1 1 1\\n\", \"13 16\\n2 1 1 1 1 1 1 1 1 1 1 1 1\\n\", \"90 45\\n15 1 1 1 1 3 1 1 5 3 5 1 1 15 15 3 1 15 1 1 1 15 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 15 1 1 1 1 1 1 1 1 1 15 1 1 1 1 5 1 1 1 1 15 1 1 1 15 1 1 1 1 1 1 1 1 3 1 1 15 3 1 1 1 15 15 1 1 1 1 15\\n\", \"9 209\\n2 7 31 673 853 1669 5821 7621 16677\\n\", \"8 27\\n8 3 9 8 3 10 7 1\\n\", \"50 5\\n1 5 11 10 3 6 9 3 9 6 1 1 4 7 8 6 11 2 6 10 4 1 8 11 5 6 5 6 7 2 2 3 1 10 8 8 2 10 7 3 8 10 11 7 3 2 10 11 7 4\\n\", \"16 1\\n2 6 1 11 5 9 5 9 7 5 8 5 3 2 7 2\\n\", \"13 38\\n1 10 4 1 5 5 4 4 8 4 11 1 9\\n\", \"5 15\\n10 9 5 2 2\\n\"], \"outputs\": [\"1\\n\", \"2\\n\", \"5\\n\", \"0\\n\", \"0\\n\", \"0\\n\", \"5\\n\", \"0\\n\", \"0\\n\", \"0\\n\", \"16\\n\", \"0\\n\", \"0\\n\"]}", "source": "primeintellect"}
Nastya received one more array on her birthday, this array can be used to play a traditional Byteland game on it. However, to play the game the players should first select such a subsegment of the array that $\frac{p}{s} = k$, where p is the product of all integers on the given array, s is their sum, and k is a given constant for all subsegments. Nastya wonders how many subsegments of the array fit the described conditions. A subsegment of an array is several consecutive integers of the array. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 2·10^5, 1 ≤ k ≤ 10^5), where n is the length of the array and k is the constant described above. The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^8) — the elements of the array. -----Output----- In the only line print the number of subsegments such that the ratio between the product and the sum on them is equal to k. -----Examples----- Input 1 1 1 Output 1 Input 4 2 6 3 8 1 Output 2 -----Note----- In the first example the only subsegment is [1]. The sum equals 1, the product equals 1, so it suits us because $\frac{1}{1} = 1$. There are two suitable subsegments in the second example — [6, 3] and [3, 8, 1]. Subsegment [6, 3] has sum 9 and product 18, so it suits us because $\frac{18}{9} = 2$. Subsegment [3, 8, 1] has sum 12 and product 24, so it suits us because $\frac{24}{12} = 2$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.875
{"tests": "{\"inputs\": [\"3 4\\n\", \"3 5\\n\", \"42 1337\\n\", \"100000 200000\\n\", \"150000 200000\\n\", \"2 2\\n\", \"3 3\\n\", \"3 200000\\n\", \"200000 200000\\n\", \"20 20\\n\", \"2 3\\n\", \"1000 200000\\n\", \"2 10\\n\"], \"outputs\": [\"6\\n\", \"10\\n\", \"806066790\\n\", \"707899035\\n\", \"270223789\\n\", \"0\\n\", \"3\\n\", \"35012940\\n\", \"668956439\\n\", \"47185920\\n\", \"0\\n\", \"664520775\\n\", \"0\\n\"]}", "source": "primeintellect"}
Your task is to calculate the number of arrays such that: each array contains $n$ elements; each element is an integer from $1$ to $m$; for each array, there is exactly one pair of equal elements; for each array $a$, there exists an index $i$ such that the array is strictly ascending before the $i$-th element and strictly descending after it (formally, it means that $a_j < a_{j + 1}$, if $j < i$, and $a_j > a_{j + 1}$, if $j \ge i$). -----Input----- The first line contains two integers $n$ and $m$ ($2 \le n \le m \le 2 \cdot 10^5$). -----Output----- Print one integer — the number of arrays that meet all of the aforementioned conditions, taken modulo $998244353$. -----Examples----- Input 3 4 Output 6 Input 3 5 Output 10 Input 42 1337 Output 806066790 Input 100000 200000 Output 707899035 -----Note----- The arrays in the first example are: $[1, 2, 1]$; $[1, 3, 1]$; $[1, 4, 1]$; $[2, 3, 2]$; $[2, 4, 2]$; $[3, 4, 3]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 3 2\\n1\\n2\\n3\\n4\\n\", \"2 1 1\\n1\\n2\\n\", \"7 5 20\\n26\\n27\\n28\\n29\\n30\\n31\\n32\\n\", \"10 10 1\\n1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10\\n\", \"10 1 10\\n1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10\\n\", \"1 999999 1000000\\n1000000000\\n\", \"5 5 6\\n999999999\\n999999998\\n999999997\\n999999996\\n999999995\\n\", \"10 13 27\\n3\\n21\\n23\\n17\\n15\\n23\\n16\\n7\\n24\\n20\\n\", \"1 1 1\\n1\\n\", \"5 999999 1000000\\n1999997\\n1999998\\n1999999\\n2000000\\n2000001\\n\", \"5 999998 1000000\\n999997\\n999998\\n999999\\n1000000\\n1000001\\n\", \"20 5 15\\n20\\n21\\n22\\n23\\n24\\n25\\n26\\n27\\n28\\n29\\n30\\n31\\n32\\n33\\n34\\n35\\n36\\n37\\n38\\n39\\n\", \"11 22 33\\n55\\n56\\n57\\n58\\n59\\n60\\n61\\n62\\n63\\n64\\n65\\n\"], \"outputs\": [\"Vanya\\nVova\\nVanya\\nBoth\\n\", \"Both\\nBoth\\n\", \"Vova\\nVova\\nVova\\nBoth\\nBoth\\nVova\\nVova\\n\", \"Vanya\\nVanya\\nVanya\\nVanya\\nVanya\\nVanya\\nVanya\\nVanya\\nVanya\\nBoth\\n\", \"Vova\\nVova\\nVova\\nVova\\nVova\\nVova\\nVova\\nVova\\nVova\\nBoth\\n\", \"Vanya\\n\", \"Vova\\nVanya\\nVova\\nVanya\\nVova\\n\", \"Vanya\\nVanya\\nVova\\nVova\\nVanya\\nVova\\nVova\\nVova\\nVanya\\nVova\\n\", \"Both\\n\", \"Vova\\nBoth\\nBoth\\nVova\\nVanya\\n\", \"Vova\\nBoth\\nBoth\\nVova\\nVanya\\n\", \"Both\\nVova\\nVova\\nBoth\\nBoth\\nVova\\nVova\\nBoth\\nBoth\\nVova\\nVova\\nBoth\\nBoth\\nVova\\nVova\\nBoth\\nBoth\\nVova\\nVova\\nBoth\\n\", \"Both\\nVova\\nVanya\\nVova\\nBoth\\nBoth\\nVova\\nVanya\\nVova\\nBoth\\nBoth\\n\"]}", "source": "primeintellect"}
Vanya and his friend Vova play a computer game where they need to destroy n monsters to pass a level. Vanya's character performs attack with frequency x hits per second and Vova's character performs attack with frequency y hits per second. Each character spends fixed time to raise a weapon and then he hits (the time to raise the weapon is 1 / x seconds for the first character and 1 / y seconds for the second one). The i-th monster dies after he receives a_{i} hits. Vanya and Vova wonder who makes the last hit on each monster. If Vanya and Vova make the last hit at the same time, we assume that both of them have made the last hit. -----Input----- The first line contains three integers n,x,y (1 ≤ n ≤ 10^5, 1 ≤ x, y ≤ 10^6) — the number of monsters, the frequency of Vanya's and Vova's attack, correspondingly. Next n lines contain integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the number of hits needed do destroy the i-th monster. -----Output----- Print n lines. In the i-th line print word "Vanya", if the last hit on the i-th monster was performed by Vanya, "Vova", if Vova performed the last hit, or "Both", if both boys performed it at the same time. -----Examples----- Input 4 3 2 1 2 3 4 Output Vanya Vova Vanya Both Input 2 1 1 1 2 Output Both Both -----Note----- In the first sample Vanya makes the first hit at time 1 / 3, Vova makes the second hit at time 1 / 2, Vanya makes the third hit at time 2 / 3, and both boys make the fourth and fifth hit simultaneously at the time 1. In the second sample Vanya and Vova make the first and second hit simultaneously at time 1. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1 0\\n\", \"2 1\\n\", \"4 0\\n\", \"2 0\\n\", \"3 0\\n\", \"4 1\\n\", \"4 2\\n\", \"6 3\\n\", \"34 17\\n\", \"50 1\\n\", \"55 0\\n\", \"3 1\\n\", \"6 0\\n\"], \"outputs\": [\"1 2\", \"3 2 1 4\\n\", \"2 7 4 6 1 3 5 8\\n\", \"1 2 3 4\", \"1 2 3 4 5 6\", \"2 1 3 4 5 6 7 8\", \"2 1 4 3 5 6 7 8\", \"2 1 4 3 6 5 7 8 9 10 11 12\", \"2 1 4 3 6 5 8 7 10 9 12 11 14 13 16 15 18 17 20 19 22 21 24 23 26 25 28 27 30 29 32 31 34 33 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68\", \"2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100\", \"1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110\", \"2 1 3 4 5 6\", \"1 2 3 4 5 6 7 8 9 10 11 12\"]}", "source": "primeintellect"}
A permutation p is an ordered group of numbers p_1, p_2, ..., p_{n}, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p_1, p_2, ..., p_{n}. Simon has a positive integer n and a non-negative integer k, such that 2k ≤ n. Help him find permutation a of length 2n, such that it meets this equation: $\sum_{i = 1}^{n}|a_{2 i - 1} - a_{2i}|-|\sum_{i = 1}^{n} a_{2 i - 1} - a_{2i}|= 2 k$. -----Input----- The first line contains two integers n and k (1 ≤ n ≤ 50000, 0 ≤ 2k ≤ n). -----Output----- Print 2n integers a_1, a_2, ..., a_2n — the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them. -----Examples----- Input 1 0 Output 1 2 Input 2 1 Output 3 2 1 4 Input 4 0 Output 2 7 4 6 1 3 5 8 -----Note----- Record |x| represents the absolute value of number x. In the first sample |1 - 2| - |1 - 2| = 0. In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2. In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n\", \"10\\n\", \"2\\n\", \"20\\n\", \"21\\n\", \"23\\n\", \"24\\n\", \"28\\n\", \"29\\n\", \"30\\n\", \"4\\n\", \"5\\n\", \"7\\n\", \"8\\n\", \"9\\n\", \"13\\n\", \"14\\n\", \"19\\n\"], \"outputs\": [\"7\\n\", \"39916801\\n\", \"3\\n\", \"232792561\\n\", \"232792561\\n\", \"5354228881\\n\", \"5354228881\\n\", \"80313433201\\n\", \"2329089562801\\n\", \"2329089562801\\n\", \"13\\n\", \"61\\n\", \"421\\n\", \"841\\n\", \"2521\\n\", \"360361\\n\", \"360361\\n\", \"232792561\\n\"]}", "source": "primeintellect"}
We have an integer N. Print an integer x between N and 10^{13} (inclusive) such that, for every integer y between 2 and N (inclusive), the remainder when x is divided by y is 1. Under the constraints of this problem, there is always at least one such integer x. -----Constraints----- - All values in input are integers. - 2 \leq N \leq 30 -----Input----- Input is given from Standard Input in the following format: N -----Output----- Print an integer x between N and 10^{13} (inclusive) such that, for every integer y between 2 and N (inclusive), the remainder when x is divided by y is 1. If there are multiple such integers, any of them will be accepted. -----Sample Input----- 3 -----Sample Output----- 7 The remainder when 7 is divided by 2 is 1, and the remainder when 7 is divided by 3 is 1, too. 7 is an integer between 3 and 10^{13}, so this is a desirable output. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"2\\n\", \"8\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\", \"7\\n\", \"11\\n\", \"13\\n\", \"21\\n\", \"31\\n\", \"43\\n\"], \"outputs\": [\"-1\\n1 2\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 7\\n1 8\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n1 8\\n\", \"-1\\n1 2\\n1 3\\n\", \"-1\\n1 2\\n1 3\\n1 4\\n\", \"-1\\n1 2\\n1 3\\n1 4\\n1 5\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 7\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n1 14\\n1 15\\n1 16\\n1 17\\n1 18\\n1 19\\n1 20\\n1 21\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n1 14\\n1 15\\n1 16\\n1 17\\n1 18\\n1 19\\n1 20\\n1 21\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n1 14\\n1 15\\n1 16\\n1 17\\n1 18\\n1 19\\n1 20\\n1 21\\n1 22\\n1 23\\n1 24\\n1 25\\n1 26\\n1 27\\n1 28\\n1 29\\n1 30\\n1 31\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n1 14\\n1 15\\n1 16\\n1 17\\n1 18\\n1 19\\n1 20\\n1 21\\n1 22\\n1 23\\n1 24\\n1 25\\n1 26\\n1 27\\n1 28\\n1 29\\n1 30\\n1 31\\n\", \"1 2\\n1 3\\n1 4\\n2 5\\n2 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n1 14\\n1 15\\n1 16\\n1 17\\n1 18\\n1 19\\n1 20\\n1 21\\n1 22\\n1 23\\n1 24\\n1 25\\n1 26\\n1 27\\n1 28\\n1 29\\n1 30\\n1 31\\n1 32\\n1 33\\n1 34\\n1 35\\n1 36\\n1 37\\n1 38\\n1 39\\n1 40\\n1 41\\n1 42\\n1 43\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n1 13\\n1 14\\n1 15\\n1 16\\n1 17\\n1 18\\n1 19\\n1 20\\n1 21\\n1 22\\n1 23\\n1 24\\n1 25\\n1 26\\n1 27\\n1 28\\n1 29\\n1 30\\n1 31\\n1 32\\n1 33\\n1 34\\n1 35\\n1 36\\n1 37\\n1 38\\n1 39\\n1 40\\n1 41\\n1 42\\n1 43\\n\"]}", "source": "primeintellect"}
Mahmoud was trying to solve the vertex cover problem on trees. The problem statement is: Given an undirected tree consisting of n nodes, find the minimum number of vertices that cover all the edges. Formally, we need to find a set of vertices such that for each edge (u, v) that belongs to the tree, either u is in the set, or v is in the set, or both are in the set. Mahmoud has found the following algorithm: Root the tree at node 1. Count the number of nodes at an even depth. Let it be evenCnt. Count the number of nodes at an odd depth. Let it be oddCnt. The answer is the minimum between evenCnt and oddCnt. The depth of a node in a tree is the number of edges in the shortest path between this node and the root. The depth of the root is 0. Ehab told Mahmoud that this algorithm is wrong, but he didn't believe because he had tested his algorithm against many trees and it worked, so Ehab asked you to find 2 trees consisting of n nodes. The algorithm should find an incorrect answer for the first tree and a correct answer for the second one. -----Input----- The only line contains an integer n (2 ≤ n ≤ 10^5), the number of nodes in the desired trees. -----Output----- The output should consist of 2 independent sections, each containing a tree. The algorithm should find an incorrect answer for the tree in the first section and a correct answer for the tree in the second. If a tree doesn't exist for some section, output "-1" (without quotes) for that section only. If the answer for a section exists, it should contain n - 1 lines, each containing 2 space-separated integers u and v (1 ≤ u, v ≤ n), which means that there's an undirected edge between node u and node v. If the given graph isn't a tree or it doesn't follow the format, you'll receive wrong answer verdict. If there are multiple answers, you can print any of them. -----Examples----- Input 2 Output -1 1 2 Input 8 Output 1 2 1 3 2 4 2 5 3 6 4 7 4 8 1 2 1 3 2 4 2 5 2 6 3 7 6 8 -----Note----- In the first sample, there is only 1 tree with 2 nodes (node 1 connected to node 2). The algorithm will produce a correct answer in it so we printed - 1 in the first section, but notice that we printed this tree in the second section. In the second sample: In the first tree, the algorithm will find an answer with 4 nodes, while there exists an answer with 3 nodes like this: [Image] In the second tree, the algorithm will find an answer with 3 nodes which is correct: [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n1 2\\n2 1\\n5 10\\n10 9\\n19 1\\n\", \"5\\n1 2\\n2 1\\n5 10\\n10 9\\n20 1\\n\", \"4\\n10 4\\n15 1\\n19 3\\n20 1\\n\", \"35\\n1 7\\n3 11\\n6 12\\n7 6\\n8 5\\n9 11\\n15 3\\n16 10\\n22 2\\n23 3\\n25 7\\n27 3\\n34 5\\n35 10\\n37 3\\n39 4\\n40 5\\n41 1\\n44 1\\n47 7\\n48 11\\n50 6\\n52 5\\n57 2\\n58 7\\n60 4\\n62 1\\n67 3\\n68 12\\n69 8\\n70 1\\n71 5\\n72 5\\n73 6\\n74 4\\n\", \"40\\n1 1\\n2 1\\n3 1\\n4 1\\n5 1\\n6 1\\n7 1\\n8 1\\n9 1\\n10 1\\n11 1\\n12 1\\n13 1\\n14 1\\n15 1\\n16 1\\n17 1\\n18 1\\n19 1\\n20 1\\n21 1\\n22 1\\n23 1\\n24 1\\n25 1\\n26 1\\n27 1\\n28 1\\n29 1\\n30 1\\n31 1\\n32 1\\n33 1\\n34 1\\n35 1\\n36 1\\n37 1\\n38 1\\n39 1\\n40 1\\n\", \"67\\n1 1\\n3 8\\n4 10\\n7 8\\n9 2\\n10 1\\n11 5\\n12 8\\n13 4\\n16 6\\n18 3\\n19 3\\n22 5\\n24 6\\n27 5\\n28 3\\n29 3\\n30 5\\n32 5\\n33 10\\n34 7\\n35 8\\n36 5\\n41 3\\n42 2\\n43 5\\n46 4\\n48 4\\n49 9\\n52 4\\n53 9\\n55 1\\n56 4\\n59 7\\n68 7\\n69 4\\n71 9\\n72 10\\n74 5\\n76 4\\n77 9\\n80 7\\n81 9\\n82 5\\n83 5\\n84 9\\n85 7\\n86 9\\n87 4\\n88 7\\n89 10\\n90 3\\n91 5\\n92 10\\n93 5\\n94 8\\n95 4\\n96 2\\n97 10\\n98 1\\n99 3\\n100 1\\n101 5\\n102 4\\n103 8\\n104 8\\n105 8\\n\", \"1\\n1000000000 1000000000\\n\", \"10\\n7 12\\n10 2\\n12 2\\n15 1\\n19 2\\n20 1\\n53 25\\n63 10\\n75 12\\n87 1\\n\", \"3\\n1 1\\n1000 1000\\n1000000000 1000000000\\n\", \"2\\n1 999999999\\n1000000000 1000000000\\n\", \"10\\n999999900 1000000000\\n999999901 1000000000\\n999999902 1000000000\\n999999903 1000000000\\n999999904 1000000000\\n999999905 1000000000\\n999999906 1000000000\\n999999907 1000000000\\n999999908 1000000000\\n999999909 1000000000\\n\", \"2\\n100000000 1000000000\\n1000000000 1000000000\\n\"], \"outputs\": [\"3\\n\", \"4\\n\", \"4\\n\", \"10\\n\", \"2\\n\", \"5\\n\", \"1\\n\", \"9\\n\", \"3\\n\", \"2\\n\", \"2\\n\", \"2\\n\"]}", "source": "primeintellect"}
Little Susie listens to fairy tales before bed every day. Today's fairy tale was about wood cutters and the little girl immediately started imagining the choppers cutting wood. She imagined the situation that is described below. There are n trees located along the road at points with coordinates x_1, x_2, ..., x_{n}. Each tree has its height h_{i}. Woodcutters can cut down a tree and fell it to the left or to the right. After that it occupies one of the segments [x_{i} - h_{i}, x_{i}] or [x_{i};x_{i} + h_{i}]. The tree that is not cut down occupies a single point with coordinate x_{i}. Woodcutters can fell a tree if the segment to be occupied by the fallen tree doesn't contain any occupied point. The woodcutters want to process as many trees as possible, so Susie wonders, what is the maximum number of trees to fell. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of trees. Next n lines contain pairs of integers x_{i}, h_{i} (1 ≤ x_{i}, h_{i} ≤ 10^9) — the coordinate and the height of the і-th tree. The pairs are given in the order of ascending x_{i}. No two trees are located at the point with the same coordinate. -----Output----- Print a single number — the maximum number of trees that you can cut down by the given rules. -----Examples----- Input 5 1 2 2 1 5 10 10 9 19 1 Output 3 Input 5 1 2 2 1 5 10 10 9 20 1 Output 4 -----Note----- In the first sample you can fell the trees like that: fell the 1-st tree to the left — now it occupies segment [ - 1;1] fell the 2-nd tree to the right — now it occupies segment [2;3] leave the 3-rd tree — it occupies point 5 leave the 4-th tree — it occupies point 10 fell the 5-th tree to the right — now it occupies segment [19;20] In the second sample you can also fell 4-th tree to the right, after that it will occupy segment [10;19]. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"6\\n1 2 2 1 5\\n2 1 1 1 1 1\\n\", \"7\\n1 1 2 3 1 4\\n3 3 1 1 1 2 3\\n\", \"2\\n1\\n2 2\\n\", \"3\\n1 1\\n2 2 2\\n\", \"4\\n1 2 1\\n1 2 3 4\\n\", \"4\\n1 2 3\\n4 1 2 4\\n\", \"5\\n1 2 1 4\\n1 1 1 2 2\\n\", \"3\\n1 2\\n2 1 1\\n\", \"4\\n1 1 1\\n3 1 3 1\\n\", \"4\\n1 1 2\\n4 1 4 1\\n\", \"4\\n1 2 2\\n3 1 2 3\\n\", \"3\\n1 1\\n1 2 2\\n\"], \"outputs\": [\"3\\n\", \"5\\n\", \"1\\n\", \"1\\n\", \"4\\n\", \"4\\n\", \"2\\n\", \"2\\n\", \"3\\n\", \"2\\n\", \"4\\n\", \"3\\n\"]}", "source": "primeintellect"}
You are given a rooted tree with n vertices. The vertices are numbered from 1 to n, the root is the vertex number 1. Each vertex has a color, let's denote the color of vertex v by c_{v}. Initially c_{v} = 0. You have to color the tree into the given colors using the smallest possible number of steps. On each step you can choose a vertex v and a color x, and then color all vectices in the subtree of v (including v itself) in color x. In other words, for every vertex u, such that the path from root to u passes through v, set c_{u} = x. It is guaranteed that you have to color each vertex in a color different from 0. You can learn what a rooted tree is using the link: https://en.wikipedia.org/wiki/Tree_(graph_theory). -----Input----- The first line contains a single integer n (2 ≤ n ≤ 10^4) — the number of vertices in the tree. The second line contains n - 1 integers p_2, p_3, ..., p_{n} (1 ≤ p_{i} < i), where p_{i} means that there is an edge between vertices i and p_{i}. The third line contains n integers c_1, c_2, ..., c_{n} (1 ≤ c_{i} ≤ n), where c_{i} is the color you should color the i-th vertex into. It is guaranteed that the given graph is a tree. -----Output----- Print a single integer — the minimum number of steps you have to perform to color the tree into given colors. -----Examples----- Input 6 1 2 2 1 5 2 1 1 1 1 1 Output 3 Input 7 1 1 2 3 1 4 3 3 1 1 1 2 3 Output 5 -----Note----- The tree from the first sample is shown on the picture (numbers are vetices' indices): $A$ On first step we color all vertices in the subtree of vertex 1 into color 2 (numbers are colors): [Image] On seond step we color all vertices in the subtree of vertex 5 into color 1: [Image] On third step we color all vertices in the subtree of vertex 2 into color 1: [Image] The tree from the second sample is shown on the picture (numbers are vetices' indices): [Image] On first step we color all vertices in the subtree of vertex 1 into color 3 (numbers are colors): [Image] On second step we color all vertices in the subtree of vertex 3 into color 1: $8$ On third step we color all vertices in the subtree of vertex 6 into color 2: [Image] On fourth step we color all vertices in the subtree of vertex 4 into color 1: [Image] On fith step we color all vertices in the subtree of vertex 7 into color 3: [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.5
{"tests": "{\"inputs\": [\"3 3 2\\n1 2\\n2 3\\n3 1\\n\", \"4 6 3\\n4 3\\n1 2\\n1 3\\n1 4\\n2 3\\n2 4\\n\", \"9 9 2\\n5 6\\n6 7\\n7 8\\n8 9\\n9 5\\n1 2\\n2 3\\n3 4\\n4 1\\n\", \"5 10 4\\n1 2\\n2 3\\n1 3\\n1 5\\n4 1\\n2 5\\n2 4\\n5 3\\n5 4\\n3 4\\n\", \"10 15 3\\n9 4\\n4 8\\n4 2\\n2 9\\n9 6\\n6 2\\n6 5\\n1 10\\n1 7\\n10 5\\n10 7\\n1 8\\n8 3\\n3 5\\n3 7\\n\", \"12 40 3\\n6 11\\n7 11\\n9 2\\n9 1\\n9 3\\n3 4\\n4 12\\n4 6\\n6 7\\n7 2\\n2 10\\n10 1\\n10 12\\n12 1\\n3 5\\n5 8\\n5 11\\n8 4\\n8 10\\n1 5\\n2 11\\n5 12\\n3 7\\n10 3\\n9 6\\n10 4\\n2 6\\n3 11\\n5 7\\n10 7\\n12 11\\n6 3\\n2 4\\n2 8\\n8 9\\n2 1\\n3 2\\n6 8\\n1 8\\n1 6\\n\", \"22 44 4\\n2 16\\n9 15\\n9 6\\n9 11\\n18 17\\n2 18\\n2 15\\n2 10\\n10 14\\n10 8\\n16 5\\n16 19\\n16 6\\n17 21\\n21 10\\n12 18\\n12 22\\n22 15\\n9 21\\n14 15\\n20 17\\n17 5\\n5 13\\n5 7\\n7 8\\n8 6\\n18 11\\n11 19\\n11 3\\n3 20\\n20 14\\n3 13\\n3 22\\n22 8\\n1 14\\n1 19\\n1 20\\n1 21\\n6 4\\n4 7\\n7 19\\n12 4\\n4 13\\n13 12\\n\", \"30 70 4\\n22 1\\n1 25\\n27 5\\n25 10\\n10 2\\n29 4\\n29 26\\n28 18\\n15 18\\n25 11\\n11 10\\n11 28\\n4 11\\n4 26\\n26 7\\n26 18\\n18 21\\n14 17\\n17 30\\n30 9\\n30 6\\n30 27\\n27 22\\n22 15\\n22 8\\n8 4\\n8 17\\n17 16\\n16 19\\n16 12\\n12 23\\n23 20\\n20 1\\n1 23\\n23 15\\n15 24\\n24 28\\n28 21\\n21 9\\n21 27\\n29 13\\n13 5\\n20 19\\n20 5\\n5 2\\n2 29\\n25 2\\n3 16\\n3 8\\n3 24\\n3 12\\n12 13\\n13 24\\n9 6\\n9 7\\n7 19\\n7 10\\n6 14\\n6 19\\n14 8\\n14 28\\n8 19\\n13 22\\n19 26\\n10 17\\n18 4\\n20 12\\n6 2\\n11 7\\n13 28\\n\", \"10 45 3\\n1 8\\n1 4\\n1 9\\n2 6\\n2 3\\n2 4\\n3 9\\n3 4\\n5 4\\n5 2\\n5 7\\n6 3\\n6 9\\n7 8\\n7 9\\n8 2\\n10 5\\n10 9\\n10 7\\n5 6\\n1 3\\n3 8\\n7 3\\n6 7\\n5 8\\n4 8\\n10 8\\n8 9\\n7 2\\n5 9\\n4 10\\n5 3\\n10 3\\n10 2\\n9 2\\n1 7\\n8 6\\n4 9\\n1 10\\n4 6\\n1 6\\n4 7\\n2 1\\n5 1\\n10 6\\n\", \"10 27 4\\n1 5\\n1 9\\n1 2\\n1 8\\n2 9\\n2 6\\n2 5\\n3 9\\n3 2\\n3 1\\n3 7\\n4 7\\n4 2\\n4 1\\n4 9\\n5 9\\n5 8\\n6 9\\n6 7\\n6 1\\n7 2\\n8 7\\n8 9\\n10 2\\n10 1\\n10 8\\n10 4\\n\", \"15 71 8\\n1 13\\n1 7\\n1 15\\n1 6\\n1 4\\n1 2\\n1 11\\n1 9\\n2 8\\n2 7\\n2 5\\n2 12\\n2 11\\n2 10\\n2 14\\n3 1\\n3 8\\n3 2\\n3 15\\n3 10\\n3 9\\n3 6\\n3 5\\n4 11\\n4 12\\n4 9\\n4 6\\n4 13\\n4 5\\n4 7\\n5 9\\n5 6\\n5 14\\n5 8\\n5 10\\n6 7\\n6 9\\n6 2\\n6 12\\n7 10\\n7 13\\n7 8\\n7 15\\n8 6\\n8 15\\n8 4\\n8 14\\n9 15\\n9 2\\n9 8\\n10 8\\n10 11\\n10 6\\n10 13\\n11 8\\n11 5\\n11 15\\n11 3\\n12 8\\n12 13\\n12 10\\n12 14\\n12 9\\n13 5\\n13 14\\n13 11\\n14 1\\n14 3\\n14 4\\n15 10\\n15 13\\n\", \"7 8 2\\n1 2\\n2 3\\n3 4\\n4 2\\n1 5\\n5 6\\n6 7\\n7 5\\n\"], \"outputs\": [\"3\\n1 2 3 \", \"4\\n3 4 1 2 \", \"4\\n1 2 3 4 \", \"5\\n1 2 3 5 4 \", \"7\\n6 9 4 8 1 10 5 \", \"7\\n3 9 2 7 11 6 4 \", \"5\\n2 16 5 17 18 \", \"9\\n11 25 1 22 27 5 13 29 4 \", \"5\\n1 8 7 5 4 \", \"5\\n1 5 2 9 3 \", \"9\\n2 1 13 4 11 10 3 8 5 \", \"3\\n5 6 7 \"]}", "source": "primeintellect"}
You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph indexed by integers from 1 to n. We know that each node of graph G is connected by edges with at least k other nodes of this graph. Your task is to find in the given graph a simple cycle of length of at least k + 1. A simple cycle of length d (d > 1) in graph G is a sequence of distinct graph nodes v_1, v_2, ..., v_{d} such, that nodes v_1 and v_{d} are connected by an edge of the graph, also for any integer i (1 ≤ i < d) nodes v_{i} and v_{i} + 1 are connected by an edge of the graph. -----Input----- The first line contains three integers n, m, k (3 ≤ n, m ≤ 10^5; 2 ≤ k ≤ n - 1) — the number of the nodes of the graph, the number of the graph's edges and the lower limit on the degree of the graph node. Next m lines contain pairs of integers. The i-th line contains integers a_{i}, b_{i} (1 ≤ a_{i}, b_{i} ≤ n; a_{i} ≠ b_{i}) — the indexes of the graph nodes that are connected by the i-th edge. It is guaranteed that the given graph doesn't contain any multiple edges or self-loops. It is guaranteed that each node of the graph is connected by the edges with at least k other nodes of the graph. -----Output----- In the first line print integer r (r ≥ k + 1) — the length of the found cycle. In the next line print r distinct integers v_1, v_2, ..., v_{r} (1 ≤ v_{i} ≤ n) — the found simple cycle. It is guaranteed that the answer exists. If there are multiple correct answers, you are allowed to print any of them. -----Examples----- Input 3 3 2 1 2 2 3 3 1 Output 3 1 2 3 Input 4 6 3 4 3 1 2 1 3 1 4 2 3 2 4 Output 4 3 4 1 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n123456789\\n100000000\\n100123456\\n\", \"4\\n123456789\\n193456789\\n134567819\\n934567891\\n\", \"1\\n167038488\\n\", \"5\\n115830748\\n403459907\\n556271610\\n430358099\\n413961410\\n\", \"5\\n139127034\\n452751056\\n193432721\\n894001929\\n426470953\\n\", \"5\\n343216531\\n914073407\\n420246472\\n855857272\\n801664978\\n\", \"5\\n567323818\\n353474649\\n468171032\\n989223926\\n685081078\\n\", \"5\\n774610315\\n325796798\\n989859836\\n707706423\\n310546337\\n\", \"10\\n181033039\\n210698534\\n971006898\\n391227170\\n323096464\\n560766866\\n377374442\\n654389922\\n544146403\\n779261493\\n\", \"10\\n197120216\\n680990683\\n319631438\\n442393410\\n888300189\\n170777450\\n164487872\\n487350759\\n651751346\\n652859411\\n\", \"10\\n302417715\\n621211824\\n474451896\\n961495400\\n633841010\\n839982537\\n797812119\\n510708100\\n770758643\\n228046084\\n\", \"10\\n506504092\\n561611075\\n265260859\\n557114891\\n838578824\\n985006846\\n456984731\\n856424964\\n658005674\\n666280709\\n\", \"10\\n510613599\\n931933224\\n693094490\\n508960931\\n313762868\\n396027639\\n164098962\\n749880019\\n709024305\\n498545812\\n\", \"3\\n638631659\\n929648227\\n848163730\\n\", \"4\\n898855826\\n343430636\\n210120107\\n467957087\\n\", \"5\\n202080398\\n357502772\\n269676952\\n711559315\\n111366203\\n\", \"9\\n111111111\\n111111110\\n111111100\\n111111000\\n111110000\\n111100000\\n111000000\\n110000000\\n100000000\\n\"], \"outputs\": [\"9\\n000\\n01\\n\", \"2\\n193\\n81\\n91\\n\", \"4\\n\", \"15\\n40\\n2\\n35\\n14\\n\", \"39\\n05\\n32\\n8\\n53\\n\", \"32\\n07\\n46\\n27\\n78\\n\", \"67\\n35\\n03\\n26\\n78\\n\", \"61\\n32\\n89\\n23\\n37\\n\", \"18\\n53\\n97\\n27\\n09\\n07\\n42\\n99\\n41\\n93\\n\", \"12\\n09\\n43\\n42\\n01\\n74\\n72\\n73\\n46\\n11\\n\", \"15\\n62\\n47\\n14\\n33\\n98\\n97\\n100\\n43\\n60\\n\", \"40\\n75\\n60\\n55\\n78\\n68\\n31\\n96\\n74\\n66\\n\", \"61\\n22\\n44\\n08\\n68\\n39\\n64\\n88\\n43\\n12\\n\", \"5\\n2\\n0\\n\", \"98\\n3\\n1\\n57\\n\", \"8\\n57\\n26\\n93\\n62\\n\", \"111111111\\n111111110\\n111111100\\n111111000\\n111110000\\n111100000\\n111000000\\n110000000\\n00000000\\n\"]}", "source": "primeintellect"}
There are n phone numbers in Polycarp's contacts on his phone. Each number is a 9-digit integer, starting with a digit different from 0. All the numbers are distinct. There is the latest version of Berdroid OS installed on Polycarp's phone. If some number is entered, is shows up all the numbers in the contacts for which there is a substring equal to the entered sequence of digits. For example, is there are three phone numbers in Polycarp's contacts: 123456789, 100000000 and 100123456, then: if he enters 00 two numbers will show up: 100000000 and 100123456, if he enters 123 two numbers will show up 123456789 and 100123456, if he enters 01 there will be only one number 100123456. For each of the phone numbers in Polycarp's contacts, find the minimum in length sequence of digits such that if Polycarp enters this sequence, Berdroid shows this only phone number. -----Input----- The first line contains single integer n (1 ≤ n ≤ 70000) — the total number of phone contacts in Polycarp's contacts. The phone numbers follow, one in each line. Each number is a positive 9-digit integer starting with a digit from 1 to 9. All the numbers are distinct. -----Output----- Print exactly n lines: the i-th of them should contain the shortest non-empty sequence of digits, such that if Polycarp enters it, the Berdroid OS shows up only the i-th number from the contacts. If there are several such sequences, print any of them. -----Examples----- Input 3 123456789 100000000 100123456 Output 9 000 01 Input 4 123456789 193456789 134567819 934567891 Output 2 193 81 91 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\na 3\\n2\\nb 1\\nc 1\\n\\nb 2\\n0\\n\\nb 1\\n1\\nb 2\\n\\nc 1\\n1\\nb 2\\n\", \"9\\ncodehorses 5\\n3\\nwebfrmk 6\\nmashadb 1\\nmashadb 2\\n\\ncommons 2\\n0\\n\\nmashadb 3\\n0\\n\\nwebfrmk 6\\n2\\nmashadb 3\\ncommons 2\\n\\nextra 4\\n1\\nextra 3\\n\\nextra 3\\n0\\n\\nextra 1\\n0\\n\\nmashadb 1\\n1\\nextra 3\\n\\nmashadb 2\\n1\\nextra 1\\n\", \"3\\nabc 1\\n2\\nabc 3\\ncba 2\\n\\nabc 3\\n0\\n\\ncba 2\\n0\\n\", \"1\\nabc 1000000\\n0\\n\", \"3\\nppdpd 283157\\n1\\npddpdpp 424025\\n\\nppdpd 529292\\n1\\nppdpd 283157\\n\\npddpdpp 424025\\n0\\n\", \"5\\nabbzzz 646068\\n0\\n\\nzabza 468048\\n2\\nbb 902619\\nzabza 550912\\n\\nzabza 217401\\n2\\nabbzzz 646068\\nbb 902619\\n\\nzabza 550912\\n1\\nzabza 217401\\n\\nbb 902619\\n1\\nabbzzz 646068\\n\", \"5\\nyyyy 223967\\n1\\nyyyyyyy 254197\\n\\nyyyyyyy 254197\\n0\\n\\ny 442213\\n0\\n\\ny 965022\\n1\\nyyyyyyy 254197\\n\\nyyyy 766922\\n4\\nyyyyyyy 254197\\ny 442213\\nyyyy 223967\\ny 965022\\n\", \"3\\nvvgvvgv 991444\\n1\\ngvgvgvgvgg 206648\\n\\nvvgvvgv 296188\\n0\\n\\ngvgvgvgvgg 206648\\n1\\nvvgvvgv 296188\\n\", \"5\\ntctocototo 984516\\n1\\ncttocottt 486791\\n\\ntctocototo 688522\\n2\\ncttocottt 486791\\ntctocototo 984516\\n\\ncttocottt 486791\\n0\\n\\ntctocototo 676435\\n1\\ntctocototo 394244\\n\\ntctocototo 394244\\n2\\ntctocototo 688522\\ncttocottt 486791\\n\", \"5\\nggggggggg 202537\\n4\\ngggggggg 868552\\ngggg 234633\\ngggg 402994\\ngggggggg 86863\\n\\ngggg 402994\\n0\\n\\ngggggggg 868552\\n1\\ngggg 234633\\n\\ngggg 234633\\n1\\ngggg 402994\\n\\ngggggggg 86863\\n0\\n\", \"4\\ncf 1\\n2\\ndb 1\\ndb 2\\n\\ndb 2\\n0\\n\\ndb 1\\n1\\nold 1\\n\\nold 1\\n0\\n\", \"5\\ncf 1\\n2\\ndb 1\\ndb 2\\n\\ndb 2\\n1\\nold 1\\n\\ndb 1\\n1\\nold 2\\n\\nold 1\\n0\\n\\nold 2\\n0\\n\"], \"outputs\": [\"2\\nb 1\\nc 1\\n\", \"4\\ncommons 2\\nextra 1\\nmashadb 2\\nwebfrmk 6\\n\", \"1\\ncba 2\\n\", \"0\\n\", \"1\\npddpdpp 424025\\n\", \"0\\n\", \"1\\nyyyyyyy 254197\\n\", \"1\\ngvgvgvgvgg 206648\\n\", \"1\\ncttocottt 486791\\n\", \"2\\ngggg 402994\\ngggggggg 868552\\n\", \"1\\ndb 2\\n\", \"2\\ndb 2\\nold 1\\n\"]}", "source": "primeintellect"}
Polycarp is currently developing a project in Vaja language and using a popular dependency management system called Vamen. From Vamen's point of view both Vaja project and libraries are treated projects for simplicity. A project in Vaja has its own uniqie non-empty name consisting of lowercase latin letters with length not exceeding 10 and version — positive integer from 1 to 10^6. Each project (keep in mind that it is determined by both its name and version) might depend on other projects. For sure, there are no cyclic dependencies. You're given a list of project descriptions. The first of the given projects is the one being developed by Polycarp at this moment. Help Polycarp determine all projects that his project depends on (directly or via a certain chain). It's possible that Polycarp's project depends on two different versions of some project. In this case collision resolving is applied, i.e. for each such project the system chooses the version that minimizes the distance from it to Polycarp's project. If there are several options, the newer (with the maximum version) is preferred. This version is considered actual; other versions and their dependencies are ignored. More formal, choose such a set of projects of minimum possible size that the following conditions hold: Polycarp's project is chosen; Polycarp's project depends (directly or indirectly) on all other projects in the set; no two projects share the name; for each project x that some other project in the set depends on we have either x or some y with other version and shorter chain to Polycarp's project chosen. In case of ties the newer one is chosen. Output all Polycarp's project's dependencies (Polycarp's project itself should't be printed) in lexicographical order. -----Input----- The first line contains an only integer n (1 ≤ n ≤ 1 000) — the number of projects in Vaja. The following lines contain the project descriptions. Each project is described by a line consisting of its name and version separated by space. The next line gives the number of direct dependencies (from 0 to n - 1) and the dependencies themselves (one in a line) in arbitrary order. Each dependency is specified by its name and version. The projects are also given in arbitrary order, but the first of them is always Polycarp's. Project descriptions are separated by one empty line. Refer to samples for better understanding. It's guaranteed that there are no cyclic dependencies. -----Output----- Output all Polycarp's project's dependencies in lexicographical order. -----Examples----- Input 4 a 3 2 b 1 c 1   b 2 0   b 1 1 b 2   c 1 1 b 2 Output 2 b 1 c 1 Input 9 codehorses 5 3 webfrmk 6 mashadb 1 mashadb 2   commons 2 0   mashadb 3 0   webfrmk 6 2 mashadb 3 commons 2   extra 4 1 extra 3   extra 3 0   extra 1 0   mashadb 1 1 extra 3   mashadb 2 1 extra 1 Output 4 commons 2 extra 1 mashadb 2 webfrmk 6 Input 3 abc 1 2 abc 3 cba 2 abc 3 0 cba 2 0 Output 1 cba 2 -----Note----- The first sample is given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «a» (version 3) depends on are painted black. [Image] The second sample is again given in the pic below. Arrow from A to B means that B directly depends on A. Projects that Polycarp's project «codehorses» (version 5) depends on are paint it black. Note that «extra 1» is chosen instead of «extra 3» since «mashadb 1» and all of its dependencies are ignored due to «mashadb 2». [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n1 4\\n2 5\\n3 1\\n4 5\\n\", \"2\\n1 2\\n\", \"2\\n2 1\\n\", \"3\\n1 2\\n2 3\\n\", \"3\\n3 1\\n2 3\\n\", \"3\\n3 1\\n2 1\\n\", \"8\\n5 7\\n1 6\\n4 3\\n8 2\\n5 6\\n4 2\\n7 8\\n\", \"64\\n2 20\\n37 55\\n42 29\\n36 54\\n35 50\\n60 41\\n25 61\\n31 51\\n9 43\\n26 28\\n48 56\\n13 64\\n17 19\\n8 12\\n52 16\\n24 44\\n1 18\\n58 15\\n5 30\\n3 7\\n62 22\\n53 14\\n11 38\\n10 49\\n47 59\\n40 45\\n33 4\\n23 39\\n63 57\\n32 46\\n34 6\\n27 21\\n20 55\\n29 54\\n35 60\\n61 51\\n9 26\\n48 13\\n19 12\\n52 44\\n1 58\\n5 7\\n62 53\\n11 10\\n59 40\\n4 39\\n57 32\\n34 27\\n37 36\\n60 51\\n43 56\\n12 52\\n15 30\\n22 11\\n47 4\\n32 21\\n20 35\\n48 16\\n58 53\\n59 57\\n51 9\\n1 59\\n28 38\\n\", \"10\\n10 6\\n8 10\\n6 2\\n8 7\\n8 4\\n1 7\\n8 5\\n3 10\\n10 9\\n\", \"10\\n4 6\\n10 4\\n7 10\\n8 10\\n5 1\\n9 5\\n2 1\\n3 9\\n8 5\\n\", \"10\\n7 4\\n2 7\\n10 2\\n5 6\\n9 5\\n1 9\\n8 6\\n3 7\\n10 9\\n\", \"10\\n10 9\\n9 1\\n8 3\\n3 4\\n5 8\\n6 5\\n1 8\\n6 7\\n7 2\\n\"], \"outputs\": [\"3 1 4 2 5\\n\", \"1 2\\n\", \"2 1\\n\", \"3 1 2\\n\", \"2 3 1\\n\", \"2 3 1\\n\", \"5 7 1 6 4 3 8 2\\n\", \"2 20 37 55 42 29 36 54 35 50 60 41 25 61 31 51 9 43 26 28 48 56 13 64 17 19 8 12 52 16 24 44 1 18 58 15 5 30 3 7 62 22 53 14 11 38 10 49 47 59 40 45 33 4 23 39 63 57 32 46 34 6 27 21\\n\", \"9 3 5 1 4 7 2 8 10 6\\n\", \"8 7 10 4 6 3 2 9 5 1\\n\", \"3 10 2 7 4 8 1 9 5 6\\n\", \"2 7 1 10 9 6 5 4 8 3\\n\"]}", "source": "primeintellect"}
Asya loves animals very much. Recently, she purchased $n$ kittens, enumerated them from $1$ and $n$ and then put them into the cage. The cage consists of one row of $n$ cells, enumerated with integers from $1$ to $n$ from left to right. Adjacent cells had a partially transparent partition wall between them, hence there were $n - 1$ partitions originally. Initially, each cell contained exactly one kitten with some number. Observing the kittens, Asya noticed, that they are very friendly and often a pair of kittens in neighboring cells wants to play together. So Asya started to remove partitions between neighboring cells. In particular, on the day $i$, Asya: Noticed, that the kittens $x_i$ and $y_i$, located in neighboring cells want to play together. Removed the partition between these two cells, efficiently creating a single cell, having all kittens from two original cells. Since Asya has never putted partitions back, after $n - 1$ days the cage contained a single cell, having all kittens. For every day, Asya remembers numbers of kittens $x_i$ and $y_i$, who wanted to play together, however she doesn't remember how she placed kittens in the cage in the beginning. Please help her and find any possible initial arrangement of the kittens into $n$ cells. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 150\,000$) — the number of kittens. Each of the following $n - 1$ lines contains integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$, $x_i \ne y_i$) — indices of kittens, which got together due to the border removal on the corresponding day. It's guaranteed, that the kittens $x_i$ and $y_i$ were in the different cells before this day. -----Output----- For every cell from $1$ to $n$ print a single integer — the index of the kitten from $1$ to $n$, who was originally in it. All printed integers must be distinct. It's guaranteed, that there is at least one answer possible. In case there are multiple possible answers, print any of them. -----Example----- Input 5 1 4 2 5 3 1 4 5 Output 3 1 4 2 5 -----Note----- The answer for the example contains one of several possible initial arrangements of the kittens. The picture below shows how the cells were united for this initial arrangement. Note, that the kittens who wanted to play together on each day were indeed in adjacent cells. [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1\\n0 1\\n\", \"3\\n0 1\\n0 2\\n0 -3\\n\", \"2\\n0 1\\n1 1\\n\", \"4\\n-10000000 1\\n10000000 1\\n-10000000 10000000\\n10000000 10000000\\n\", \"20\\n7571542 9868879\\n3634934 5986987\\n2297651 5538795\\n9701582 8250721\\n-2913168 4968483\\n8838674 5771883\\n-3348778 8326335\\n-577797 750161\\n-7223138 9113512\\n-1808146 6044926\\n-756446 417388\\n4700417 1550148\\n-5568937 20029\\n-9170707 6103893\\n7445655 7161843\\n8820510 4145867\\n-4466974 9280518\\n5842792 1905697\\n-8784063 3253524\\n-796596 1060279\\n\", \"9\\n0 1\\n1 2\\n2 3\\n1 4\\n0 5\\n-1 4\\n-2 3\\n-1 2\\n0 3\\n\", \"2\\n0 -1\\n1 -1\\n\", \"3\\n-9218650 9873914\\n401586 3632998\\n-6935541 4222557\\n\", \"3\\n8376968 152285\\n-8928555 5949944\\n-4450793 4397692\\n\", \"2\\n0 1\\n0 2\\n\", \"4\\n7562857 4638477\\n2230312 4253055\\n-9291686 9317264\\n-6643279 5426447\\n\", \"3\\n1367642 1559668\\n-190618 8836498\\n-976468 4184452\\n\"], \"outputs\": [\"0.4999999998750000\\n\", \"-1\\n\", \"0.6249999997500000\\n\", \"50000000000000.4949989318847656\\n\", \"28974952.6680223650746484\\n\", \"2.4999999999750000\\n\", \"0.6249999997500000\\n\", \"5836809.6818175388057170\\n\", \"21291365.1020918242811604\\n\", \"0.9999999999375000\\n\", \"8992361.5726685714635096\\n\", \"4418249.0000000000000000\\n\"]}", "source": "primeintellect"}
There is a forest that we model as a plane and live $n$ rare animals. Animal number $i$ has its lair in the point $(x_{i}, y_{i})$. In order to protect them, a decision to build a nature reserve has been made. The reserve must have a form of a circle containing all lairs. There is also a straight river flowing through the forest. All animals drink from this river, therefore it must have at least one common point with the reserve. On the other hand, ships constantly sail along the river, so the reserve must not have more than one common point with the river. For convenience, scientists have made a transformation of coordinates so that the river is defined by $y = 0$. Check whether it is possible to build a reserve, and if possible, find the minimum possible radius of such a reserve. -----Input----- The first line contains one integer $n$ ($1 \le n \le 10^5$) — the number of animals. Each of the next $n$ lines contains two integers $x_{i}$, $y_{i}$ ($-10^7 \le x_{i}, y_{i} \le 10^7$) — the coordinates of the $i$-th animal's lair. It is guaranteed that $y_{i} \neq 0$. No two lairs coincide. -----Output----- If the reserve cannot be built, print $-1$. Otherwise print the minimum radius. Your answer will be accepted if absolute or relative error does not exceed $10^{-6}$. Formally, let your answer be $a$, and the jury's answer be $b$. Your answer is considered correct if $\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$. -----Examples----- Input 1 0 1 Output 0.5 Input 3 0 1 0 2 0 -3 Output -1 Input 2 0 1 1 1 Output 0.625 -----Note----- In the first sample it is optimal to build the reserve with the radius equal to $0.5$ and the center in $(0,\ 0.5)$. In the second sample it is impossible to build a reserve. In the third sample it is optimal to build the reserve with the radius equal to $\frac{5}{8}$ and the center in $(\frac{1}{2},\ \frac{5}{8})$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"2 1\\n1 2\\n1 2\\n\", \"3 3\\n3 1 2\\n1 2\\n3 1\\n3 2\\n\", \"5 2\\n3 1 5 4 2\\n5 2\\n5 4\\n\", \"5 11\\n5 1 3 4 2\\n5 1\\n5 2\\n1 5\\n2 1\\n1 2\\n1 4\\n2 5\\n1 3\\n5 4\\n5 3\\n3 1\\n\", \"10 23\\n6 9 8 10 4 3 7 1 5 2\\n7 2\\n3 2\\n2 4\\n2 3\\n7 5\\n6 4\\n10 7\\n7 1\\n6 8\\n6 2\\n8 10\\n3 5\\n3 1\\n6 1\\n10 2\\n8 2\\n10 1\\n7 4\\n10 5\\n6 9\\n6 5\\n9 1\\n10 4\\n\", \"2 0\\n1 2\\n\", \"1 0\\n1\\n\", \"10 20\\n2 1 3 9 5 4 7 8 6 10\\n4 7\\n6 4\\n1 4\\n2 8\\n1 6\\n7 9\\n1 9\\n5 4\\n1 3\\n10 6\\n8 6\\n5 6\\n7 6\\n8 10\\n5 10\\n7 10\\n2 7\\n1 10\\n10 3\\n6 9\\n\", \"20 47\\n4 6 11 15 9 17 3 1 19 14 12 8 2 5 7 20 16 18 13 10\\n18 10\\n6 3\\n15 17\\n18 7\\n6 5\\n19 10\\n6 7\\n11 3\\n1 10\\n17 3\\n6 14\\n7 10\\n19 5\\n12 10\\n1 8\\n6 11\\n18 5\\n6 8\\n12 8\\n1 5\\n20 10\\n16 8\\n6 10\\n20 19\\n17 8\\n13 10\\n2 5\\n19 8\\n6 9\\n16 3\\n16 10\\n19 7\\n17 16\\n10 16\\n8 9\\n12 5\\n17 10\\n2 9\\n6 15\\n4 9\\n10 1\\n17 14\\n19 14\\n2 10\\n17 5\\n2 13\\n1 14\\n\", \"3 2\\n1 2 3\\n1 2\\n2 1\\n\", \"5 4\\n1 2 3 4 5\\n4 5\\n2 5\\n1 3\\n1 5\\n\", \"2 1\\n1 2\\n2 1\\n\"], \"outputs\": [\"1\", \"2\", \"1\", \"2\", \"4\", \"0\", \"0\", \"4\", \"11\", \"0\", \"1\", \"0\"]}", "source": "primeintellect"}
At the big break Nastya came to the school dining room. There are $n$ pupils in the school, numbered from $1$ to $n$. Unfortunately, Nastya came pretty late, so that all pupils had already stood in the queue, i.e. Nastya took the last place in the queue. Of course, it's a little bit sad for Nastya, but she is not going to despond because some pupils in the queue can agree to change places with some other pupils. Formally, there are some pairs $u$, $v$ such that if the pupil with number $u$ stands directly in front of the pupil with number $v$, Nastya can ask them and they will change places. Nastya asks you to find the maximal number of places in queue she can move forward. -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n \leq 3 \cdot 10^{5}$, $0 \leq m \leq 5 \cdot 10^{5}$) — the number of pupils in the queue and number of pairs of pupils such that the first one agrees to change places with the second one if the first is directly in front of the second. The second line contains $n$ integers $p_1$, $p_2$, ..., $p_n$ — the initial arrangement of pupils in the queue, from the queue start to its end ($1 \leq p_i \leq n$, $p$ is a permutation of integers from $1$ to $n$). In other words, $p_i$ is the number of the pupil who stands on the $i$-th position in the queue. The $i$-th of the following $m$ lines contains two integers $u_i$, $v_i$ ($1 \leq u_i, v_i \leq n, u_i \neq v_i$), denoting that the pupil with number $u_i$ agrees to change places with the pupil with number $v_i$ if $u_i$ is directly in front of $v_i$. It is guaranteed that if $i \neq j$, than $v_i \neq v_j$ or $u_i \neq u_j$. Note that it is possible that in some pairs both pupils agree to change places with each other. Nastya is the last person in the queue, i.e. the pupil with number $p_n$. -----Output----- Print a single integer — the number of places in queue she can move forward. -----Examples----- Input 2 1 1 2 1 2 Output 1 Input 3 3 3 1 2 1 2 3 1 3 2 Output 2 Input 5 2 3 1 5 4 2 5 2 5 4 Output 1 -----Note----- In the first example Nastya can just change places with the first pupil in the queue. Optimal sequence of changes in the second example is change places for pupils with numbers $1$ and $3$. change places for pupils with numbers $3$ and $2$. change places for pupils with numbers $1$ and $2$. The queue looks like $[3, 1, 2]$, then $[1, 3, 2]$, then $[1, 2, 3]$, and finally $[2, 1, 3]$ after these operations. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 3\\n...\\n..#\\n...\\n\", \"4 2\\n..\\n..\\n..\\n..\\n\", \"4 5\\n#...#\\n#...#\\n###.#\\n###.#\\n\", \"6 4\\n####\\n####\\n....\\n####\\n####\\n####\\n\", \"4 6\\n######\\n######\\n......\\n######\\n\", \"6 4\\n##.#\\n##.#\\n##.#\\n##.#\\n##.#\\n##.#\\n\", \"4 6\\n##.###\\n##.###\\n##.###\\n##.###\\n\", \"9 37\\n.......#................#.##...#.....\\n..#......................##..........\\n..#......#.........#.................\\n..........#..#..#..#...........#.....\\n...#............#..##.#.....##.......\\n......................#..............\\n......#........##....#......#........\\n........#......................#.....\\n................#.......#............\\n\", \"4 60\\n##...#.......#......#.....##.#.#...#.#..#....#..#....#.#.###\\n..#.##.###..#...#......###.....##....#..........#...###..##.\\n#.....#.##..##.#..#.###..##.#...#.#..#....#...#.#.##...##..#\\n......#...#..#####...#.##..#.#...#......#.#.##.#...#.#......\\n\", \"50 4\\n.###\\n....\\n..##\\n.###\\n.#.#\\n###.\\n.#..\\n....\\n.##.\\n.#.#\\n..#.\\n#.##\\n..##\\n.###\\n.#..\\n#.##\\n#..#\\n.#..\\n.##.\\n##.#\\n.###\\n####\\n.##.\\n#...\\n####\\n..##\\n.###\\n.##.\\n###.\\n...#\\n##..\\n.###\\n#..#\\n##..\\n..##\\n....\\n##.#\\n#..#\\n###.\\n....\\n##.#\\n....\\n##..\\n.#.#\\n#...\\n...#\\n####\\n...#\\n##..\\n..#.\\n\", \"10 10\\n##........\\n#.#....#..\\n#.#...#..#\\n.........#\\n#..#......\\n......#..#\\n..#...#...\\n..#...#...\\n...#.....#\\n..........\\n\", \"2 2\\n..\\n..\\n\"], \"outputs\": [\"3\\n\", \"2\\n\", \"4\\n\", \"1\\n\", \"1\\n\", \"1\\n\", \"1\\n\", \"2255\\n\", \"116\\n\", \"30\\n\", \"157\\n\", \"0\\n\"]}", "source": "primeintellect"}
On a certain meeting of a ruling party "A" minister Pavel suggested to improve the sewer system and to create a new pipe in the city. The city is an n × m rectangular squared field. Each square of the field is either empty (then the pipe can go in it), or occupied (the pipe cannot go in such square). Empty squares are denoted by character '.', occupied squares are denoted by character '#'. The pipe must meet the following criteria: the pipe is a polyline of width 1, the pipe goes in empty squares, the pipe starts from the edge of the field, but not from a corner square, the pipe ends at the edge of the field but not in a corner square, the pipe has at most 2 turns (90 degrees), the border squares of the field must share exactly two squares with the pipe, if the pipe looks like a single segment, then the end points of the pipe must lie on distinct edges of the field, for each non-border square of the pipe there are exacly two side-adjacent squares that also belong to the pipe, for each border square of the pipe there is exactly one side-adjacent cell that also belongs to the pipe. Here are some samples of allowed piping routes: ....# ....# .*..# ***** ****. .***. ..#.. ..#*. ..#*. #...# #..*# #..*# ..... ...*. ...*. Here are some samples of forbidden piping routes: .**.# *...# .*.*# ..... ****. .*.*. ..#.. ..#*. .*#*. #...# #..*# #*.*# ..... ...*. .***. In these samples the pipes are represented by characters ' * '. You were asked to write a program that calculates the number of distinct ways to make exactly one pipe in the city. The two ways to make a pipe are considered distinct if they are distinct in at least one square. -----Input----- The first line of the input contains two integers n, m (2 ≤ n, m ≤ 2000) — the height and width of Berland map. Each of the next n lines contains m characters — the map of the city. If the square of the map is marked by character '.', then the square is empty and the pipe can through it. If the square of the map is marked by character '#', then the square is full and the pipe can't through it. -----Output----- In the first line of the output print a single integer — the number of distinct ways to create a pipe. -----Examples----- Input 3 3 ... ..# ... Output 3 Input 4 2 .. .. .. .. Output 2 Input 4 5 #...# #...# ###.# ###.# Output 4 -----Note----- In the first sample there are 3 ways to make a pipe (the squares of the pipe are marked by characters ' * '): .*. .*. ... .*# **# **# .*. ... .*. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n1\\n1\\n1\\n\", \"7\\n1\\n1\\n1\\n2\\n2\\n2\\n\", \"8\\n1\\n1\\n1\\n1\\n3\\n3\\n3\\n\", \"3\\n1\\n1\\n\", \"13\\n1\\n2\\n2\\n2\\n1\\n6\\n6\\n6\\n1\\n10\\n10\\n10\\n\", \"7\\n1\\n2\\n2\\n1\\n1\\n1\\n\", \"7\\n1\\n1\\n1\\n1\\n2\\n2\\n\", \"8\\n1\\n1\\n1\\n1\\n5\\n5\\n5\\n\", \"9\\n1\\n1\\n1\\n1\\n2\\n6\\n6\\n6\\n\", \"12\\n1\\n1\\n1\\n2\\n5\\n5\\n5\\n5\\n1\\n2\\n2\\n\", \"20\\n1\\n1\\n1\\n1\\n2\\n2\\n2\\n3\\n3\\n3\\n4\\n4\\n4\\n5\\n5\\n5\\n1\\n1\\n1\\n\", \"7\\n1\\n1\\n1\\n3\\n3\\n3\\n\"], \"outputs\": [\"Yes\\n\", \"No\\n\", \"Yes\\n\", \"No\\n\", \"No\\n\", \"No\\n\", \"No\\n\", \"Yes\\n\", \"No\\n\", \"No\\n\", \"Yes\\n\", \"No\\n\"]}", "source": "primeintellect"}
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed from the root. Vertex u is called a child of vertex v and vertex v is called a parent of vertex u if there exists a directed edge from v to u. A vertex is called a leaf if it doesn't have children and has a parent. Let's call a rooted tree a spruce if its every non-leaf vertex has at least 3 leaf children. You are given a rooted tree, check whether it's a spruce. The definition of a rooted tree can be found here. -----Input----- The first line contains one integer n — the number of vertices in the tree (3 ≤ n ≤ 1 000). Each of the next n - 1 lines contains one integer p_{i} (1 ≤ i ≤ n - 1) — the index of the parent of the i + 1-th vertex (1 ≤ p_{i} ≤ i). Vertex 1 is the root. It's guaranteed that the root has at least 2 children. -----Output----- Print "Yes" if the tree is a spruce and "No" otherwise. -----Examples----- Input 4 1 1 1 Output Yes Input 7 1 1 1 2 2 2 Output No Input 8 1 1 1 1 3 3 3 Output Yes -----Note----- The first example: [Image] The second example: $8$ It is not a spruce, because the non-leaf vertex 1 has only 2 leaf children. The third example: [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.75
{"tests": "{\"inputs\": [\"3 2\\n1 2\\n2 3\\n\", \"3 3\\n1 2\\n2 3\\n3 1\\n\", \"3 1\\n1 3\\n\", \"1 0\\n\", \"4 5\\n1 3\\n4 3\\n2 3\\n2 4\\n1 4\\n\", \"4 6\\n3 4\\n2 3\\n2 1\\n1 3\\n1 4\\n4 2\\n\", \"7 4\\n3 4\\n3 5\\n5 7\\n2 5\\n\", \"5 3\\n1 2\\n2 3\\n1 3\\n\", \"3 1\\n1 2\\n\", \"4 1\\n3 2\\n\", \"2 1\\n1 2\\n\", \"2 0\\n\"], \"outputs\": [\"2\\n1 3\\n1 1\\n3\\n2 4\\n2 1\\n2 2\\n2\\n3 5\\n3 2\\n\", \"3\\n1 4\\n1 1\\n1 3\\n3\\n2 5\\n2 1\\n2 2\\n3\\n3 6\\n3 2\\n3 3\\n\", \"2\\n1 2\\n1 1\\n1\\n2 3\\n2\\n3 4\\n3 1\\n\", \"1\\n1 1\\n\", \"3\\n1 6\\n1 1\\n1 5\\n3\\n2 7\\n2 3\\n2 4\\n4\\n3 8\\n3 1\\n3 2\\n3 3\\n4\\n4 9\\n4 2\\n4 4\\n4 5\\n\", \"4\\n1 7\\n1 3\\n1 4\\n1 5\\n4\\n2 8\\n2 2\\n2 3\\n2 6\\n4\\n3 9\\n3 1\\n3 2\\n3 4\\n4\\n4 10\\n4 1\\n4 5\\n4 6\\n\", \"1\\n1 5\\n2\\n2 6\\n2 4\\n3\\n3 7\\n3 1\\n3 2\\n2\\n4 8\\n4 1\\n4\\n5 9\\n5 2\\n5 3\\n5 4\\n1\\n6 10\\n2\\n7 11\\n7 3\\n\", \"3\\n1 4\\n1 1\\n1 3\\n3\\n2 5\\n2 1\\n2 2\\n3\\n3 6\\n3 2\\n3 3\\n1\\n4 7\\n1\\n5 8\\n\", \"2\\n1 2\\n1 1\\n2\\n2 3\\n2 1\\n1\\n3 4\\n\", \"1\\n1 2\\n2\\n2 3\\n2 1\\n2\\n3 4\\n3 1\\n1\\n4 5\\n\", \"2\\n1 2\\n1 1\\n2\\n2 3\\n2 1\\n\", \"1\\n1 1\\n1\\n2 2\\n\"]}", "source": "primeintellect"}
Ivan is a novice painter. He has $n$ dyes of different colors. He also knows exactly $m$ pairs of colors which harmonize with each other. Ivan also enjoy playing chess. He has $5000$ rooks. He wants to take $k$ rooks, paint each of them in one of $n$ colors and then place this $k$ rooks on a chessboard of size $10^{9} \times 10^{9}$. Let's call the set of rooks on the board connected if from any rook we can get to any other rook in this set moving only through cells with rooks from this set. Assume that rooks can jump over other rooks, in other words a rook can go to any cell which shares vertical and to any cell which shares horizontal. Ivan wants his arrangement of rooks to have following properties: For any color there is a rook of this color on a board; For any color the set of rooks of this color is connected; For any two different colors $a$ $b$ union of set of rooks of color $a$ and set of rooks of color $b$ is connected if and only if this two colors harmonize with each other. Please help Ivan find such an arrangement. -----Input----- The first line of input contains $2$ integers $n$, $m$ ($1 \le n \le 100$, $0 \le m \le min(1000, \,\, \frac{n(n-1)}{2})$) — number of colors and number of pairs of colors which harmonize with each other. In next $m$ lines pairs of colors which harmonize with each other are listed. Colors are numbered from $1$ to $n$. It is guaranteed that no pair occurs twice in this list. -----Output----- Print $n$ blocks, $i$-th of them describes rooks of $i$-th color. In the first line of block print one number $a_{i}$ ($1 \le a_{i} \le 5000$) — number of rooks of color $i$. In each of next $a_{i}$ lines print two integers $x$ and $y$ ($1 \le x, \,\, y \le 10^{9}$) — coordinates of the next rook. All rooks must be on different cells. Total number of rooks must not exceed $5000$. It is guaranteed that the solution exists. -----Examples----- Input 3 2 1 2 2 3 Output 2 3 4 1 4 4 1 2 2 2 2 4 5 4 1 5 1 Input 3 3 1 2 2 3 3 1 Output 1 1 1 1 1 2 1 1 3 Input 3 1 1 3 Output 1 1 1 1 2 2 1 3 1 -----Note----- Rooks arrangements for all three examples (red is color $1$, green is color $2$ and blue is color $3$). $\left. \begin{array}{|l|l|l|l|l|l|l|} \hline 5 & {} & {} & {} & {} & {} \\ \hline 4 & {} & {} & {} & {} & {} \\ \hline 3 & {} & {} & {} & {} & {} \\ \hline 2 & {} & {} & {} & {} & {} \\ \hline 1 & {} & {} & {} & {} & {} \\ \hline & {1} & {2} & {3} & {4} & {5} \\ \hline \end{array} \right.$ $\left. \begin{array}{|l|l|l|l|} \hline 2 & {} & {} & {} \\ \hline 1 & {} & {} & {} \\ \hline & {1} & {2} & {3} \\ \hline \end{array} \right.$ $\left. \begin{array}{|l|l|l|l|} \hline 2 & {} & {} & {} \\ \hline 1 & {} & {} & {} \\ \hline & {1} & {2} & {3} \\ \hline \end{array} \right.$ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"7 2\\n1 2 1 1 3 5 1\\n3 5\\n\", \"3 3\\n1 3 2\\n1 2 3\\n\", \"5 0\\n4 3 1 2 3\\n\", \"10 3\\n1 3 5 6 12 9 8 10 13 15\\n2 4 9\\n\", \"20 5\\n19 4 11 10 7 18 7 10 12 13 14 15 16 17 22 26 29 23 30 22\\n2 9 11 12 14\\n\", \"50 15\\n1 2 3 5 9 10 14 15 17 16 18 19 21 23 22 27 27 36 26 23 23 32 27 38 40 41 42 46 51 56 55 52 55 59 60 62 66 68 69 76 83 84 89 90 29 92 94 95 98 99\\n1 3 4 8 12 24 27 29 34 35 36 41 43 48 49\\n\", \"50 40\\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50\\n1 2 4 5 6 7 8 10 11 12 13 14 15 17 18 19 21 22 23 24 25 26 28 29 30 31 33 35 36 37 38 40 41 42 45 46 47 48 49 50\\n\", \"50 35\\n69 2 3 15 16 18 18 20 21 22 33 31 38 47 50 42 50 13 1 58 58 59 60 67 61 52 61 61 64 65 33 28 66 4 8 96 32 68 69 74 83 84 85 87 87 88 64 89 89 93\\n2 3 4 5 6 7 8 9 10 11 13 14 15 17 20 21 22 23 27 28 29 30 33 38 39 40 41 42 43 44 45 46 48 49 50\\n\", \"100 0\\n4 20 52 2 7 36 8 73 9 99 13 19 14 89 32 2 6 2 16 21 47 22 66 23 25 95 28 4 29 64 30 31 7 48 32 51 35 37 77 57 40 42 56 40 43 44 46 13 47 12 48 49 24 51 69 53 54 98 21 57 59 61 62 52 63 30 68 32 69 39 8 67 71 59 72 81 76 77 11 82 85 14 83 33 86 89 89 90 43 96 91 31 93 69 58 94 45 96 2 99\\n\", \"1 0\\n1337\\n\", \"1 1\\n1337\\n1\\n\", \"4 2\\n1 1 2 2\\n2 4\\n\"], \"outputs\": [\"4\\n\", \"-1\\n\", \"2\\n\", \"3\\n\", \"8\\n\", \"13\\n\", \"0\\n\", \"-1\\n\", \"80\\n\", \"0\\n\", \"0\\n\", \"-1\\n\"]}", "source": "primeintellect"}
You are given an array of $n$ integers $a_1$, $a_2$, ..., $a_n$, and a set $b$ of $k$ distinct integers from $1$ to $n$. In one operation, you may choose two integers $i$ and $x$ ($1 \le i \le n$, $x$ can be any integer) and assign $a_i := x$. This operation can be done only if $i$ does not belong to the set $b$. Calculate the minimum number of operations you should perform so the array $a$ is increasing (that is, $a_1 < a_2 < a_3 < \dots < a_n$), or report that it is impossible. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 5 \cdot 10^5$, $0 \le k \le n$) — the size of the array $a$ and the set $b$, respectively. The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($1 \le a_i \le 10^9$). Then, if $k \ne 0$, the third line follows, containing $k$ integers $b_1$, $b_2$, ..., $b_k$ ($1 \le b_1 < b_2 < \dots < b_k \le n$). If $k = 0$, this line is skipped. -----Output----- If it is impossible to make the array $a$ increasing using the given operations, print $-1$. Otherwise, print one integer — the minimum number of operations you have to perform. -----Examples----- Input 7 2 1 2 1 1 3 5 1 3 5 Output 4 Input 3 3 1 3 2 1 2 3 Output -1 Input 5 0 4 3 1 2 3 Output 2 Input 10 3 1 3 5 6 12 9 8 10 13 15 2 4 9 Output 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 2\\n1 3 1\\n\", \"3 6\\n3 3 3\\n\", \"5 6\\n4 2 3 1 3\\n\", \"6 1\\n1 1 1 300 1 1\\n\", \"3 10\\n5 8 1\\n\", \"1 1\\n1\\n\", \"4 14\\n4 9 2 7\\n\", \"8 20\\n3 2 1 1 6 2 6 1\\n\", \"10 6\\n6 4 8 9 9 10 5 2 8 1\\n\", \"48 14\\n1 1 1 1 1 6 1 1 1 1 1 1 3 3 1 1 1 1 1 2 2 2 1 1 1 1 1 1 2 1 4 1 1 1 1 1 1 1 1 1 1 7 1 1 1 1 1 5\\n\", \"18 445\\n89 24 6 83 53 67 17 38 39 45 2 98 72 29 38 59 78 98\\n\", \"2 179\\n444 57\\n\"], \"outputs\": [\"5\", \"12\", \"15\", \"300\", \"45\", \"1\", \"66\", \"55\", \"45\", \"42\", \"18022\", \"63545\"]}", "source": "primeintellect"}
You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until now. And just now you found out that she lives in a faraway place called Naha. You immediately decided to take a vacation and visit Coronavirus-chan. Your vacation lasts exactly $x$ days and that's the exact number of days you will spend visiting your friend. You will spend exactly $x$ consecutive (successive) days visiting Coronavirus-chan. They use a very unusual calendar in Naha: there are $n$ months in a year, $i$-th month lasts exactly $d_i$ days. Days in the $i$-th month are numbered from $1$ to $d_i$. There are no leap years in Naha. The mood of Coronavirus-chan (and, accordingly, her desire to hug you) depends on the number of the day in a month. In particular, you get $j$ hugs if you visit Coronavirus-chan on the $j$-th day of the month. You know about this feature of your friend and want to plan your trip to get as many hugs as possible (and then maybe you can win the heart of Coronavirus-chan). Please note that your trip should not necessarily begin and end in the same year. -----Input----- The first line of input contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$) — the number of months in the year and the number of days you can spend with your friend. The second line contains $n$ integers $d_1, d_2, \ldots, d_n$, $d_i$ is the number of days in the $i$-th month ($1 \le d_i \le 10^6$). It is guaranteed that $1 \le x \le d_1 + d_2 + \ldots + d_n$. -----Output----- Print one integer — the maximum number of hugs that you can get from Coronavirus-chan during the best vacation in your life. -----Examples----- Input 3 2 1 3 1 Output 5 Input 3 6 3 3 3 Output 12 Input 5 6 4 2 3 1 3 Output 15 -----Note----- In the first test case, the numbers of the days in a year are (indices of days in a corresponding month) $\{1,1,2,3,1\}$. Coronavirus-chan will hug you the most if you come on the third day of the year: $2+3=5$ hugs. In the second test case, the numbers of the days are $\{1,2,3,1,2,3,1,2,3\}$. You will get the most hugs if you arrive on the third day of the year: $3+1+2+3+1+2=12$ hugs. In the third test case, the numbers of the days are $\{1,2,3,4,1,2, 1,2,3, 1, 1,2,3\}$. You will get the most hugs if you come on the twelfth day of the year: your friend will hug you $2+3+1+2+3+4=15$ times. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.125
{"tests": "{\"inputs\": [\"6\\n010000\\n000001\\n\", \"10\\n1111100000\\n0000011111\\n\", \"8\\n10101010\\n01010101\\n\", \"10\\n1111100000\\n1111100001\\n\", \"12\\n011001010101\\n111001010101\\n\", \"9\\n111110000\\n111100000\\n\", \"5\\n10100\\n01010\\n\", \"10\\n1010111110\\n1011101101\\n\", \"3\\n000\\n000\\n\", \"5\\n11011\\n10111\\n\", \"10\\n0111101111\\n0111111110\\n\", \"10\\n1100000100\\n0010000011\\n\"], \"outputs\": [\"1\", \"5\", \"1\", \"-1\", \"-1\", \"-1\", \"1\", \"2\", \"0\", \"1\", \"1\", \"2\"]}", "source": "primeintellect"}
Naman has two binary strings $s$ and $t$ of length $n$ (a binary string is a string which only consists of the characters "0" and "1"). He wants to convert $s$ into $t$ using the following operation as few times as possible. In one operation, he can choose any subsequence of $s$ and rotate it clockwise once. For example, if $s = 1\textbf{1}101\textbf{00}$, he can choose a subsequence corresponding to indices ($1$-based) $\{2, 6, 7 \}$ and rotate them clockwise. The resulting string would then be $s = 1\textbf{0}101\textbf{10}$. A string $a$ is said to be a subsequence of string $b$ if $a$ can be obtained from $b$ by deleting some characters without changing the ordering of the remaining characters. To perform a clockwise rotation on a sequence $c$ of size $k$ is to perform an operation which sets $c_1:=c_k, c_2:=c_1, c_3:=c_2, \ldots, c_k:=c_{k-1}$ simultaneously. Determine the minimum number of operations Naman has to perform to convert $s$ into $t$ or say that it is impossible. -----Input----- The first line contains a single integer $n$ $(1 \le n \le 10^6)$ — the length of the strings. The second line contains the binary string $s$ of length $n$. The third line contains the binary string $t$ of length $n$. -----Output----- If it is impossible to convert $s$ to $t$ after any number of operations, print $-1$. Otherwise, print the minimum number of operations required. -----Examples----- Input 6 010000 000001 Output 1 Input 10 1111100000 0000011111 Output 5 Input 8 10101010 01010101 Output 1 Input 10 1111100000 1111100001 Output -1 -----Note----- In the first test, Naman can choose the subsequence corresponding to indices $\{2, 6\}$ and rotate it once to convert $s$ into $t$. In the second test, he can rotate the subsequence corresponding to all indices $5$ times. It can be proved, that it is the minimum required number of operations. In the last test, it is impossible to convert $s$ into $t$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 1 100 100\\n1 3 8\\n\", \"3 100 1 100\\n1 3 8\\n\", \"3 100 100 1\\n1 3 8\\n\", \"5 1 2 4\\n5 5 3 6 5\\n\", \"5 1 2 2\\n5 5 3 6 5\\n\", \"10 7 8 3\\n3 10 4 9 2 7 6 10 4 8\\n\", \"10 7122 8827 3205\\n452685204 144160553 743427456 403576146 888744473 313727757 22025193 886601085 576370788 787974081\\n\", \"100 52 92 72\\n67 100 56 15 0 92 26 74 44 61 6 44 45 19 22 19 9 85 67 78 24 61 81 99 62 84 5 71 11 65 5 97 9 72 11 81 81 64 65 91 52 21 20 53 68 91 4 75 84 88 72 27 48 23 9 65 14 6 54 55 6 1 89 78 40 45 98 45 27 51 94 72 67 81 20 19 38 64 14 40 57 21 82 99 37 92 21 22 30 14 65 93 60 8 63 60 27 30 32 11\\n\", \"10 4078 1859 607\\n160396657 831922387 330524879 901162754 847008736 781626686 496115244 470330335 824475169 620982654\\n\", \"10 1299 4362 8516\\n177913931 857265729 858027469 743427538 70328308 334897178 739985290 903278439 602456716 623851298\\n\", \"10 8521 9022 6018\\n195431204 251205289 385530059 954288541 703713298 183134963 983855337 409855471 11842043 921687235\\n\", \"10 7901 3681 1769\\n286577406 276548631 281628869 796553324 632065579 736405456 932758091 211399795 789823590 924555879\\n\"], \"outputs\": [\"12\\n\", \"9\\n\", \"4\\n\", \"4\\n\", \"3\\n\", \"57\\n\", \"4081476227653\\n\", \"95816\\n\", \"639731251326\\n\", \"3721721948256\\n\", \"9408102096630\\n\", \"2284083174243\\n\"]}", "source": "primeintellect"}
You have to restore the wall. The wall consists of $N$ pillars of bricks, the height of the $i$-th pillar is initially equal to $h_{i}$, the height is measured in number of bricks. After the restoration all the $N$ pillars should have equal heights. You are allowed the following operations: put a brick on top of one pillar, the cost of this operation is $A$; remove a brick from the top of one non-empty pillar, the cost of this operation is $R$; move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is $M$. You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes $0$. What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height? -----Input----- The first line of input contains four integers $N$, $A$, $R$, $M$ ($1 \le N \le 10^{5}$, $0 \le A, R, M \le 10^{4}$) — the number of pillars and the costs of operations. The second line contains $N$ integers $h_{i}$ ($0 \le h_{i} \le 10^{9}$) — initial heights of pillars. -----Output----- Print one integer — the minimal cost of restoration. -----Examples----- Input 3 1 100 100 1 3 8 Output 12 Input 3 100 1 100 1 3 8 Output 9 Input 3 100 100 1 1 3 8 Output 4 Input 5 1 2 4 5 5 3 6 5 Output 4 Input 5 1 2 2 5 5 3 6 5 Output 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n1 2 1 2 1\\n\", \"4\\n1 1 1 1\\n\", \"4\\n1 2 1 2\\n\", \"8\\n2 1 2 1 1 1 1 1\\n\", \"14\\n2 1 2 1 1 1 1 2 1 1 2 1 2 1\\n\", \"10\\n1 1 2 2 1 1 2 2 1 1\\n\", \"20\\n1 1 2 2 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 1\\n\", \"186\\n2 1 2 1 1 1 1 1 2 1 1 2 2 2 1 1 2 2 1 1 1 2 1 1 2 2 1 1 1 2 2 1 1 1 1 1 2 1 1 1 2 1 2 1 1 2 1 1 1 2 2 2 2 2 2 2 1 2 1 2 1 1 2 1 2 2 1 1 1 1 1 2 2 1 2 2 1 2 2 1 1 1 2 2 1 1 2 2 1 2 2 1 2 2 2 2 2 1 1 1 1 2 1 1 2 2 2 2 2 2 1 1 1 1 1 2 1 1 2 2 1 2 2 1 1 1 1 1 2 2 1 1 2 2 1 2 2 2 1 2 1 2 1 1 2 1 2 2 2 2 1 2 1 2 2 1 2 1 1 1 1 1 2 1 1 2 2 1 1 1 2 2 2 1 2 2 1 1 2 1 1 1 1 2 1 1\\n\", \"82\\n1 1 1 2 2 2 2 1 1 1 2 2 2 2 1 1 1 2 2 2 1 1 1 1 2 2 2 1 1 1 1 2 2 2 2 1 1 1 1 2 2 2 1 1 1 2 2 2 2 1 1 1 1 2 2 2 1 1 1 2 2 2 1 1 1 2 2 2 2 1 1 1 2 2 2 1 1 1 1 2 2 2\\n\", \"83\\n1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 2 1 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 2 2 2 2 2 2 1 1 1 1 1 1 1\\n\", \"1\\n1\\n\", \"1\\n2\\n\"], \"outputs\": [\"2\\n1 3\\n3 1\\n\", \"3\\n1 4\\n2 2\\n4 1\\n\", \"0\\n\", \"3\\n1 6\\n2 3\\n6 1\\n\", \"3\\n1 9\\n3 3\\n9 1\\n\", \"4\\n1 6\\n2 3\\n3 2\\n6 1\\n\", \"0\\n\", \"8\\n1 100\\n2 50\\n6 11\\n8 8\\n19 4\\n25 3\\n40 2\\n100 1\\n\", \"0\\n\", \"5\\n1 45\\n3 10\\n3 15\\n4 7\\n45 1\\n\", \"1\\n1 1\\n\", \"1\\n1 1\\n\"]}", "source": "primeintellect"}
Petya and Gena love playing table tennis. A single match is played according to the following rules: a match consists of multiple sets, each set consists of multiple serves. Each serve is won by one of the players, this player scores one point. As soon as one of the players scores t points, he wins the set; then the next set starts and scores of both players are being set to 0. As soon as one of the players wins the total of s sets, he wins the match and the match is over. Here s and t are some positive integer numbers. To spice it up, Petya and Gena choose new numbers s and t before every match. Besides, for the sake of history they keep a record of each match: that is, for each serve they write down the winner. Serve winners are recorded in the chronological order. In a record the set is over as soon as one of the players scores t points and the match is over as soon as one of the players wins s sets. Petya and Gena have found a record of an old match. Unfortunately, the sequence of serves in the record isn't divided into sets and numbers s and t for the given match are also lost. The players now wonder what values of s and t might be. Can you determine all the possible options? -----Input----- The first line contains a single integer n — the length of the sequence of games (1 ≤ n ≤ 10^5). The second line contains n space-separated integers a_{i}. If a_{i} = 1, then the i-th serve was won by Petya, if a_{i} = 2, then the i-th serve was won by Gena. It is not guaranteed that at least one option for numbers s and t corresponds to the given record. -----Output----- In the first line print a single number k — the number of options for numbers s and t. In each of the following k lines print two integers s_{i} and t_{i} — the option for numbers s and t. Print the options in the order of increasing s_{i}, and for equal s_{i} — in the order of increasing t_{i}. -----Examples----- Input 5 1 2 1 2 1 Output 2 1 3 3 1 Input 4 1 1 1 1 Output 3 1 4 2 2 4 1 Input 4 1 2 1 2 Output 0 Input 8 2 1 2 1 1 1 1 1 Output 3 1 6 2 3 6 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n1 2 3 4 5\\n\", \"3\\n1 2 4\\n\", \"3\\n1 20 22\\n\", \"10\\n109070199 215498062 361633800 406156967 452258663 530571268 670482660 704334662 841023955 967424642\\n\", \"30\\n61 65 67 71 73 75 77 79 129 131 135 137 139 141 267 520 521 522 524 526 1044 1053 6924600 32125372 105667932 109158064 192212084 202506108 214625360 260071380\\n\", \"40\\n6 7 10 11 18 19 33 65 129 258 514 515 1026 2049 4741374 8220406 14324390 17172794 17931398 33354714 34796238 38926670 39901570 71292026 72512934 77319030 95372470 102081830 114152702 120215390 133853238 134659386 159128594 165647058 219356350 225884742 236147130 240926050 251729234 263751314\\n\", \"1\\n536870912\\n\", \"1\\n1\\n\", \"1\\n536870911\\n\", \"2\\n536870911 536870912\\n\", \"38\\n37750369 37750485 37750546 37751012 37751307 37751414 37751958 37751964 37752222 37752448 75497637 75497768 75497771 75498087 75498145 75498177 75498298 75498416 75498457 150994987 150994994 150994999 150995011 150995012 150995015 150995016 150995023 150995040 150995053 805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\\n\", \"39\\n37749932 37750076 37750391 37750488 37750607 37750812 37750978 37751835 37752173 37752254 75497669 75497829 75497852 75498044 75498061 75498155 75498198 75498341 75498382 75498465 150994988 150994989 150995009 150995019 150995024 150995030 150995031 150995069 150995072 805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\\n\"], \"outputs\": [\"2\\n4 5\\n\", \"1\\n4\\n\", \"2\\n20 22\\n\", \"6\\n361633800 406156967 452258663 530571268 841023955 967424642\\n\", \"8\\n520 521 522 524 526 109158064 202506108 260071380\\n\", \"13\\n2049 4741374 8220406 17172794 17931398 38926670 39901570 77319030 134659386 159128594 219356350 225884742 240926050\\n\", \"1\\n536870912\\n\", \"1\\n1\\n\", \"1\\n536870911\\n\", \"1\\n536870912\\n\", \"9\\n805306375 805306377 805306379 805306387 805306389 805306390 805306392 805306396 805306400\\n\", \"10\\n805306369 805306373 805306375 805306379 805306380 805306384 805306387 805306389 805306398 805306400\\n\"]}", "source": "primeintellect"}
Vasily the bear has got a sequence of positive integers a_1, a_2, ..., a_{n}. Vasily the Bear wants to write out several numbers on a piece of paper so that the beauty of the numbers he wrote out was maximum. The beauty of the written out numbers b_1, b_2, ..., b_{k} is such maximum non-negative integer v, that number b_1 and b_2 and ... and b_{k} is divisible by number 2^{v} without a remainder. If such number v doesn't exist (that is, for any non-negative integer v, number b_1 and b_2 and ... and b_{k} is divisible by 2^{v} without a remainder), the beauty of the written out numbers equals -1. Tell the bear which numbers he should write out so that the beauty of the written out numbers is maximum. If there are multiple ways to write out the numbers, you need to choose the one where the bear writes out as many numbers as possible. Here expression x and y means applying the bitwise AND operation to numbers x and y. In programming languages C++ and Java this operation is represented by "&", in Pascal — by "and". -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_1 < a_2 < ... < a_{n} ≤ 10^9). -----Output----- In the first line print a single integer k (k > 0), showing how many numbers to write out. In the second line print k integers b_1, b_2, ..., b_{k} — the numbers to write out. You are allowed to print numbers b_1, b_2, ..., b_{k} in any order, but all of them must be distinct. If there are multiple ways to write out the numbers, choose the one with the maximum number of numbers to write out. If there still are multiple ways, you are allowed to print any of them. -----Examples----- Input 5 1 2 3 4 5 Output 2 4 5 Input 3 1 2 4 Output 1 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"0\\n1\\n\", \"2\\n1\\n\", \"0\\n3\\n\", \"1\\n1\\n\", \"3\\n7\\n\", \"37\\n29\\n\", \"1\\n0\\n\", \"0\\n1\\n\", \"0\\n0\\n\", \"2\\n0\\n\", \"5\\n0\\n\", \"100\\n4\\n\"], \"outputs\": [\"2 1 \\n\", \"2 3 4 1 \\n\", \"4 3 2 1 \\n\", \"2 3 1 \\n\", \"8 9 10 11 7 6 5 4 3 2 1 \\n\", \"30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 \\n\", \"1 2 \\n\", \"2 1 \\n\", \"1 \\n\", \"1 2 3 \\n\", \"1 2 3 4 5 6 \\n\", \"5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 4 3 2 1 \\n\"]}", "source": "primeintellect"}
Hiking club "Up the hill" just returned from a walk. Now they are trying to remember which hills they've just walked through. It is known that there were N stops, all on different integer heights between 1 and N kilometers (inclusive) above the sea level. On the first day they've traveled from the first stop to the second stop, on the second day they've traveled from the second to the third and so on, and on the last day they've traveled from the stop N - 1 to the stop N and successfully finished their expedition. They are trying to find out which heights were their stops located at. They have an entry in a travel journal specifying how many days did they travel up the hill, and how many days did they walk down the hill. Help them by suggesting some possible stop heights satisfying numbers from the travel journal. -----Input----- In the first line there is an integer non-negative number A denoting the number of days of climbing up the hill. Second line contains an integer non-negative number B — the number of days of walking down the hill (A + B + 1 = N, 1 ≤ N ≤ 100 000). -----Output----- Output N space-separated distinct integers from 1 to N inclusive, denoting possible heights of the stops in order of visiting. -----Examples----- Input 0 1 Output 2 1 Input 2 1 Output 1 3 4 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5 1 2\\n-2 1\\n2 1\\n3 -1\\n5 -1\\n7 -1\\n\", \"4 10 1\\n-20 1\\n-10 -1\\n0 1\\n10 -1\\n\", \"1 100000000 98765432\\n73740702 1\\n\", \"10 2 3\\n-1 -1\\n-4 1\\n-6 -1\\n1 1\\n10 -1\\n-8 -1\\n6 1\\n8 1\\n4 -1\\n-10 -1\\n\", \"3 100000000 100000000\\n-100000000 1\\n100000000 1\\n0 -1\\n\", \"9 25000000 989\\n-100000000 -1\\n-75000000 1\\n75000000 1\\n50000000 -1\\n-50000000 1\\n0 1\\n25000000 1\\n-25000000 -1\\n100000000 -1\\n\", \"2 5 1\\n-2 1\\n5 -1\\n\", \"2 5 1\\n-9 -1\\n-2 1\\n\", \"3 4 5\\n9 1\\n-4 1\\n-8 -1\\n\", \"5 1 1\\n-6 1\\n15 1\\n-7 1\\n-13 -1\\n12 -1\\n\", \"50 1 19\\n-5213 -1\\n2021 -1\\n-4479 1\\n1569 -1\\n1618 1\\n-8318 1\\n3854 1\\n8190 -1\\n9162 1\\n8849 1\\n-5545 -1\\n-7898 -1\\n728 1\\n-2175 -1\\n6453 -1\\n2999 1\\n4716 1\\n-2192 -1\\n7938 -1\\n1910 -1\\n-6863 -1\\n5230 -1\\n-2782 -1\\n-2587 -1\\n-3389 1\\n-332 -1\\n5915 1\\n-2604 1\\n-8907 1\\n-2019 1\\n2992 1\\n-3279 -1\\n6720 1\\n4332 1\\n8789 -1\\n2003 1\\n-8046 -1\\n-594 -1\\n-4133 -1\\n-7954 -1\\n-6270 -1\\n4042 -1\\n3650 1\\n-8569 1\\n2529 -1\\n266 -1\\n3405 -1\\n-9753 1\\n1205 -1\\n6437 -1\\n\", \"50 100 40\\n4843 -1\\n7653 1\\n5391 1\\n-1651 1\\n-8530 -1\\n9770 1\\n2721 1\\n7321 1\\n-3636 -1\\n-1525 -1\\n-3060 1\\n1877 -1\\n3771 -1\\n-7651 1\\n581 -1\\n1127 -1\\n6966 -1\\n-6089 1\\n1465 -1\\n3147 -1\\n-6927 -1\\n4477 1\\n-6535 1\\n5991 -1\\n-2740 1\\n5021 1\\n-7761 -1\\n4626 1\\n9958 1\\n4275 1\\n5695 1\\n8835 -1\\n7791 -1\\n189 -1\\n-170 1\\n-4468 -1\\n-708 1\\n34 -1\\n-9068 1\\n6424 -1\\n-2066 -1\\n-7367 1\\n6224 1\\n3329 1\\n-1809 -1\\n7105 1\\n-4607 -1\\n-3174 -1\\n-9782 -1\\n1350 -1\\n\"], \"outputs\": [\"4\\n\", \"1\\n\", \"0\\n\", \"5\\n\", \"1\\n\", \"11\\n\", \"1\\n\", \"0\\n\", \"0\\n\", \"2\\n\", \"262\\n\", \"253\\n\"]}", "source": "primeintellect"}
Gathering darkness shrouds the woods and the world. The moon sheds its light on the boat and the river. "To curtain off the moonlight should be hardly possible; the shades present its mellow beauty and restful nature." Intonates Mino. "See? The clouds are coming." Kanno gazes into the distance. "That can't be better," Mino turns to Kanno. The sky can be seen as a one-dimensional axis. The moon is at the origin whose coordinate is $0$. There are $n$ clouds floating in the sky. Each cloud has the same length $l$. The $i$-th initially covers the range of $(x_i, x_i + l)$ (endpoints excluded). Initially, it moves at a velocity of $v_i$, which equals either $1$ or $-1$. Furthermore, no pair of clouds intersect initially, that is, for all $1 \leq i \lt j \leq n$, $\lvert x_i - x_j \rvert \geq l$. With a wind velocity of $w$, the velocity of the $i$-th cloud becomes $v_i + w$. That is, its coordinate increases by $v_i + w$ during each unit of time. Note that the wind can be strong and clouds can change their direction. You are to help Mino count the number of pairs $(i, j)$ ($i < j$), such that with a proper choice of wind velocity $w$ not exceeding $w_\mathrm{max}$ in absolute value (possibly negative and/or fractional), the $i$-th and $j$-th clouds both cover the moon at the same future moment. This $w$ doesn't need to be the same across different pairs. -----Input----- The first line contains three space-separated integers $n$, $l$, and $w_\mathrm{max}$ ($1 \leq n \leq 10^5$, $1 \leq l, w_\mathrm{max} \leq 10^8$) — the number of clouds, the length of each cloud and the maximum wind speed, respectively. The $i$-th of the following $n$ lines contains two space-separated integers $x_i$ and $v_i$ ($-10^8 \leq x_i \leq 10^8$, $v_i \in \{-1, 1\}$) — the initial position and the velocity of the $i$-th cloud, respectively. The input guarantees that for all $1 \leq i \lt j \leq n$, $\lvert x_i - x_j \rvert \geq l$. -----Output----- Output one integer — the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity $w$. -----Examples----- Input 5 1 2 -2 1 2 1 3 -1 5 -1 7 -1 Output 4 Input 4 10 1 -20 1 -10 -1 0 1 10 -1 Output 1 -----Note----- In the first example, the initial positions and velocities of clouds are illustrated below. [Image] The pairs are: $(1, 3)$, covering the moon at time $2.5$ with $w = -0.4$; $(1, 4)$, covering the moon at time $3.5$ with $w = -0.6$; $(1, 5)$, covering the moon at time $4.5$ with $w = -0.7$; $(2, 5)$, covering the moon at time $2.5$ with $w = -2$. Below is the positions of clouds at time $2.5$ with $w = -0.4$. At this moment, the $1$-st and $3$-rd clouds both cover the moon. [Image] In the second example, the only pair is $(1, 4)$, covering the moon at time $15$ with $w = 0$. Note that all the times and wind velocities given above are just examples among infinitely many choices. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"HHHH\\n\", \"HQHQH\\n\", \"HHQHHQH\\n\", \"HHQQHHQQHH\\n\", \"Q\\n\", \"HHHHHHHHHHQHHH\\n\", \"HHQHQQQHHH\\n\", \"QQQQQQHQQQQQQQQQQHQQQQQQQQQQHQQQQQQQQQQHQQQQQQQQQQHQQQQQQQQQQHQQQQHQQQQQQHQQQQQQQQQQHQQQQQQQQQQHQQQQQQQQQQHQQQQ\\n\", \"QHQHHQQQQQQQQQQQHQQHQHQQQQQQHQHQQQQQQQQQQQHQQQQQQQHQQHQQHQQQQQQQQQQQQQQQQQQQQHHQQQQQQQQQQHQQQQHHQHQQHQQQQQHQQQQQQQHQQQQQHQ\\n\", \"QHQHQQHQQQQHQHHQQHQQHQHQQQQQQQHHQHHQQQHQQQQQQQQHQQQQQHQQHHQQHQQHQQHQQQHQQHQQHQQQQQQQQQHQQQQQQHQHQQQQQHQQQQHHQQQQQQQQQQQQQQQQHQQHQQQQH\\n\", \"HQQQHQQHQHQQQQHQQQHQHQHQQQHQQQQHQQHHQQQQQHQQQQHQQQQQHQQQQQHQQQQQHHQQQQQHQQQQHHQQHHHQHQQQQQQQQHQHQHQHQQQQQQHHHQQHHQQQHQQQHQQQQQQHHQQQHQHQQHQHHHQQ\\n\", \"HQQQQQQQHQQQQHQHQQQHHQHHHQQHQQQQHHQHHQHHHHHHQQQQQQQQHHQQQQQHHQQQQHHHQQQQQQQQHQQQHQHQQQQQQHHHQHHQHQHHQQQQQHQQHQHQQQHQHQHHHHQQHQHQQQQQHQQQHQQQHQQHQHQQHQQQQQQ\\n\"], \"outputs\": [\"Yes\\n\", \"No\\n\", \"No\\n\", \"Yes\\n\", \"Yes\\n\", \"No\\n\", \"No\\n\", \"Yes\\n\", \"No\\n\", \"No\\n\", \"No\\n\", \"No\\n\"]}", "source": "primeintellect"}
The famous joke programming language HQ9+ has only 4 commands. In this problem we will explore its subset — a language called HQ... -----Input----- The only line of the input is a string between 1 and 10^6 characters long. -----Output----- Output "Yes" or "No". -----Examples----- Input HHHH Output Yes Input HQHQH Output No Input HHQHHQH Output No Input HHQQHHQQHH Output Yes -----Note----- The rest of the problem statement was destroyed by a stray raccoon. We are terribly sorry for the inconvenience. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"(1?1)\\n1 0\\n\", \"(2?(1?2))\\n1 1\\n\", \"((1?(5?7))?((6?2)?7))\\n3 2\\n\", \"((1?(5?7))?((6?2)?7))\\n2 3\\n\", \"((4?3)?(((2?(4?((4?((2?(3?(7?3)))?(((((3?(6?2))?3)?(((((6?6)?(1?5))?((8?8)?1))?(5?((7?((6?((((3?8)?8)?(8?5))?7))?8))?((8?(2?8))?3))))?6))?(8?(7?5)))?8)))?((((7?(4?3))?4)?5)?((1?(((2?2)?((((4?((7?6)?(1?4)))?(8?(1?(((1?3)?(((2?2)?(3?(8?(9?((2?(4?6))?(7?8))))))?(9?(7?9))))?(7?3)))))?((2?((2?((8?6)?1))?(3?1)))?(7?1)))?5))?((((6?(9?(((5?4)?7)?((5?8)?8))))?5)?7)?(2?2))))?4)))))?2)?(7?(4?((6?6)?6)))))\\n50 49\\n\", \"(((6?((2?(9?(3?(2?((3?((1?(1?(6?(9?(((6?((3?(((((6?(1?(1?((((2?(1?(1?((6?(6?(4?(8?((9?(((7?((5?(9?(3?((((7?((9?(4?(8?(((((9?(1?(((8?(6?(6?((5?(((5?(7?((((1?((1?(4?((7?(8?(((3?((((4?((((((1?((3?((5?1)?9))?3))?9)?7)?7)?2)?8))?9)?1)?6))?9)?2)))?6)))?7))?4)?1)?1)))?6)?1))?9))))?8)?7)))?9)?3)?7)?7))))?1))?9)?6)?1))))?2))?2)?6))?6)))))?4))))?2)?3)?8))))?9)?1)?3)?1))?3))?9)?4)))))?2))?7)))))?4))?1)?7)\\n50 49\\n\", \"(4?(((3?2)?((((((4?(4?((2?3)?((7?((3?3)?(6?(((2?(3?6))?6)?(((1?5)?8)?(((8?1)?(5?7))?6))))))?(3?8)))))?((8?8)?5))?(7?(8?((((8?((((2?(8?(((3?6)?8)?7)))?5)?(8?(7?((4?(3?4))?(5?((1?(2?((2?(4?7))?(((6?1)?(4?(8?1)))?(1?(3?(((2?(2?(3?(8?9))))?(((((2?4)?6)?7)?(8?9))?7))?(((9?(7?3))?2)?((((((2?8)?6)?(1?3))?1)?7)?1)))))))))?(5?(6?(9?(5?4))))))))))?7))?5)?(8?(8?((5?7)?2))))?2))))?4)?(2?((7?4)?6)))?6))?6))\\n50 49\\n\", \"8\\n0 0\\n\", \"(9?((1?(3?(5?((9?7)?(((((4?5)?2)?1)?(7?6))?(3?(9?7)))))))?(1?4)))\\n0 16\\n\", \"(((4?7)?(9?((6?(3?2))?((6?3)?7))))?((5?((7?(7?(8?3)))?(4?2)))?2))\\n16 0\\n\", \"((((3?(2?((5?(((((((5?(5?6))?(((5?8)?(8?(2?2)))?(((8?(2?(((8?2)?3)?(4?8))))?(((((5?((3?1)?((3?8)?4)))?4)?5)?(6?2))?3))?(2?(((1?((((((9?1)?4)?7)?6)?(2?((((4?((((((1?8)?8)?(9?5))?(8?(3?((5?(5?2))?9))))?8)?5))?3)?(((5?(8?(2?(6?(6?(2?((4?(8?8))?(5?2))))))))?6)?3))?1)))?((9?(7?((7?(2?1))?6)))?4)))?((1?((((3?(5?(((2?6)?(7?((5?4)?(6?8))))?((2?(5?4))?9))))?((3?9)?7))?6)?8))?(2?9)))?6)))))?1)?4)?2)?(8?4))?(6?6)))?(2?((5?(9?8))?6)))))?9)?(4?4))?((7?2)?5))\\n100 12\\n\", \"((((4?6)?9)?((5?(7?1))?(6?(4?(((((((7?3)?7)?(((((8?(6?7))?((1?2)?(5?8)))?8)?7)?4))?6)?7)?1)?((7?(2?((1?(((8?(((((2?7)?(((((3?6)?3)?(((((9?5)?7)?(1?(5?5)))?(8?((3?(1?2))?((8?5)?(7?((9?((9?8)?7))?1))))))?1))?1)?(9?2)))?((7?5)?((9?(4?(9?6)))?(8?(((5?7)?2)?(6?(3?8)))))))?(((4?(2?2))?(7?9))?((7?((((4?(3?(7?3)))?8)?3)?(5?(((9?2)?(9?((((2?(7?3))?(3?(1?(9?(6?(9?8))))))?2)?2)))?2))))?((9?((2?(2?3))?((9?((7?4)?1))?5)))?((((9?6)?3)?(4?1))?7)))))?5))?4)?4))?7)))?5))))))?1)\\n2 114\\n\"], \"outputs\": [\"2\\n\", \"1\\n\", \"18\\n\", \"16\\n\", \"423\\n\", \"422\\n\", \"439\\n\", \"8\\n\", \"11\\n\", \"85\\n\", \"550\\n\", \"86\\n\"]}", "source": "primeintellect"}
Ancient Egyptians are known to have understood difficult concepts in mathematics. The ancient Egyptian mathematician Ahmes liked to write a kind of arithmetic expressions on papyrus paper which he called as Ahmes arithmetic expression. An Ahmes arithmetic expression can be defined as: "d" is an Ahmes arithmetic expression, where d is a one-digit positive integer; "(E_1 op E_2)" is an Ahmes arithmetic expression, where E_1 and E_2 are valid Ahmes arithmetic expressions (without spaces) and op is either plus ( + ) or minus ( - ). For example 5, (1-1) and ((1+(2-3))-5) are valid Ahmes arithmetic expressions. On his trip to Egypt, Fafa found a piece of papyrus paper having one of these Ahmes arithmetic expressions written on it. Being very ancient, the papyrus piece was very worn out. As a result, all the operators were erased, keeping only the numbers and the brackets. Since Fafa loves mathematics, he decided to challenge himself with the following task: Given the number of plus and minus operators in the original expression, find out the maximum possible value for the expression on the papyrus paper after putting the plus and minus operators in the place of the original erased operators. -----Input----- The first line contains a string E (1 ≤ |E| ≤ 10^4) — a valid Ahmes arithmetic expression. All operators are erased and replaced with '?'. The second line contains two space-separated integers P and M (0 ≤ min(P, M) ≤ 100) — the number of plus and minus operators, respectively. It is guaranteed that P + M = the number of erased operators. -----Output----- Print one line containing the answer to the problem. -----Examples----- Input (1?1) 1 0 Output 2 Input (2?(1?2)) 1 1 Output 1 Input ((1?(5?7))?((6?2)?7)) 3 2 Output 18 Input ((1?(5?7))?((6?2)?7)) 2 3 Output 16 -----Note----- The first sample will be (1 + 1) = 2. The second sample will be (2 + (1 - 2)) = 1. The third sample will be ((1 - (5 - 7)) + ((6 + 2) + 7)) = 18. The fourth sample will be ((1 + (5 + 7)) - ((6 - 2) - 7)) = 16. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1 2\\nSP.-SS.S-S.S\\n\", \"4 9\\nPP.-PPPS-S.S\\nPSP-PPSP-.S.\\n.S.-S..P-SS.\\nP.S-P.PP-PSP\\n\", \"3 7\\n.S.-SSSP-..S\\nS..-.SPP-S.P\\n.S.-PPPP-PSP\\n\", \"5 6\\nPP.-PS.P-P..\\nPPS-SP..-P.P\\nP.P-....-S..\\nSPP-.P.S-.S.\\nSP.-S.PS-PPP\\n\", \"1 1\\n..S-PS..-.PP\\n\", \"2 2\\nPP.-S.SS-.S.\\nSSP-SSSS-S.S\\n\", \"10 92\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n\", \"30 1\\nPPP-PPP.-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\n\", \"1 1\\nSPS-....-P.P\\n\", \"2 1\\nSSS-S.S.-SSS\\nSSP-.PP.-S.S\\n\", \"10 54\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n\", \"30 1\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPP.-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\n\"], \"outputs\": [\"5\\nSPx-SSxS-S.S\\n\", \"15\\nPPx-PPPS-S.S\\nPSP-PPSP-xSx\\nxSx-SxxP-SSx\\nP.S-PxPP-PSP\\n\", \"13\\nxSx-SSSP-xxS\\nSxx-xSPP-S.P\\n.S.-PPPP-PSP\\n\", \"6\\nPPx-PS.P-Pxx\\nPPS-SPxx-PxP\\nP.P-....-S..\\nSPP-.P.S-.S.\\nSP.-S.PS-PPP\\n\", \"1\\nx.S-PS..-.PP\\n\", \"12\\nPPx-S.SS-xS.\\nSSP-SSSS-S.S\\n\", \"0\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxx.-....-...\\n\", \"0\\nPPP-PPPx-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\n\", \"2\\nSPS-x...-P.P\\n\", \"11\\nSSS-S.S.-SSS\\nSSP-xPP.-S.S\\n\", \"0\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-xxxx-xxx\\nxxx-x...-...\\n...-....-...\\n...-....-...\\n...-....-...\\n...-....-...\\n\", \"0\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPx-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\nPPP-PPPP-PPP\\n\"]}", "source": "primeintellect"}
В самолёте есть n рядов мест. Если смотреть на ряды сверху, то в каждом ряду есть 3 места слева, затем проход между рядами, затем 4 центральных места, затем ещё один проход между рядами, а затем ещё 3 места справа. Известно, что некоторые места уже заняты пассажирами. Всего есть два вида пассажиров — статусные (те, которые часто летают) и обычные. Перед вами стоит задача рассадить ещё k обычных пассажиров так, чтобы суммарное число соседей у статусных пассажиров было минимально возможным. Два пассажира считаются соседями, если они сидят в одном ряду и между ними нет других мест и прохода между рядами. Если пассажир является соседним пассажиром для двух статусных пассажиров, то его следует учитывать в сумме соседей дважды. -----Входные данные----- В первой строке следуют два целых числа n и k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10·n) — количество рядов мест в самолёте и количество пассажиров, которых нужно рассадить. Далее следует описание рядов мест самолёта по одному ряду в строке. Если очередной символ равен '-', то это проход между рядами. Если очередной символ равен '.', то это свободное место. Если очередной символ равен 'S', то на текущем месте будет сидеть статусный пассажир. Если очередной символ равен 'P', то на текущем месте будет сидеть обычный пассажир. Гарантируется, что количество свободных мест не меньше k. Гарантируется, что все ряды удовлетворяют описанному в условии формату. -----Выходные данные----- В первую строку выведите минимальное суммарное число соседей у статусных пассажиров. Далее выведите план рассадки пассажиров, который минимизирует суммарное количество соседей у статусных пассажиров, в том же формате, что и во входных данных. Если в свободное место нужно посадить одного из k пассажиров, выведите строчную букву 'x' вместо символа '.'. -----Примеры----- Входные данные 1 2 SP.-SS.S-S.S Выходные данные 5 SPx-SSxS-S.S Входные данные 4 9 PP.-PPPS-S.S PSP-PPSP-.S. .S.-S..P-SS. P.S-P.PP-PSP Выходные данные 15 PPx-PPPS-S.S PSP-PPSP-xSx xSx-SxxP-SSx P.S-PxPP-PSP -----Примечание----- В первом примере нужно посадить ещё двух обычных пассажиров. Для минимизации соседей у статусных пассажиров, нужно посадить первого из них на третье слева место, а второго на любое из оставшихся двух мест, так как независимо от выбора места он станет соседом двух статусных пассажиров. Изначально, у статусного пассажира, который сидит на самом левом месте уже есть сосед. Также на четвёртом и пятом местах слева сидят статусные пассажиры, являющиеся соседями друг для друга (что добавляет к сумме 2). Таким образом, после посадки ещё двух обычных пассажиров, итоговое суммарное количество соседей у статусных пассажиров станет равно пяти. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"11 3\\n\", \"5 2\\n\", \"2 2\\n\", \"3 2\\n\", \"6 2\\n\", \"999999 333334\\n\", \"9 3\\n\", \"365561 143151\\n\", \"950059 419028\\n\", \"20354 8642\\n\", \"18 6\\n\", \"10 2\\n\"], \"outputs\": [\"3 1 2 1 1 2 3 2 2 3 1\\n\", \"-1\\n\", \"-1\\n\", \"-1\\n\", \"1 1 2 2 1 2\\n\", \"-1\\n\", \"3 1 1 2 1 3 2 2 3\\n\", \"-1\\n\", \"-1\\n\", \"-1\\n\", \"1 1 5 2 1 6 4 6 3 4 5 3 3 2 6 5 4 2\\n\", \"1 1 2 1 1 2 2 1 2 2\\n\"]}", "source": "primeintellect"}
The Greatest Secret Ever consists of n words, indexed by positive integers from 1 to n. The secret needs dividing between k Keepers (let's index them by positive integers from 1 to k), the i-th Keeper gets a non-empty set of words with numbers from the set U_{i} = (u_{i}, 1, u_{i}, 2, ..., u_{i}, |U_{i}|). Here and below we'll presuppose that the set elements are written in the increasing order. We'll say that the secret is safe if the following conditions are hold: for any two indexes i, j (1 ≤ i < j ≤ k) the intersection of sets U_{i} and U_{j} is an empty set; the union of sets U_1, U_2, ..., U_{k} is set (1, 2, ..., n); in each set U_{i}, its elements u_{i}, 1, u_{i}, 2, ..., u_{i}, |U_{i}| do not form an arithmetic progression (in particular, |U_{i}| ≥ 3 should hold). Let us remind you that the elements of set (u_1, u_2, ..., u_{s}) form an arithmetic progression if there is such number d, that for all i (1 ≤ i < s) fulfills u_{i} + d = u_{i} + 1. For example, the elements of sets (5), (1, 10) and (1, 5, 9) form arithmetic progressions and the elements of sets (1, 2, 4) and (3, 6, 8) don't. Your task is to find any partition of the set of words into subsets U_1, U_2, ..., U_{k} so that the secret is safe. Otherwise indicate that there's no such partition. -----Input----- The input consists of a single line which contains two integers n and k (2 ≤ k ≤ n ≤ 10^6) — the number of words in the secret and the number of the Keepers. The numbers are separated by a single space. -----Output----- If there is no way to keep the secret safe, print a single integer "-1" (without the quotes). Otherwise, print n integers, the i-th of them representing the number of the Keeper who's got the i-th word of the secret. If there are multiple solutions, print any of them. -----Examples----- Input 11 3 Output 3 1 2 1 1 2 3 2 2 3 1 Input 5 2 Output -1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 4\\n1 2 3 4\\n\", \"5 2\\n3 2 4 1 3\\n\", \"5 4\\n3 2 4 3 5\\n\", \"4 3\\n5 6 7 8\\n\", \"5 6\\n3 7 2 1 2\\n\", \"9 5\\n5 8 7 3 10 1 4 6 3\\n\", \"2 1\\n7 2\\n\", \"56 97\\n96 81 39 97 2 75 85 17 9 90 2 31 32 10 42 87 71 100 39 81 2 38 90 81 96 7 57 23 2 25 5 62 22 61 47 94 63 83 91 51 8 93 33 65 38 50 5 64 76 57 96 19 13 100 56 39\\n\", \"87 71\\n44 88 67 57 57 80 69 69 40 32 92 54 64 51 69 54 31 53 29 42 32 85 100 90 46 56 40 46 68 81 60 42 99 89 61 96 48 42 78 95 71 67 30 42 57 82 41 76 29 79 32 62 100 89 81 55 88 90 86 54 54 31 28 67 69 49 45 54 68 77 64 32 60 60 66 66 83 57 56 89 57 82 73 86 60 61 62\\n\", \"100 67\\n82 34 100 55 38 32 97 34 100 49 49 41 48 100 74 51 53 50 46 38 35 69 93 61 96 86 43 59 90 45 52 100 48 45 63 60 52 66 83 46 66 47 74 37 56 48 42 88 39 68 38 66 77 40 60 60 92 38 45 57 63 91 85 85 89 53 64 66 99 89 49 54 48 58 94 65 78 34 78 62 95 47 64 50 84 52 98 79 57 69 39 61 92 46 63 45 90 51 79 39\\n\", \"1 1\\n1\\n\", \"1 100\\n1\\n\"], \"outputs\": [\"YES\\n1 \\n1 1 \\n1 1 2 \\n1 1 2 3 \\n\", \"NO\\n\", \"YES\\n1 1 1 \\n1 1 \\n1 1 1 2 \\n1 1 1 \\n1 1 1 2 3 \\n\", \"YES\\n1 1 1 1 1 \\n1 1 1 1 1 1 \\n1 1 1 1 1 1 2 \\n1 1 1 1 1 1 2 3 \\n\", \"YES\\n1 1 2 \\n1 1 2 3 4 5 6 \\n1 1 \\n1 \\n1 1 \\n\", \"NO\\n\", \"NO\\n\", \"NO\\n\", \"NO\\n\", \"NO\\n\", \"YES\\n1 \\n\", \"YES\\n1 \\n\"]}", "source": "primeintellect"}
There are n piles of pebbles on the table, the i-th pile contains a_{i} pebbles. Your task is to paint each pebble using one of the k given colors so that for each color c and any two piles i and j the difference between the number of pebbles of color c in pile i and number of pebbles of color c in pile j is at most one. In other words, let's say that b_{i}, c is the number of pebbles of color c in the i-th pile. Then for any 1 ≤ c ≤ k, 1 ≤ i, j ≤ n the following condition must be satisfied |b_{i}, c - b_{j}, c| ≤ 1. It isn't necessary to use all k colors: if color c hasn't been used in pile i, then b_{i}, c is considered to be zero. -----Input----- The first line of the input contains positive integers n and k (1 ≤ n, k ≤ 100), separated by a space — the number of piles and the number of colors respectively. The second line contains n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 100) denoting number of pebbles in each of the piles. -----Output----- If there is no way to paint the pebbles satisfying the given condition, output "NO" (without quotes) . Otherwise in the first line output "YES" (without quotes). Then n lines should follow, the i-th of them should contain a_{i} space-separated integers. j-th (1 ≤ j ≤ a_{i}) of these integers should be equal to the color of the j-th pebble in the i-th pile. If there are several possible answers, you may output any of them. -----Examples----- Input 4 4 1 2 3 4 Output YES 1 1 4 1 2 4 1 2 3 4 Input 5 2 3 2 4 1 3 Output NO Input 5 4 3 2 4 3 5 Output YES 1 2 3 1 3 1 2 3 4 1 3 4 1 1 2 3 4 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 3 4\\nH 2\\nV 2\\nV 3\\nV 1\\n\", \"7 6 5\\nH 4\\nV 3\\nV 5\\nH 2\\nV 1\\n\", \"2 2 1\\nV 1\\n\", \"2 2 1\\nH 1\\n\", \"2 2 2\\nV 1\\nH 1\\n\", \"2 2 2\\nH 1\\nV 1\\n\", \"10 10 10\\nV 6\\nH 8\\nV 4\\nV 8\\nH 2\\nH 5\\nV 9\\nH 7\\nH 3\\nV 7\\n\", \"5 15 10\\nH 8\\nH 9\\nV 1\\nH 2\\nH 6\\nH 4\\nH 1\\nV 2\\nH 13\\nV 3\\n\", \"15 5 10\\nV 13\\nV 10\\nV 3\\nH 2\\nV 9\\nV 7\\nV 2\\nH 1\\nV 4\\nH 3\\n\", \"2 3 1\\nH 1\\n\", \"200000 200000 1\\nH 1\\n\", \"2 4 1\\nH 2\\n\"], \"outputs\": [\"8\\n4\\n4\\n2\\n\", \"28\\n16\\n12\\n6\\n4\\n\", \"2\\n\", \"2\\n\", \"2\\n1\\n\", \"2\\n1\\n\", \"60\\n48\\n32\\n32\\n24\\n12\\n12\\n12\\n8\\n8\\n\", \"40\\n40\\n32\\n24\\n24\\n24\\n24\\n18\\n12\\n8\\n\", \"65\\n50\\n35\\n21\\n18\\n12\\n12\\n12\\n9\\n6\\n\", \"4\\n\", \"39999800000\\n\", \"4\\n\"]}", "source": "primeintellect"}
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how. In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments. After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process. Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree? -----Input----- The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000). Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts. -----Output----- After each cut print on a single line the area of the maximum available glass fragment in mm^2. -----Examples----- Input 4 3 4 H 2 V 2 V 3 V 1 Output 8 4 4 2 Input 7 6 5 H 4 V 3 V 5 H 2 V 1 Output 28 16 12 6 4 -----Note----- Picture for the first sample test: [Image] Picture for the second sample test: $\square$ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.375
{"tests": "{\"inputs\": [\"2 1 1 5\\n\", \"1 2 100 1\\n\", \"1 100 1 100\\n\", \"18 18 40 79\\n\", \"18 18 40 81\\n\", \"1 100 100 100\\n\", \"1 100 33 65\\n\", \"1 100 33 67\\n\", \"1 100 50 100\\n\", \"1 100 99 99\\n\", \"77 7 25 49\\n\", \"77 7 25 51\\n\"], \"outputs\": [\"1\\n\", \"101\\n\", \"199\\n\", \"40\\n\", \"40\\n\", \"10000\\n\", \"6468\\n\", \"6567\\n\", \"9950\\n\", \"9900\\n\", \"3406\\n\", \"3475\\n\"]}", "source": "primeintellect"}
There are two 100-story buildings, called A and B. (In this problem, the ground floor is called the first floor.) For each i = 1,\dots, 100, the i-th floor of A and that of B are connected by a corridor. Also, for each i = 1,\dots, 99, there is a corridor that connects the (i+1)-th floor of A and the i-th floor of B. You can traverse each of those corridors in both directions, and it takes you x minutes to get to the other end. Additionally, both of the buildings have staircases. For each i = 1,\dots, 99, a staircase connects the i-th and (i+1)-th floors of a building, and you need y minutes to get to an adjacent floor by taking the stairs. Find the minimum time needed to reach the b-th floor of B from the a-th floor of A. -----Constraints----- - 1 \leq a,b,x,y \leq 100 - All values in input are integers. -----Input----- Input is given from Standard Input in the following format: a b x y -----Output----- Print the minimum time needed to reach the b-th floor of B from the a-th floor of A. -----Sample Input----- 2 1 1 5 -----Sample Output----- 1 There is a corridor that directly connects the 2-nd floor of A and the 1-st floor of B, so you can travel between them in 1 minute. This is the fastest way to get there, since taking the stairs just once costs you 5 minutes. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"ABRACADABRA\\n\", \"ABBBCBDB\\n\", \"AB\\n\", \"ABBCDEFB\\n\", \"THISISATEST\\n\", \"Z\\n\", \"ZZ\\n\", \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\\n\", \"ABACBCABACACB\\n\", \"LEHLLLLLLHAFGEGLLHAFDLHHLLLLLDGGEHGGHLLLLLLLLDFLCBLLEFLLCBLLCGLEDLGGLECLDGLEHLLLGELLLEGLLLLGDLLLDALD\\n\", \"THISISTHELASTTEST\\n\"], \"outputs\": [\"3\\n\", \"3\\n\", \"1\\n\", \"3\\n\", \"3\\n\", \"1\\n\", \"2\\n\", \"1\\n\", \"4\\n\", \"49\\n\", \"3\\n\"]}", "source": "primeintellect"}
You will receive 5 points for solving this problem. Manao has invented a new operation on strings that is called folding. Each fold happens between a pair of consecutive letters and places the second part of the string above first part, running in the opposite direction and aligned to the position of the fold. Using this operation, Manao converts the string into a structure that has one more level than there were fold operations performed. See the following examples for clarity. We will denote the positions of folds with '|' characters. For example, the word "ABRACADABRA" written as "AB|RACA|DAB|RA" indicates that it has been folded three times: first, between the leftmost pair of 'B' and 'R' letters; second, between 'A' and 'D'; and third, between the rightmost pair of 'B' and 'R' letters. Here are several examples of folded strings: "ABCDEF|GHIJK" | "A|BCDEFGHIJK" | "AB|RACA|DAB|RA" | "X|XXXXX|X|X|XXXXXX" | | | XXXXXX KJIHG | KJIHGFEDCB | AR | X ABCDEF | A | DAB | X | | ACAR | XXXXX | | AB | X One last example for "ABCD|EFGH|IJ|K": K IJ HGFE ABCD Manao noticed that each folded string can be viewed as several piles of letters. For instance, in the previous example, there are four piles, which can be read as "AHI", "BGJK", "CF", and "DE" from bottom to top. Manao wonders what is the highest pile of identical letters he can build using fold operations on a given word. Note that the pile should not contain gaps and should start at the bottom level. For example, in the rightmost of the four examples above, none of the piles would be considered valid since each of them has gaps, starts above the bottom level, or both. -----Input----- The input will consist of one line containing a single string of n characters with 1 ≤ n ≤ 1000 and no spaces. All characters of the string will be uppercase letters. This problem doesn't have subproblems. You will get 5 points for the correct submission. -----Output----- Print a single integer — the size of the largest pile composed of identical characters that can be seen in a valid result of folding operations on the given string. -----Examples----- Input ABRACADABRA Output 3 Input ABBBCBDB Output 3 Input AB Output 1 -----Note----- Consider the first example. Manao can create a pile of three 'A's using the folding "AB|RACAD|ABRA", which results in the following structure: ABRA DACAR AB In the second example, Manao can create a pile of three 'B's using the following folding: "AB|BB|CBDB". CBDB BB AB Another way for Manao to create a pile of three 'B's with "ABBBCBDB" is the following folding: "AB|B|BCBDB". BCBDB B AB In the third example, there are no folds performed and the string is just written in one line. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"6 7\\n1 2\\n2 3\\n3 4\\n4 6\\n6 5\\n4 4\\n3 6\\n4 2 7 3\\n1 10 2\\n3 8 2\\n5 1 0\\n6 5 4\\n3 7\\n5 2\\n\", \"1 1\\n1 1\\n1 2 498328473 730315543\\n1 267535146 337021959\\n1 340728578\\n1 82073826\\n\", \"1 1\\n1 1\\n2 1 709189476 658667824\\n1 511405192 843598992\\n1 638564514 680433292\\n1 584582554\\n\", \"1 1\\n1 1\\n2 2 551454259 292052813\\n1 755275238 645143315\\n1 936400451 457379982\\n1 873148501\\n1 181471857\\n\", \"1 0\\n1 1 760669141 361439344\\n1 515372386 379329282\\n1 446687639\\n\", \"1 0\\n1 1 154608701 257538154\\n1 148757376 932599775\\n1 953264671\\n\", \"1 0\\n1 1 179952043 785040744\\n1 77109657 780837560\\n1 754808995\\n\", \"1 1\\n1 1\\n2 1 657705832 300429230\\n1 435788130 187757098\\n1 464180683 238039721\\n1 101041215\\n\", \"1 1\\n1 1\\n2 2 205003324 933814220\\n1 974625469 989301423\\n1 762016619 720019121\\n1 389607162\\n1 162417396\\n\", \"1 0\\n1 1 288141798 31112026\\n1 862166501 30583621\\n1 790845747\\n\", \"1 0\\n1 1 682081360 558614617\\n1 790518782 878821407\\n1 297422778\\n\"], \"outputs\": [\"12\\n\", \"498328473\\n\", \"709189476\\n\", \"584105626\\n\", \"361439344\\n\", \"0\\n\", \"0\\n\", \"600858460\\n\", \"410006648\\n\", \"31112026\\n\", \"558614617\\n\"]}", "source": "primeintellect"}
Thanks to the Doctor's help, the rebels managed to steal enough gold to launch a full-scale attack on the Empire! However, Darth Vader is looking for revenge and wants to take back his gold. The rebels have hidden the gold in various bases throughout the galaxy. Darth Vader and the Empire are looking to send out their spaceships to attack these bases. The galaxy can be represented as an undirected graph with $n$ planets (nodes) and $m$ wormholes (edges), each connecting two planets. A total of $s$ empire spaceships and $b$ rebel bases are located at different planets in the galaxy. Each spaceship is given a location $x$, denoting the index of the planet on which it is located, an attacking strength $a$, and a certain amount of fuel $f$. Each base is given a location $x$, and a defensive strength $d$. A spaceship can attack a base if both of these conditions hold: the spaceship's attacking strength is greater or equal than the defensive strength of the base the spaceship's fuel is greater or equal to the shortest distance, computed as the number of wormholes, between the spaceship's planet and the base's planet Vader is very particular about his attacking formations. He requires that each spaceship is to attack at most one base and that each base is to be attacked by at most one spaceship. Vader knows that the rebels have hidden $k$ gold in each base, so he will assign the spaceships to attack bases in such a way that maximizes the number of bases attacked. Therefore, for each base that is attacked, the rebels lose $k$ gold. However, the rebels have the ability to create any number of dummy bases. With the Doctor's help, these bases would exist beyond space and time, so all spaceship can reach them and attack them. Moreover, a dummy base is designed to seem irresistible: that is, it will always be attacked by some spaceship. Of course, dummy bases do not contain any gold, but creating such a dummy base costs $h$ gold. What is the minimum gold the rebels can lose if they create an optimal number of dummy bases? -----Input----- The first line contains two integers $n$ and $m$ ($1 \leq n \leq 100$, $0 \leq m \leq 10000$), the number of nodes and the number of edges, respectively. The next $m$ lines contain two integers $u$ and $v$ ($1 \leq u$, $v \leq n$) denoting an undirected edge between the two nodes. The next line contains four integers $s$, $b$, $k$ and $h$ ($1 \leq s$, $b \leq 1000$, $0 \leq k$, $h \leq 10^9$), the number of spaceships, the number of bases, the cost of having a base attacked, and the cost of creating a dummy base, respectively. The next $s$ lines contain three integers $x$, $a$, $f$ ($1 \leq x \leq n$, $0 \leq a$, $f \leq 10^9$), denoting the location, attack, and fuel of the spaceship. The next $b$ lines contain two integers $x$, $d$ ($1 \leq x \leq n$, $0 \leq d \leq 10^9$), denoting the location and defence of the base. -----Output----- Print a single integer, the minimum cost in terms of gold. -----Example----- Input 6 7 1 2 2 3 3 4 4 6 6 5 4 4 3 6 4 2 7 3 1 10 2 3 8 2 5 1 0 6 5 4 3 7 5 2 Output 12 -----Note----- One way to minimize the cost is to build $4$ dummy bases, for a total cost of $4 \times 3 = 12$. One empire spaceship will be assigned to attack each of these dummy bases, resulting in zero actual bases attacked. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 1 3\\n1 2 3\\n1 1\\n\", \"3 3 6\\n1 2 3\\n1 1\\n\", \"1 1 10000\\n10001\\n\", \"3 2 3\\n1 2 3\\n1 1\\n\", \"6 3 100\\n1 100 1 1 1 1\\n1 1 2 3 3\\n\", \"2 1 1\\n1 1\\n1\\n\", \"2 100000 1000000000000000000\\n1000000000 1000000000\\n1\\n\", \"3 3 2200000000\\n1000000000 1000000000 1000000000\\n1 2\\n\", \"7 2 91648\\n7 3 6 2 5 4 1\\n1 1 1 1 3 2\\n\", \"2 2 8\\n8 1\\n1\\n\", \"1 100000 1\\n1\\n\"], \"outputs\": [\"3\", \"2\", \"-1\", \"2\", \"4\", \"2\", \"1\", \"2\", \"4\", \"2\", \"1\"]}", "source": "primeintellect"}
You are given a rooted tree on $n$ vertices, its root is the vertex number $1$. The $i$-th vertex contains a number $w_i$. Split it into the minimum possible number of vertical paths in such a way that each path contains no more than $L$ vertices and the sum of integers $w_i$ on each path does not exceed $S$. Each vertex should belong to exactly one path. A vertical path is a sequence of vertices $v_1, v_2, \ldots, v_k$ where $v_i$ ($i \ge 2$) is the parent of $v_{i - 1}$. -----Input----- The first line contains three integers $n$, $L$, $S$ ($1 \le n \le 10^5$, $1 \le L \le 10^5$, $1 \le S \le 10^{18}$) — the number of vertices, the maximum number of vertices in one path and the maximum sum in one path. The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($1 \le w_i \le 10^9$) — the numbers in the vertices of the tree. The third line contains $n - 1$ integers $p_2, \ldots, p_n$ ($1 \le p_i < i$), where $p_i$ is the parent of the $i$-th vertex in the tree. -----Output----- Output one number  — the minimum number of vertical paths. If it is impossible to split the tree, output $-1$. -----Examples----- Input 3 1 3 1 2 3 1 1 Output 3 Input 3 3 6 1 2 3 1 1 Output 2 Input 1 1 10000 10001 Output -1 -----Note----- In the first sample the tree is split into $\{1\},\ \{2\},\ \{3\}$. In the second sample the tree is split into $\{1,\ 2\},\ \{3\}$ or $\{1,\ 3\},\ \{2\}$. In the third sample it is impossible to split the tree. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n4 4 4\\n\", \"5\\n2 3 5 5\\n\", \"2\\n2\\n\", \"10\\n2 10 8 7 8 8 10 9 10\\n\", \"3\\n3 3\\n\", \"4\\n3 3 4\\n\", \"5\\n4 4 4 5\\n\", \"6\\n3 3 6 6 6\\n\", \"7\\n7 3 4 6 6 7\\n\", \"8\\n3 7 7 8 8 7 8\\n\", \"9\\n2 9 7 6 9 7 8 9\\n\"], \"outputs\": [\"6\\n\", \"17\\n\", \"1\\n\", \"63\\n\", \"3\\n\", \"8\\n\", \"13\\n\", \"21\\n\", \"35\\n\", \"37\\n\", \"52\\n\"]}", "source": "primeintellect"}
Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to a_{i} inclusive. No tickets are sold at the last station. Let ρ_{i}, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρ_{i}, j among all pairs 1 ≤ i < j ≤ n. -----Input----- The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations. The second line contains n - 1 integer a_{i} (i + 1 ≤ a_{i} ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to a_{i} inclusive. -----Output----- Print the sum of ρ_{i}, j among all pairs of 1 ≤ i < j ≤ n. -----Examples----- Input 4 4 4 4 Output 6 Input 5 2 3 5 5 Output 17 -----Note----- In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6. Consider the second sample: ρ_{1, 2} = 1 ρ_{1, 3} = 2 ρ_{1, 4} = 3 ρ_{1, 5} = 3 ρ_{2, 3} = 1 ρ_{2, 4} = 2 ρ_{2, 5} = 2 ρ_{3, 4} = 1 ρ_{3, 5} = 1 ρ_{4, 5} = 1 Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"8\\n20 1000\\n32 37\\n40 1000\\n45 50\\n16 16\\n16 16\\n14 1000\\n2 1000\\n\", \"7\\n4 4\\n4 4\\n4 4\\n4 4\\n4 4\\n4 4\\n5 5\\n\", \"7\\n14000000003 1000000000000000000\\n81000000000 88000000000\\n5000000000 7000000000\\n15000000000 39000000000\\n46000000000 51000000000\\n0 1000000000\\n0 0\\n\", \"2\\n100 150\\n5 100000\\n\", \"9\\n4 70\\n32 56\\n32 65\\n77 78\\n5 29\\n72 100\\n0 55\\n42 52\\n66 72\\n\", \"3\\n1 2\\n12 19\\n25 45\\n\", \"5\\n2 23\\n1 13\\n3 9\\n0 20\\n6 7\\n\", \"10\\n19 22\\n10 77\\n3 52\\n16 42\\n25 67\\n14 42\\n44 85\\n37 39\\n36 62\\n6 85\\n\", \"15\\n143 698\\n269 879\\n100 728\\n86 855\\n368 478\\n174 368\\n442 980\\n812 825\\n121 220\\n137 198\\n599 706\\n423 586\\n96 647\\n177 439\\n54 620\\n\", \"3\\n1000 1000\\n1001 1001\\n700 1000000\\n\", \"5\\n4 100\\n10 11\\n10 11\\n3 3\\n3 3\\n\"], \"outputs\": [\"3\\n\", \"2\\n\", \"2\\n\", \"1\\n\", \"7\\n\", \"3\\n\", \"3\\n\", \"4\\n\", \"9\\n\", \"1\\n\", \"2\\n\"]}", "source": "primeintellect"}
One tradition of ACM-ICPC contests is that a team gets a balloon for every solved problem. We assume that the submission time doesn't matter and teams are sorted only by the number of balloons they have. It means that one's place is equal to the number of teams with more balloons, increased by 1. For example, if there are seven teams with more balloons, you get the eight place. Ties are allowed. You should know that it's important to eat before a contest. If the number of balloons of a team is greater than the weight of this team, the team starts to float in the air together with their workstation. They eventually touch the ceiling, what is strictly forbidden by the rules. The team is then disqualified and isn't considered in the standings. A contest has just finished. There are n teams, numbered 1 through n. The i-th team has t_{i} balloons and weight w_{i}. It's guaranteed that t_{i} doesn't exceed w_{i} so nobody floats initially. Limak is a member of the first team. He doesn't like cheating and he would never steal balloons from other teams. Instead, he can give his balloons away to other teams, possibly making them float. Limak can give away zero or more balloons of his team. Obviously, he can't give away more balloons than his team initially has. What is the best place Limak can get? -----Input----- The first line of the standard input contains one integer n (2 ≤ n ≤ 300 000) — the number of teams. The i-th of n following lines contains two integers t_{i} and w_{i} (0 ≤ t_{i} ≤ w_{i} ≤ 10^18) — respectively the number of balloons and the weight of the i-th team. Limak is a member of the first team. -----Output----- Print one integer denoting the best place Limak can get. -----Examples----- Input 8 20 1000 32 37 40 1000 45 50 16 16 16 16 14 1000 2 1000 Output 3 Input 7 4 4 4 4 4 4 4 4 4 4 4 4 5 5 Output 2 Input 7 14000000003 1000000000000000000 81000000000 88000000000 5000000000 7000000000 15000000000 39000000000 46000000000 51000000000 0 1000000000 0 0 Output 2 -----Note----- In the first sample, Limak has 20 balloons initially. There are three teams with more balloons (32, 40 and 45 balloons), so Limak has the fourth place initially. One optimal strategy is: Limak gives 6 balloons away to a team with 32 balloons and weight 37, which is just enough to make them fly. Unfortunately, Limak has only 14 balloons now and he would get the fifth place. Limak gives 6 balloons away to a team with 45 balloons. Now they have 51 balloons and weight 50 so they fly and get disqualified. Limak gives 1 balloon to each of two teams with 16 balloons initially. Limak has 20 - 6 - 6 - 1 - 1 = 6 balloons. There are three other teams left and their numbers of balloons are 40, 14 and 2. Limak gets the third place because there are two teams with more balloons. In the second sample, Limak has the second place and he can't improve it. In the third sample, Limak has just enough balloons to get rid of teams 2, 3 and 5 (the teams with 81 000 000 000, 5 000 000 000 and 46 000 000 000 balloons respectively). With zero balloons left, he will get the second place (ex-aequo with team 6 and team 7). Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1\\n1\\n2\\n7\\n\", \"2\\n1 2\\n2 3\\n11\\n\", \"100\\n1 8 10 6 5 3 2 3 4 2 3 7 1 1 5 1 4 1 8 1 5 5 6 5 3 7 4 5 5 3 8 7 8 6 8 9 10 7 8 5 8 9 1 3 7 2 6 1 7 7 2 8 1 5 4 2 10 4 9 8 1 10 1 5 9 8 1 9 5 1 5 7 1 6 7 8 8 2 2 3 3 7 2 10 6 3 6 3 5 3 10 4 4 6 9 9 3 2 6 6\\n4 3 8 4 4 2 4 6 6 3 3 5 8 4 1 6 2 7 6 1 6 10 7 9 2 9 2 9 10 1 1 1 1 7 4 5 3 6 8 6 10 4 3 4 8 6 5 3 1 2 2 4 1 9 1 3 1 9 6 8 9 4 8 8 4 2 1 4 6 2 6 3 4 7 7 7 8 10 7 8 8 6 4 10 10 7 4 5 5 8 3 8 2 8 6 4 5 2 10 2\\n29056621\\n\", \"100\\n6 1 10 4 8 7 7 3 2 4 6 3 2 5 3 7 1 6 9 8 3 10 1 6 8 1 4 2 5 6 3 5 4 6 3 10 2 8 10 4 2 6 4 5 3 1 8 6 9 8 5 2 7 1 10 5 10 2 9 1 6 4 9 5 2 4 6 7 10 10 10 6 6 9 2 3 3 1 2 4 1 6 9 8 4 10 10 9 9 2 5 7 10 1 9 7 6 6 4 5\\n4 9 2 5 5 4 6 9 1 2 6 3 8 9 4 4 4 3 1 3 6 2 9 1 10 6 5 1 9 10 6 2 10 9 8 7 8 2 1 5 8 4 3 2 10 9 5 7 1 8 4 4 4 2 1 3 4 5 3 6 10 3 8 9 5 6 3 9 3 6 5 1 9 1 4 3 8 4 4 8 10 6 4 9 8 4 2 3 1 9 9 1 4 1 8 4 7 9 10 9\\n66921358\\n\", \"100\\n2 2 10 3 5 6 4 7 9 8 2 7 5 5 1 7 5 9 2 2 10 3 6 10 9 9 10 7 3 9 7 8 8 3 9 3 9 3 3 6 3 7 9 9 7 10 9 1 1 3 6 2 9 5 9 9 6 2 6 5 6 8 2 10 1 1 6 8 8 4 5 2 6 8 8 5 9 2 3 3 7 7 10 5 4 2 10 6 7 6 5 4 10 6 10 3 9 9 1 5\\n3 5 6 4 2 3 2 9 3 8 3 1 10 7 4 3 6 9 3 5 9 5 3 10 4 7 9 7 4 3 3 6 9 8 1 1 10 9 1 6 8 8 8 2 1 6 10 1 8 6 3 5 7 7 10 4 6 6 9 1 5 3 5 10 4 4 1 7 9 7 5 10 6 5 4 1 9 6 4 5 7 3 1 10 2 10 6 6 1 10 7 5 1 4 2 9 2 7 3 10\\n727992321\\n\", \"100\\n2 5 5 6 5 2 8 10 6 1 5 3 10 3 8 6 4 5 7 9 7 1 3 3 5 2 3 7 9 3 7 2 7 6 7 10 5 9 2 4 8 2 3 8 6 6 8 4 1 2 10 5 2 8 4 3 1 3 8 3 2 4 4 6 8 1 9 8 9 9 1 7 1 9 2 4 6 2 1 9 2 7 9 6 6 7 1 9 3 1 6 10 3 9 10 5 3 3 9 8\\n6 5 3 1 3 3 8 6 5 4 2 3 9 3 9 9 10 5 10 6 7 8 8 7 8 4 2 4 4 9 1 3 1 5 8 4 8 9 7 9 7 8 4 9 9 9 4 2 9 1 3 10 6 4 5 3 2 8 1 5 1 8 10 10 3 3 7 1 2 4 4 3 3 5 9 8 9 8 5 9 4 8 10 6 7 4 1 9 4 7 1 8 3 3 5 9 8 6 5 4\\n608692736\\n\", \"2\\n1 2\\n1 2\\n4\\n\", \"4\\n1 2 3 4\\n4 3 2 1\\n1009\\n\", \"5\\n1 2 3 3 5\\n1 2 3 5 3\\n12\\n\", \"1\\n1000000000\\n1000000000\\n2\\n\", \"2\\n1 2\\n2 2\\n4\\n\"], \"outputs\": [\"1\\n\", \"2\\n\", \"5236748\\n\", \"12938646\\n\", \"340960284\\n\", \"550164992\\n\", \"1\\n\", \"16\\n\", \"0\\n\", \"1\\n\", \"3\\n\"]}", "source": "primeintellect"}
Little Dima has two sequences of points with integer coordinates: sequence (a_1, 1), (a_2, 2), ..., (a_{n}, n) and sequence (b_1, 1), (b_2, 2), ..., (b_{n}, n). Now Dima wants to count the number of distinct sequences of points of length 2·n that can be assembled from these sequences, such that the x-coordinates of points in the assembled sequence will not decrease. Help him with that. Note that each element of the initial sequences should be used exactly once in the assembled sequence. Dima considers two assembled sequences (p_1, q_1), (p_2, q_2), ..., (p_{2·}n, q_{2·}n) and (x_1, y_1), (x_2, y_2), ..., (x_{2·}n, y_{2·}n) distinct, if there is such i (1 ≤ i ≤ 2·n), that (p_{i}, q_{i}) ≠ (x_{i}, y_{i}). As the answer can be rather large, print the remainder from dividing the answer by number m. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9). The third line contains n integers b_1, b_2, ..., b_{n} (1 ≤ b_{i} ≤ 10^9). The numbers in the lines are separated by spaces. The last line contains integer m (2 ≤ m ≤ 10^9 + 7). -----Output----- In the single line print the remainder after dividing the answer to the problem by number m. -----Examples----- Input 1 1 2 7 Output 1 Input 2 1 2 2 3 11 Output 2 -----Note----- In the first sample you can get only one sequence: (1, 1), (2, 1). In the second sample you can get such sequences : (1, 1), (2, 2), (2, 1), (3, 2); (1, 1), (2, 1), (2, 2), (3, 2). Thus, the answer is 2. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n-3 -4 -2 -3 3\\n\", \"5\\n2 -3 -1 -4 3\\n\", \"6\\n94 65 -33 -43 60 -24\\n\", \"3\\n-10000 10000 -10000\\n\", \"2\\n0 0\\n\", \"7\\n-1 -2 -4 -10 6 6 5\\n\", \"10\\n-10 3 -16 -15 14 -16 13 -6 -8 18\\n\", \"50\\n-67 -84 -89 80 40 42 -38 30 74 -12 -66 27 1 11 -45 -44 2 -70 -59 -70 -59 -59 62 100 -5 1 91 79 47 -64 -51 -88 -5 37 82 87 79 46 76 47 60 57 59 -24 47 -49 -63 24 -84 -54\\n\", \"100\\n246 485 -940 -186 -841 -98 711 429 -154 164 -244 -111 886 -447 22 480 224 -132 927 812 -243 -152 -843 403 -320 -346 -407 827 645 -903 -172 540 -359 498 270 284 374 -52 -983 -164 -707 -242 -159 -825 -889 661 -629 212 849 -891 -622 810 957 897 -96 -293 -257 822 690 369 -914 212 -338 -928 -862 525 -537 782 727 665 964 -559 -675 -835 -800 254 -522 -504 239 909 638 -589 -700 907 127 -77 -748 999 152 -253 -505 889 -967 -481 -312 161 28 258 118 -870\\n\", \"2\\n-9116 9298\\n\", \"2\\n-10000 10000\\n\"], \"outputs\": [\"2\\n-3 1\\n\", \"1\\n-4 2\\n\", \"25\\n-67 34\\n\", \"5000\\n-15000 10000\\n\", \"0\\n0 0\\n\", \"3\\n-9 3\\n\", \"6\\n-20 4\\n\", \"15\\n-97 4\\n\", \"83\\n-1065 21\\n\", \"0\\n-9116 18414\\n\", \"0\\n-10000 20000\\n\"]}", "source": "primeintellect"}
The Physical education teacher at SESC is a sort of mathematician too. His most favorite topic in mathematics is progressions. That is why the teacher wants the students lined up in non-decreasing height form an arithmetic progression. To achieve the goal, the gym teacher ordered a lot of magical buns from the dining room. The magic buns come in two types: when a student eats one magic bun of the first type, his height increases by one, when the student eats one magical bun of the second type, his height decreases by one. The physical education teacher, as expected, cares about the health of his students, so he does not want them to eat a lot of buns. More precisely, he wants the maximum number of buns eaten by some student to be minimum. Help the teacher, get the maximum number of buns that some pupils will have to eat to achieve the goal of the teacher. Also, get one of the possible ways for achieving the objective, namely, the height of the lowest student in the end and the step of the resulting progression. -----Input----- The single line contains integer n (2 ≤ n ≤ 10^3) — the number of students. The second line contains n space-separated integers — the heights of all students. The height of one student is an integer which absolute value doesn't exceed 10^4. -----Output----- In the first line print the maximum number of buns eaten by some student to achieve the teacher's aim. In the second line, print two space-separated integers — the height of the lowest student in the end and the step of the progression. Please, pay attention that the step should be non-negative. If there are multiple possible answers, you can print any of them. -----Examples----- Input 5 -3 -4 -2 -3 3 Output 2 -3 1 Input 5 2 -3 -1 -4 3 Output 1 -4 2 -----Note----- Lets look at the first sample. We can proceed in the following manner: don't feed the 1-st student, his height will stay equal to -3; give two buns of the first type to the 2-nd student, his height become equal to -2; give two buns of the first type to the 3-rd student, his height become equal to 0; give two buns of the first type to the 4-th student, his height become equal to -1; give two buns of the second type to the 5-th student, his height become equal to 1. To sum it up, when the students line up in non-decreasing height it will be an arithmetic progression: -3, -2, -1, 0, 1. The height of the lowest student is equal to -3, the step of the progression is equal to 1. The maximum number of buns eaten by one student is equal to 2. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 5\\n1 2 3\\n1 3 2 3 1\\n\", \"3 3\\n10 20 30\\n1 2 3\\n\", \"2 2\\n10 12\\n2 1\\n\", \"10 10\\n61 59 97 16 2 94 57 48 91 93\\n2 8 6 5 3 1 3 4 9 10\\n\", \"50 50\\n75 71 23 37 28 23 69 75 5 62 3 11 96 100 13 50 57 51 8 90 4 6 84 27 11 89 95 81 10 62 48 52 69 87 97 95 30 74 21 42 36 64 31 80 81 50 56 53 33 99\\n26 30 5 33 35 29 6 15 36 17 32 16 14 1 29 34 22 40 12 42 38 48 39 50 13 47 18 43 10 8 49 45 11 31 21 37 46 28 20 41 2 7 9 24 27 23 3 44 15 14\\n\", \"50 60\\n86 57 45 93 17 12 40 10 47 80 18 80 3 9 6 55 13 99 5 76 4 70 100 55 27 91 71 3 65 93 41 74 80 56 90 50 58 13 71 9 47 52 26 73 72 21 15 81 88 28\\n40 32 5 16 49 23 3 17 14 10 1 15 1 21 28 22 13 45 12 25 44 48 46 32 36 43 11 8 49 7 7 35 10 14 39 4 42 10 30 27 1 17 31 15 8 41 44 33 25 26 19 18 29 37 50 6 36 38 47 9\\n\", \"2 1\\n1 2\\n1\\n\", \"2 3\\n20 30\\n1 1 1\\n\", \"2 7\\n20 30\\n1 1 1 2 2 2 2\\n\", \"2 10\\n39 26\\n1 1 2 2 2 2 2 2 1 2\\n\", \"5 1\\n16 87 36 16 81\\n3\\n\"], \"outputs\": [\"12\\n\", \"40\\n\", \"12\\n\", \"2137\\n\", \"63929\\n\", \"62514\\n\", \"0\\n\", \"0\\n\", \"20\\n\", \"104\\n\", \"0\\n\"]}", "source": "primeintellect"}
New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ i ≤ n) book is w_{i}. As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below. He lifts all the books above book x. He pushes book x out of the stack. He puts down the lifted books without changing their order. After reading book x, he puts book x on the top of the stack. [Image] He decided to read books for m days. In the j-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integer b_{j} (1 ≤ b_{j} ≤ n). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times. After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him? -----Input----- The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books. The second line contains n space-separated integers w_1, w_2, ..., w_{n} (1 ≤ w_{i} ≤ 100) — the weight of each book. The third line contains m space separated integers b_1, b_2, ..., b_{m} (1 ≤ b_{j} ≤ n) — the order of books that he would read. Note that he can read the same book more than once. -----Output----- Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books. -----Examples----- Input 3 5 1 2 3 1 3 2 3 1 Output 12 -----Note----- Here's a picture depicting the example. Each vertical column presents the stacked books. [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5 6 1 1 3\\n2\\n5\\n3\\n1 1 5 6\\n1 3 5 4\\n3 3 5 3\\n\", \"2 2 0 1 1\\n\\n1\\n1\\n1 2 2 2\\n\", \"4 4 1 0 1\\n4\\n\\n5\\n1 1 2 2\\n1 3 2 2\\n3 3 4 3\\n3 2 2 2\\n1 2 2 3\\n\", \"10 10 1 8 4\\n10\\n2 3 4 5 6 7 8 9\\n10\\n1 1 3 1\\n2 1 7 1\\n1 1 9 1\\n7 1 4 1\\n10 1 7 1\\n2 1 7 1\\n3 1 2 1\\n5 1 2 1\\n10 1 5 1\\n6 1 9 1\\n\", \"2 5 1 0 1\\n2\\n\\n1\\n1 4 1 5\\n\", \"2 10 1 1 1\\n1\\n10\\n1\\n1 5 1 8\\n\", \"4 4 1 0 1\\n1\\n\\n1\\n1 2 1 4\\n\", \"2 4 1 1 1\\n1\\n2\\n1\\n2 3 2 4\\n\", \"1000 1000 1 1 10\\n1\\n2\\n1\\n1 900 1 1000\\n\", \"2 4 1 1 1\\n1\\n4\\n1\\n1 2 1 3\\n\", \"5 5 1 1 1\\n3\\n2\\n1\\n1 5 1 1\\n\"], \"outputs\": [\"7\\n5\\n4\\n\", \"3\\n\", \"6\\n4\\n3\\n5\\n4\\n\", \"3\\n4\\n4\\n3\\n3\\n4\\n3\\n3\\n4\\n3\\n\", \"1\\n\", \"3\\n\", \"2\\n\", \"1\\n\", \"100\\n\", \"1\\n\", \"4\\n\"]}", "source": "primeintellect"}
In the year of $30XX$ participants of some world programming championship live in a single large hotel. The hotel has $n$ floors. Each floor has $m$ sections with a single corridor connecting all of them. The sections are enumerated from $1$ to $m$ along the corridor, and all sections with equal numbers on different floors are located exactly one above the other. Thus, the hotel can be represented as a rectangle of height $n$ and width $m$. We can denote sections with pairs of integers $(i, j)$, where $i$ is the floor, and $j$ is the section number on the floor. The guests can walk along the corridor on each floor, use stairs and elevators. Each stairs or elevator occupies all sections $(1, x)$, $(2, x)$, $\ldots$, $(n, x)$ for some $x$ between $1$ and $m$. All sections not occupied with stairs or elevators contain guest rooms. It takes one time unit to move between neighboring sections on the same floor or to move one floor up or down using stairs. It takes one time unit to move up to $v$ floors in any direction using an elevator. You can assume you don't have to wait for an elevator, and the time needed to enter or exit an elevator is negligible. You are to process $q$ queries. Each query is a question "what is the minimum time needed to go from a room in section $(x_1, y_1)$ to a room in section $(x_2, y_2)$?" -----Input----- The first line contains five integers $n, m, c_l, c_e, v$ ($2 \leq n, m \leq 10^8$, $0 \leq c_l, c_e \leq 10^5$, $1 \leq c_l + c_e \leq m - 1$, $1 \leq v \leq n - 1$) — the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator, respectively. The second line contains $c_l$ integers $l_1, \ldots, l_{c_l}$ in increasing order ($1 \leq l_i \leq m$), denoting the positions of the stairs. If $c_l = 0$, the second line is empty. The third line contains $c_e$ integers $e_1, \ldots, e_{c_e}$ in increasing order, denoting the elevators positions in the same format. It is guaranteed that all integers $l_i$ and $e_i$ are distinct. The fourth line contains a single integer $q$ ($1 \leq q \leq 10^5$) — the number of queries. The next $q$ lines describe queries. Each of these lines contains four integers $x_1, y_1, x_2, y_2$ ($1 \leq x_1, x_2 \leq n$, $1 \leq y_1, y_2 \leq m$) — the coordinates of starting and finishing sections for the query. It is guaranteed that the starting and finishing sections are distinct. It is also guaranteed that these sections contain guest rooms, i. e. $y_1$ and $y_2$ are not among $l_i$ and $e_i$. -----Output----- Print $q$ integers, one per line — the answers for the queries. -----Example----- Input 5 6 1 1 3 2 5 3 1 1 5 6 1 3 5 4 3 3 5 3 Output 7 5 4 -----Note----- In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit. In the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in the section 2. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n7\\n4 6 11 11 15 18 20\\n4\\n10 10 10 11\\n3\\n1 1 1000000000\\n\", \"1\\n3\\n78788 78788 100000\\n\", \"1\\n3\\n78788 78788 157577\\n\", \"1\\n10\\n1 7 7 7 7 9 9 9 9 9\\n\", \"1\\n6\\n1 1 1 2 2 3\\n\", \"1\\n3\\n5623 5624 10000000\\n\", \"1\\n3\\n5739271 5739272 20000000\\n\", \"1\\n14\\n1 2 2 2 2 2 2 2 2 2 2 2 2 4\\n\", \"1\\n3\\n1 65535 10000000\\n\", \"1\\n3\\n21 78868 80000\\n\", \"1\\n15\\n3 4 7 8 9 10 11 12 13 14 15 16 32 36 39\\n\"], \"outputs\": [\"1 2 7\\n-1\\n1 2 3\\n\", \"-1\\n\", \"1 2 3\\n\", \"1 2 10\\n\", \"1 2 6\\n\", \"1 2 3\\n\", \"1 2 3\\n\", \"1 2 14\\n\", \"1 2 3\\n\", \"1 2 3\\n\", \"1 2 15\\n\"]}", "source": "primeintellect"}
You are given an array $a_1, a_2, \dots , a_n$, which is sorted in non-decreasing order ($a_i \le a_{i + 1})$. Find three indices $i$, $j$, $k$ such that $1 \le i < j < k \le n$ and it is impossible to construct a non-degenerate triangle (a triangle with nonzero area) having sides equal to $a_i$, $a_j$ and $a_k$ (for example it is possible to construct a non-degenerate triangle with sides $3$, $4$ and $5$ but impossible with sides $3$, $4$ and $7$). If it is impossible to find such triple, report it. -----Input----- The first line contains one integer $t$ ($1 \le t \le 1000$) — the number of test cases. The first line of each test case contains one integer $n$ ($3 \le n \le 5 \cdot 10^4$) — the length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots , a_n$ ($1 \le a_i \le 10^9$; $a_{i - 1} \le a_i$) — the array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print the answer to it in one line. If there is a triple of indices $i$, $j$, $k$ ($i < j < k$) such that it is impossible to construct a non-degenerate triangle having sides equal to $a_i$, $a_j$ and $a_k$, print that three indices in ascending order. If there are multiple answers, print any of them. Otherwise, print -1. -----Example----- Input 3 7 4 6 11 11 15 18 20 4 10 10 10 11 3 1 1 1000000000 Output 2 3 6 -1 1 2 3 -----Note----- In the first test case it is impossible with sides $6$, $11$ and $18$. Note, that this is not the only correct answer. In the second test case you always can construct a non-degenerate triangle. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n1 2 4 5\\n\", \"6\\n62 22 60 61 48 49\\n\", \"2\\n1 4\\n\", \"2\\n5 4\\n\", \"10\\n9 6 8 5 5 2 8 9 2 2\\n\", \"10\\n7776 32915 1030 71664 7542 72359 65387 75222 95899 40333\\n\", \"10\\n1 1 1 1 1 1 1 1 1 1\\n\", \"4\\n15 11 28 17\\n\", \"3\\n1 36 6\\n\", \"6\\n3 12 4 12 5 6\\n\", \"6\\n7 20 21 22 23 28\\n\"], \"outputs\": [\"4\\n\", \"5\\n\", \"2\\n\", \"2\\n\", \"9\\n\", \"6\\n\", \"10\\n\", \"2\\n\", \"3\\n\", \"6\\n\", \"6\\n\"]}", "source": "primeintellect"}
Alice is a beginner composer and now she is ready to create another masterpiece. And not even the single one but two at the same time! Alice has a sheet with n notes written on it. She wants to take two such non-empty non-intersecting subsequences that both of them form a melody and sum of their lengths is maximal. Subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. Subsequence forms a melody when each two adjacent notes either differs by 1 or are congruent modulo 7. You should write a program which will calculate maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody. -----Input----- The first line contains one integer number n (2 ≤ n ≤ 5000). The second line contains n integer numbers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5) — notes written on a sheet. -----Output----- Print maximum sum of lengths of such two non-empty non-intersecting subsequences that both of them form a melody. -----Examples----- Input 4 1 2 4 5 Output 4 Input 6 62 22 60 61 48 49 Output 5 -----Note----- In the first example subsequences [1, 2] and [4, 5] give length 4 in total. In the second example subsequences [62, 48, 49] and [60, 61] give length 5 in total. If you choose subsequence [62, 61] in the first place then the second melody will have maximum length 2, that gives the result of 4, which is not maximal. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.25
{"tests": "{\"inputs\": [\"6 1\\n2 0 4 0 6 0\\n\", \"6 2\\n2 3 0 5 6 0\\n\", \"4 1\\n0 0 0 0\\n\", \"6 2\\n0 0 1 0 4 5\\n\", \"10 7\\n10 8 6 5 0 0 0 4 3 9\\n\", \"10 1\\n8 7 0 2 0 10 0 0 3 5\\n\", \"10 4\\n0 1 4 2 7 0 10 0 5 8\\n\", \"10 2\\n0 7 0 10 8 0 4 2 3 0\\n\", \"10 2\\n10 0 9 0 0 4 2 6 8 0\\n\", \"10 7\\n7 9 2 10 0 0 0 3 5 1\\n\", \"20 20\\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\\n\"], \"outputs\": [\"2\\n4\\n6\\n\", \"2\\n5\\n\", \"1\\n2\\n3\\n4\\n\", \"1\\n3\\n4\\n6\\n\", \"1\\n5\\n6\\n10\\n\", \"2\\n4\\n5\\n7\\n8\\n10\\n\", \"3\\n4\\n8\\n9\\n\", \"4\\n5\\n6\\n7\\n8\\n\", \"1\\n2\\n3\\n4\\n6\\n7\\n8\\n9\\n\", \"1\\n2\\n6\\n7\\n\", \"1\\n2\\n3\\n4\\n5\\n6\\n7\\n8\\n9\\n10\\n11\\n12\\n13\\n14\\n15\\n16\\n17\\n18\\n19\\n20\\n\"]}", "source": "primeintellect"}
In the rush of modern life, people often forget how beautiful the world is. The time to enjoy those around them is so little that some even stand in queues to several rooms at the same time in the clinic, running from one queue to another. (Cultural note: standing in huge and disorganized queues for hours is a native tradition in Russia, dating back to the Soviet period. Queues can resemble crowds rather than lines. Not to get lost in such a queue, a person should follow a strict survival technique: you approach the queue and ask who the last person is, somebody answers and you join the crowd. Now you're the last person in the queue till somebody else shows up. You keep an eye on the one who was last before you as he is your only chance to get to your destination) I'm sure many people have had the problem when a stranger asks who the last person in the queue is and even dares to hint that he will be the last in the queue and then bolts away to some unknown destination. These are the representatives of the modern world, in which the ratio of lack of time is so great that they do not even watch foreign top-rated TV series. Such people often create problems in queues, because the newcomer does not see the last person in the queue and takes a place after the "virtual" link in this chain, wondering where this legendary figure has left. The Smart Beaver has been ill and he's made an appointment with a therapist. The doctor told the Beaver the sad news in a nutshell: it is necessary to do an electrocardiogram. The next day the Smart Beaver got up early, put on the famous TV series on download (three hours till the download's complete), clenched his teeth and bravely went to join a queue to the electrocardiogram room, which is notorious for the biggest queues at the clinic. Having stood for about three hours in the queue, the Smart Beaver realized that many beavers had not seen who was supposed to stand in the queue before them and there was a huge mess. He came up to each beaver in the ECG room queue and asked who should be in front of him in the queue. If the beaver did not know his correct position in the queue, then it might be his turn to go get an ECG, or maybe he should wait for a long, long time... As you've guessed, the Smart Beaver was in a hurry home, so he gave you all the necessary information for you to help him to determine what his number in the queue can be. -----Input----- The first line contains two integers n (1 ≤ n ≤ 10^3) and x (1 ≤ x ≤ n) — the number of beavers that stand in the queue and the Smart Beaver's number, correspondingly. All willing to get to the doctor are numbered from 1 to n. The second line contains n integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ n) — the number of the beaver followed by the i-th beaver. If a_{i} = 0, then the i-th beaver doesn't know who is should be in front of him. It is guaranteed that values a_{i} are correct. That is there is no cycles in the dependencies. And any beaver is followed by at most one beaver in the queue. The input limits for scoring 30 points are (subproblem B1): It is guaranteed that the number of zero elements a_{i} doesn't exceed 20. The input limits for scoring 100 points are (subproblems B1+B2): The number of zero elements a_{i} is arbitrary. -----Output----- Print all possible positions of the Smart Beaver in the line in the increasing order. -----Examples----- Input 6 1 2 0 4 0 6 0 Output 2 4 6 Input 6 2 2 3 0 5 6 0 Output 2 5 Input 4 1 0 0 0 0 Output 1 2 3 4 Input 6 2 0 0 1 0 4 5 Output 1 3 4 6 -----Note----- [Image] Picture for the fourth test. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n1 5 8 123 7\\n123 7 5 1\\n5 1 7\\n\", \"6\\n1 4 3 3 5 7\\n3 7 5 4 3\\n4 3 7 5\\n\", \"3\\n1 2 3\\n3 2\\n2\\n\", \"10\\n460626451 802090732 277246428 661369649 388684428 784303821 376287098 656422756 9301599 25720377\\n277246428 388684428 661369649 460626451 656422756 802090732 9301599 784303821 376287098\\n376287098 802090732 388684428 9301599 656422756 784303821 460626451 277246428\\n\", \"3\\n796067435 964699482 819602309\\n964699482 796067435\\n964699482\\n\", \"3\\n374054998 726316780 902899520\\n902899520 726316780\\n726316780\\n\", \"3\\n168638990 939116221 323703261\\n168638990 323703261\\n168638990\\n\", \"3\\n77 77 77\\n77 77\\n77\\n\", \"3\\n84 30 9\\n9 84\\n9\\n\", \"6\\n5 4 3 3 5 5\\n3 5 5 4 3\\n3 5 4 3\\n\", \"4\\n1 5 7 8\\n1 5 7\\n1 5\\n\"], \"outputs\": [\"8\\n123\\n\", \"1\\n3\\n\", \"1\\n3\\n\", \"25720377\\n661369649\\n\", \"819602309\\n796067435\\n\", \"374054998\\n902899520\\n\", \"939116221\\n323703261\\n\", \"77\\n77\\n\", \"30\\n84\\n\", \"5\\n5\\n\", \"8\\n7\\n\"]}", "source": "primeintellect"}
A and B are preparing themselves for programming contests. B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code. Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake. However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared — the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change. Can you help B find out exactly what two errors he corrected? -----Input----- The first line of the input contains integer n (3 ≤ n ≤ 10^5) — the initial number of compilation errors. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the errors the compiler displayed for the first time. The third line contains n - 1 space-separated integers b_1, b_2, ..., b_{n} - 1 — the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one. The fourth line contains n - 2 space-separated integers с_1, с_2, ..., с_{n} - 2 — the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one. -----Output----- Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively. -----Examples----- Input 5 1 5 8 123 7 123 7 5 1 5 1 7 Output 8 123 Input 6 1 4 3 3 5 7 3 7 5 4 3 4 3 7 5 Output 1 3 -----Note----- In the first test sample B first corrects the error number 8, then the error number 123. In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n4\\n1 2 4 2\\n2\\n5 3\\n3\\n1 3 1\\n3\\n0 0 0\\n\", \"5\\n4\\n5 3 1 5\\n1\\n3\\n3\\n5 5 1\\n3\\n2 3 0\\n1\\n5\\n\", \"5\\n9\\n2 0 2 3 1 3 1 4 5\\n5\\n5 4 7 9 7\\n5\\n8 1 3 3 7\\n3\\n9 6 1\\n3\\n6 8 2\\n\", \"5\\n4\\n3 5 2 4\\n9\\n5 4 5 6 4 2 0 1 0\\n3\\n1 1 2\\n7\\n4 6 0 5 5 5 6\\n1\\n1\\n\", \"5\\n2\\n1 6\\n3\\n3 2 7\\n1\\n6\\n3\\n5 5 6\\n2\\n6 0\\n\", \"5\\n3\\n2 8 6\\n1\\n5\\n1\\n3\\n2\\n0 0\\n1\\n5\\n\", \"5\\n3\\n0 8 9\\n3\\n1 6 4\\n3\\n1 4 6\\n3\\n7 5 4\\n2\\n3 4\\n\", \"5\\n1\\n8\\n2\\n8 1\\n3\\n9 6 1\\n1\\n8\\n2\\n3 5\\n\", \"5\\n2\\n0 9\\n3\\n1 6 1\\n2\\n4 1\\n1\\n7\\n1\\n5\\n\", \"5\\n2\\n8 5\\n2\\n3 4\\n3\\n3 1 2\\n1\\n0\\n2\\n1 6\\n\", \"1\\n20\\n20 19 20 19 21 20 20 20 20 20 19 19 20 21 19 21 19 20 20 21\\n\", \"1\\n19\\n22 20 20 22 22 21 20 20 20 20 21 21 21 21 21 21 22 21 22\\n\", \"1\\n99\\n1 2 0 2 0 1 1 1 0 2 0 0 2 1 0 1 1 1 1 1 0 1 1 2 0 0 0 1 2 1 0 0 2 1 2 0 1 2 1 2 0 0 2 1 1 1 2 0 0 1 1 2 0 2 0 2 1 0 2 1 2 0 1 2 0 2 2 1 2 2 2 1 2 1 1 2 0 0 1 2 2 2 2 0 2 2 2 0 0 1 0 1 1 2 2 1 0 1 2\\n\", \"1\\n100\\n2 2 3 3 2 1 2 1 1 3 2 2 1 1 1 3 1 3 1 2 2 3 3 3 1 2 2 1 1 1 3 1 2 1 2 2 3 2 1 2 1 3 1 1 2 1 3 1 2 1 3 2 1 2 3 3 2 1 1 3 1 1 2 3 2 2 2 2 2 3 2 3 3 3 3 1 3 1 1 1 1 3 1 2 3 3 1 3 2 3 3 2 3 3 3 1 1 3 3 3\\n\", \"1\\n100\\n0 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 1 0 0 1 0 0 1 0 1 0 1 0 1 0 0 0 0 1 1 1 0 0 1 0 0 1 0 1 0 0 0 0 0 1 0 0 1 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0 0 0 1 0 0 0 1 0 0 1 0 1 0 1 0 1 0 0 1 1 0\\n\", \"1\\n1\\n1\\n\"], \"outputs\": [\"aaaa\\nabbb\\nabaa\\nabaa\\nabbb\\naaaaa\\naaaaa\\naaabb\\naaa\\nabb\\nabb\\naaa\\na\\nb\\na\\nb\\n\", \"aaaaa\\naaaaa\\naaabb\\nabbbb\\nabbbb\\naaa\\naaa\\naaaaa\\naaaaa\\naaaaa\\nabbbb\\naaa\\naab\\naab\\nbbb\\naaaaa\\naaaaa\\n\", \"aaaaa\\naabbb\\nbbbbb\\nbbaaa\\nbbabb\\nbaaaa\\nbaabb\\nbbbbb\\nbbbba\\nbbbba\\naaaaaaaaa\\naaaaabbbb\\naaaabbbbb\\naaaabbbaa\\naaaabbbaa\\naaaabbbbb\\naaaaaaaa\\naaaaaaaa\\nabbbbbbb\\nabbaaaaa\\nabbbbbbb\\nabbbbbba\\naaaaaaaaa\\naaaaaaaaa\\naaaaaabbb\\nabbbbbbbb\\naaaaaaaa\\naaaaaabb\\naaaaaabb\\naabbbbbb\\n\", \"aaaaa\\naaabb\\naaabb\\naabbb\\naabba\\naaaaaa\\naaaaab\\naaaabb\\naaaaba\\naaaaba\\naaaaaa\\naabbbb\\nbbbbbb\\nbaaaaa\\naaaaaa\\naa\\nab\\naa\\naa\\naaaaaa\\naaaabb\\naaaabb\\nbbbbbb\\nbbbbba\\nbbbbbb\\nbbbbba\\nbbbbba\\na\\na\\n\", \"aaaaaa\\nabbbbb\\nabbbbb\\naaaaaaa\\naaabbbb\\naabbbbb\\naabbbbb\\naaaaaa\\naaaaaa\\naaaaaa\\naaaaab\\naaaaaa\\naaaaaa\\naaaaaa\\naaaaaa\\nbbbbbb\\n\", \"aaaaaaaa\\naabbbbbb\\naabbbbbb\\naabbbbaa\\naaaaa\\naaaaa\\naaa\\naaa\\na\\nb\\na\\naaaaa\\naaaaa\\n\", \"aaaaaaaaa\\nbbbbbbbbb\\nbbbbbbbba\\nbbbbbbbba\\naaaaaa\\nabbbbb\\nabbbbb\\nabbbaa\\naaaaaa\\nabbbbb\\nabbbaa\\nabbbaa\\naaaaaaa\\naaaaaaa\\naaaaabb\\naaaabbb\\naaaa\\naaab\\naaab\\n\", \"aaaaaaaa\\naaaaaaaa\\naaaaaaaa\\naaaaaaaa\\nabbbbbbb\\naaaaaaaaa\\naaaaaaaaa\\naaaaaabbb\\nabbbbbbbb\\naaaaaaaa\\naaaaaaaa\\naaaaa\\naaabb\\naaabb\\n\", \"aaaaaaaaa\\nbbbbbbbbb\\nbbbbbbbbb\\naaaaaa\\nabbbbb\\nabbbbb\\naaaaaa\\naaaa\\naaaa\\nabbb\\naaaaaaa\\naaaaaaa\\naaaaa\\naaaaa\\n\", \"aaaaaaaa\\naaaaaaaa\\naaaaabbb\\naaaa\\naaab\\naaab\\naaa\\naaa\\nabb\\naba\\na\\nb\\naaaaaa\\nabbbbb\\nabbbbb\\n\", \"aaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaabb\\naaaaaaaaaaaaaaaaaaaba\\naaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaabb\\naaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaabb\\naaaaaaaaaaaaaaaaaaabb\\naaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaa\\n\", \"aaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaabb\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaabb\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaabb\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaaa\\naaaaaaaaaaaaaaaaaaaaab\\naaaaaaaaaaaaaaaaaaaaab\\n\", \"aa\\nab\\nab\\nbb\\nbb\\naa\\nab\\naa\\nab\\nbb\\nbb\\naa\\nbb\\nbb\\nba\\naa\\nab\\naa\\nab\\naa\\nab\\nbb\\nba\\nbb\\nbb\\naa\\nbb\\naa\\nab\\nab\\naa\\nbb\\naa\\naa\\nab\\nab\\nbb\\nba\\nba\\nbb\\nbb\\naa\\nbb\\nbb\\nba\\nbb\\nba\\nba\\naa\\nbb\\nba\\nbb\\nbb\\naa\\naa\\nbb\\nbb\\nba\\naa\\naa\\nab\\nab\\nbb\\nba\\nba\\naa\\naa\\naa\\nab\\nab\\nab\\nab\\naa\\naa\\nab\\naa\\naa\\nbb\\naa\\nab\\nab\\nab\\nab\\nab\\nbb\\nbb\\nbb\\nbb\\naa\\nbb\\nba\\naa\\nab\\naa\\naa\\naa\\nab\\nbb\\nba\\nba\\n\", \"aaa\\naab\\naaa\\naaa\\naaa\\naab\\nabb\\naba\\naaa\\nabb\\nabb\\naba\\nabb\\naaa\\nabb\\naaa\\naaa\\nabb\\nabb\\naaa\\naab\\naaa\\naaa\\naaa\\naaa\\nabb\\naba\\nabb\\naaa\\nabb\\naaa\\naaa\\nabb\\naba\\naaa\\naab\\naaa\\naaa\\naab\\nabb\\naba\\naaa\\naaa\\nabb\\naaa\\naab\\nabb\\nabb\\naaa\\naab\\nabb\\nabb\\naba\\naaa\\naab\\naab\\naab\\naaa\\nabb\\naaa\\naaa\\nabb\\naaa\\naab\\naab\\naaa\\naab\\naaa\\naab\\naaa\\naaa\\naab\\naab\\naab\\naab\\naab\\nabb\\nabb\\naaa\\nabb\\naaa\\nabb\\nabb\\naaa\\naab\\naab\\naab\\nabb\\nabb\\naba\\naba\\naba\\nabb\\nabb\\nabb\\nabb\\naaa\\nabb\\nabb\\nabb\\nabb\\n\", \"a\\nb\\na\\na\\nb\\nb\\na\\nb\\nb\\na\\nb\\nb\\na\\nb\\nb\\na\\nb\\nb\\nb\\na\\nb\\nb\\nb\\na\\na\\na\\nb\\nb\\nb\\na\\nb\\nb\\na\\nb\\nb\\na\\na\\nb\\nb\\na\\na\\nb\\na\\nb\\na\\na\\na\\na\\nb\\na\\na\\nb\\na\\na\\nb\\nb\\na\\nb\\na\\nb\\na\\na\\nb\\na\\na\\na\\nb\\nb\\na\\nb\\na\\na\\nb\\na\\nb\\na\\nb\\nb\\nb\\na\\nb\\na\\na\\nb\\na\\nb\\nb\\na\\nb\\nb\\na\\na\\nb\\nb\\na\\na\\nb\\na\\na\\na\\nb\\n\", \"a\\na\\n\"]}", "source": "primeintellect"}
The length of the longest common prefix of two strings $s = s_1 s_2 \ldots s_n$ and $t = t_1 t_2 \ldots t_m$ is defined as the maximum integer $k$ ($0 \le k \le min(n,m)$) such that $s_1 s_2 \ldots s_k$ equals $t_1 t_2 \ldots t_k$. Koa the Koala initially has $n+1$ strings $s_1, s_2, \dots, s_{n+1}$. For each $i$ ($1 \le i \le n$) she calculated $a_i$ — the length of the longest common prefix of $s_i$ and $s_{i+1}$. Several days later Koa found these numbers, but she couldn't remember the strings. So Koa would like to find some strings $s_1, s_2, \dots, s_{n+1}$ which would have generated numbers $a_1, a_2, \dots, a_n$. Can you help her? If there are many answers print any. We can show that answer always exists for the given constraints. -----Input----- Each test contains multiple test cases. The first line contains $t$ ($1 \le t \le 100$) — the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 100$) — the number of elements in the list $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 50$) — the elements of $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $100$. -----Output----- For each test case: Output $n+1$ lines. In the $i$-th line print string $s_i$ ($1 \le |s_i| \le 200$), consisting of lowercase Latin letters. Length of the longest common prefix of strings $s_i$ and $s_{i+1}$ has to be equal to $a_i$. If there are many answers print any. We can show that answer always exists for the given constraints. -----Example----- Input 4 4 1 2 4 2 2 5 3 3 1 3 1 3 0 0 0 Output aeren ari arousal around ari monogon monogamy monthly kevinvu kuroni kurioni korone anton loves adhoc problems -----Note----- In the $1$-st test case one of the possible answers is $s = [aeren, ari, arousal, around, ari]$. Lengths of longest common prefixes are: Between $\color{red}{a}eren$ and $\color{red}{a}ri$ $\rightarrow 1$ Between $\color{red}{ar}i$ and $\color{red}{ar}ousal$ $\rightarrow 2$ Between $\color{red}{arou}sal$ and $\color{red}{arou}nd$ $\rightarrow 4$ Between $\color{red}{ar}ound$ and $\color{red}{ar}i$ $\rightarrow 2$ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n1 2 2 1\\n\", \"5\\n1 3 2 2 5\\n\", \"1\\n1\\n\", \"10\\n1 1 1 1 1 1 1 1 1 1\\n\", \"10\\n1 9 7 6 2 4 7 8 1 3\\n\", \"100\\n82 51 81 14 37 17 78 92 64 15 8 86 89 8 87 77 66 10 15 12 100 25 92 47 21 78 20 63 13 49 41 36 41 79 16 87 87 69 3 76 80 60 100 49 70 59 72 8 38 71 45 97 71 14 76 54 81 4 59 46 39 29 92 3 49 22 53 99 59 52 74 31 92 43 42 23 44 9 82 47 7 40 12 9 3 55 37 85 46 22 84 52 98 41 21 77 63 17 62 91\\n\", \"3\\n1 2 6\\n\", \"136\\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\\n\", \"3\\n1 2 3\\n\", \"7\\n999999998 999999999 999999999 999999999 999999999 999999999 1000000000\\n\", \"3\\n100 1 2\\n\"], \"outputs\": [\"1 2 1 2\\n\", \"1 5 2 3 2\\n\", \"1\\n\", \"1 1 1 1 1 1 1 1 1 1\\n\", \"1 9 1 8 2 7 3 7 4 6\\n\", \"3 100 3 100 3 99 4 98 7 97 8 92 8 92 8 92 9 92 9 91 10 89 12 87 12 87 13 87 14 86 14 85 15 84 15 82 16 82 17 81 17 81 20 80 21 79 21 78 22 78 22 77 23 77 25 76 29 76 31 74 36 72 37 71 37 71 38 70 39 69 40 66 41 64 41 63 41 63 42 62 43 60 44 59 45 59 46 59 46 55 47 54 47 53 49 52 49 52 49 51\\n\", \"1 6 2\\n\", \"1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\\n\", \"1 3 2\\n\", \"999999998 1000000000 999999999 999999999 999999999 999999999 999999999\\n\", \"1 100 2\\n\"]}", "source": "primeintellect"}
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: a_{i} ≥ a_{i} - 1 for all even i, a_{i} ≤ a_{i} - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted. Can you make the array z-sorted? -----Input----- The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a. The second line contains n integers a_{i} (1 ≤ a_{i} ≤ 10^9) — the elements of the array a. -----Output----- If it's possible to make the array a z-sorted print n space separated integers a_{i} — the elements after z-sort. Otherwise print the only word "Impossible". -----Examples----- Input 4 1 2 2 1 Output 1 2 1 2 Input 5 1 3 2 2 5 Output 1 5 2 3 2 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n10 6 15\\n\", \"3\\n2 4 6\\n\", \"7\\n30 60 21 42 70 15 30\\n\", \"10\\n91630 147840 12600 52206 270270 48510 16170 33495 25080 60060\\n\", \"30\\n13469 121221 107752 94283 107752 296318 67345 296318 161628 215504 121221 215504 296318 134690 282849 161628 148159 202035 242442 107752 80814 80814 107752 255911 67345 53876 255911 215504 202035 175097\\n\", \"1\\n1\\n\", \"1\\n2\\n\", \"7\\n255255 170170 102102 72930 46410 39270 30030\\n\", \"10\\n30030 4290 120120 105105 240240 80080 60060 12012 9240 21840\\n\", \"10\\n735 209585 127695 85795 28815 52080 53555 157060 133665 22010\\n\", \"25\\n32942 232281 102984 3409 259245 57281 34629 15897 182413 163065 26397 79415 73227 299110 37051 296639 243565 229726 222334 24822 159992 89677 18592 166614 231980\\n\"], \"outputs\": [\"3\\n\", \"-1\\n\", \"3\\n\", \"5\\n\", \"-1\\n\", \"1\\n\", \"-1\\n\", \"7\\n\", \"6\\n\", \"-1\\n\", \"-1\\n\"]}", "source": "primeintellect"}
Janusz is a businessman. He owns a company "Januszex", which produces games for teenagers. Last hit of Januszex was a cool one-person game "Make it one". The player is given a sequence of $n$ integers $a_i$. It is allowed to select any subset of them, and the score is equal to the greatest common divisor of selected elements. The goal is to take as little elements as it is possible, getting the score $1$. Now Janusz wonders, for given sequence, how much elements should the player choose? -----Input----- The first line contains an only integer $n$ ($1 \le n \le 300\,000$) — the number of integers in the sequence. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 300\,000$). -----Output----- If there is no subset of the given sequence with gcd equal to $1$, output -1. Otherwise, output exactly one integer — the size of the smallest subset with gcd equal to $1$. -----Examples----- Input 3 10 6 15 Output 3 Input 3 2 4 6 Output -1 Input 7 30 60 21 42 70 15 30 Output 3 -----Note----- In the first example, selecting a subset of all numbers gives a gcd of $1$ and for all smaller subsets the gcd is greater than $1$. In the second example, for all subsets of numbers the gcd is at least $2$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n1 2\\n1 3\\n2 4\\n2 5\\n3 6\\n3 13\\n3 14\\n4 7\\n4 8\\n5 9\\n5 10\\n6 11\\n6 12\\n\", \"2\\n1 2\\n\", \"3\\n1 2\\n2 3\\n3 4\\n4 5\\n5 6\\n\", \"4\\n1 2\\n1 3\\n2 4\\n2 5\\n3 6\\n3 7\\n4 8\\n4 9\\n5 12\\n5 13\\n7 10\\n7 11\\n1 14\\n\", \"4\\n1 2\\n1 3\\n2 4\\n2 5\\n3 6\\n3 7\\n4 8\\n4 9\\n5 12\\n5 13\\n7 10\\n7 11\\n5 14\\n\", \"6\\n53 61\\n35 53\\n35 39\\n34 53\\n34 54\\n34 62\\n43 54\\n23 43\\n12 43\\n1 54\\n1 7\\n1 57\\n40 62\\n30 40\\n28 40\\n4 62\\n4 60\\n4 45\\n19 61\\n19 36\\n36 42\\n36 52\\n10 19\\n10 48\\n10 46\\n2 61\\n2 51\\n2 27\\n5 51\\n3 51\\n15 27\\n22 27\\n39 47\\n6 47\\n6 59\\n6 24\\n9 47\\n9 49\\n9 21\\n20 39\\n14 20\\n14 18\\n13 14\\n8 20\\n8 44\\n8 29\\n33 35\\n33 38\\n16 33\\n16 17\\n16 26\\n17 55\\n17 41\\n26 31\\n11 26\\n25 38\\n25 50\\n25 32\\n37 38\\n37 58\\n37 56\\n\", \"4\\n5 9\\n5 6\\n5 10\\n5 13\\n10 11\\n10 12\\n3 6\\n11 14\\n7 10\\n8 10\\n1 13\\n4 6\\n2 9\\n\", \"4\\n5 8\\n10 11\\n6 9\\n8 10\\n2 4\\n10 13\\n1 6\\n7 8\\n3 10\\n4 14\\n8 9\\n4 9\\n3 12\\n\", \"4\\n1 9\\n6 13\\n2 4\\n2 9\\n7 12\\n3 4\\n7 8\\n1 11\\n5 7\\n6 12\\n2 12\\n6 10\\n1 14\\n\", \"5\\n1 2\\n2 3\\n3 30\\n3 5\\n3 6\\n2 7\\n7 8\\n2 9\\n9 10\\n10 11\\n10 12\\n9 13\\n13 14\\n13 15\\n1 16\\n16 17\\n17 18\\n18 19\\n18 20\\n17 21\\n21 22\\n21 23\\n16 24\\n24 25\\n25 26\\n25 27\\n24 28\\n28 29\\n4 28\\n\"], \"outputs\": [\"1\\n3\\n\", \"2\\n1 2\\n\", \"0\\n\", \"0\\n\", \"0\\n\", \"2\\n35 53\\n\", \"0\\n\", \"0\\n\", \"0\\n\", \"0\\n\"]}", "source": "primeintellect"}
You have an integer $n$. Let's define following tree generation as McDic's generation: Make a complete and full binary tree of $2^{n} - 1$ vertices. Complete and full binary tree means a tree that exactly one vertex is a root, all leaves have the same depth (distance from the root), and all non-leaf nodes have exactly two child nodes. Select a non-root vertex $v$ from that binary tree. Remove $v$ from tree and make new edges between $v$'s parent and $v$'s direct children. If $v$ has no children, then no new edges will be made. You have a tree. Determine if this tree can be made by McDic's generation. If yes, then find the parent vertex of removed vertex in tree. -----Input----- The first line contains integer $n$ ($2 \le n \le 17$). The $i$-th of the next $2^{n} - 3$ lines contains two integers $a_{i}$ and $b_{i}$ ($1 \le a_{i} \lt b_{i} \le 2^{n} - 2$) — meaning there is an edge between $a_{i}$ and $b_{i}$. It is guaranteed that the given edges form a tree. -----Output----- Print two lines. In the first line, print a single integer — the number of answers. If given tree cannot be made by McDic's generation, then print $0$. In the second line, print all possible answers in ascending order, separated by spaces. If the given tree cannot be made by McDic's generation, then don't print anything. -----Examples----- Input 4 1 2 1 3 2 4 2 5 3 6 3 13 3 14 4 7 4 8 5 9 5 10 6 11 6 12 Output 1 3 Input 2 1 2 Output 2 1 2 Input 3 1 2 2 3 3 4 4 5 5 6 Output 0 -----Note----- In the first example, $3$ is the only possible answer. $8$ In the second example, there are $2$ possible answers. [Image] In the third example, the tree can't be generated by McDic's generation. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1 0\\n\", \"3 1\\n1 2\\n\", \"4 3\\n1 2\\n1 3\\n2 4\\n\", \"2 0\\n\", \"2 1\\n2 1\\n\", \"3 0\\n\", \"10 45\\n6 8\\n5 8\\n6 10\\n2 9\\n1 3\\n7 6\\n10 2\\n9 7\\n9 3\\n10 1\\n1 4\\n10 7\\n8 3\\n6 2\\n2 7\\n9 6\\n1 5\\n8 10\\n10 9\\n4 2\\n7 4\\n5 7\\n7 1\\n10 4\\n10 5\\n8 9\\n5 3\\n7 8\\n3 4\\n1 6\\n1 2\\n3 7\\n9 1\\n5 6\\n6 3\\n9 5\\n6 4\\n3 10\\n9 4\\n1 8\\n3 2\\n5 2\\n5 4\\n8 4\\n8 2\\n\", \"10 40\\n8 3\\n6 8\\n7 1\\n9 10\\n1 3\\n2 8\\n3 7\\n1 8\\n8 10\\n4 5\\n5 1\\n10 3\\n9 7\\n4 6\\n5 3\\n2 3\\n3 9\\n9 8\\n7 8\\n4 1\\n1 6\\n4 7\\n7 6\\n6 10\\n6 9\\n2 6\\n5 10\\n10 4\\n7 2\\n5 9\\n2 4\\n3 6\\n9 2\\n1 10\\n5 8\\n5 7\\n4 8\\n1 9\\n6 5\\n5 2\\n\", \"5 4\\n5 3\\n5 4\\n3 2\\n4 3\\n\", \"5 10\\n4 2\\n1 5\\n5 2\\n1 4\\n2 3\\n4 3\\n1 2\\n5 3\\n5 4\\n3 1\\n\"], \"outputs\": [\"NO\\n\", \"YES\\n1 3 2 \\n1 3 1 \\n\", \"YES\\n1 3 4 2 \\n1 3 4 1 \\n\", \"YES\\n1 2 \\n1 1 \\n\", \"NO\\n\", \"YES\\n1 2 3 \\n1 1 3 \\n\", \"NO\\n\", \"YES\\n1 2 3 4 5 6 7 8 9 10 \\n1 1 3 4 5 6 7 8 9 10 \\n\", \"YES\\n1 2 3 4 5 \\n1 1 3 4 5 \\n\", \"NO\\n\"]}", "source": "primeintellect"}
Vasya had an array of $n$ integers, each element of the array was from $1$ to $n$. He chose $m$ pairs of different positions and wrote them down to a sheet of paper. Then Vasya compared the elements at these positions, and wrote down the results of the comparisons to another sheet of paper. For each pair he wrote either "greater", "less", or "equal". After several years, he has found the first sheet of paper, but he couldn't find the second one. Also he doesn't remember the array he had. In particular, he doesn't remember if the array had equal elements. He has told this sad story to his informatics teacher Dr Helen. She told him that it could be the case that even if Vasya finds his second sheet, he would still not be able to find out whether the array had two equal elements. Now Vasya wants to find two arrays of integers, each of length $n$. All elements of the first array must be distinct, and there must be two equal elements in the second array. For each pair of positions Vasya wrote at the first sheet of paper, the result of the comparison must be the same for the corresponding elements of the first array, and the corresponding elements of the second array. Help Vasya find two such arrays of length $n$, or find out that there are no such arrays for his sets of pairs. -----Input----- The first line of input contains two integers $n$, $m$ — the number of elements in the array and number of comparisons made by Vasya ($1 \le n \le 100\,000$, $0 \le m \le 100\,000$). Each of the following $m$ lines contains two integers $a_i$, $b_i$  — the positions of the $i$-th comparison ($1 \le a_i, b_i \le n$; $a_i \ne b_i$). It's guaranteed that any unordered pair is given in the input at most once. -----Output----- The first line of output must contain "YES" if there exist two arrays, such that the results of comparisons would be the same, and all numbers in the first one are distinct, and the second one contains two equal numbers. Otherwise it must contain "NO". If the arrays exist, the second line must contain the array of distinct integers, the third line must contain the array, that contains at least one pair of equal elements. Elements of the arrays must be integers from $1$ to $n$. -----Examples----- Input 1 0 Output NO Input 3 1 1 2 Output YES 1 3 2 1 3 1 Input 4 3 1 2 1 3 2 4 Output YES 1 3 4 2 1 3 4 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n2 2 2\\n\", \"5\\n1 4 1 1 1\\n\", \"3\\n1 1 1\\n\", \"3\\n1 1 2\\n\", \"4\\n2 1 2 1\\n\", \"5\\n1 2 1 1 2\\n\", \"5\\n1 1 1 3 1\\n\", \"100\\n1 5 1 1 1 2 3 2 1 4 2 2 1 2 1 2 2 2 2 1 1 2 2 2 2 2 2 1 2 2 2 1 1 1 2 2 1 1 2 3 2 2 3 1 2 4 2 1 2 1 2 4 2 1 2 3 1 3 1 2 2 1 2 2 3 1 1 1 5 2 2 2 3 3 1 3 1 2 2 4 1 2 1 3 1 5 3 3 1 1 2 1 3 2 1 2 2 2 3 2\\n\", \"17\\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 2\\n\", \"3\\n1 2 2\\n\"], \"outputs\": [\"YES 2\\n2\\n1 2\\n2 3\\n\", \"YES 2\\n4\\n5 2\\n2 4\\n2 3\\n2 1\\n\", \"NO\\n\", \"YES 2\\n2\\n2 3\\n3 1\\n\", \"YES 3\\n3\\n4 1\\n1 3\\n3 2\\n\", \"NO\\n\", \"NO\\n\", \"NO\\n\", \"YES 16\\n16\\n1 2\\n2 3\\n3 4\\n4 5\\n5 6\\n6 7\\n7 8\\n8 9\\n9 10\\n10 11\\n11 12\\n12 13\\n13 14\\n14 15\\n15 16\\n16 17\\n\", \"YES 2\\n2\\n1 2\\n2 3\\n\"]}", "source": "primeintellect"}
Graph constructive problems are back! This time the graph you are asked to build should match the following properties. The graph is connected if and only if there exists a path between every pair of vertices. The diameter (aka "longest shortest path") of a connected undirected graph is the maximum number of edges in the shortest path between any pair of its vertices. The degree of a vertex is the number of edges incident to it. Given a sequence of $n$ integers $a_1, a_2, \dots, a_n$ construct a connected undirected graph of $n$ vertices such that: the graph contains no self-loops and no multiple edges; the degree $d_i$ of the $i$-th vertex doesn't exceed $a_i$ (i.e. $d_i \le a_i$); the diameter of the graph is maximum possible. Output the resulting graph or report that no solution exists. -----Input----- The first line contains a single integer $n$ ($3 \le n \le 500$) — the number of vertices in the graph. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n - 1$) — the upper limits to vertex degrees. -----Output----- Print "NO" if no graph can be constructed under the given conditions. Otherwise print "YES" and the diameter of the resulting graph in the first line. The second line should contain a single integer $m$ — the number of edges in the resulting graph. The $i$-th of the next $m$ lines should contain two integers $v_i, u_i$ ($1 \le v_i, u_i \le n$, $v_i \neq u_i$) — the description of the $i$-th edge. The graph should contain no multiple edges — for each pair $(x, y)$ you output, you should output no more pairs $(x, y)$ or $(y, x)$. -----Examples----- Input 3 2 2 2 Output YES 2 2 1 2 2 3 Input 5 1 4 1 1 1 Output YES 2 4 1 2 3 2 4 2 5 2 Input 3 1 1 1 Output NO -----Note----- Here are the graphs for the first two example cases. Both have diameter of $2$. [Image] $d_1 = 1 \le a_1 = 2$ $d_2 = 2 \le a_2 = 2$ $d_3 = 1 \le a_3 = 2$ [Image] $d_1 = 1 \le a_1 = 1$ $d_2 = 4 \le a_2 = 4$ $d_3 = 1 \le a_3 = 1$ $d_4 = 1 \le a_4 = 1$ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1\\n\", \"3\\n\", \"10\\n\", \"9\\n\", \"13\\n\", \"20\\n\", \"2\\n\", \"4\\n\", \"37\\n\", \"22\\n\"], \"outputs\": [\"No\", \"Yes\\n1 2\\n2 1 3 \\n\", \"Yes\\n1 5\\n9 1 2 3 4 6 7 8 9 10 \\n\", \"Yes\\n1 5\\n8 1 2 3 4 6 7 8 9 \\n\", \"Yes\\n1 7\\n12 1 2 3 4 5 6 8 9 10 11 12 13 \\n\", \"Yes\\n1 10\\n19 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20 \\n\", \"No\", \"Yes\\n1 2\\n3 1 3 4 \\n\", \"Yes\\n1 19\\n36 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 \\n\", \"Yes\\n1 11\\n21 1 2 3 4 5 6 7 8 9 10 12 13 14 15 16 17 18 19 20 21 22 \\n\"]}", "source": "primeintellect"}
Find out if it is possible to partition the first $n$ positive integers into two non-empty disjoint sets $S_1$ and $S_2$ such that:$\mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) > 1$ Here $\mathrm{sum}(S)$ denotes the sum of all elements present in set $S$ and $\mathrm{gcd}$ means thegreatest common divisor. Every integer number from $1$ to $n$ should be present in exactly one of $S_1$ or $S_2$. -----Input----- The only line of the input contains a single integer $n$ ($1 \le n \le 45\,000$) -----Output----- If such partition doesn't exist, print "No" (quotes for clarity). Otherwise, print "Yes" (quotes for clarity), followed by two lines, describing $S_1$ and $S_2$ respectively. Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty. If there are multiple possible partitions — print any of them. -----Examples----- Input 1 Output No Input 3 Output Yes 1 2 2 1 3 -----Note----- In the first example, there is no way to partition a single number into two non-empty sets, hence the answer is "No". In the second example, the sums of the sets are $2$ and $4$ respectively. The $\mathrm{gcd}(2, 4) = 2 > 1$, hence that is one of the possible answers. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"2\\n1 999\\n999 1\\n\", \"3\\n400 600\\n400 600\\n400 600\\n\", \"2\\n500 500\\n500 500\\n\", \"1\\n1 999\\n\", \"10\\n1 999\\n1 999\\n1 999\\n1 999\\n1 999\\n1 999\\n1 999\\n1 999\\n1 999\\n1 999\\n\", \"2\\n499 501\\n501 499\\n\", \"3\\n500 500\\n1 999\\n400 600\\n\", \"1\\n0 1000\\n\", \"1\\n500 500\\n\", \"1\\n1000 0\\n\"], \"outputs\": [\"AG\\n\", \"AGA\\n\", \"AG\\n\", \"A\\n\", \"AAAAAAAAAA\\n\", \"AG\\n\", \"AGA\\n\", \"A\\n\", \"A\\n\", \"G\\n\"]}", "source": "primeintellect"}
The Bitlandians are quite weird people. They have very peculiar customs. As is customary, Uncle J. wants to have n eggs painted for Bitruz (an ancient Bitland festival). He has asked G. and A. to do the work. The kids are excited because just as is customary, they're going to be paid for the job! Overall uncle J. has got n eggs. G. named his price for painting each egg. Similarly, A. named his price for painting each egg. It turns out that for each egg the sum of the money both A. and G. want for the painting equals 1000. Uncle J. wants to distribute the eggs between the children so as to give each egg to exactly one child. Also, Uncle J. wants the total money paid to A. to be different from the total money paid to G. by no more than 500. Help Uncle J. Find the required distribution of eggs or otherwise say that distributing the eggs in the required manner is impossible. -----Input----- The first line contains integer n (1 ≤ n ≤ 10^6) — the number of eggs. Next n lines contain two integers a_{i} and g_{i} each (0 ≤ a_{i}, g_{i} ≤ 1000; a_{i} + g_{i} = 1000): a_{i} is the price said by A. for the i-th egg and g_{i} is the price said by G. for the i-th egg. -----Output----- If it is impossible to assign the painting, print "-1" (without quotes). Otherwise print a string, consisting of n letters "G" and "A". The i-th letter of this string should represent the child who will get the i-th egg in the required distribution. Letter "A" represents A. and letter "G" represents G. If we denote the money Uncle J. must pay A. for the painting as S_{a}, and the money Uncle J. must pay G. for the painting as S_{g}, then this inequality must hold: |S_{a} - S_{g}| ≤ 500. If there are several solutions, you are allowed to print any of them. -----Examples----- Input 2 1 999 999 1 Output AG Input 3 400 600 400 600 400 600 Output AGA Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n5 2 5 1 4\\n\", \"6\\n10 20 20 40 60 60\\n\", \"2\\n101 100\\n\", \"1\\n1000\\n\", \"2\\n1000000000 -1000000000\\n\", \"8\\n5 2 6 8 3 1 6 8\\n\", \"2\\n200000000 199999999\\n\", \"3\\n100000000 100000002 100000001\\n\", \"5\\n1000000000 -10000000 0 8888888 7777777\\n\", \"5\\n10 30 20 50 40\\n\"], \"outputs\": [\"2\\n0 3\\n4 2\\n\", \"0\\n\", \"1\\n0 1\\n\", \"0\\n\", \"1\\n0 1\\n\", \"4\\n0 5\\n4 2\\n5 3\\n6 5\\n\", \"1\\n0 1\\n\", \"1\\n1 2\\n\", \"3\\n0 1\\n2 1\\n4 2\\n\", \"2\\n1 2\\n4 3\\n\"]}", "source": "primeintellect"}
In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another. Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n. -----Input----- The first line of the input contains integer n (1 ≤ n ≤ 3000) — the number of array elements. The second line contains elements of array: a_0, a_1, ..., a_{n} - 1 ( - 10^9 ≤ a_{i} ≤ 10^9), where a_{i} is the i-th element of the array. The elements are numerated from 0 to n - 1 from left to right. Some integers may appear in the array more than once. -----Output----- In the first line print k (0 ≤ k ≤ n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers i, j (0 ≤ i, j ≤ n - 1), representing the swap of elements a_{i} and a_{j}. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i = j and swap the same pair of elements multiple times. If there are multiple answers, print any of them. It is guaranteed that at least one answer exists. -----Examples----- Input 5 5 2 5 1 4 Output 2 0 3 4 2 Input 6 10 20 20 40 60 60 Output 0 Input 2 101 100 Output 1 0 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n\", \"4\\n\", \"1\\n\", \"2\\n\", \"5\\n\", \"6\\n\", \"7\\n\", \"8\\n\", \"9\\n\", \"10\\n\"], \"outputs\": [\"2\\n1 2\\n2 3\\n\", \"8\\n1 2\\n3 4\\n1 3\\n2 4\\n1 2\\n3 4\\n1 3\\n2 4\\n\", \"0\\n\", \"2\\n1 2\\n1 2\\n\", \"8\\n1 2\\n3 4\\n1 3\\n2 4\\n2 3\\n4 5\\n2 4\\n3 5\\n\", \"8\\n1 2\\n3 4\\n1 3\\n2 4\\n3 4\\n5 6\\n3 5\\n4 6\\n\", \"8\\n1 2\\n3 4\\n1 3\\n2 4\\n4 5\\n6 7\\n4 6\\n5 7\\n\", \"24\\n1 2\\n3 4\\n1 3\\n2 4\\n5 6\\n7 8\\n5 7\\n6 8\\n1 5\\n2 6\\n3 7\\n4 8\\n1 2\\n3 4\\n1 3\\n2 4\\n5 6\\n7 8\\n5 7\\n6 8\\n1 5\\n2 6\\n3 7\\n4 8\\n\", \"24\\n1 2\\n3 4\\n1 3\\n2 4\\n5 6\\n7 8\\n5 7\\n6 8\\n1 5\\n2 6\\n3 7\\n4 8\\n2 3\\n4 5\\n2 4\\n3 5\\n6 7\\n8 9\\n6 8\\n7 9\\n2 6\\n3 7\\n4 8\\n5 9\\n\", \"24\\n1 2\\n3 4\\n1 3\\n2 4\\n5 6\\n7 8\\n5 7\\n6 8\\n1 5\\n2 6\\n3 7\\n4 8\\n3 4\\n5 6\\n3 5\\n4 6\\n7 8\\n9 10\\n7 9\\n8 10\\n3 7\\n4 8\\n5 9\\n6 10\\n\"]}", "source": "primeintellect"}
You are given an integer $n$. You should find a list of pairs $(x_1, y_1)$, $(x_2, y_2)$, ..., $(x_q, y_q)$ ($1 \leq x_i, y_i \leq n$) satisfying the following condition. Let's consider some function $f: \mathbb{N} \times \mathbb{N} \to \mathbb{N}$ (we define $\mathbb{N}$ as the set of positive integers). In other words, $f$ is a function that returns a positive integer for a pair of positive integers. Let's make an array $a_1, a_2, \ldots, a_n$, where $a_i = i$ initially. You will perform $q$ operations, in $i$-th of them you will: assign $t = f(a_{x_i}, a_{y_i})$ ($t$ is a temporary variable, it is used only for the next two assignments); assign $a_{x_i} = t$; assign $a_{y_i} = t$. In other words, you need to simultaneously change $a_{x_i}$ and $a_{y_i}$ to $f(a_{x_i}, a_{y_i})$. Note that during this process $f(p, q)$ is always the same for a fixed pair of $p$ and $q$. In the end, there should be at most two different numbers in the array $a$. It should be true for any function $f$. Find any possible list of pairs. The number of pairs should not exceed $5 \cdot 10^5$. -----Input----- The single line contains a single integer $n$ ($1 \leq n \leq 15\,000$). -----Output----- In the first line print $q$ ($0 \leq q \leq 5 \cdot 10^5$) — the number of pairs. In each of the next $q$ lines print two integers. In the $i$-th line print $x_i$, $y_i$ ($1 \leq x_i, y_i \leq n$). The condition described in the statement should be satisfied. If there exists multiple answers you can print any of them. -----Examples----- Input 3 Output 1 1 2 Input 4 Output 2 1 2 3 4 -----Note----- In the first example, after performing the only operation the array $a$ will be $[f(a_1, a_2), f(a_1, a_2), a_3]$. It will always have at most two different numbers. In the second example, after performing two operations the array $a$ will be $[f(a_1, a_2), f(a_1, a_2), f(a_3, a_4), f(a_3, a_4)]$. It will always have at most two different numbers. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 1 2\\n1\\n\", \"9 2 26\\n2 3\\n\", \"12 3 1\\n2 5 6\\n\", \"2 1 1\\n1\\n\", \"2 1 234\\n1\\n\", \"6 2 5\\n1 3\\n\", \"2 1 1000000000\\n1\\n\", \"1000000000 1 1\\n1\\n\", \"1000000000 2 2\\n1 500000000\\n\", \"8 1 8\\n2\\n\"], \"outputs\": [\"6\\n\", \"150352234\\n\", \"1\\n\", \"1\\n\", \"27495\\n\", \"4875\\n\", \"858035449\\n\", \"1\\n\", \"933660593\\n\", \"8519680\\n\"]}", "source": "primeintellect"}
Consider some set of distinct characters $A$ and some string $S$, consisting of exactly $n$ characters, where each character is present in $A$. You are given an array of $m$ integers $b$ ($b_1 < b_2 < \dots < b_m$). You are allowed to perform the following move on the string $S$: Choose some valid $i$ and set $k = b_i$; Take the first $k$ characters of $S = Pr_k$; Take the last $k$ characters of $S = Su_k$; Substitute the first $k$ characters of $S$ with the reversed $Su_k$; Substitute the last $k$ characters of $S$ with the reversed $Pr_k$. For example, let's take a look at $S =$ "abcdefghi" and $k = 2$. $Pr_2 =$ "ab", $Su_2 =$ "hi". Reversed $Pr_2 =$ "ba", $Su_2 =$ "ih". Thus, the resulting $S$ is "ihcdefgba". The move can be performed arbitrary number of times (possibly zero). Any $i$ can be selected multiple times over these moves. Let's call some strings $S$ and $T$ equal if and only if there exists such a sequence of moves to transmute string $S$ to string $T$. For the above example strings "abcdefghi" and "ihcdefgba" are equal. Also note that this implies $S = S$. The task is simple. Count the number of distinct strings. The answer can be huge enough, so calculate it modulo $998244353$. -----Input----- The first line contains three integers $n$, $m$ and $|A|$ ($2 \le n \le 10^9$, $1 \le m \le min(\frac n 2, 2 \cdot 10^5)$, $1 \le |A| \le 10^9$) — the length of the strings, the size of the array $b$ and the size of the set $A$, respectively. The second line contains $m$ integers $b_1, b_2, \dots, b_m$ ($1 \le b_i \le \frac n 2$, $b_1 < b_2 < \dots < b_m$). -----Output----- Print a single integer — the number of distinct strings of length $n$ with characters from set $A$ modulo $998244353$. -----Examples----- Input 3 1 2 1 Output 6 Input 9 2 26 2 3 Output 150352234 Input 12 3 1 2 5 6 Output 1 -----Note----- Here are all the distinct strings for the first example. The chosen letters 'a' and 'b' are there just to show that the characters in $A$ are different. "aaa" "aab" = "baa" "aba" "abb" = "bba" "bab" "bbb" Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n1 4 1 1\\n\", \"5\\n1 0 1 0 1\\n\", \"1\\n1000000000\\n\", \"4\\n3 3 3 3\\n\", \"4\\n3 3 2 3\\n\", \"16\\n1 1 2 3 3 4 5 6 6 6 4 3 2 1 1 1\\n\", \"1\\n1\\n\", \"20\\n1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2\\n\", \"1\\n0\\n\", \"2\\n0 1\\n\"], \"outputs\": [\"2\\n\", \"3\\n\", \"1\\n\", \"3\\n\", \"4\\n\", \"6\\n\", \"1\\n\", \"11\\n\", \"0\\n\", \"1\\n\"]}", "source": "primeintellect"}
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$. You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$. What is the minimum number of operations required to delete all elements from the multiset? -----Input----- The first line contains one integer $n$ ($1 \le n \le 5000$). The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$). -----Output----- Print one integer — the minimum number of operations required to delete all elements from the multiset. -----Examples----- Input 4 1 4 1 1 Output 2 Input 5 1 0 1 0 1 Output 3 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"7 10\\n\", \"5 1\\n\", \"7 1000\\n\", \"11 1000000000\\n\", \"999999937 1\\n\", \"999999929 2\\n\", \"999999929 1000000000\\n\", \"603530531 585244\\n\", \"7369339 31415926\\n\", \"2341 5001\\n\"], \"outputs\": [\"108.395919545675\", \"1.122569941449\", \"1083959.195456745256\", \"1061689977712182980.125000000000\", \"1.047197551197\", \"4.188790204786\", \"1047197551196597556.500000000000\", \"358676198261.124618709087\", \"1033542520749354.968872070312\", \"26190419.663812126875\"]}", "source": "primeintellect"}
It was decided in IT City to distinguish successes of local IT companies by awards in the form of stars covered with gold from one side. To order the stars it is necessary to estimate order cost that depends on the area of gold-plating. Write a program that can calculate the area of a star. A "star" figure having n ≥ 5 corners where n is a prime number is constructed the following way. On the circle of radius r n points are selected so that the distances between the adjacent ones are equal. Then every point is connected by a segment with two maximally distant points. All areas bounded by the segments parts are the figure parts. [Image] -----Input----- The only line of the input contains two integers n (5 ≤ n < 10^9, n is prime) and r (1 ≤ r ≤ 10^9) — the number of the star corners and the radius of the circumcircle correspondingly. -----Output----- Output one number — the star area. The relative error of your answer should not be greater than 10^{ - 7}. -----Examples----- Input 7 10 Output 108.395919545675 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 4\\n1 2 1\\n2 3 1\\n3 4 1\\n\", \"4 6\\n1 2 0\\n1 3 0\\n1 4 0\\n\", \"3 5\\n1 2 1\\n2 3 0\\n\", \"13 3\\n9 13 1\\n8 3 1\\n11 9 0\\n8 13 0\\n10 9 0\\n2 7 0\\n4 8 1\\n11 5 0\\n10 12 0\\n12 1 1\\n5 7 0\\n6 8 1\\n\", \"4 19\\n2 4 1\\n2 3 0\\n1 4 0\\n\", \"7 12\\n4 5 0\\n2 7 1\\n7 6 1\\n2 5 0\\n2 3 0\\n1 6 0\\n\", \"2 20\\n2 1 0\\n\", \"19 20\\n8 10 1\\n17 15 1\\n14 5 0\\n6 14 0\\n7 15 0\\n19 8 1\\n12 14 0\\n2 18 0\\n13 3 0\\n17 1 1\\n4 2 1\\n11 3 0\\n4 6 0\\n6 9 0\\n7 11 1\\n16 4 1\\n5 13 1\\n16 19 0\\n\", \"2 2\\n1 2 0\\n\", \"2 2\\n1 2 1\\n\"], \"outputs\": [\"252\", \"0\", \"210\", \"1842\", \"876856450\", \"824505797\", \"0\", \"928042611\", \"0\", \"2\"]}", "source": "primeintellect"}
You are given a tree (a connected undirected graph without cycles) of $n$ vertices. Each of the $n - 1$ edges of the tree is colored in either black or red. You are also given an integer $k$. Consider sequences of $k$ vertices. Let's call a sequence $[a_1, a_2, \ldots, a_k]$ good if it satisfies the following criterion: We will walk a path (possibly visiting same edge/vertex multiple times) on the tree, starting from $a_1$ and ending at $a_k$. Start at $a_1$, then go to $a_2$ using the shortest path between $a_1$ and $a_2$, then go to $a_3$ in a similar way, and so on, until you travel the shortest path between $a_{k-1}$ and $a_k$. If you walked over at least one black edge during this process, then the sequence is good. [Image] Consider the tree on the picture. If $k=3$ then the following sequences are good: $[1, 4, 7]$, $[5, 5, 3]$ and $[2, 3, 7]$. The following sequences are not good: $[1, 4, 6]$, $[5, 5, 5]$, $[3, 7, 3]$. There are $n^k$ sequences of vertices, count how many of them are good. Since this number can be quite large, print it modulo $10^9+7$. -----Input----- The first line contains two integers $n$ and $k$ ($2 \le n \le 10^5$, $2 \le k \le 100$), the size of the tree and the length of the vertex sequence. Each of the next $n - 1$ lines contains three integers $u_i$, $v_i$ and $x_i$ ($1 \le u_i, v_i \le n$, $x_i \in \{0, 1\}$), where $u_i$ and $v_i$ denote the endpoints of the corresponding edge and $x_i$ is the color of this edge ($0$ denotes red edge and $1$ denotes black edge). -----Output----- Print the number of good sequences modulo $10^9 + 7$. -----Examples----- Input 4 4 1 2 1 2 3 1 3 4 1 Output 252 Input 4 6 1 2 0 1 3 0 1 4 0 Output 0 Input 3 5 1 2 1 2 3 0 Output 210 -----Note----- In the first example, all sequences ($4^4$) of length $4$ except the following are good: $[1, 1, 1, 1]$ $[2, 2, 2, 2]$ $[3, 3, 3, 3]$ $[4, 4, 4, 4]$ In the second example, all edges are red, hence there aren't any good sequences. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.875
{"tests": "{\"inputs\": [\"4 3\\n2 14 3 4\\n\", \"5 2\\n0 2 1 255 254\\n\", \"10 3\\n112 184 161 156 118 231 191 128 91 229\\n\", \"9 3\\n174 149 118 124 166 146 219 233 107\\n\", \"8 4\\n180 195 13 195 61 24 132 160\\n\", \"1 4\\n51\\n\", \"2 4\\n218 213\\n\", \"3 3\\n212 167 3\\n\", \"100 7\\n39 242 238 155 95 30 129 132 72 59 22 61 133 113 99 81 253 204 160 109 92 187 11 227 145 148 100 42 78 14 113 91 140 197 123 182 4 185 208 27 182 207 63 146 102 79 159 195 26 100 72 127 157 159 152 53 0 117 111 125 19 188 179 145 240 50 205 207 113 246 42 56 181 13 188 244 159 222 119 204 87 182 212 175 199 69 81 45 30 32 238 255 81 61 177 125 149 121 35 38\\n\", \"4 2\\n122 108 224 154\\n\"], \"outputs\": [\"0 12 3 3\\n\", \"0 1 1 254 254\\n\", \"110 182 159 154 116 229 189 126 89 229\\n\", \"172 147 116 122 164 144 217 231 105\\n\", \"177 192 10 192 58 21 129 157\\n\", \"48\\n\", \"215 210\\n\", \"210 165 1\\n\", \"33 236 236 149 89 24 123 130 66 53 16 60 130 107 96 75 247 198 156 107 89 181 5 221 139 146 96 40 75 12 107 89 139 191 123 181 0 181 205 24 181 205 60 146 96 75 156 191 24 96 66 123 156 156 149 53 0 114 107 123 16 188 173 139 236 44 205 205 107 243 40 53 181 12 188 243 156 221 114 198 82 181 209 173 198 66 75 44 24 31 236 254 75 60 173 123 149 120 33 33\\n\", \"121 107 223 153\\n\"]}", "source": "primeintellect"}
Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked his students to implement the Posterization Image Filter. Their algorithm will be tested on an array of integers, where the $i$-th integer represents the color of the $i$-th pixel in the image. The image is in black and white, therefore the color of each pixel will be an integer between 0 and 255 (inclusive). To implement the filter, students are required to divide the black and white color range [0, 255] into groups of consecutive colors, and select one color in each group to be the group’s key. In order to preserve image details, the size of a group must not be greater than $k$, and each color should belong to exactly one group. Finally, the students will replace the color of each pixel in the array with that color’s assigned group key. To better understand the effect, here is an image of a basking turtle where the Posterization Filter was applied with increasing $k$ to the right. [Image] To make the process of checking the final answer easier, Professor Ibrahim wants students to divide the groups and assign the keys in a way that produces the lexicographically smallest possible array. -----Input----- The first line of input contains two integers $n$ and $k$ ($1 \leq n \leq 10^5$, $1 \leq k \leq 256$), the number of pixels in the image, and the maximum size of a group, respectively. The second line contains $n$ integers $p_1, p_2, \dots, p_n$ ($0 \leq p_i \leq 255$), where $p_i$ is the color of the $i$-th pixel. -----Output----- Print $n$ space-separated integers; the lexicographically smallest possible array that represents the image after applying the Posterization filter. -----Examples----- Input 4 3 2 14 3 4 Output 0 12 3 3 Input 5 2 0 2 1 255 254 Output 0 1 1 254 254 -----Note----- One possible way to group colors and assign keys for the first sample: Color $2$ belongs to the group $[0,2]$, with group key $0$. Color $14$ belongs to the group $[12,14]$, with group key $12$. Colors $3$ and $4$ belong to group $[3, 5]$, with group key $3$. Other groups won't affect the result so they are not listed here. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n0 0 0 0 1\\n0 1 2 3 4\\n\", \"5\\n0 0 1 0 1\\n0 1 2 2 4\\n\", \"4\\n1 0 0 0\\n2 3 4 2\\n\", \"10\\n0 0 0 0 0 0 0 0 0 1\\n4 0 8 4 7 8 5 5 7 2\\n\", \"50\\n0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0\\n28 4 33 22 4 35 36 31 42 25 50 33 25 36 18 23 23 28 43 3 18 31 1 2 15 22 40 43 29 32 28 35 18 27 48 40 14 36 27 50 40 5 48 14 36 24 32 33 26 50\\n\", \"100\\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0\\n86 12 47 46 45 31 20 47 58 79 23 70 35 72 37 20 16 64 46 87 57 7 84 72 70 3 14 40 17 42 30 99 12 20 38 98 14 40 4 83 10 15 47 30 83 58 12 7 97 46 17 6 41 13 87 37 36 12 7 25 26 35 69 13 18 5 9 53 72 28 13 51 5 57 14 64 28 25 91 96 57 69 9 12 97 7 56 42 31 15 88 16 41 88 86 13 89 81 3 42\\n\", \"10\\n1 0 0 0 0 0 0 0 0 0\\n6 2 7 8 2 9 0 5 4 2\\n\", \"100\\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\\n38 2 49 55 6 42 12 100 25 69 85 76 13 22 78 73 37 64 5 21 0 23 61 87 4 16 44 3 98 54 1 91 18 26 82 24 18 50 95 21 75 97 51 9 67 73 51 19 63 92 27 82 8 7 20 84 2 93 40 11 39 80 58 85 74 48 72 78 34 33 31 65 46 71 32 36 33 88 47 4 66 84 16 27 16 14 90 16 79 41 99 30 57 73 28 89 45 81 86 29\\n\", \"2\\n1 1\\n0 0\\n\", \"1\\n1\\n0\\n\"], \"outputs\": [\"5\\n1 2 3 4 5\\n\", \"2\\n4 5\\n\", \"1\\n1\\n\", \"2\\n2 10\\n\", \"2\\n3 20\\n\", \"1\\n44\\n\", \"6\\n5 8 4 9 6 1\\n\", \"52\\n57 93 58 63 49 3 28 95 39 61 23 22 14 86 99 91 32 75 41 90 87 24 36 76 12 7 54 30 92 50 38 1 31 71 74 65 72 67 45 97 42 6 5 19 48 66 81 98 29 100 8 53\\n\", \"1\\n1\\n\", \"1\\n1\\n\"]}", "source": "primeintellect"}
Valera's finally decided to go on holiday! He packed up and headed for a ski resort. Valera's fancied a ski trip but he soon realized that he could get lost in this new place. Somebody gave him a useful hint: the resort has n objects (we will consider the objects indexed in some way by integers from 1 to n), each object is either a hotel or a mountain. Valera has also found out that the ski resort had multiple ski tracks. Specifically, for each object v, the resort has at most one object u, such that there is a ski track built from object u to object v. We also know that no hotel has got a ski track leading from the hotel to some object. Valera is afraid of getting lost on the resort. So he wants you to come up with a path he would walk along. The path must consist of objects v_1, v_2, ..., v_{k} (k ≥ 1) and meet the following conditions: Objects with numbers v_1, v_2, ..., v_{k} - 1 are mountains and the object with number v_{k} is the hotel. For any integer i (1 ≤ i < k), there is exactly one ski track leading from object v_{i}. This track goes to object v_{i} + 1. The path contains as many objects as possible (k is maximal). Help Valera. Find such path that meets all the criteria of our hero! -----Input----- The first line contains integer n (1 ≤ n ≤ 10^5) — the number of objects. The second line contains n space-separated integers type_1, type_2, ..., type_{n} — the types of the objects. If type_{i} equals zero, then the i-th object is the mountain. If type_{i} equals one, then the i-th object is the hotel. It is guaranteed that at least one object is a hotel. The third line of the input contains n space-separated integers a_1, a_2, ..., a_{n} (0 ≤ a_{i} ≤ n) — the description of the ski tracks. If number a_{i} equals zero, then there is no such object v, that has a ski track built from v to i. If number a_{i} doesn't equal zero, that means that there is a track built from object a_{i} to object i. -----Output----- In the first line print k — the maximum possible path length for Valera. In the second line print k integers v_1, v_2, ..., v_{k} — the path. If there are multiple solutions, you can print any of them. -----Examples----- Input 5 0 0 0 0 1 0 1 2 3 4 Output 5 1 2 3 4 5 Input 5 0 0 1 0 1 0 1 2 2 4 Output 2 4 5 Input 4 1 0 0 0 2 3 4 2 Output 1 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"6 8 16\\n3 5 2 9 8 7\\n\", \"4 20 32\\n21 11 11 12\\n\", \"4 11 32\\n5 5 16 16\\n\", \"5 12 20\\n7 8 4 11 9\\n\", \"2 1 1\\n1 1\\n\", \"2 1 1\\n1 1000000\\n\", \"2 1 1\\n1000000000 1000000000\\n\", \"2 1 2\\n1 1\\n\", \"15 250 200\\n71 2 77 69 100 53 54 40 73 32 82 58 24 82 41\\n\", \"4 12 11\\n4 4 6 11\\n\"], \"outputs\": [\"Yes\\n4 2\\n3 1 2 6\\n5 4\\n\", \"Yes\\n1 3\\n1\\n2 3 4\\n\", \"No\\n\", \"No\\n\", \"Yes\\n1 1\\n1\\n2\\n\", \"Yes\\n1 1\\n1\\n2\\n\", \"Yes\\n1 1\\n1\\n2\\n\", \"No\\n\", \"Yes\\n11 3\\n13 10 8 15 6 7 12 4 1 9 3\\n11 14 5\\n\", \"Yes\\n3 1\\n1 2 3\\n4\\n\"]}", "source": "primeintellect"}
One department of some software company has $n$ servers of different specifications. Servers are indexed with consecutive integers from $1$ to $n$. Suppose that the specifications of the $j$-th server may be expressed with a single integer number $c_j$ of artificial resource units. In order for production to work, it is needed to deploy two services $S_1$ and $S_2$ to process incoming requests using the servers of the department. Processing of incoming requests of service $S_i$ takes $x_i$ resource units. The described situation happens in an advanced company, that is why each service may be deployed using not only one server, but several servers simultaneously. If service $S_i$ is deployed using $k_i$ servers, then the load is divided equally between these servers and each server requires only $x_i / k_i$ (that may be a fractional number) resource units. Each server may be left unused at all, or be used for deploying exactly one of the services (but not for two of them simultaneously). The service should not use more resources than the server provides. Determine if it is possible to deploy both services using the given servers, and if yes, determine which servers should be used for deploying each of the services. -----Input----- The first line contains three integers $n$, $x_1$, $x_2$ ($2 \leq n \leq 300\,000$, $1 \leq x_1, x_2 \leq 10^9$) — the number of servers that the department may use, and resource units requirements for each of the services. The second line contains $n$ space-separated integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$) — the number of resource units provided by each of the servers. -----Output----- If it is impossible to deploy both services using the given servers, print the only word "No" (without the quotes). Otherwise print the word "Yes" (without the quotes). In the second line print two integers $k_1$ and $k_2$ ($1 \leq k_1, k_2 \leq n$) — the number of servers used for each of the services. In the third line print $k_1$ integers, the indices of the servers that will be used for the first service. In the fourth line print $k_2$ integers, the indices of the servers that will be used for the second service. No index may appear twice among the indices you print in the last two lines. If there are several possible answers, it is allowed to print any of them. -----Examples----- Input 6 8 16 3 5 2 9 8 7 Output Yes 3 2 1 2 6 5 4 Input 4 20 32 21 11 11 12 Output Yes 1 3 1 2 3 4 Input 4 11 32 5 5 16 16 Output No Input 5 12 20 7 8 4 11 9 Output No -----Note----- In the first sample test each of the servers 1, 2 and 6 will will provide $8 / 3 = 2.(6)$ resource units and each of the servers 5, 4 will provide $16 / 2 = 8$ resource units. In the second sample test the first server will provide $20$ resource units and each of the remaining servers will provide $32 / 3 = 10.(6)$ resource units. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n9 3 5 7 3\\n5 8 1 4 5\\n\", \"3\\n1 2 9\\n10 1 1\\n\", \"1\\n7\\n4\\n\", \"5\\n3 10 9 10 6\\n4 3 3 6 9\\n\", \"1\\n5\\n8\\n\", \"1\\n5\\n1\\n\", \"5\\n1 7 6 9 1\\n6 1 1 7 10\\n\", \"5\\n7 3 3 1 8\\n7 2 1 1 2\\n\", \"100\\n4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1\\n1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4\\n\", \"100\\n1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4\\n4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1 4 1\\n\"], \"outputs\": [\"29\\n\", \"19\\n\", \"7\\n\", \"36\\n\", \"8\\n\", \"5\\n\", \"33\\n\", \"21\\n\", \"400\\n\", \"400\\n\"]}", "source": "primeintellect"}
Finally, a basketball court has been opened in SIS, so Demid has decided to hold a basketball exercise session. $2 \cdot n$ students have come to Demid's exercise session, and he lined up them into two rows of the same size (there are exactly $n$ people in each row). Students are numbered from $1$ to $n$ in each row in order from left to right. [Image] Now Demid wants to choose a team to play basketball. He will choose players from left to right, and the index of each chosen player (excluding the first one taken) will be strictly greater than the index of the previously chosen player. To avoid giving preference to one of the rows, Demid chooses students in such a way that no consecutive chosen students belong to the same row. The first student can be chosen among all $2n$ students (there are no additional constraints), and a team can consist of any number of students. Demid thinks, that in order to compose a perfect team, he should choose students in such a way, that the total height of all chosen students is maximum possible. Help Demid to find the maximum possible total height of players in a team he can choose. -----Input----- The first line of the input contains a single integer $n$ ($1 \le n \le 10^5$) — the number of students in each row. The second line of the input contains $n$ integers $h_{1, 1}, h_{1, 2}, \ldots, h_{1, n}$ ($1 \le h_{1, i} \le 10^9$), where $h_{1, i}$ is the height of the $i$-th student in the first row. The third line of the input contains $n$ integers $h_{2, 1}, h_{2, 2}, \ldots, h_{2, n}$ ($1 \le h_{2, i} \le 10^9$), where $h_{2, i}$ is the height of the $i$-th student in the second row. -----Output----- Print a single integer — the maximum possible total height of players in a team Demid can choose. -----Examples----- Input 5 9 3 5 7 3 5 8 1 4 5 Output 29 Input 3 1 2 9 10 1 1 Output 19 Input 1 7 4 Output 7 -----Note----- In the first example Demid can choose the following team as follows: [Image] In the second example Demid can choose the following team as follows: [Image] Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.375
{"tests": "{\"inputs\": [\"1000 1000000\\n\", \"1000 0\\n\", \"1000 1\\n\", \"1000 2\\n\", \"10000 3\\n\", \"10000 2000000000\\n\", \"4907 244618708\\n\", \"9030 982505993\\n\", \"4054 287739817\\n\", \"1587 1941728048\\n\"], \"outputs\": [\"1011.060722383550382782399454922040\\n\", \"1000\\n\", \"1000.000011000\\n\", \"1000.000022000000121000\\n\", \"10000.000330000003630000013310000\\n\", \"35849124123571.66604124267909180280\\n\", \"72346.60521729832602747499118899866\\n\", \"446019624.4250800721903194914485562\\n\", \"96047.41901816849953518710733057692\\n\", \"2996928752532.893524588882808611548\\n\"]}", "source": "primeintellect"}
The city administration of IT City decided to fix up a symbol of scientific and technical progress in the city's main square, namely an indicator board that shows the effect of Moore's law in real time. Moore's law is the observation that the number of transistors in a dense integrated circuit doubles approximately every 24 months. The implication of Moore's law is that computer performance as function of time increases exponentially as well. You are to prepare information that will change every second to display on the indicator board. Let's assume that every second the number of transistors increases exactly 1.000000011 times. -----Input----- The only line of the input contains a pair of integers n (1000 ≤ n ≤ 10 000) and t (0 ≤ t ≤ 2 000 000 000) — the number of transistors in the initial time and the number of seconds passed since the initial time. -----Output----- Output one number — the estimate of the number of transistors in a dence integrated circuit in t seconds since the initial time. The relative error of your answer should not be greater than 10^{ - 6}. -----Examples----- Input 1000 1000000 Output 1011.060722383550382782399454922040 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 5\\n..+.#\\n#+..+\\n+.#+.\\n\", \"4 10\\n...+.##+.+\\n+#++..+++#\\n++.#++++..\\n.+##.++#.+\\n\", \"2 2\\n..\\n++\\n\", \"5 1\\n.\\n.\\n.\\n.\\n.\\n\", \"20 20\\n..+#+.+++.+++#+..#++\\n..####+++#..++#+.+.+\\n+.+..+++..#.++++++++\\n+##++..+.##..#+++.++\\n++.+.+.+.++++.+++.++\\n.+++++.+#+++++...+#+\\n.+++#+++++++.+.++.++\\n...+.++++++.++#...++\\n+++.+++.+....#....+.\\n.++++++.+.+..++.++##\\n++++++..+.#++..+..+.\\n+..#+++++..+##+#++.+\\n+.+#+#....+.#+++#+.+\\n++.+.+++.++.+.#..#..\\n+.+..+++.+.+.++.++++\\n..#+++.++.++.#+.+++.\\n++++.#.+.+#..+++.+.+\\n+..+.+...+....+.....\\n#.###++++.+.++.+.+++\\n++..+.+.++.+..+.++++\\n\", \"4 100\\n.++++.+++++..+++.++++.+++++++++++.++++++++.+++.++++.+++++.+.+++.+++++++++++.+++++++++.+.++++++++++++\\n++++++++.++++++.++.++++++++.++.++...+++++++++++++++++++++++++.+++++++.++++++++++++++++++++++++++++.+\\n++++++++++++++++++++++++++++++++++++++++++++++++++++++++.++..++.++++.++++++++.+++++++++++.+++++++++.\\n++++++++.+++++++++++.+.+.+.+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\\n\", \"100 1\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n.\\n\", \"100 3\\n.++\\n+.+\\n++.\\n+.+\\n.++\\n+.+\\n+.+\\n++.\\n.++\\n++.\\n++.\\n+.+\\n+.+\\n.++\\n.++\\n+.+\\n+.+\\n.++\\n.++\\n.++\\n.++\\n+.+\\n++.\\n++.\\n+.+\\n.++\\n.++\\n+.+\\n+.+\\n+.+\\n++.\\n++.\\n++.\\n+.+\\n.++\\n++.\\n.++\\n++.\\n++.\\n+.+\\n++.\\n.++\\n+.+\\n+.+\\n+.+\\n++.\\n+.+\\n.++\\n.++\\n++.\\n++.\\n++.\\n++.\\n+.+\\n.++\\n.++\\n+.+\\n++.\\n+.+\\n+.+\\n++.\\n+.+\\n++.\\n+.+\\n+.+\\n++.\\n+.+\\n+.+\\n+.+\\n+.+\\n++.\\n++.\\n++.\\n.++\\n++.\\n++.\\n.++\\n.++\\n+.+\\n++.\\n+.+\\n+.+\\n+.+\\n+.+\\n++.\\n++.\\n++.\\n+.+\\n.++\\n+.+\\n++.\\n.++\\n.++\\n+.+\\n++.\\n++.\\n.++\\n++.\\n++.\\n+.+\\n\", \"10 10\\n.+++++++++\\n+++++++++.\\n.+++++++++\\n+++++++++.\\n.+++++++++\\n+++++++++.\\n.+++++++++\\n+++++++++.\\n.+++++++++\\n+++++++++.\\n\", \"10 10\\n.++++++++.\\n#########.\\n.++++++++.\\n.#########\\n.++++++++.\\n#########.\\n.++++++++.\\n.#########\\n.++++++++.\\n#########.\\n\"], \"outputs\": [\"14\", \"42\", \"Never\", \"4\", \"155\", \"4930\", \"99\", \"401\", \"908\", \"418\"]}", "source": "primeintellect"}
Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path. Joe's house has n floors, each floor is a segment of m cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right. Now Joe is on the n-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right. Joe moves by a particular algorithm. Every second he makes one of the following actions: If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved. Otherwise consider the next cell in the current direction of the gaze. If the cell is empty, then Joe moves into it, the gaze direction is preserved. If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite. If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits). Joe calms down as soon as he reaches any cell of the first floor. The figure below shows an example Joe's movements around the house. [Image] Determine how many seconds Joe will need to calm down. -----Input----- The first line contains two integers n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 10^4). Next n lines contain the description of Joe's house. The i-th of these lines contains the description of the (n - i + 1)-th floor of the house — a line that consists of m characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall. It is guaranteed that the first cell of the n-th floor is empty. -----Output----- Print a single number — the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen. 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 3 5 ..+.# #+..+ +.#+. Output 14 Input 4 10 ...+.##+.+ +#++..+++# ++.#++++.. .+##.++#.+ Output 42 Input 2 2 .. ++ Output Never Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 2 5\\n1 2 3 1\\n\", \"1 9 10\\n1\\n\", \"3 2 10\\n1 2 1\\n\", \"1 10 10\\n1\\n\", \"1 1000000000 1000000000\\n100000\\n\", \"2 4 1\\n1 1\\n\", \"5 3 2\\n1 1 2 1 1\\n\", \"5 3 3\\n1 2 2 1 1\\n\", \"10 5 1000000000\\n1 1 1 2 2 1 2 2 2 2\\n\", \"5 2 4\\n1 2 3 2 1\\n\"], \"outputs\": [\"12\\n\", \"1\\n\", \"0\\n\", \"0\\n\", \"0\\n\", \"2\\n\", \"7\\n\", \"0\\n\", \"10000000000\\n\", \"0\\n\"]}", "source": "primeintellect"}
This time the Berland Team Olympiad in Informatics is held in a remote city that can only be reached by one small bus. Bus has n passenger seats, seat i can be occupied only by a participant from the city a_{i}. Today the bus has completed m trips, each time bringing n participants. The participants were then aligned in one line in the order they arrived, with people from the same bus standing in the order of their seats (i. e. if we write down the cities where the participants came from, we get the sequence a_1, a_2, ..., a_{n} repeated m times). After that some teams were formed, each consisting of k participants form the same city standing next to each other in the line. Once formed, teams left the line. The teams were formed until there were no k neighboring participants from the same city. Help the organizers determine how many participants have left in the line after that process ended. We can prove that answer doesn't depend on the order in which teams were selected. -----Input----- The first line contains three integers n, k and m (1 ≤ n ≤ 10^5, 2 ≤ k ≤ 10^9, 1 ≤ m ≤ 10^9). The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5), where a_{i} is the number of city, person from which must take seat i in the bus. -----Output----- Output the number of remaining participants in the line. -----Examples----- Input 4 2 5 1 2 3 1 Output 12 Input 1 9 10 1 Output 1 Input 3 2 10 1 2 1 Output 0 -----Note----- In the second example, the line consists of ten participants from the same city. Nine of them will form a team. At the end, only one participant will stay in the line. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"snow affects sports such as skiing, snowboarding, and snowmachine travel.\\nsnowboarding is a recreational activity and olympic and paralympic sport.\\n\", \"'co-co-co, codeforces?!'\\n\", \"thun-thun-thunder, thunder, thunder\\nthunder, thun-, thunder\\nthun-thun-thunder, thunder\\nthunder, feel the thunder\\nlightning then the thunder\\nthunder, feel the thunder\\nlightning then the thunder\\nthunder, thunder\\n\", \"a'b-c-aa'ab.ac.ba!bb-bc-ca?cb-cc.aaa,aab'aac'aba-abb'abc-aca.acb!acc?baa'bab'bac!bba-bbb.bbc.bca\\n\", \"aaa!bbb'ccc?aaaaaa,aaabbb.aaaccc,bbbaaa-bbbbbb-bbbccc,cccaaa,cccbbb.cccccc'aaaaaaaaa-aaaaaabbb\\n\", \"bca'bbc,bbb!bba!bac'bab.baa,acc?acb.aca'abc?abb?aba?aac,aab?aaa?cc?cb'ca'bc!bb.ba-ac!ab.aa!c,b.a\\n\", \"a'aa!aaa.aab,aac,ab!aba,abb.abc'ac.aca'acb,acc!b'ba.baa.bab,bac!bb.bba,bbb,bbc,bc,bca'c?ca.cb,cc\\n\", \"aaa?c?ba,ac?cc?b.abb-bab.cb-a'aac'bb'aab-bbc'bca.ca.acb-baa.acc-aba?aa-bbb.bac?bc,aca-bba!abc!ab\\n\", \"baacbcaac-bcaabbaabaacccacc'acbb.b!-ababcab!b'a'aacaccaacbab!c\\nb\\nbca'bbcc!!\\n\", \"aaa aaaaaa\\n\"], \"outputs\": [\"141\\n\", \"25\\n\", \"183\\n\", \"97\\n\", \"91\\n\", \"97\\n\", \"97\\n\", \"97\\n\", \"76\\n\", \"10\\n\"]}", "source": "primeintellect"}
Arcady is a copywriter. His today's task is to type up an already well-designed story using his favorite text editor. Arcady types words, punctuation signs and spaces one after another. Each letter and each sign (including line feed) requires one keyboard click in order to be printed. Moreover, when Arcady has a non-empty prefix of some word on the screen, the editor proposes a possible autocompletion for this word, more precisely one of the already printed words such that its prefix matches the currently printed prefix if this word is unique. For example, if Arcady has already printed «codeforces», «coding» and «codeforces» once again, then there will be no autocompletion attempt for «cod», but if he proceeds with «code», the editor will propose «codeforces». With a single click Arcady can follow the editor's proposal, i.e. to transform the current prefix to it. Note that no additional symbols are printed after the autocompletion (no spaces, line feeds, etc). What is the minimum number of keyboard clicks Arcady has to perform to print the entire text, if he is not allowed to move the cursor or erase the already printed symbols? A word here is a contiguous sequence of latin letters bordered by spaces, punctuation signs and line/text beginnings/ends. Arcady uses only lowercase letters. For example, there are 20 words in «it's well-known that tic-tac-toe is a paper-and-pencil game for two players, x and o.». -----Input----- The only line contains Arcady's text, consisting only of lowercase latin letters, spaces, line feeds and the following punctuation signs: «.», «,», «?», «!», «'» and «-». The total amount of symbols doesn't exceed 3·10^5. It's guaranteed that all lines are non-empty. -----Output----- Print a single integer — the minimum number of clicks. -----Examples----- Input snow affects sports such as skiing, snowboarding, and snowmachine travel. snowboarding is a recreational activity and olympic and paralympic sport. Output 141 Input 'co-co-co, codeforces?!' Output 25 Input thun-thun-thunder, thunder, thunder thunder, thun-, thunder thun-thun-thunder, thunder thunder, feel the thunder lightning then the thunder thunder, feel the thunder lightning then the thunder thunder, thunder Output 183 -----Note----- In sample case one it's optimal to use autocompletion for the first instance of «snowboarding» after typing up «sn» and for the second instance of «snowboarding» after typing up «snowb». This will save 7 clicks. In sample case two it doesn't matter whether to use autocompletion or not. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n2 1 6\\n0 4 1\\n2 -1 3\\n1 -2 1\\n4 -1 1\\n\", \"8\\n0 0 1\\n0 0 2\\n0 0 3\\n0 0 4\\n0 0 5\\n0 0 6\\n0 0 7\\n0 0 8\\n\", \"4\\n1000000 -1000000 2\\n1000000 -1000000 3\\n-1000000 1000000 2\\n-1000000 1000000 1000000\\n\", \"15\\n-848 0 848\\n-758 0 758\\n-442 0 442\\n-372 0 372\\n-358 0 358\\n-355 0 355\\n-325 0 325\\n-216 0 216\\n-74 0 74\\n-14 0 14\\n-13 0 13\\n51 0 51\\n225 0 225\\n272 0 272\\n664 0 664\\n\", \"1\\n72989 14397 49999\\n\", \"2\\n281573 0 281573\\n706546 0 706546\\n\", \"2\\n425988 -763572 27398\\n425988 -763572 394103\\n\", \"4\\n-1000000 -1000000 1000000\\n-1000000 1000000 1000000\\n1000000 -1000000 1000000\\n1000000 1000000 1000000\\n\", \"20\\n-961747 0 961747\\n-957138 0 957138\\n-921232 0 921232\\n-887450 0 887450\\n-859109 0 859109\\n-686787 0 686787\\n-664613 0 664613\\n-625553 0 625553\\n-464803 0 464803\\n-422784 0 422784\\n-49107 0 49107\\n-37424 0 37424\\n134718 0 134718\\n178903 0 178903\\n304415 0 304415\\n335362 0 335362\\n365052 0 365052\\n670652 0 670652\\n812251 0 812251\\n986665 0 986665\\n\", \"2\\n-1000000 1000000 1000000\\n1000000 -1000000 1000000\\n\"], \"outputs\": [\"138.23007676\\n\", \"289.02652413\\n\", \"3141592653643.20020000\\n\", \"5142746.33322199\\n\", \"7853667477.85071660\\n\", \"1817381833095.13090000\\n\", \"490301532522.57819000\\n\", \"12566370614359.17200000\\n\", \"8507336011516.24610000\\n\", \"6283185307179.58590000\\n\"]}", "source": "primeintellect"}
The crowdedness of the discotheque would never stop our friends from having fun, but a bit more spaciousness won't hurt, will it? The discotheque can be seen as an infinite xy-plane, in which there are a total of n dancers. Once someone starts moving around, they will move only inside their own movement range, which is a circular area C_{i} described by a center (x_{i}, y_{i}) and a radius r_{i}. No two ranges' borders have more than one common point, that is for every pair (i, j) (1 ≤ i < j ≤ n) either ranges C_{i} and C_{j} are disjoint, or one of them is a subset of the other. Note that it's possible that two ranges' borders share a single common point, but no two dancers have exactly the same ranges. Tsukihi, being one of them, defines the spaciousness to be the area covered by an odd number of movement ranges of dancers who are moving. An example is shown below, with shaded regions representing the spaciousness if everyone moves at the same time. [Image] But no one keeps moving for the whole night after all, so the whole night's time is divided into two halves — before midnight and after midnight. Every dancer moves around in one half, while sitting down with friends in the other. The spaciousness of two halves are calculated separately and their sum should, of course, be as large as possible. The following figure shows an optimal solution to the example above. [Image] By different plans of who dances in the first half and who does in the other, different sums of spaciousness over two halves are achieved. You are to find the largest achievable value of this sum. -----Input----- The first line of input contains a positive integer n (1 ≤ n ≤ 1 000) — the number of dancers. The following n lines each describes a dancer: the i-th line among them contains three space-separated integers x_{i}, y_{i} and r_{i} ( - 10^6 ≤ x_{i}, y_{i} ≤ 10^6, 1 ≤ r_{i} ≤ 10^6), describing a circular movement range centered at (x_{i}, y_{i}) with radius r_{i}. -----Output----- Output one decimal number — the largest achievable sum of spaciousness over two halves of the night. The output is considered correct if it has a relative or absolute error of at most 10^{ - 9}. Formally, let your answer be a, and the jury's answer be b. Your answer is considered correct if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-9}$. -----Examples----- Input 5 2 1 6 0 4 1 2 -1 3 1 -2 1 4 -1 1 Output 138.23007676 Input 8 0 0 1 0 0 2 0 0 3 0 0 4 0 0 5 0 0 6 0 0 7 0 0 8 Output 289.02652413 -----Note----- The first sample corresponds to the illustrations in the legend. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3 3\\n1 2 1\\n1 2\\n2 3\\n1 3\\n\", \"3 2\\n1 1 0\\n1 2\\n1 3\\n\", \"4 4\\n1 2 0 1\\n1 3\\n1 2\\n2 3\\n2 4\\n\", \"5 5\\n1 1 1 2 1\\n3 4\\n1 2\\n2 3\\n4 5\\n4 5\\n\", \"4 10\\n2 4 1 4\\n3 2\\n4 2\\n4 1\\n3 1\\n4 1\\n1 3\\n3 2\\n2 1\\n3 1\\n2 4\\n\", \"6 27\\n4 2 10 1 3 10\\n1 2\\n5 2\\n6 3\\n4 6\\n3 1\\n3 2\\n5 6\\n6 5\\n2 3\\n3 6\\n2 1\\n1 3\\n4 3\\n4 2\\n5 2\\n1 5\\n6 4\\n1 4\\n4 1\\n3 1\\n3 6\\n1 6\\n5 6\\n4 3\\n1 3\\n2 6\\n1 3\\n\", \"50 49\\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\\n2 14\\n32 29\\n40 11\\n4 31\\n15 1\\n3 6\\n41 33\\n10 25\\n12 42\\n9 39\\n40 17\\n39 13\\n28 48\\n22 47\\n10 38\\n36 44\\n37 11\\n13 12\\n2 25\\n49 24\\n17 36\\n32 15\\n28 7\\n20 4\\n16 43\\n9 21\\n50 35\\n19 37\\n30 18\\n27 33\\n30 8\\n45 3\\n50 29\\n5 26\\n41 48\\n21 46\\n1 14\\n34 23\\n49 35\\n24 23\\n20 43\\n16 7\\n26 34\\n46 18\\n19 31\\n8 22\\n44 45\\n47 27\\n6 38\\n\", \"50 49\\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\\n36 50\\n5 8\\n16 12\\n4 9\\n35 21\\n17 22\\n34 32\\n43 19\\n28 13\\n48 8\\n47 25\\n33 24\\n41 3\\n16 24\\n15 21\\n11 49\\n38 18\\n47 39\\n38 39\\n18 46\\n25 5\\n14 7\\n9 13\\n27 45\\n6 20\\n29 42\\n10 22\\n50 45\\n32 28\\n2 17\\n19 34\\n1 11\\n31 37\\n36 31\\n3 23\\n6 49\\n10 26\\n29 27\\n48 37\\n12 41\\n33 4\\n46 35\\n30 7\\n43 42\\n26 1\\n20 30\\n40 2\\n14 15\\n44 23\\n\", \"50 50\\n1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1\\n25 30\\n7 47\\n42 15\\n19 38\\n16 10\\n8 6\\n26 50\\n3 44\\n41 26\\n32 17\\n48 23\\n15 50\\n39 12\\n24 2\\n11 13\\n21 20\\n44 19\\n16 35\\n43 45\\n35 41\\n38 34\\n42 4\\n40 9\\n30 5\\n24 21\\n14 28\\n7 46\\n8 9\\n12 20\\n17 36\\n33 3\\n31 29\\n48 43\\n49 28\\n13 1\\n5 32\\n37 18\\n2 25\\n33 46\\n1 27\\n37 6\\n49 10\\n14 23\\n27 47\\n39 11\\n45 29\\n22 4\\n18 34\\n40 22\\n31 36\\n\", \"3 10\\n1 2 9\\n3 2\\n3 2\\n3 2\\n1 3\\n3 2\\n3 1\\n1 2\\n1 3\\n1 3\\n1 2\\n\"], \"outputs\": [\"ALIVE\\n3 2 1 \\n\", \"ALIVE\\n2 1 \\n\", \"ALIVE\\n1 3 2 4 \\n\", \"ALIVE\\n5 4 1 3 2 \\n\", \"DEAD\\n\", \"ALIVE\\n14 19 18 11 1 16 15 2 27 25 24 20 13 12 9 6 5 26 23 22 21 17 10 8 7 4 3 \\n\", \"ALIVE\\n27 39 33 2 22 5 37 1 19 8 15 49 6 32 47 16 21 11 3 17 28 45 4 24 41 25 42 23 13 35 7 30 48 14 46 31 29 44 36 26 10 12 18 9 20 40 38 43 34 \\n\", \"ALIVE\\n31 8 44 26 38 24 28 1 34 33 39 10 2 21 11 18 19 17 20 42 5 15 48 22 43 46 25 36 7 29 9 23 4 41 12 14 3 40 13 35 49 16 32 45 37 27 6 30 47 \\n\", \"ALIVE\\n7 9 20 18 5 42 34 26 43 11 33 19 46 32 50 30 10 36 24 1 38 14 25 16 29 13 45 15 35 40 44 2 27 39 31 8 12 3 17 4 21 48 37 41 6 28 23 49 47 22 \\n\", \"ALIVE\\n10 7 9 8 6 5 4 3 2 1 \\n\"]}", "source": "primeintellect"}
Lee bought some food for dinner time, but Lee's friends eat dinner in a deadly way. Lee is so scared, he doesn't want to die, at least not before seeing Online IOI 2020... There are $n$ different types of food and $m$ Lee's best friends. Lee has $w_i$ plates of the $i$-th type of food and each friend has two different favorite types of food: the $i$-th friend's favorite types of food are $x_i$ and $y_i$ ($x_i \ne y_i$). Lee will start calling his friends one by one. Whoever is called will go to the kitchen and will try to eat one plate of each of his favorite food types. Each of the friends will go to the kitchen exactly once. The only problem is the following: if a friend will eat at least one plate of food (in total) then he will be harmless. But if there is nothing left for him to eat (neither $x_i$ nor $y_i$), he will eat Lee instead $\times\_\times$. Lee can choose the order of friends to call, so he'd like to determine if he can survive dinner or not. Also, he'd like to know the order itself. -----Input----- The first line contains two integers $n$ and $m$ ($2 \le n \le 10^5$; $1 \le m \le 2 \cdot 10^5$) — the number of different food types and the number of Lee's friends. The second line contains $n$ integers $w_1, w_2, \ldots, w_n$ ($0 \le w_i \le 10^6$) — the number of plates of each food type. The $i$-th line of the next $m$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$) — the favorite types of food of the $i$-th friend. -----Output----- If Lee can survive the dinner then print ALIVE (case insensitive), otherwise print DEAD (case insensitive). Also, if he can survive the dinner, print the order Lee should call friends. If there are multiple valid orders, print any of them. -----Examples----- Input 3 3 1 2 1 1 2 2 3 1 3 Output ALIVE 3 2 1 Input 3 2 1 1 0 1 2 1 3 Output ALIVE 2 1 Input 4 4 1 2 0 1 1 3 1 2 2 3 2 4 Output ALIVE 1 3 2 4 Input 5 5 1 1 1 2 1 3 4 1 2 2 3 4 5 4 5 Output ALIVE 5 4 1 3 2 Input 4 10 2 4 1 4 3 2 4 2 4 1 3 1 4 1 1 3 3 2 2 1 3 1 2 4 Output DEAD -----Note----- In the first example, any of the following orders of friends are correct : $[1, 3, 2]$, $[3, 1, 2]$, $[2, 3, 1]$, $[3, 2, 1]$. In the second example, Lee should call the second friend first (the friend will eat a plate of food $1$) and then call the first friend (the friend will eat a plate of food $2$). If he calls the first friend sooner than the second one, then the first friend will eat one plate of food $1$ and food $2$ and there will be no food left for the second friend to eat. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"10 32\\n1 2 3 4 5 5 5 5 5 5\\n\", \"5 8\\n4 4 8 2 1\\n\", \"5 353\\n191 148 145 73 150\\n\", \"100 12436\\n827 238 69 955 468 828 762 562 285 690 406 39 814 447 871 560 224 677 65 764 864 827 659 43 512 547 619 313 206 605 446 471 980 115 561 746 150 787 963 426 239 984 752 212 732 977 831 521 943 921 661 221 133 309 897 113 265 873 85 89 799 492 459 110 417 423 190 885 886 465 653 950 6 990 851 62 327 235 410 45 939 660 139 33 64 767 151 737 334 414 513 83 509 561 122 759 68 170 516 402\\n\", \"100 52010\\n722 943 547 336 1342 426 621 1319 1666 1843 1899 1744 1767 201 1144 1514 1114 1422 1539 1545 524 824 1839 1190 866 1585 1460 813 518 1256 508 30 180 1358 1639 1021 1422 794 1927 594 1849 1286 1045 1914 1662 415 650 868 553 408 1319 1588 1150 504 686 1267 79 791 566 941 263 357 1722 961 82 1588 1819 1269 562 841 938 736 972 319 1407 1726 201 1107 1701 1664 1046 313 843 1961 1492 806 500 802 1624 1651 904 230 940 49 769 1239 1564 1903 47 1559\\n\", \"100 75533\\n1535 204 1393 1665 561 1702 1961 35 1609 604 1884 242 1259 1302 710 814 17 1548 572 1627 1783 860 404 1041 1693 1160 152 1611 25 1926 1815 1561 1698 574 1268 110 1702 400 1893 2 709 123 805 481 1619 215 1330 986 986 188 735 1984 870 489 804 13 1719 542 793 1818 1586 1458 159 1657 305 109 1719 299 654 1583 264 1370 1292 40 1933 448 638 1550 1653 1950 318 810 1938 823 1016 475 170 1730 1980 433 137 203 1263 1331 861 1794 842 1626 1273 890\\n\", \"100 89826\\n1076 402 6 202 1196 687 966 243 1715 847 608 1446 1901 106 1622 222 865 204 1700 1001 1834 726 1158 1497 1928 1787 254 275 1788 1972 687 611 1983 1085 1876 311 1979 1845 1706 1360 528 1842 1041 1636 657 443 895 158 725 1884 13 1916 326 406 216 1040 677 620 785 1424 596 454 990 1303 186 1170 1783 1425 675 46 681 1347 494 512 1302 464 1469 494 467 1349 856 134 633 1390 474 151 1333 1902 1794 1830 937 172 756 891 1560 1344 1222 1713 1181 1418\\n\", \"1 1000000000\\n1000000000\\n\", \"1 1\\n4\\n\", \"1 2\\n1000000000\\n\"], \"outputs\": [\"1 2 3 3 3 4 4 4 4 4 \\n\", \"2 2 2 1 1 \\n\", \"71 71 70 70 71 \\n\", \"136 135 69 136 135 136 135 135 135 135 135 39 136 135 136 135 135 135 65 135 136 136 135 43 135 135 135 135 135 135 135 135 136 115 135 135 135 135 136 135 135 136 135 135 135 136 136 135 136 136 135 135 133 135 136 113 135 136 85 89 135 135 135 110 135 135 135 136 136 135 135 136 6 136 136 62 135 135 135 45 136 135 135 33 64 135 135 135 135 135 135 83 135 135 122 135 68 135 135 135 \\n\", \"585 585 547 336 585 426 585 585 585 585 585 585 585 201 585 585 585 585 585 585 524 585 585 585 585 585 585 585 518 585 508 30 180 585 585 585 585 585 586 585 585 585 585 585 585 415 585 585 553 408 585 585 585 504 585 585 79 585 566 585 263 357 585 585 82 585 585 585 562 585 585 585 585 319 585 585 201 585 585 585 585 313 585 586 585 585 500 585 585 585 585 230 585 49 585 585 585 585 47 585 \\n\", \"1077 204 1077 1078 561 1078 1078 35 1078 604 1078 242 1077 1077 710 814 17 1077 572 1078 1078 860 404 1041 1078 1077 152 1078 25 1078 1078 1078 1078 574 1077 110 1078 400 1078 2 709 123 805 481 1078 215 1077 986 986 188 735 1078 870 489 804 13 1078 542 793 1078 1078 1077 159 1078 305 109 1078 299 654 1078 264 1077 1077 40 1078 448 638 1077 1078 1078 318 810 1078 823 1016 475 170 1078 1078 433 137 203 1077 1077 861 1078 842 1078 1077 890 \\n\", \"1076 402 6 202 1196 687 966 243 1406 847 608 1406 1407 106 1406 222 865 204 1406 1001 1407 726 1158 1406 1407 1406 254 275 1406 1407 687 611 1407 1085 1407 311 1407 1407 1406 1360 528 1407 1041 1406 657 443 895 158 725 1407 13 1407 326 406 216 1040 677 620 785 1406 596 454 990 1303 186 1170 1406 1406 675 46 681 1347 494 512 1302 464 1406 494 467 1349 856 134 633 1390 474 151 1333 1407 1407 1407 937 172 756 891 1406 1344 1222 1406 1181 1406 \\n\", \"1000000000 \\n\", \"1 \\n\", \"2 \\n\"]}", "source": "primeintellect"}
Uh oh! Applications to tech companies are due soon, and you've been procrastinating by doing contests instead! (Let's pretend for now that it is actually possible to get a job in these uncertain times.) You have completed many programming projects. In fact, there are exactly $n$ types of programming projects, and you have completed $a_i$ projects of type $i$. Your résumé has limited space, but you want to carefully choose them in such a way that maximizes your chances of getting hired. You want to include several projects of the same type to emphasize your expertise, but you also don't want to include so many that the low-quality projects start slipping in. Specifically, you determine the following quantity to be a good indicator of your chances of getting hired: $$ f(b_1,\ldots,b_n)=\sum\limits_{i=1}^n b_i(a_i-b_i^2). $$ Here, $b_i$ denotes the number of projects of type $i$ you include in your résumé. Of course, you cannot include more projects than you have completed, so you require $0\le b_i \le a_i$ for all $i$. Your résumé only has enough room for $k$ projects, and you will absolutely not be hired if your résumé has empty space, so you require $\sum\limits_{i=1}^n b_i=k$. Find values for $b_1,\ldots, b_n$ that maximize the value of $f(b_1,\ldots,b_n)$ while satisfying the above two constraints. -----Input----- The first line contains two integers $n$ and $k$ ($1\le n\le 10^5$, $1\le k\le \sum\limits_{i=1}^n a_i$) — the number of types of programming projects and the résumé size, respectively. The next line contains $n$ integers $a_1,\ldots,a_n$ ($1\le a_i\le 10^9$) — $a_i$ is equal to the number of completed projects of type $i$. -----Output----- In a single line, output $n$ integers $b_1,\ldots, b_n$ that achieve the maximum value of $f(b_1,\ldots,b_n)$, while satisfying the requirements $0\le b_i\le a_i$ and $\sum\limits_{i=1}^n b_i=k$. If there are multiple solutions, output any. Note that you do not have to output the value $f(b_1,\ldots,b_n)$. -----Examples----- Input 10 32 1 2 3 4 5 5 5 5 5 5 Output 1 2 3 3 3 4 4 4 4 4 Input 5 8 4 4 8 2 1 Output 2 2 2 1 1 -----Note----- For the first test, the optimal answer is $f=-269$. Note that a larger $f$ value is possible if we ignored the constraint $\sum\limits_{i=1}^n b_i=k$. For the second test, the optimal answer is $f=9$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5 3\\n1 1 5 5\\n1 2\\n4 1\\n3 3\\n\", \"84 5\\n67 59 41 2\\n39 56\\n7 2\\n15 3\\n74 18\\n22 7\\n\", \"88294752 4\\n35290308 52335089 69408342 32650662\\n53981397 84728746\\n56670292 81664574\\n16110353 77620219\\n45476005 495904\\n\", \"41323044 1\\n28208465 38217978 31290636 25974675\\n39500641 36959975\\n\", \"40173958 3\\n16116682 7224989 33857761 5761096\\n26986287 15349000\\n23222390 8295154\\n5574952 21081873\\n\", \"49397978 9\\n35828361 4692148 30832063 21157937\\n38015170 25815246\\n25302604 1245031\\n35710367 30096200\\n20411634 24957987\\n31626304 14768886\\n43944112 42021291\\n14679371 40895905\\n18936340 12122304\\n48071293 32576660\\n\", \"995078784 16\\n623812784 347467913 233267735 582455459\\n306396207 277980472\\n78770458 786182235\\n815219022 611815511\\n786044046 363376661\\n290398527 925153345\\n846155104 388600335\\n91118406 155695413\\n287078777 918827034\\n432395923 126620751\\n52941102 329010981\\n237509699 743843421\\n556130019 602129300\\n441860811 912490531\\n65845315 380971409\\n575577330 104368064\\n962688269 452922228\\n\", \"1000 0\\n1000 1 1 1000\\n\", \"1000000000 0\\n1 1000000000 1000000000 1\\n\", \"1 0\\n1 1 1 1\\n\"], \"outputs\": [\"5\\n\", \"42\\n\", \"53802461\\n\", \"15325474\\n\", \"14239594\\n\", \"11385349\\n\", \"252255028\\n\", \"1998\\n\", \"1999999998\\n\", \"0\\n\"]}", "source": "primeintellect"}
Yura has been walking for some time already and is planning to return home. He needs to get home as fast as possible. To do this, Yura can use the instant-movement locations around the city. Let's represent the city as an area of $n \times n$ square blocks. Yura needs to move from the block with coordinates $(s_x,s_y)$ to the block with coordinates $(f_x,f_y)$. In one minute Yura can move to any neighboring by side block; in other words, he can move in four directions. Also, there are $m$ instant-movement locations in the city. Their coordinates are known to you and Yura. Yura can move to an instant-movement location in no time if he is located in a block with the same coordinate $x$ or with the same coordinate $y$ as the location. Help Yura to find the smallest time needed to get home. -----Input----- The first line contains two integers $n$ and $m$ — the size of the city and the number of instant-movement locations ($1 \le n \le 10^9$, $0 \le m \le 10^5$). The next line contains four integers $s_x$ $s_y$ $f_x$ $f_y$ — the coordinates of Yura's initial position and the coordinates of his home ($ 1 \le s_x, s_y, f_x, f_y \le n$). Each of the next $m$ lines contains two integers $x_i$ $y_i$ — coordinates of the $i$-th instant-movement location ($1 \le x_i, y_i \le n$). -----Output----- In the only line print the minimum time required to get home. -----Examples----- Input 5 3 1 1 5 5 1 2 4 1 3 3 Output 5 Input 84 5 67 59 41 2 39 56 7 2 15 3 74 18 22 7 Output 42 -----Note----- In the first example Yura needs to reach $(5, 5)$ from $(1, 1)$. He can do that in $5$ minutes by first using the second instant-movement location (because its $y$ coordinate is equal to Yura's $y$ coordinate), and then walking $(4, 1) \to (4, 2) \to (4, 3) \to (5, 3) \to (5, 4) \to (5, 5)$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n1 5 4 3 2\\n\", \"6\\n2 2 2 2 2 2\\n\", \"2\\n0 0\\n\", \"2\\n1 2\\n\", \"2\\n10 -1\\n\", \"5\\n934 235 171 111 197\\n\", \"100\\n0 1 1 1 0 0 0 2 1 2 2 1 2 2 2 0 0 2 1 2 0 1 1 0 2 0 1 2 2 0 2 0 1 0 1 2 0 2 1 1 0 1 0 1 0 0 1 2 2 2 2 1 1 1 0 2 1 0 0 0 0 0 1 0 2 0 1 0 0 2 0 2 2 1 0 2 2 0 2 0 2 1 2 1 1 1 0 2 1 0 2 1 1 2 1 2 0 1 2 2\\n\", \"2\\n-492673762 -496405053\\n\", \"100\\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\\n\"], \"outputs\": [\"3 2 1 0\\n\", \"0 0 0 0 0\\n\", \"0\\n\", \"0\\n\", \"1\\n\", \"3 4 4 4\\n\", \"36 29 38 33 35 33 34 31 28 21 21 21 17 14 17 18 21 22 23 24 24 25 25 26 25 25 25 25 24 24 23 23 22 22 22 21 21 21 21 20 20 19 19 18 17 17 17 17 17 17 17 17 17 16 16 16 15 14 13 12 11 11 10 10 9 9 8 7 7 6 6 6 6 5 5 5 4 4 3 3 3 3 3 3 3 2 2 2 1 1 1 1 1 1 1 0 0 0 0\\n\", \"1\\n\", \"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\\n\"]}", "source": "primeintellect"}
Andrew skipped lessons on the subject 'Algorithms and Data Structures' for the entire term. When he came to the final test, the teacher decided to give him a difficult task as a punishment. The teacher gave Andrew an array of n numbers a_1, ..., a_{n}. After that he asked Andrew for each k from 1 to n - 1 to build a k-ary heap on the array and count the number of elements for which the property of the minimum-rooted heap is violated, i.e. the value of an element is less than the value of its parent. Andrew looked up on the Wikipedia that a k-ary heap is a rooted tree with vertices in elements of the array. If the elements of the array are indexed from 1 to n, then the children of element v are elements with indices k(v - 1) + 2, ..., kv + 1 (if some of these elements lie outside the borders of the array, the corresponding children are absent). In any k-ary heap every element except for the first one has exactly one parent; for the element 1 the parent is absent (this element is the root of the heap). Denote p(v) as the number of the parent of the element with the number v. Let's say that for a non-root element v the property of the heap is violated if a_{v} < a_{p}(v). Help Andrew cope with the task! -----Input----- The first line contains a single integer n (2 ≤ n ≤ 2·10^5). The second line contains n space-separated integers a_1, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9). -----Output----- in a single line print n - 1 integers, separate the consecutive numbers with a single space — the number of elements for which the property of the k-ary heap is violated, for k = 1, 2, ..., n - 1. -----Examples----- Input 5 1 5 4 3 2 Output 3 2 1 0 Input 6 2 2 2 2 2 2 Output 0 0 0 0 0 -----Note----- Pictures with the heaps for the first sample are given below; elements for which the property of the heap is violated are marked with red. [Image] [Image] $\therefore$ [Image] In the second sample all elements are equal, so the property holds for all pairs. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.375
{"tests": "{\"inputs\": [\"5 4\\n3 5 1 4\\n\", \"4 3\\n1 2 4\\n\", \"5 1\\n1\\n\", \"5 5\\n1 1 4 2 2\\n\", \"10 20\\n10 1 5 7 1 2 5 3 6 3 9 4 3 4 9 6 8 4 9 6\\n\", \"1 1\\n1\\n\", \"1 2\\n1 1\\n\", \"2 1\\n2\\n\", \"2 1\\n1\\n\"], \"outputs\": [\"1 3\\n2 5\\n1 4\\n1 5\\n1 5\\n\", \"1 3\\n1 2\\n3 4\\n1 4\\n\", \"1 1\\n2 2\\n3 3\\n4 4\\n5 5\\n\", \"1 3\\n1 3\\n3 4\\n1 4\\n5 5\\n\", \"1 8\\n1 7\\n1 6\\n1 9\\n1 6\\n1 8\\n1 9\\n1 10\\n1 10\\n1 10\\n\", \"1 1\\n\", \"1 1\\n\", \"1 2\\n1 2\\n\", \"1 1\\n2 2\\n\"]}", "source": "primeintellect"}
Polycarp is a frequent user of the very popular messenger. He's chatting with his friends all the time. He has $n$ friends, numbered from $1$ to $n$. Recall that a permutation of size $n$ is an array of size $n$ such that each integer from $1$ to $n$ occurs exactly once in this array. So his recent chat list can be represented with a permutation $p$ of size $n$. $p_1$ is the most recent friend Polycarp talked to, $p_2$ is the second most recent and so on. Initially, Polycarp's recent chat list $p$ looks like $1, 2, \dots, n$ (in other words, it is an identity permutation). After that he receives $m$ messages, the $j$-th message comes from the friend $a_j$. And that causes friend $a_j$ to move to the first position in a permutation, shifting everyone between the first position and the current position of $a_j$ by $1$. Note that if the friend $a_j$ is in the first position already then nothing happens. For example, let the recent chat list be $p = [4, 1, 5, 3, 2]$: if he gets messaged by friend $3$, then $p$ becomes $[3, 4, 1, 5, 2]$; if he gets messaged by friend $4$, then $p$ doesn't change $[4, 1, 5, 3, 2]$; if he gets messaged by friend $2$, then $p$ becomes $[2, 4, 1, 5, 3]$. For each friend consider all position he has been at in the beginning and after receiving each message. Polycarp wants to know what were the minimum and the maximum positions. -----Input----- The first line contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$) — the number of Polycarp's friends and the number of received messages, respectively. The second line contains $m$ integers $a_1, a_2, \dots, a_m$ ($1 \le a_i \le n$) — the descriptions of the received messages. -----Output----- Print $n$ pairs of integers. For each friend output the minimum and the maximum positions he has been in the beginning and after receiving each message. -----Examples----- Input 5 4 3 5 1 4 Output 1 3 2 5 1 4 1 5 1 5 Input 4 3 1 2 4 Output 1 3 1 2 3 4 1 4 -----Note----- In the first example, Polycarp's recent chat list looks like this: $[1, 2, 3, 4, 5]$ $[3, 1, 2, 4, 5]$ $[5, 3, 1, 2, 4]$ $[1, 5, 3, 2, 4]$ $[4, 1, 5, 3, 2]$ So, for example, the positions of the friend $2$ are $2, 3, 4, 4, 5$, respectively. Out of these $2$ is the minimum one and $5$ is the maximum one. Thus, the answer for the friend $2$ is a pair $(2, 5)$. In the second example, Polycarp's recent chat list looks like this: $[1, 2, 3, 4]$ $[1, 2, 3, 4]$ $[2, 1, 3, 4]$ $[4, 2, 1, 3]$ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 4 4\\n\", \"1 1 1\\n\", \"50 50 1\\n\", \"4 4 9\\n\", \"10 10 10\\n\", \"50 50 1\\n\", \"50 50 10\\n\", \"2 2 3\\n\", \"1 2 1\\n\"], \"outputs\": [\"8\\n(1,1) (2,1) (2,2)\\n(1,1) (1,2)\\n(1,1) (2,1)\\n(1,1)\\n\", \"1\\n(1,1)\\n\", \"1\\n(1,1)\\n\", \"26\\n(1,1) (1,2) (1,3) (2,3)\\n(1,1) (1,2) (2,2) (3,2)\\n(1,1) (2,1) (3,1) (4,1)\\n(1,1) (1,2) (1,3)\\n(1,1) (1,2) (2,2)\\n(1,1) (2,1) (3,1)\\n(1,1) (1,2)\\n(1,1) (2,1)\\n(1,1)\\n\", \"30\\n(1,1) (1,2) (1,3) (1,4)\\n(1,1) (1,2) (1,3) (2,3)\\n(1,1) (1,2) (2,2) (3,2)\\n(1,1) (2,1) (3,1) (4,1)\\n(1,1) (1,2) (1,3)\\n(1,1) (1,2) (2,2)\\n(1,1) (2,1) (3,1)\\n(1,1) (1,2)\\n(1,1) (2,1)\\n(1,1)\\n\", \"1\\n(1,1)\\n\", \"30\\n(1,1) (1,2) (1,3) (1,4)\\n(1,1) (1,2) (1,3) (2,3)\\n(1,1) (1,2) (2,2) (3,2)\\n(1,1) (2,1) (3,1) (4,1)\\n(1,1) (1,2) (1,3)\\n(1,1) (1,2) (2,2)\\n(1,1) (2,1) (3,1)\\n(1,1) (1,2)\\n(1,1) (2,1)\\n(1,1)\\n\", \"5\\n(1,1) (1,2)\\n(1,1) (2,1)\\n(1,1)\\n\", \"1\\n(1,1)\\n\"]}", "source": "primeintellect"}
Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix". Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in the i-th row and j-th column as (i, j). Two cells (i, j) and (p, q) of the matrix are adjacent if |i - p| + |j - q| = 1. A path is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length. Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of the k candies in the matrix one by one. For each candy Inna chooses cell (i, j) that will contains the candy, and also chooses the path that starts in cell (1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell (1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used. Help Inna to minimize the penalty in the game. -----Input----- The first line of the input contains three integers n, m and k (1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m). -----Output----- In the first line print an integer — Inna's minimum penalty in the game. In the next k lines print the description of the path for each candy. The description of the path of the candy that is placed i-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format (i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them. Please follow the output format strictly! If your program passes the first pretest, then the output format is correct. -----Examples----- Input 4 4 4 Output 8 (1,1) (2,1) (2,2) (1,1) (1,2) (1,1) (2,1) (1,1) -----Note----- Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8. Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"15 5\\n0 3 7 14 15\\n11 11\\n\", \"13 4\\n0 3 7 13\\n9 9\\n\", \"4 5\\n0 3 1 2 4\\n1 1\\n\", \"4 5\\n0 3 1 2 4\\n2 1\\n\", \"15 5\\n15 3 7 0 14\\n11 11\\n\", \"15 5\\n15 14 7 3 0\\n11 11\\n\", \"4 3\\n0 4 2\\n2 2\\n\", \"100 5\\n3 100 7 13 0\\n99 5\\n\", \"30 8\\n0 1 9 10 23 24 26 30\\n40 7\\n\"], \"outputs\": [\"45\", \"-1\", \"7\", \"5\", \"45\", \"45\", \"6\", \"191\", \"30\"]}", "source": "primeintellect"}
If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya. On the way from Denis's house to the girl's house is a road of $n$ lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light. Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up $g$ seconds green, and then $r$ seconds red, then again $g$ seconds green and so on. Formally, the road can be represented as a segment $[0, n]$. Initially, Denis is at point $0$. His task is to get to point $n$ in the shortest possible time. He knows many different integers $d_1, d_2, \ldots, d_m$, where $0 \leq d_i \leq n$  — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on. Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed: He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by $\pm 1$ in $1$ second. While doing so, he must always stay inside the segment $[0, n]$. He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by $+1$ and he walked on a safety island, then he can change his position by $\pm 1$. Otherwise, he can change his position only by $+1$. Similarly, if in the previous second he changed his position by $-1$, on a safety island he can change position by $\pm 1$, and at any other point by $-1$. At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on. Denis has crossed the road as soon as his coordinate becomes equal to $n$. This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do. -----Input----- The first line contains two integers $n$ and $m$ $(1 \leq n \leq 10^6, 2 \leq m \leq min(n + 1, 10^4))$  — road width and the number of safety islands. The second line contains $m$ distinct integers $d_1, d_2, \ldots, d_m$ $(0 \leq d_i \leq n)$  — the points where the safety islands are located. It is guaranteed that there are $0$ and $n$ among them. The third line contains two integers $g, r$ $(1 \leq g, r \leq 1000)$  — the time that the green light stays on and the time that the red light stays on. -----Output----- Output a single integer  — the minimum time for which Denis can cross the road with obeying all the rules. If it is impossible to cross the road output $-1$. -----Examples----- Input 15 5 0 3 7 14 15 11 11 Output 45 Input 13 4 0 3 7 13 9 9 Output -1 -----Note----- In the first test, the optimal route is:      for the first green light, go to $7$ and return to $3$. In this case, we will change the direction of movement at the point $7$, which is allowed, since there is a safety island at this point. In the end, we will be at the point of $3$, where there is also a safety island. The next $11$ seconds we have to wait for the red light.      for the second green light reaches $14$. Wait for the red light again.      for $1$ second go to $15$. As a result, Denis is at the end of the road. In total, $45$ seconds are obtained. In the second test, it is impossible to cross the road according to all the rules. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n2 1 3\\n3 3 6\\n99995 9998900031 9998900031\\n\", \"1\\n4 13 13\\n\", \"10\\n2 1 3\\n2 1 3\\n2 1 3\\n2 1 3\\n2 1 3\\n2 1 3\\n2 1 3\\n2 1 3\\n2 1 3\\n2 1 3\\n\", \"1\\n4 12 13\\n\", \"5\\n3 7 7\\n4 13 13\\n5 21 21\\n6 31 31\\n7 42 43\\n\", \"1\\n5 4 4\\n\", \"1\\n3 1 1\\n\", \"1\\n3 7 7\\n\", \"1\\n2 2 3\\n\"], \"outputs\": [\"1 2 1 \\n1 3 2 3 \\n1 \\n\", \"1 \\n\", \"1 2 1 \\n1 2 1 \\n1 2 1 \\n1 2 1 \\n1 2 1 \\n1 2 1 \\n1 2 1 \\n1 2 1 \\n1 2 1 \\n1 2 1 \\n\", \"4 1 \\n\", \"1 \\n1 \\n1 \\n1 \\n7 1 \\n\", \"3 \\n\", \"1 \\n\", \"1 \\n\", \"2 1 \\n\"]}", "source": "primeintellect"}
You are given a complete directed graph $K_n$ with $n$ vertices: each pair of vertices $u \neq v$ in $K_n$ have both directed edges $(u, v)$ and $(v, u)$; there are no self-loops. You should find such a cycle in $K_n$ that visits every directed edge exactly once (allowing for revisiting vertices). We can write such cycle as a list of $n(n - 1) + 1$ vertices $v_1, v_2, v_3, \dots, v_{n(n - 1) - 1}, v_{n(n - 1)}, v_{n(n - 1) + 1} = v_1$ — a visiting order, where each $(v_i, v_{i + 1})$ occurs exactly once. Find the lexicographically smallest such cycle. It's not hard to prove that the cycle always exists. Since the answer can be too large print its $[l, r]$ segment, in other words, $v_l, v_{l + 1}, \dots, v_r$. -----Input----- The first line contains the single integer $T$ ($1 \le T \le 100$) — the number of test cases. Next $T$ lines contain test cases — one per line. The first and only line of each test case contains three integers $n$, $l$ and $r$ ($2 \le n \le 10^5$, $1 \le l \le r \le n(n - 1) + 1$, $r - l + 1 \le 10^5$) — the number of vertices in $K_n$, and segment of the cycle to print. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$ and the total sum of $r - l + 1$ doesn't exceed $10^5$. -----Output----- For each test case print the segment $v_l, v_{l + 1}, \dots, v_r$ of the lexicographically smallest cycle that visits every edge exactly once. -----Example----- Input 3 2 1 3 3 3 6 99995 9998900031 9998900031 Output 1 2 1 1 3 2 3 1 -----Note----- In the second test case, the lexicographically minimum cycle looks like: $1, 2, 1, 3, 2, 3, 1$. In the third test case, it's quite obvious that the cycle should start and end in vertex $1$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"2\\n0 40 100 100\\n60 0 40 40\\n0 60 0 45\\n0 60 55 0\\n\", \"3\\n0 0 100 0 100 0 0 0\\n100 0 100 0 0 0 100 100\\n0 0 0 100 100 0 0 0\\n100 100 0 0 0 0 100 100\\n0 100 0 100 0 0 100 0\\n100 100 100 100 100 0 0 0\\n100 0 100 0 0 100 0 0\\n100 0 100 0 100 100 100 0\\n\", \"2\\n0 21 41 26\\n79 0 97 33\\n59 3 0 91\\n74 67 9 0\\n\", \"3\\n0 7 38 22 3 66 32 77\\n93 0 93 61 45 40 6 92\\n62 7 0 32 8 46 56 29\\n78 39 68 0 37 24 84 42\\n97 55 92 63 0 46 62 100\\n34 60 54 76 54 0 39 67\\n68 94 44 16 38 61 0 98\\n23 8 71 58 0 33 2 0\\n\", \"2\\n0 50 50 50\\n50 0 50 50\\n50 50 0 50\\n50 50 50 0\\n\", \"2\\n0 70 12 95\\n30 0 98 85\\n88 2 0 81\\n5 15 19 0\\n\", \"2\\n0 0 100 100\\n100 0 100 100\\n0 0 0 0\\n0 0 100 0\\n\", \"2\\n0 31 4 83\\n69 0 1 74\\n96 99 0 71\\n17 26 29 0\\n\", \"3\\n0 53 31 33 2 34 22 21\\n47 0 72 36 11 16 3 86\\n69 28 0 49 25 6 92 54\\n67 64 51 0 54 65 15 24\\n98 89 75 46 0 48 65 31\\n66 84 94 35 52 0 64 46\\n78 97 8 85 35 36 0 62\\n79 14 46 76 69 54 38 0\\n\"], \"outputs\": [\"1.75\\n\", \"12\\n\", \"3.141592\\n\", \"6.8196427571\\n\", \"1.5\\n\", \"2.51764\\n\", \"4\\n\", \"2.792594\\n\", \"4.23995819508\\n\"]}", "source": "primeintellect"}
The annual college sports-ball tournament is approaching, which for trademark reasons we'll refer to as Third Month Insanity. There are a total of 2^{N} teams participating in the tournament, numbered from 1 to 2^{N}. The tournament lasts N rounds, with each round eliminating half the teams. The first round consists of 2^{N} - 1 games, numbered starting from 1. In game i, team 2·i - 1 will play against team 2·i. The loser is eliminated and the winner advances to the next round (there are no ties). Each subsequent round has half as many games as the previous round, and in game i the winner of the previous round's game 2·i - 1 will play against the winner of the previous round's game 2·i. Every year the office has a pool to see who can create the best bracket. A bracket is a set of winner predictions for every game. For games in the first round you may predict either team to win, but for games in later rounds the winner you predict must also be predicted as a winner in the previous round. Note that the bracket is fully constructed before any games are actually played. Correct predictions in the first round are worth 1 point, and correct predictions in each subsequent round are worth twice as many points as the previous, so correct predictions in the final game are worth 2^{N} - 1 points. For every pair of teams in the league, you have estimated the probability of each team winning if they play against each other. Now you want to construct a bracket with the maximum possible expected score. -----Input----- Input will begin with a line containing N (2 ≤ N ≤ 6). 2^{N} lines follow, each with 2^{N} integers. The j-th column of the i-th row indicates the percentage chance that team i will defeat team j, unless i = j, in which case the value will be 0. It is guaranteed that the i-th column of the j-th row plus the j-th column of the i-th row will add to exactly 100. -----Output----- Print the maximum possible expected score over all possible brackets. Your answer must be correct to within an absolute or relative error of 10^{ - 9}. Formally, let your answer be a, and the jury's answer be b. Your answer will be considered correct, if $\frac{|a - b|}{\operatorname{max}(1,|b|)} \leq 10^{-9}$. -----Examples----- Input 2 0 40 100 100 60 0 40 40 0 60 0 45 0 60 55 0 Output 1.75 Input 3 0 0 100 0 100 0 0 0 100 0 100 0 0 0 100 100 0 0 0 100 100 0 0 0 100 100 0 0 0 0 100 100 0 100 0 100 0 0 100 0 100 100 100 100 100 0 0 0 100 0 100 0 0 100 0 0 100 0 100 0 100 100 100 0 Output 12 Input 2 0 21 41 26 79 0 97 33 59 3 0 91 74 67 9 0 Output 3.141592 -----Note----- In the first example, you should predict teams 1 and 4 to win in round 1, and team 1 to win in round 2. Recall that the winner you predict in round 2 must also be predicted as a winner in round 1. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\nMisha ILoveCodeforces\\nVasya Petrov\\nPetrov VasyaPetrov123\\nILoveCodeforces MikeMirzayanov\\nPetya Ivanov\\n\", \"1\\nMisha Vasya\\n\", \"10\\na b\\nb c\\nc d\\nd e\\ne f\\nf g\\ng h\\nh i\\ni j\\nj k\\n\", \"5\\n123abc abc123\\nabc123 a1b2c3\\na1b2c3 1A2B3C\\n1 2\\n2 Misha\\n\", \"8\\nM F\\nS D\\n1 2\\nF G\\n2 R\\nD Q\\nQ W\\nW e\\n\", \"17\\nn5WhQ VCczxtxKwFio5U\\nVCczxtxKwFio5U 1WMVGA17cd1LRcp4r\\n1WMVGA17cd1LRcp4r SJl\\nSJl D8bPUoIft5v1\\nNAvvUgunbPZNCL9ZY2 jnLkarKYsotz\\nD8bPUoIft5v1 DnDkHi7\\njnLkarKYsotz GfjX109HSQ81gFEBJc\\nGfjX109HSQ81gFEBJc kBJ0zrH78mveJ\\nkBJ0zrH78mveJ 9DrAypYW\\nDnDkHi7 3Wkho2PglMDaFQw\\n3Wkho2PglMDaFQw pOqW\\n9DrAypYW G3y0cXXGsWAh\\npOqW yr1Ec\\nG3y0cXXGsWAh HrmWWg5u4Hsy\\nyr1Ec GkFeivXjQ01\\nGkFeivXjQ01 mSsWgbCCZcotV4goiA\\nHrmWWg5u4Hsy zkCmEV\\n\", \"10\\nH1nauWCJOImtVqXk gWPMQ9DHv5CtkYp9lwm9\\nSEj 2knOMLyzr\\n0v69ijnAc S7d7zGTjmlku01Gv\\n2knOMLyzr otGmEd\\nacwr3TfMV7oCIp RUSVFa9TIWlLsd7SB\\nS7d7zGTjmlku01Gv Gd6ZufVmQnBpi\\nS1 WOJLpk\\nWOJLpk Gu\\nRUSVFa9TIWlLsd7SB RFawatGnbVB\\notGmEd OTB1zKiOI\\n\", \"14\\nTPdoztSZROpjZe z6F8bYFvnER4V5SP0n\\n8Aa3PQY3hzHZTPEUz fhrZZPJ3iUS\\nm9p888KaZAoQaO KNmdRSAlUVn8zXOM0\\nAO s1VGWTCbHzM\\ni 4F\\nfhrZZPJ3iUS j0OVZQF6MvNcKN9xDZFJ\\nDnlkXtaKNlYEI2ApBuwu DMA9i8ScKRxwhe72a3\\nj0OVZQF6MvNcKN9xDZFJ DzjmeNqN0H4Teq0Awr\\n4F wJcdxt1kwqfDeJ\\nqxXlsa5t RHCL1K6aUyns\\nr6WYbDaXt hEHw\\nJ0Usg DKdKMFJ6tK8XA\\nz6F8bYFvnER4V5SP0n 0alJ\\nMijh2O6 qic8kXWuR6\\n\", \"14\\nHAXRxayyf1Dj1F0mT hjR4A8IQMb0nyBtqG\\nWNuMJa5Jg05qkqZOrL noNkWXrSidHGwxgbQ\\nmOitVy6W52s0FENMz6 oLUkLNfojssvLvb1t\\nhjR4A8IQMb0nyBtqG oA7uBFu4Oo\\noA7uBFu4Oo M450\\nM450 LXEzO4\\noLUkLNfojssvLvb1t YG5\\nnoNkWXrSidHGwxgbQ L\\nL YBWzu4W\\nYBWzu4W ML\\nML scVZE9m8JnH\\nLXEzO4 Ne0oBPY0Iy\\nscVZE9m8JnH GXhznv\\nYG5 UY08abilYF1LaXj49hQ\\n\"], \"outputs\": [\"3\\nPetya Ivanov\\nMisha MikeMirzayanov\\nVasya VasyaPetrov123\\n\", \"1\\nMisha Vasya\\n\", \"1\\na k\\n\", \"2\\n123abc 1A2B3C\\n1 Misha\\n\", \"3\\nM G\\n1 R\\nS e\\n\", \"2\\nn5WhQ mSsWgbCCZcotV4goiA\\nNAvvUgunbPZNCL9ZY2 zkCmEV\\n\", \"5\\n0v69ijnAc Gd6ZufVmQnBpi\\nS1 Gu\\nSEj OTB1zKiOI\\nacwr3TfMV7oCIp RFawatGnbVB\\nH1nauWCJOImtVqXk gWPMQ9DHv5CtkYp9lwm9\\n\", \"10\\nTPdoztSZROpjZe 0alJ\\nJ0Usg DKdKMFJ6tK8XA\\nDnlkXtaKNlYEI2ApBuwu DMA9i8ScKRxwhe72a3\\n8Aa3PQY3hzHZTPEUz DzjmeNqN0H4Teq0Awr\\nm9p888KaZAoQaO KNmdRSAlUVn8zXOM0\\nqxXlsa5t RHCL1K6aUyns\\nr6WYbDaXt hEHw\\nMijh2O6 qic8kXWuR6\\nAO s1VGWTCbHzM\\ni wJcdxt1kwqfDeJ\\n\", \"3\\nWNuMJa5Jg05qkqZOrL GXhznv\\nHAXRxayyf1Dj1F0mT Ne0oBPY0Iy\\nmOitVy6W52s0FENMz6 UY08abilYF1LaXj49hQ\\n\"]}", "source": "primeintellect"}
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point. Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that. -----Input----- The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests. Next q lines contain the descriptions of the requests, one per line. Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20. The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone. -----Output----- In the first line output the integer n — the number of users that changed their handles at least once. In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order. Each user who changes the handle must occur exactly once in this description. -----Examples----- Input 5 Misha ILoveCodeforces Vasya Petrov Petrov VasyaPetrov123 ILoveCodeforces MikeMirzayanov Petya Ivanov Output 3 Petya Ivanov Misha MikeMirzayanov Vasya VasyaPetrov123 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n+\\n+\\n- 2\\n+\\n- 3\\n+\\n- 1\\n- 4\\n\", \"1\\n- 1\\n+\\n\", \"3\\n+\\n+\\n+\\n- 2\\n- 1\\n- 3\\n\", \"1\\n+\\n- 1\\n\", \"10\\n+\\n- 3\\n+\\n+\\n+\\n+\\n- 1\\n- 7\\n- 9\\n- 10\\n+\\n+\\n+\\n- 4\\n- 5\\n- 8\\n+\\n- 6\\n+\\n- 2\\n\", \"10\\n+\\n- 3\\n+\\n+\\n+\\n+\\n- 1\\n- 7\\n- 9\\n- 10\\n+\\n+\\n+\\n- 4\\n- 5\\n- 8\\n+\\n+\\n- 6\\n- 2\\n\", \"10\\n+\\n- 3\\n+\\n+\\n+\\n+\\n- 1\\n- 9\\n- 7\\n- 10\\n+\\n+\\n+\\n+\\n+\\n- 2\\n- 4\\n- 5\\n- 6\\n- 8\\n\", \"5\\n+\\n+\\n+\\n- 3\\n- 4\\n+\\n- 1\\n- 2\\n+\\n- 5\\n\", \"4\\n+\\n+\\n+\\n- 3\\n- 4\\n+\\n- 1\\n- 2\\n\"], \"outputs\": [\"YES\\n4 2 3 1 \\n\", \"NO\\n\", \"NO\\n\", \"YES\\n1 \\n\", \"YES\\n3 10 9 7 1 8 5 4 6 2 \\n\", \"NO\\n\", \"NO\\n\", \"NO\\n\", \"NO\\n\"]}", "source": "primeintellect"}
Tenten runs a weapon shop for ninjas. Today she is willing to sell $n$ shurikens which cost $1$, $2$, ..., $n$ ryo (local currency). During a day, Tenten will place the shurikens onto the showcase, which is empty at the beginning of the day. Her job is fairly simple: sometimes Tenten places another shuriken (from the available shurikens) on the showcase, and sometimes a ninja comes in and buys a shuriken from the showcase. Since ninjas are thrifty, they always buy the cheapest shuriken from the showcase. Tenten keeps a record for all events, and she ends up with a list of the following types of records: + means that she placed another shuriken on the showcase; - x means that the shuriken of price $x$ was bought. Today was a lucky day, and all shurikens were bought. Now Tenten wonders if her list is consistent, and what could be a possible order of placing the shurikens on the showcase. Help her to find this out! -----Input----- The first line contains the only integer $n$ ($1\leq n\leq 10^5$) standing for the number of shurikens. The following $2n$ lines describe the events in the format described above. It's guaranteed that there are exactly $n$ events of the first type, and each price from $1$ to $n$ occurs exactly once in the events of the second type. -----Output----- If the list is consistent, print "YES". Otherwise (that is, if the list is contradictory and there is no valid order of shurikens placement), print "NO". In the first case the second line must contain $n$ space-separated integers denoting the prices of shurikens in order they were placed. If there are multiple answers, print any. -----Examples----- Input 4 + + - 2 + - 3 + - 1 - 4 Output YES 4 2 3 1 Input 1 - 1 + Output NO Input 3 + + + - 2 - 1 - 3 Output NO -----Note----- In the first example Tenten first placed shurikens with prices $4$ and $2$. After this a customer came in and bought the cheapest shuriken which costed $2$. Next, Tenten added a shuriken with price $3$ on the showcase to the already placed $4$-ryo. Then a new customer bought this $3$-ryo shuriken. After this she added a $1$-ryo shuriken. Finally, the last two customers bought shurikens $1$ and $4$, respectively. Note that the order $[2, 4, 3, 1]$ is also valid. In the second example the first customer bought a shuriken before anything was placed, which is clearly impossible. In the third example Tenten put all her shurikens onto the showcase, after which a customer came in and bought a shuriken with price $2$. This is impossible since the shuriken was not the cheapest, we know that the $1$-ryo shuriken was also there. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 3\\n1 2 2 3\\n4 1 1\\n\", \"6 10\\n5 8 1 10 8 7\\n6 6 4 4 3 2 2 2 1 1\\n\", \"5 1\\n1 1 1 1 1\\n5\\n\", \"5 1\\n1 1 1 1 1\\n1\\n\", \"1 1\\n1\\n1\\n\", \"1 10\\n10\\n1 1 1 1 1 1 1 1 1 1\\n\", \"10 5\\n2 2 4 4 3 1 1 2 3 2\\n9 6 6 4 1\\n\", \"10 20\\n20 1 15 17 11 2 15 3 16 3\\n10 9 9 9 8 8 8 7 7 7 7 6 6 4 4 4 3 3 3 2\\n\", \"100 50\\n20 13 10 38 7 22 40 15 27 32 37 44 42 50 33 46 7 47 43 5 18 29 26 3 32 5 1 29 17 1 1 43 2 38 23 23 49 36 14 18 36 3 49 47 11 19 6 29 14 9 6 46 15 22 31 45 24 5 31 2 24 14 7 15 21 44 8 7 38 50 17 1 29 39 16 35 10 22 19 8 6 42 44 45 25 26 16 34 36 23 17 11 41 15 19 28 44 27 46 8\\n100 97 94 93 92 91 89 87 85 83 82 79 79 78 78 68 68 66 66 66 57 55 55 53 51 49 47 47 47 45 41 38 35 30 28 26 26 25 23 22 22 22 20 19 15 9 5 5 3 2\\n\"], \"outputs\": [\"3\\n1 2\\n1 3\\n2 2 1\\n\", \"2\\n3 8 5 7\\n3 10 8 1\\n\", \"1\\n5 1 1 1 1 1\\n\", \"5\\n1 1\\n1 1\\n1 1\\n1 1\\n1 1\\n\", \"1\\n1 1\\n\", \"1\\n1 10\\n\", \"2\\n4 2 2 1 1\\n6 3 4 3 2 4 2\\n\", \"2\\n6 2 11 3 3 15 1\\n4 20 15 16 17\\n\", \"2\\n49 8 23 7 29 15 38 49 15 29 3 19 23 47 41 43 7 38 31 14 15 31 27 46 44 14 14 42 42 8 47 7 46 19 3 29 21 43 20 40 2 26 26 7 39 15 8 2 16 23\\n51 11 11 36 25 1 5 45 50 22 24 5 10 36 17 33 18 6 49 44 17 16 1 50 18 35 27 38 1 9 44 10 37 17 5 29 46 22 44 6 32 6 34 28 1 13 24 45 32 22 19 36\\n\"]}", "source": "primeintellect"}
So you decided to hold a contest on Codeforces. You prepared the problems: statements, solutions, checkers, validators, tests... Suddenly, your coordinator asks you to change all your tests to multiple testcases in the easiest problem! Initially, each test in that problem is just an array. The maximum size of an array is $k$. For simplicity, the contents of arrays don't matter. You have $n$ tests — the $i$-th test is an array of size $m_i$ ($1 \le m_i \le k$). Your coordinator asks you to distribute all of your arrays into multiple testcases. Each testcase can include multiple arrays. However, each testcase should include no more than $c_1$ arrays of size greater than or equal to $1$ ($\ge 1$), no more than $c_2$ arrays of size greater than or equal to $2$, $\dots$, no more than $c_k$ arrays of size greater than or equal to $k$. Also, $c_1 \ge c_2 \ge \dots \ge c_k$. So now your goal is to create the new testcases in such a way that: each of the initial arrays appears in exactly one testcase; for each testcase the given conditions hold; the number of testcases is minimum possible. Print the minimum possible number of testcases you can achieve and the sizes of arrays included in each testcase. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n, k \le 2 \cdot 10^5$) — the number of initial tests and the limit for the size of each array. The second line contains $n$ integers $m_1, m_2, \dots, m_n$ ($1 \le m_i \le k$) — the sizes of the arrays in the original tests. The third line contains $k$ integers $c_1, c_2, \dots, c_k$ ($n \ge c_1 \ge c_2 \ge \dots \ge c_k \ge 1$); $c_i$ is the maximum number of arrays of size greater than or equal to $i$ you can have in a single testcase. -----Output----- In the first line print a single integer $ans$ ($1 \le ans \le n$) — the minimum number of testcases you can achieve. Each of the next $ans$ lines should contain the description of a testcase in the following format: $t$ $a_1$ $a_2$ $\dots$ $a_{t}$ ($1 \le t\le n$) — the testcase includes $t$ arrays, $a_i$ is the size of the $i$-th array in that testcase. Each of the initial arrays should appear in exactly one testcase. In particular, it implies that the sum of $t$ over all $ans$ testcases should be equal to $n$. Note that the answer always exists due to $c_k \ge 1$ (and therefore $c_1 \ge 1$). If there are multiple answers, you can output any one of them. -----Examples----- Input 4 3 1 2 2 3 4 1 1 Output 3 1 2 2 1 3 1 2 Input 6 10 5 8 1 10 8 7 6 6 4 4 3 2 2 2 1 1 Output 2 3 8 5 7 3 10 8 1 Input 5 1 1 1 1 1 1 5 Output 1 5 1 1 1 1 1 Input 5 1 1 1 1 1 1 1 Output 5 1 1 1 1 1 1 1 1 1 1 -----Note----- In the first example there is no way to distribute the tests into less than $3$ testcases. The given answer satisfies the conditions: each of the testcases includes no more than $4$ arrays of size greater than or equal to $1$ and no more than $1$ array of sizes greater than or equal to $2$ and $3$. Note that there are multiple valid answers for this test. For example, testcases with sizes $[[2], [1, 2], [3]]$ would also be correct. However, testcases with sizes $[[1, 2], [2, 3]]$ would be incorrect because there are $2$ arrays of size greater than or equal to $2$ in the second testcase. Note the difference between the third and the fourth examples. You can include up to $5$ arrays of size greater than or equal to $1$ in the third example, so you can put all arrays into a single testcase. And you can have only up to $1$ array in the fourth example. Thus, every array should be included in a separate testcase. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"7 2\\n0 1 0 2 1 0 2\\n2 1\\n\", \"10 3\\n0 0 1 2 3 0 2 0 1 2\\n1 1 4\\n\", \"5 1\\n1 1 1 1 1\\n5\\n\", \"100 10\\n1 1 6 6 6 2 5 7 6 5 3 7 10 10 8 9 7 6 9 2 6 7 8 6 7 5 2 5 10 1 10 1 8 10 2 9 7 1 6 8 3 10 9 4 4 8 8 6 6 1 5 5 6 5 6 6 6 9 4 7 5 4 6 6 1 1 2 1 8 10 6 2 1 7 2 1 8 10 9 2 7 3 1 5 10 2 8 10 10 10 8 9 5 4 6 10 8 9 6 6\\n2 4 10 11 5 2 6 7 2 15\\n\", \"1 1\\n1\\n1\\n\", \"3 2\\n0 0 0\\n2 1\\n\", \"4 2\\n0 1 0 2\\n1 1\\n\", \"10 1\\n0 1 0 0 0 0 0 0 0 1\\n1\\n\", \"5 1\\n0 0 0 0 1\\n1\\n\"], \"outputs\": [\"5\\n\", \"9\\n\", \"-1\\n\", \"74\\n\", \"-1\\n\", \"-1\\n\", \"4\\n\", \"2\\n\", \"5\\n\"]}", "source": "primeintellect"}
Vasiliy has an exam period which will continue for n days. He has to pass exams on m subjects. Subjects are numbered from 1 to m. About every day we know exam for which one of m subjects can be passed on that day. Perhaps, some day you can't pass any exam. It is not allowed to pass more than one exam on any day. On each day Vasiliy can either pass the exam of that day (it takes the whole day) or prepare all day for some exam or have a rest. About each subject Vasiliy know a number a_{i} — the number of days he should prepare to pass the exam number i. Vasiliy can switch subjects while preparing for exams, it is not necessary to prepare continuously during a_{i} days for the exam number i. He can mix the order of preparation for exams in any way. Your task is to determine the minimum number of days in which Vasiliy can pass all exams, or determine that it is impossible. Each exam should be passed exactly one time. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 10^5) — the number of days in the exam period and the number of subjects. The second line contains n integers d_1, d_2, ..., d_{n} (0 ≤ d_{i} ≤ m), where d_{i} is the number of subject, the exam of which can be passed on the day number i. If d_{i} equals 0, it is not allowed to pass any exams on the day number i. The third line contains m positive integers a_1, a_2, ..., a_{m} (1 ≤ a_{i} ≤ 10^5), where a_{i} is the number of days that are needed to prepare before passing the exam on the subject i. -----Output----- Print one integer — the minimum number of days in which Vasiliy can pass all exams. If it is impossible, print -1. -----Examples----- Input 7 2 0 1 0 2 1 0 2 2 1 Output 5 Input 10 3 0 0 1 2 3 0 2 0 1 2 1 1 4 Output 9 Input 5 1 1 1 1 1 1 5 Output -1 -----Note----- In the first example Vasiliy can behave as follows. On the first and the second day he can prepare for the exam number 1 and pass it on the fifth day, prepare for the exam number 2 on the third day and pass it on the fourth day. In the second example Vasiliy should prepare for the exam number 3 during the first four days and pass it on the fifth day. Then on the sixth day he should prepare for the exam number 2 and then pass it on the seventh day. After that he needs to prepare for the exam number 1 on the eighth day and pass it on the ninth day. In the third example Vasiliy can't pass the only exam because he hasn't anough time to prepare for it. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"3\\n5 8\\n1 4\\n3 10\\n\", \"4\\n1 2\\n2 3\\n3 4\\n4 5\\n\", \"1\\n1 1\\n\", \"2\\n1 9\\n4 5\\n\", \"2\\n1 2\\n2 8\\n\", \"6\\n216986951114298167 235031205335543871\\n148302405431848579 455670351549314242\\n506251128322958430 575521452907339082\\n1 768614336404564650\\n189336074809158272 622104412002885672\\n588320087414024192 662540324268197150\\n\", \"20\\n1 12\\n5 24\\n12 15\\n15 21\\n7 18\\n5 7\\n8 13\\n13 19\\n16 19\\n15 25\\n1 22\\n20 24\\n2 9\\n2 11\\n3 23\\n10 17\\n14 17\\n15 19\\n5 18\\n17 25\\n\", \"13\\n8 10\\n8 32\\n8 10\\n8 32\\n31 34\\n8 8\\n1 34\\n8 24\\n26 34\\n18 31\\n1 8\\n1 8\\n2 7\\n\", \"3\\n34 34\\n2 8\\n34 36\\n\"], \"outputs\": [\"1 1\\n\", \"0 0\\n\", \"0 1\\n\", \"0 0\\n\", \"1 0\\n\", \"1 0\\n\", \"0 0\\n\", \"1 1\\n\", \"0 1\\n\"]}", "source": "primeintellect"}
Lee is used to finish his stories in a stylish way, this time he barely failed it, but Ice Bear came and helped him. Lee is so grateful for it, so he decided to show Ice Bear his new game called "Critic"... The game is a one versus one game. It has $t$ rounds, each round has two integers $s_i$ and $e_i$ (which are determined and are known before the game begins, $s_i$ and $e_i$ may differ from round to round). The integer $s_i$ is written on the board at the beginning of the corresponding round. The players will take turns. Each player will erase the number on the board (let's say it was $a$) and will choose to write either $2 \cdot a$ or $a + 1$ instead. Whoever writes a number strictly greater than $e_i$ loses that round and the other one wins that round. Now Lee wants to play "Critic" against Ice Bear, for each round he has chosen the round's $s_i$ and $e_i$ in advance. Lee will start the first round, the loser of each round will start the next round. The winner of the last round is the winner of the game, and the loser of the last round is the loser of the game. Determine if Lee can be the winner independent of Ice Bear's moves or not. Also, determine if Lee can be the loser independent of Ice Bear's moves or not. -----Input----- The first line contains the integer $t$ ($1 \le t \le 10^5$) — the number of rounds the game has. Then $t$ lines follow, each contains two integers $s_i$ and $e_i$ ($1 \le s_i \le e_i \le 10^{18}$) — the $i$-th round's information. The rounds are played in the same order as given in input, $s_i$ and $e_i$ for all rounds are known to everyone before the game starts. -----Output----- Print two integers. The first one should be 1 if Lee can be the winner independent of Ice Bear's moves, and 0 otherwise. The second one should be 1 if Lee can be the loser independent of Ice Bear's moves, and 0 otherwise. -----Examples----- Input 3 5 8 1 4 3 10 Output 1 1 Input 4 1 2 2 3 3 4 4 5 Output 0 0 Input 1 1 1 Output 0 1 Input 2 1 9 4 5 Output 0 0 Input 2 1 2 2 8 Output 1 0 Input 6 216986951114298167 235031205335543871 148302405431848579 455670351549314242 506251128322958430 575521452907339082 1 768614336404564650 189336074809158272 622104412002885672 588320087414024192 662540324268197150 Output 1 0 -----Note----- Remember, whoever writes an integer greater than $e_i$ loses. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"6 2\\n5 6 7 9 4 5\\n\", \"8 6\\n7 7 7 7 8 8 8 8\\n\", \"4 1\\n4 2 1 10\\n\", \"10 10\\n12 13 10 20 13 10 19 15 21 11\\n\", \"20 16\\n23 27 17 29 23 21 24 23 19 25 16 24 20 17 18 17 16 17 28 17\\n\", \"30 40\\n26 22 10 20 29 18 38 11 41 8 33 37 37 3 14 4 3 9 21 38 27 27 7 7 33 12 39 37 17 5\\n\", \"100 20\\n28 42 37 40 26 40 46 46 25 28 36 36 35 38 45 40 21 38 36 22 30 24 40 38 27 50 47 40 30 45 39 20 18 32 34 24 34 26 27 37 18 40 42 41 26 50 22 27 37 21 30 30 49 36 16 48 46 26 33 22 47 32 38 50 29 46 31 42 26 24 50 26 40 42 26 34 18 32 44 24 16 32 50 30 20 48 26 41 32 30 32 41 30 24 18 32 49 23 20 44\\n\", \"2 1\\n2 4\\n\", \"2 1000000000\\n1 1\\n\"], \"outputs\": [\"1\\n5 6 7 9 4 2 \\n\", \"6\\n7 2 4 6 8 1 3 5 \\n\", \"-1\\n\", \"2\\n12 13 10 20 2 4 19 15 21 11 \\n\", \"8\\n23 27 17 29 2 21 24 4 19 25 16 6 20 8 18 10 1 3 28 5 \\n\", \"7\\n26 22 10 20 29 18 38 11 41 8 33 37 2 3 14 4 6 9 21 16 27 24 7 28 1 12 39 13 17 5 \\n\", \"-1\\n\", \"1\\n2 1 \\n\", \"1\\n1 2 \\n\"]}", "source": "primeintellect"}
Eugeny has n cards, each of them has exactly one integer written on it. Eugeny wants to exchange some cards with Nikolay so that the number of even integers on his cards would equal the number of odd integers, and that all these numbers would be distinct. Nikolay has m cards, distinct numbers from 1 to m are written on them, one per card. It means that Nikolay has exactly one card with number 1, exactly one card with number 2 and so on. A single exchange is a process in which Eugeny gives one card to Nikolay and takes another one from those Nikolay has. Your task is to find the minimum number of card exchanges and determine which cards Eugeny should exchange. -----Input----- The first line contains two integers n and m (2 ≤ n ≤ 2·10^5, 1 ≤ m ≤ 10^9) — the number of cards Eugeny has and the number of cards Nikolay has. It is guaranteed that n is even. The second line contains a sequence of n positive integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9) — the numbers on Eugeny's cards. -----Output----- If there is no answer, print -1. Otherwise, in the first line print the minimum number of exchanges. In the second line print n integers — Eugeny's cards after all the exchanges with Nikolay. The order of cards should coincide with the card's order in the input data. If the i-th card wasn't exchanged then the i-th number should coincide with the number from the input data. Otherwise, it is considered that this card was exchanged, and the i-th number should be equal to the number on the card it was exchanged to. If there are multiple answers, it is allowed to print any of them. -----Examples----- Input 6 2 5 6 7 9 4 5 Output 1 5 6 7 9 4 2 Input 8 6 7 7 7 7 8 8 8 8 Output 6 7 2 4 6 8 1 3 5 Input 4 1 4 2 1 10 Output -1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"1\\n4\\n\", \"4\\n1 3 2 5\\n\", \"4\\n1 5 2 3\\n\", \"10\\n2 2 2 17 8 9 10 17 10 5\\n\", \"100\\n102 157 177 149 138 193 19 74 127 156 128 122 6 101 154 92 24 188 51 89 18 77 112 100 65 11 113 173 79 124 102 81 11 171 114 43 60 168 44 86 87 82 177 8 12 181 139 121 140 28 108 17 173 63 18 4 148 125 117 153 193 180 188 142 46 75 163 126 128 22 32 163 164 1 161 19 162 59 190 14 93 85 93 37 73 64 64 173 163 42 136 42 132 97 142 172 182 157 100 52\\n\", \"24\\n29 65 79 75 21 61 87 83 65 61 37 44 49 86 48 37 28 21 67 43 30 20 81 55\\n\", \"2\\n5 4\\n\", \"32\\n9 71 28 214 98 204 27 156 27 201 247 103 191 108 54 166 28 182 116 42 225 192 76 205 41 134 248 78 191 223 108 140\\n\", \"88\\n114 109 129 36 47 150 68 12 81 19 61 70 80 115 82 29 51 63 118 140 89 67 12 147 91 30 114 135 55 45 152 124 114 55 107 147 89 27 143 128 92 38 113 88 130 151 132 106 71 106 151 105 95 129 28 138 69 153 146 13 42 30 153 58 124 95 27 123 90 44 132 36 26 60 51 126 97 152 125 148 26 153 90 136 141 58 90 126\\n\"], \"outputs\": [\"3\\n\", \"3223\\n\", \"3133\\n\", \"2221333311\\n\", \"1111112111112111313112112111311311111113311111222121111122221111111221111131311111111111311111133111\\n\", \"211121111133212111311131\\n\", \"22\\n\", \"32213111111313131311131311211211\\n\", \"1112212212233131111131113131111311111113111131311111111311311111111111111111121213111111\\n\"]}", "source": "primeintellect"}
The next "Data Structures and Algorithms" lesson will be about Longest Increasing Subsequence (LIS for short) of a sequence. For better understanding, Nam decided to learn it a few days before the lesson. Nam created a sequence a consisting of n (1 ≤ n ≤ 10^5) elements a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5). A subsequence a_{i}_1, a_{i}_2, ..., a_{i}_{k} where 1 ≤ i_1 < i_2 < ... < i_{k} ≤ n is called increasing if a_{i}_1 < a_{i}_2 < a_{i}_3 < ... < a_{i}_{k}. An increasing subsequence is called longest if it has maximum length among all increasing subsequences. Nam realizes that a sequence may have several longest increasing subsequences. Hence, he divides all indexes i (1 ≤ i ≤ n), into three groups: group of all i such that a_{i} belongs to no longest increasing subsequences. group of all i such that a_{i} belongs to at least one but not every longest increasing subsequence. group of all i such that a_{i} belongs to every longest increasing subsequence. Since the number of longest increasing subsequences of a may be very large, categorizing process is very difficult. Your task is to help him finish this job. -----Input----- The first line contains the single integer n (1 ≤ n ≤ 10^5) denoting the number of elements of sequence a. The second line contains n space-separated integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^5). -----Output----- Print a string consisting of n characters. i-th character should be '1', '2' or '3' depending on which group among listed above index i belongs to. -----Examples----- Input 1 4 Output 3 Input 4 1 3 2 5 Output 3223 Input 4 1 5 2 3 Output 3133 -----Note----- In the second sample, sequence a consists of 4 elements: {a_1, a_2, a_3, a_4} = {1, 3, 2, 5}. Sequence a has exactly 2 longest increasing subsequences of length 3, they are {a_1, a_2, a_4} = {1, 3, 5} and {a_1, a_3, a_4} = {1, 2, 5}. In the third sample, sequence a consists of 4 elements: {a_1, a_2, a_3, a_4} = {1, 5, 2, 3}. Sequence a have exactly 1 longest increasing subsequence of length 3, that is {a_1, a_3, a_4} = {1, 2, 3}. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"2 1\\n\", \"2 2\\n\", \"1 1\\n\", \"2 3\\n\", \"4 4\\n\", \"1 12\\n\", \"12 1\\n\", \"24 6\\n\", \"25 5\\n\"], \"outputs\": [\"2\\n1 1\\n2 1\\n\", \"3\\n1 1\\n1 2\\n2 2\\n\", \"1\\n1 1\\n\", \"4\\n1 1\\n1 2\\n1 3\\n2 3\\n\", \"7\\n1 1\\n1 2\\n1 3\\n1 4\\n4 4\\n3 4\\n2 4\\n\", \"12\\n1 1\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n1 7\\n1 8\\n1 9\\n1 10\\n1 11\\n1 12\\n\", \"12\\n1 1\\n12 1\\n11 1\\n10 1\\n9 1\\n8 1\\n7 1\\n6 1\\n5 1\\n4 1\\n3 1\\n2 1\\n\", \"29\\n1 1\\n1 2\\n1 3\\n1 4\\n1 5\\n1 6\\n24 6\\n23 6\\n22 6\\n21 6\\n20 6\\n19 6\\n18 6\\n17 6\\n16 6\\n15 6\\n14 6\\n13 6\\n12 6\\n11 6\\n10 6\\n9 6\\n8 6\\n7 6\\n6 6\\n5 6\\n4 6\\n3 6\\n2 6\\n\", \"29\\n1 1\\n1 2\\n1 3\\n1 4\\n1 5\\n25 5\\n24 5\\n23 5\\n22 5\\n21 5\\n20 5\\n19 5\\n18 5\\n17 5\\n16 5\\n15 5\\n14 5\\n13 5\\n12 5\\n11 5\\n10 5\\n9 5\\n8 5\\n7 5\\n6 5\\n5 5\\n4 5\\n3 5\\n2 5\\n\"]}", "source": "primeintellect"}
Fox Ciel and her friends are in a dancing room. There are n boys and m girls here, and they never danced before. There will be some songs, during each song, there must be exactly one boy and one girl are dancing. Besides, there is a special rule: either the boy in the dancing pair must dance for the first time (so, he didn't dance with anyone before); or the girl in the dancing pair must dance for the first time. Help Fox Ciel to make a schedule that they can dance as many songs as possible. -----Input----- The first line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of boys and girls in the dancing room. -----Output----- In the first line print k — the number of songs during which they can dance. Then in the following k lines, print the indexes of boys and girls dancing during songs chronologically. You can assume that the boys are indexed from 1 to n, and the girls are indexed from 1 to m. -----Examples----- Input 2 1 Output 2 1 1 2 1 Input 2 2 Output 3 1 1 1 2 2 2 -----Note----- In test case 1, there are 2 boys and 1 girl. We can have 2 dances: the 1st boy and 1st girl (during the first song), the 2nd boy and 1st girl (during the second song). And in test case 2, we have 2 boys with 2 girls, the answer is 3. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4 2\\nabcd\\nbcde\\nbcad\\nbcde\\n\", \"5 3\\nbwwwz\\nhrhdh\\nsepsp\\nsqfaf\\najbvw\\n\", \"7 6\\nypnxnnp\\npnxonpm\\nnxanpou\\nxnnpmud\\nnhtdudu\\nnpmuduh\\npmutsnz\\n\", \"1 0\\ng\\n\", \"1 1\\ng\\n\", \"1 0\\na\\n\", \"1 1\\nz\\n\", \"4 16\\ndfhs\\nvgca\\ndfgd\\nvscd\\n\", \"1 0\\nz\\n\"], \"outputs\": [\"aaabcde\\n\", \"aaaepfafw\\n\", \"aaaaaaadudsnz\\n\", \"g\\n\", \"a\\n\", \"a\\n\", \"a\\n\", \"aaaaaaa\\n\", \"z\\n\"]}", "source": "primeintellect"}
You are given a matrix of size $n \times n$ filled with lowercase English letters. You can change no more than $k$ letters in this matrix. Consider all paths from the upper left corner to the lower right corner that move from a cell to its neighboring cell to the right or down. Each path is associated with the string that is formed by all the letters in the cells the path visits. Thus, the length of each string is $2n - 1$. Find the lexicographically smallest string that can be associated with a path after changing letters in at most $k$ cells of the matrix. A string $a$ is lexicographically smaller than a string $b$, if the first different letter in $a$ and $b$ is smaller in $a$. -----Input----- The first line contains two integers $n$ and $k$ ($1 \le n \le 2000$, $0 \le k \le n^2$) — the size of the matrix and the number of letters you can change. Each of the next $n$ lines contains a string of $n$ lowercase English letters denoting one row of the matrix. -----Output----- Output the lexicographically smallest string that can be associated with some valid path after changing no more than $k$ letters in the matrix. -----Examples----- Input 4 2 abcd bcde bcad bcde Output aaabcde Input 5 3 bwwwz hrhdh sepsp sqfaf ajbvw Output aaaepfafw Input 7 6 ypnxnnp pnxonpm nxanpou xnnpmud nhtdudu npmuduh pmutsnz Output aaaaaaadudsnz -----Note----- In the first sample test case it is possible to change letters 'b' in cells $(2, 1)$ and $(3, 1)$ to 'a', then the minimum path contains cells $(1, 1), (2, 1), (3, 1), (4, 1), (4, 2), (4, 3), (4, 4)$. The first coordinate corresponds to the row and the second coordinate corresponds to the column. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"4\\n0110\\n0010\\n0001\\n1000\\n4\\n1 2 3 4\\n\", \"4\\n0110\\n0010\\n1001\\n1000\\n20\\n1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4\\n\", \"3\\n011\\n101\\n110\\n7\\n1 2 3 1 3 2 1\\n\", \"4\\n0110\\n0001\\n0001\\n1000\\n3\\n1 2 4\\n\", \"2\\n01\\n00\\n2\\n1 2\\n\", \"16\\n0011101010011011\\n1001011001110111\\n1101110110011000\\n0010010011001011\\n1011010000010010\\n0110001000001011\\n1001110000010011\\n0111110001001101\\n0101000100110010\\n0010000110101011\\n0111110011010101\\n1100000101100101\\n0110101000000101\\n1111100010000001\\n1101001010000100\\n0111111101100010\\n20\\n4 16 4 16 11 3 1 5 6 2 7 12 14 16 6 2 7 15 2 7\\n\", \"5\\n00001\\n00000\\n11001\\n10001\\n11000\\n11\\n3 5 1 5 1 5 1 5 1 5 2\\n\", \"20\\n01001100001100011011\\n00111101001100001101\\n01010010000010000110\\n10101001000000000110\\n00110110111101100000\\n00111010110011100011\\n01101000111001110100\\n11111100011110000100\\n11111010011110011000\\n10001100000101100101\\n11001111010010100011\\n00001111001001111111\\n10011110000101001110\\n10001101010000111010\\n11010010001101010100\\n10101110000001001000\\n11110011110101100000\\n10110100101110010011\\n11101010110011110101\\n10010100000101110010\\n20\\n19 10 18 9 2 12 16 17 9 2 5 3 19 18 12 17 7 2 6 5\\n\", \"16\\n0100001011000101\\n0001010100010111\\n1100010111110111\\n0000010011111101\\n1001010110011110\\n0011001110110110\\n0011110011111010\\n0010100011100101\\n1001010101100100\\n1001110110111111\\n1000010011001010\\n1010001011000001\\n1100011010000111\\n1111110110110001\\n0101111100000000\\n0001100100110100\\n20\\n3 16 8 9 11 13 7 3 2 8 3 11 6 7 10 15 5 14 8 3\\n\"], \"outputs\": [\"3\\n1 2 4 \", \"11\\n1 2 4 2 4 2 4 2 4 2 4 \", \"7\\n1 2 3 1 3 2 1 \", \"2\\n1 4 \", \"2\\n1 2 \", \"14\\n4 16 4 11 1 6 2 7 14 6 2 7 2 7 \", \"10\\n3 5 1 5 1 5 1 5 1 2 \", \"14\\n19 10 9 2 16 9 2 5 19 12 17 7 6 5 \", \"14\\n3 16 9 13 3 2 3 11 7 10 15 14 8 3 \"]}", "source": "primeintellect"}
The main characters have been omitted to be short. You are given a directed unweighted graph without loops with $n$ vertexes and a path in it (that path is not necessary simple) given by a sequence $p_1, p_2, \ldots, p_m$ of $m$ vertexes; for each $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. Define the sequence $v_1, v_2, \ldots, v_k$ of $k$ vertexes as good, if $v$ is a subsequence of $p$, $v_1 = p_1$, $v_k = p_m$, and $p$ is one of the shortest paths passing through the vertexes $v_1$, $\ldots$, $v_k$ in that order. A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. It is obvious that the sequence $p$ is good but your task is to find the shortest good subsequence. If there are multiple shortest good subsequences, output any of them. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 100$) — the number of vertexes in a graph. The next $n$ lines define the graph by an adjacency matrix: the $j$-th character in the $i$-st line is equal to $1$ if there is an arc from vertex $i$ to the vertex $j$ else it is equal to $0$. It is guaranteed that the graph doesn't contain loops. The next line contains a single integer $m$ ($2 \le m \le 10^6$) — the number of vertexes in the path. The next line contains $m$ integers $p_1, p_2, \ldots, p_m$ ($1 \le p_i \le n$) — the sequence of vertexes in the path. It is guaranteed that for any $1 \leq i < m$ there is an arc from $p_i$ to $p_{i+1}$. -----Output----- In the first line output a single integer $k$ ($2 \leq k \leq m$) — the length of the shortest good subsequence. In the second line output $k$ integers $v_1$, $\ldots$, $v_k$ ($1 \leq v_i \leq n$) — the vertexes in the subsequence. If there are multiple shortest subsequences, print any. Any two consecutive numbers should be distinct. -----Examples----- Input 4 0110 0010 0001 1000 4 1 2 3 4 Output 3 1 2 4 Input 4 0110 0010 1001 1000 20 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 Output 11 1 2 4 2 4 2 4 2 4 2 4 Input 3 011 101 110 7 1 2 3 1 3 2 1 Output 7 1 2 3 1 3 2 1 Input 4 0110 0001 0001 1000 3 1 2 4 Output 2 1 4 -----Note----- Below you can see the graph from the first example: [Image] The given path is passing through vertexes $1$, $2$, $3$, $4$. The sequence $1-2-4$ is good because it is the subsequence of the given path, its first and the last elements are equal to the first and the last elements of the given path respectively, and the shortest path passing through vertexes $1$, $2$ and $4$ in that order is $1-2-3-4$. Note that subsequences $1-4$ and $1-3-4$ aren't good because in both cases the shortest path passing through the vertexes of these sequences is $1-3-4$. In the third example, the graph is full so any sequence of vertexes in which any two consecutive elements are distinct defines a path consisting of the same number of vertexes. In the fourth example, the paths $1-2-4$ and $1-3-4$ are the shortest paths passing through the vertexes $1$ and $4$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5 10\\n1 15\\n5 3\\n6 11\\n7 2\\n11 22\\n\", \"3 5\\n1 8\\n2 19\\n3 11\\n\", \"10 8\\n1 16\\n23 27\\n26 2\\n32 21\\n38 50\\n51 53\\n62 79\\n77 97\\n94 18\\n100 5\\n\", \"22 936\\n20 743\\n55 46\\n83 282\\n157 3\\n168 979\\n241 338\\n293 113\\n312 292\\n333 509\\n339 632\\n345 149\\n347 109\\n384 846\\n420 151\\n619 923\\n647 336\\n706 935\\n726 600\\n760 45\\n804 126\\n810 739\\n906 341\\n\", \"9 985\\n227 166\\n325 695\\n408 426\\n548 491\\n627 627\\n733 288\\n802 59\\n924 471\\n933 492\\n\", \"1 235\\n15 210\\n\", \"4 852\\n51 416\\n479 58\\n893 73\\n973 845\\n\", \"1 10\\n1 13\\n\", \"3 476\\n343 574\\n744 244\\n847 84\\n\"], \"outputs\": [\"13\\n\", \"0\\n\", \"6\\n\", \"3371\\n\", \"926\\n\", \"25\\n\", \"794\\n\", \"0\\n\", \"392\\n\"]}", "source": "primeintellect"}
Vasya got really tired of these credits (from problem F) and now wants to earn the money himself! He decided to make a contest to gain a profit. Vasya has $n$ problems to choose from. They are numbered from $1$ to $n$. The difficulty of the $i$-th problem is $d_i$. Moreover, the problems are given in the increasing order by their difficulties. The difficulties of all tasks are pairwise distinct. In order to add the $i$-th problem to the contest you need to pay $c_i$ burles to its author. For each problem in the contest Vasya gets $a$ burles. In order to create a contest he needs to choose a consecutive subsegment of tasks. So the total earnings for the contest are calculated as follows: if Vasya takes problem $i$ to the contest, he needs to pay $c_i$ to its author; for each problem in the contest Vasya gets $a$ burles; let $gap(l, r) = \max\limits_{l \le i < r} (d_{i + 1} - d_i)^2$. If Vasya takes all the tasks with indices from $l$ to $r$ to the contest, he also needs to pay $gap(l, r)$. If $l = r$ then $gap(l, r) = 0$. Calculate the maximum profit that Vasya can earn by taking a consecutive segment of tasks. -----Input----- The first line contains two integers $n$ and $a$ ($1 \le n \le 3 \cdot 10^5$, $1 \le a \le 10^9$) — the number of proposed tasks and the profit for a single problem, respectively. Each of the next $n$ lines contains two integers $d_i$ and $c_i$ ($1 \le d_i, c_i \le 10^9, d_i < d_{i+1}$). -----Output----- Print one integer — maximum amount of burles Vasya can earn. -----Examples----- Input 5 10 1 15 5 3 6 11 7 2 11 22 Output 13 Input 3 5 1 8 2 19 3 11 Output 0 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.75
{"tests": "{\"inputs\": [\"2\\n3\\n3 2 1\\n6\\n3 1 4 1 5 9\\n\", \"4\\n7\\n6 6 8 8 6 6 6\\n1\\n2\\n5\\n4 5 9 8 7\\n7\\n1 2 7 1 6 10 2\\n\", \"2\\n5\\n5 5 5 5 5\\n3\\n1 2 5\\n\", \"2\\n5\\n1 2 3 4 5\\n4\\n2 3 4 5\\n\", \"1\\n3\\n1 1 274005660\\n\", \"2\\n2\\n1 1\\n1\\n1\\n\", \"2\\n4\\n1 3 3 3\\n3\\n1 2 3\\n\", \"2\\n3\\n1 1 1\\n2\\n1 1\\n\", \"1\\n5\\n1 3 4 5 2\\n\"], \"outputs\": [\"3\\n5\\n\", \"2\\n1\\n5\\n5\\n\", \"1\\n3\\n\", \"5\\n4\\n\", \"2\\n\", \"1\\n1\\n\", \"2\\n3\\n\", \"1\\n1\\n\", \"5\\n\"]}", "source": "primeintellect"}
Ehab has an array $a$ of length $n$. He has just enough free time to make a new array consisting of $n$ copies of the old array, written back-to-back. What will be the length of the new array's longest increasing subsequence? A sequence $a$ is a subsequence of an array $b$ if $a$ can be obtained from $b$ by deletion of several (possibly, zero or all) elements. The longest increasing subsequence of an array is the longest subsequence such that its elements are ordered in strictly increasing order. -----Input----- The first line contains an integer $t$ — the number of test cases you need to solve. The description of the test cases follows. The first line of each test case contains an integer $n$ ($1 \le n \le 10^5$) — the number of elements in the array $a$. The second line contains $n$ space-separated integers $a_1$, $a_2$, $\ldots$, $a_{n}$ ($1 \le a_i \le 10^9$) — the elements of the array $a$. The sum of $n$ across the test cases doesn't exceed $10^5$. -----Output----- For each testcase, output the length of the longest increasing subsequence of $a$ if you concatenate it to itself $n$ times. -----Example----- Input 2 3 3 2 1 6 3 1 4 1 5 9 Output 3 5 -----Note----- In the first sample, the new array is $[3,2,\textbf{1},3,\textbf{2},1,\textbf{3},2,1]$. The longest increasing subsequence is marked in bold. In the second sample, the longest increasing subsequence will be $[1,3,4,5,9]$. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.625
{"tests": "{\"inputs\": [\"2 3 3 2\\nA.A\\n...\\nA.a\\n..C\\nX.Y\\n...\\n\", \"1 1 4 1\\nA\\n.\\nB\\n.\\n\", \"1 3 5 2\\nABA\\nBBB\\nBBA\\nBAB\\nABB\\n\", \"2 2 5 1\\n..\\nBA\\n.A\\nB.\\n..\\nA.\\nAB\\n.B\\n..\\n..\\n\", \"3 3 10 2\\nBA.\\n..A\\n.BB\\nB..\\n..B\\n.AA\\nB..\\nAB.\\n..A\\nBAB\\n.A.\\n.B.\\n..B\\nA..\\n...\\n...\\n.B.\\nBA.\\n..B\\n.AB\\n.B.\\nB.A\\n.A.\\n.BA\\n..B\\n...\\n.A.\\n.AA\\n..A\\n.B.\\n\", \"3 1 5 1\\nB\\nA\\nB\\nA\\nA\\nB\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\nA\\n\", \"3 2 10 1\\nAB\\nBA\\nAB\\nAA\\nAA\\nBA\\nAA\\nAA\\nAB\\nAB\\nAB\\nBA\\nBA\\nAB\\nAA\\nBB\\nAB\\nBA\\nBB\\nBB\\nBA\\nAA\\nAA\\nAB\\nAB\\nAB\\nBA\\nBB\\nAB\\nAA\\n\", \"2 3 10 2\\nABB\\nABA\\nAAB\\nBAB\\nAAA\\nBBA\\nBBB\\nBAA\\nBBB\\nABB\\nABA\\nBBA\\nBBB\\nAAB\\nABA\\nABB\\nBBA\\nBAB\\nBBB\\nBBB\\n\", \"1 1 1 1\\n.\\n\"], \"outputs\": [\"14\\n1 0\\n2 1\\n3 1\\n\", \"3\\n1 0\\n2 0\\n4 2\\n3 0\\n\", \"11\\n1 0\\n3 1\\n2 3\\n4 2\\n5 1\\n\", \"12\\n1 0\\n2 1\\n3 1\\n5 3\\n4 5\\n\", \"67\\n1 0\\n10 1\\n2 1\\n3 2\\n4 1\\n7 4\\n9 7\\n5 9\\n6 9\\n8 4\\n\", \"5\\n1 0\\n2 1\\n3 2\\n4 3\\n5 3\\n\", \"16\\n1 0\\n3 1\\n8 3\\n2 3\\n4 2\\n9 4\\n6 4\\n7 6\\n10 6\\n5 10\\n\", \"38\\n1 0\\n5 1\\n7 5\\n4 7\\n9 4\\n10 5\\n6 1\\n3 6\\n8 1\\n2 0\\n\", \"1\\n1 0\\n\"]}", "source": "primeintellect"}
During the loading of the game "Dungeons and Candies" you are required to get descriptions of k levels from the server. Each description is a map of an n × m checkered rectangular field. Some cells of the field contain candies (each cell has at most one candy). An empty cell is denoted as "." on the map, but if a cell has a candy, it is denoted as a letter of the English alphabet. A level may contain identical candies, in this case the letters in the corresponding cells of the map will be the same. [Image] When you transmit information via a network, you want to minimize traffic — the total size of the transferred data. The levels can be transmitted in any order. There are two ways to transmit the current level A: You can transmit the whole level A. Then you need to transmit n·m bytes via the network. You can transmit the difference between level A and some previously transmitted level B (if it exists); this operation requires to transmit d_{A}, B·w bytes, where d_{A}, B is the number of cells of the field that are different for A and B, and w is a constant. Note, that you should compare only the corresponding cells of levels A and B to calculate d_{A}, B. You cannot transform the maps of levels, i.e. rotate or shift them relatively to each other. Your task is to find a way to transfer all the k levels and minimize the traffic. -----Input----- The first line contains four integers n, m, k, w (1 ≤ n, m ≤ 10; 1 ≤ k, w ≤ 1000). Then follows the description of k levels. Each level is described by n lines, each line contains m characters. Each character is either a letter of the English alphabet or a dot ("."). Please note that the case of the letters matters. -----Output----- In the first line print the required minimum number of transferred bytes. Then print k pairs of integers x_1, y_1, x_2, y_2, ..., x_{k}, y_{k}, describing the way to transfer levels. Pair x_{i}, y_{i} means that level x_{i} needs to be transferred by way y_{i}. If y_{i} equals 0, that means that the level must be transferred using the first way, otherwise y_{i} must be equal to the number of a previously transferred level. It means that you will transfer the difference between levels y_{i} and x_{i} to transfer level x_{i}. Print the pairs in the order of transferring levels. The levels are numbered 1 through k in the order they follow in the input. If there are multiple optimal solutions, you can print any of them. -----Examples----- Input 2 3 3 2 A.A ... A.a ..C X.Y ... Output 14 1 0 2 1 3 1 Input 1 1 4 1 A . B . Output 3 1 0 2 0 4 2 3 0 Input 1 3 5 2 ABA BBB BBA BAB ABB Output 11 1 0 3 1 2 3 4 2 5 1 Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"11\\n(RaRbR)L)L(\\n\", \"11\\n(R)R(R)Ra)c\\n\", \"1\\na\\n\", \"3\\n(R)\\n\", \"3\\n(R(\\n\", \"3\\n)R)\\n\", \"3\\n)R(\\n\", \"3\\n(l)\\n\", \"21\\n(RRRRR)LLLL(RRR)LL(R)\\n\"], \"outputs\": [\"-1 -1 -1 -1 -1 -1 1 1 -1 -1 2 \", \"-1 -1 1 1 -1 -1 1 1 1 -1 1 \", \"0 \", \"-1 -1 1 \", \"-1 -1 -1 \", \"-1 -1 -1 \", \"-1 -1 -1 \", \"-1 0 -1 \", \"-1 -1 -1 -1 -1 -1 1 1 1 1 1 -1 -1 -1 -1 2 2 2 -1 -1 3 \"]}", "source": "primeintellect"}
The development of a text editor is a hard problem. You need to implement an extra module for brackets coloring in text. Your editor consists of a line with infinite length and cursor, which points to the current character. Please note that it points to only one of the characters (and not between a pair of characters). Thus, it points to an index character. The user can move the cursor left or right one position. If the cursor is already at the first (leftmost) position, then it does not move left. Initially, the cursor is in the first (leftmost) character. Also, the user can write a letter or brackets (either (, or )) to the position that the cursor is currently pointing at. A new character always overwrites the old value at that position. Your editor must check, whether the current line is the correct text. Text is correct if the brackets in them form the correct bracket sequence. Formally, correct text (CT) must satisfy the following rules: any line without brackets is CT (the line can contain whitespaces); If the first character of the string — is (, the last — is ), and all the rest form a CT, then the whole line is a CT; two consecutively written CT is also CT. Examples of correct texts: hello(codeforces), round, ((i)(write))edi(tor)s, ( me). Examples of incorrect texts: hello)oops(, round), ((me). The user uses special commands to work with your editor. Each command has its symbol, which must be written to execute this command. The correspondence of commands and characters is as follows: L — move the cursor one character to the left (remains in place if it already points to the first character); R — move the cursor one character to the right; any lowercase Latin letter or bracket (( or )) — write the entered character to the position where the cursor is now. For a complete understanding, take a look at the first example and its illustrations in the note below. You are given a string containing the characters that the user entered. For the brackets coloring module's work, after each command you need to: check if the current text in the editor is a correct text; if it is, print the least number of colors that required, to color all brackets. If two pairs of brackets are nested (the first in the second or vice versa), then these pairs of brackets should be painted in different colors. If two pairs of brackets are not nested, then they can be painted in different or the same colors. For example, for the bracket sequence ()(())()() the least number of colors is $2$, and for the bracket sequence (()(()())())(()) — is $3$. Write a program that prints the minimal number of colors after processing each command. -----Input----- The first line contains an integer $n$ ($1 \le n \le 10^6$) — the number of commands. The second line contains $s$ — a sequence of commands. The string $s$ consists of $n$ characters. It is guaranteed that all characters in a string are valid commands. -----Output----- In a single line print $n$ integers, where the $i$-th number is: $-1$ if the line received after processing the first $i$ commands is not valid text, the minimal number of colors in the case of the correct text. -----Examples----- Input 11 (RaRbR)L)L( Output -1 -1 -1 -1 -1 -1 1 1 -1 -1 2 Input 11 (R)R(R)Ra)c Output -1 -1 1 1 -1 -1 1 1 1 -1 1 -----Note----- In the first example, the text in the editor will take the following form: ( ^ ( ^ (a ^ (a ^ (ab ^ (ab ^ (ab) ^ (ab) ^ (a)) ^ (a)) ^ (()) ^ Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0.5
{"tests": "{\"inputs\": [\"5\\n! abc\\n. ad\\n. b\\n! cd\\n? c\\n\", \"8\\n! hello\\n! codeforces\\n? c\\n. o\\n? d\\n? h\\n. l\\n? e\\n\", \"7\\n! ababahalamaha\\n? a\\n? b\\n? a\\n? b\\n? a\\n? h\\n\", \"4\\n! abcd\\n! cdef\\n? d\\n? c\\n\", \"1\\n? q\\n\", \"15\\n. r\\n? e\\n. s\\n. rw\\n? y\\n. fj\\n. zftyd\\n? r\\n! wq\\n? w\\n? p\\n. ours\\n. dto\\n. lbyfru\\n? q\\n\", \"3\\n. abcdefghijklmnopqrstuvwxy\\n? a\\n? z\\n\", \"3\\n. abcdefghijklmnopqrstuvwxy\\n! z\\n? z\\n\"], \"outputs\": [\"1\\n\", \"2\\n\", \"0\\n\", \"0\\n\", \"0\\n\", \"2\\n\", \"1\\n\", \"1\\n\"]}", "source": "primeintellect"}
Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly. Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks. -----Input----- The first line contains a single integer n (1 ≤ n ≤ 10^5) — the number of actions Valentin did. The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types: Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter. All words consist only of lowercase English letters. The total length of all words does not exceed 10^5. It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter. -----Output----- Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined. -----Examples----- Input 5 ! abc . ad . b ! cd ? c Output 1 Input 8 ! hello ! codeforces ? c . o ? d ? h . l ? e Output 2 Input 7 ! ababahalamaha ? a ? b ? a ? b ? a ? h Output 0 -----Note----- In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock. In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis. In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"5\\n1 2\\n1 3\\n2 4\\n2 5\\n\", \"6\\n1 2\\n1 3\\n3 4\\n1 5\\n5 6\\n\", \"1\\n\", \"2\\n1 2\\n\", \"3\\n1 2\\n1 3\\n\", \"10\\n1 2\\n1 3\\n3 4\\n3 5\\n4 6\\n1 7\\n3 8\\n2 9\\n6 10\\n\", \"50\\n1 2\\n2 3\\n3 4\\n4 5\\n5 6\\n6 7\\n7 8\\n8 9\\n9 10\\n10 11\\n11 12\\n12 13\\n13 14\\n14 15\\n15 16\\n16 17\\n17 18\\n18 19\\n19 20\\n20 21\\n21 22\\n22 23\\n23 24\\n24 25\\n25 26\\n26 27\\n27 28\\n28 29\\n29 30\\n30 31\\n31 32\\n32 33\\n33 34\\n34 35\\n35 36\\n36 37\\n37 38\\n38 39\\n39 40\\n40 41\\n41 42\\n42 43\\n43 44\\n44 45\\n45 46\\n46 47\\n47 48\\n48 49\\n49 50\\n\", \"22\\n1 2\\n2 3\\n3 4\\n1 5\\n1 6\\n1 7\\n7 8\\n8 9\\n9 10\\n10 11\\n1 12\\n12 13\\n12 14\\n14 15\\n1 16\\n16 17\\n1 18\\n18 19\\n1 20\\n20 21\\n1 22\\n\"], \"outputs\": [\"3 2\\n\", \"3 3\\n\", \"1 1\\n\", \"1 1\\n\", \"2 2\\n\", \"5 3\\n\", \"1 1\\n\", \"10 9\\n\"]}", "source": "primeintellect"}
Demiurges Shambambukli and Mazukta love to watch the games of ordinary people. Today, they noticed two men who play the following game. There is a rooted tree on n nodes, m of which are leaves (a leaf is a nodes that does not have any children), edges of the tree are directed from parent to children. In the leaves of the tree integers from 1 to m are placed in such a way that each number appears exactly in one leaf. Initially, the root of the tree contains a piece. Two players move this piece in turns, during a move a player moves the piece from its current nodes to one of its children; if the player can not make a move, the game ends immediately. The result of the game is the number placed in the leaf where a piece has completed its movement. The player who makes the first move tries to maximize the result of the game and the second player, on the contrary, tries to minimize the result. We can assume that both players move optimally well. Demiurges are omnipotent, so before the game they can arbitrarily rearrange the numbers placed in the leaves. Shambambukli wants to rearrange numbers so that the result of the game when both players play optimally well is as large as possible, and Mazukta wants the result to be as small as possible. What will be the outcome of the game, if the numbers are rearranged by Shambambukli, and what will it be if the numbers are rearranged by Mazukta? Of course, the Demiurges choose the best possible option of arranging numbers. -----Input----- The first line contains a single integer n — the number of nodes in the tree (1 ≤ n ≤ 2·10^5). Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n) — the ends of the edge of the tree; the edge leads from node u_{i} to node v_{i}. It is guaranteed that the described graph is a rooted tree, and the root is the node 1. -----Output----- Print two space-separated integers — the maximum possible and the minimum possible result of the game. -----Examples----- Input 5 1 2 1 3 2 4 2 5 Output 3 2 Input 6 1 2 1 3 3 4 1 5 5 6 Output 3 3 -----Note----- Consider the first sample. The tree contains three leaves: 3, 4 and 5. If we put the maximum number 3 at node 3, then the first player moves there and the result will be 3. On the other hand, it is easy to see that for any rearrangement the first player can guarantee the result of at least 2. In the second sample no matter what the arragment is the first player can go along the path that ends with a leaf with number 3. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0
{"tests": "{\"inputs\": [\"7\\n3 4 1 2 2 1 1\\n\", \"5\\n1 1 3 1 1\\n\", \"5\\n10 40 20 50 30\\n\", \"100\\n10 10 15 12 15 13 15 12 10 10 15 11 13 14 13 14 10 13 12 10 14 12 13 11 14 15 12 11 11 15 12 12 11 14 14 14 15 10 10 15 15 13 13 15 10 12 14 10 12 13 11 15 11 13 14 12 10 12 11 14 13 15 13 15 13 14 14 11 12 13 11 14 10 10 15 10 15 12 15 12 13 10 11 13 15 11 10 12 10 12 14 14 13 12 14 10 12 13 11 13\\n\", \"2\\n1000000000 1000000000\\n\", \"3\\n500000000 500000000 1000000000\\n\", \"9\\n8 536870913 536870913 536870913 536870913 536870913 536870913 536870913 536870913\\n\", \"34\\n967614464 967614464 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000\\n\"], \"outputs\": [\"4\\n3 8 2 1 \\n\", \"2\\n3 4 \\n\", \"5\\n10 40 20 50 30 \\n\", \"12\\n88 240 15 44 160 192 208 224 20 24 11 26 \\n\", \"1\\n2000000000 \\n\", \"1\\n2000000000 \\n\", \"2\\n8 4294967304 \\n\", \"2\\n1935228928 32000000000 \\n\"]}", "source": "primeintellect"}
You are given an array of positive integers. While there are at least two equal elements, we will perform the following operation. We choose the smallest value $x$ that occurs in the array $2$ or more times. Take the first two occurrences of $x$ in this array (the two leftmost occurrences). Remove the left of these two occurrences, and the right one is replaced by the sum of this two values (that is, $2 \cdot x$). Determine how the array will look after described operations are performed. For example, consider the given array looks like $[3, 4, 1, 2, 2, 1, 1]$. It will be changed in the following way: $[3, 4, 1, 2, 2, 1, 1]~\rightarrow~[3, 4, 2, 2, 2, 1]~\rightarrow~[3, 4, 4, 2, 1]~\rightarrow~[3, 8, 2, 1]$. If the given array is look like $[1, 1, 3, 1, 1]$ it will be changed in the following way: $[1, 1, 3, 1, 1]~\rightarrow~[2, 3, 1, 1]~\rightarrow~[2, 3, 2]~\rightarrow~[3, 4]$. -----Input----- The first line contains a single integer $n$ ($2 \le n \le 150\,000$) — the number of elements in the array. The second line contains a sequence from $n$ elements $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$) — the elements of the array. -----Output----- In the first line print an integer $k$ — the number of elements in the array after all the performed operations. In the second line print $k$ integers — the elements of the array after all the performed operations. -----Examples----- Input 7 3 4 1 2 2 1 1 Output 4 3 8 2 1 Input 5 1 1 3 1 1 Output 2 3 4 Input 5 10 40 20 50 30 Output 5 10 40 20 50 30 -----Note----- The first two examples were considered in the statement. In the third example all integers in the given array are distinct, so it will not change. Read the inputs from stdin solve the problem and write the answer to stdout (do not directly test on the sample inputs). Enclose your code within ```python delimiters.
0